Esempio n. 1
0
        protected string PreparePrefetch(string content)
        {
            string output = null;

            if (!(String.IsNullOrEmpty(Request.Url.Query)) || (Request.Headers["X-Cot-Manifest-Request"] == "true"))
            {
                return(output);
            }
            JToken        token = ApplicationServices.TryGetJsonProperty(ApplicationServices.Current.DefaultSettings, "ui.history.dataView");
            bool          supportGridPrefetch = ((token == null) || !(Regex.IsMatch(((string)(token)), "\\b(search|sort|group|filter)\\b")));
            List <string> prefetches          = new List <string>();
            bool          prefetch            = false;
            List <Tuple <string, AttributeDictionary> > dataViews = new List <Tuple <string, AttributeDictionary> >();

            foreach (Match m in Regex.Matches(content, "<div\\s+(id=\"(?\'Id\'\\w+)\")\\s+(?\'Props\'data-controller.*?)>"))
            {
                dataViews.Add(new Tuple <string, AttributeDictionary>(m.Groups["Id"].Value, new AttributeDictionary(m.Groups["Props"].Value)));
            }
            if (dataViews.Count == 1)
            {
                prefetch = true;
            }
            else
            {
                // LEGACY MASTER DETAIL PAGE SUPPORT
                //
                //
                // 1. convert text of containers into single container with single dataview referring to virtual dashboard controller
                //
                // <div data-flow="row">
                //   <div id="view1" data-controller="Dashboards" data-view="form1" data-show-action-buttons="none"></div>
                //
                // </div>
                //
                // 2. produce response for this controller.
                // a. standalone data views become data view fields of the virtual controller
                // b. the layout of the page is optionally converted into form1 layout of the virtual controller
                // c. render json response of virtual controller with layout in it
                //
            }
            if (prefetch)
            {
                for (int i = 0; (i < dataViews.Count); i++)
                {
                    Tuple <string, AttributeDictionary> dataView = dataViews[i];
                    string dataViewId         = dataView.Item1;
                    AttributeDictionary attrs = dataView.Item2;
                    foreach (string p in UnsupportedDataViewProperties)
                    {
                        if (attrs.ContainsKey(p))
                        {
                            return(output);
                        }
                    }
                    string controllerName = attrs["data-controller"];
                    string viewId         = null;
                    string tags           = null;
                    attrs.TryGetValue("data-tags", out tags);
                    ControllerConfiguration c = Controller.CreateConfigurationInstance(GetType(), controllerName);
                    if (!(attrs.TryGetValue("data-view", out viewId)))
                    {
                        viewId = ((string)(c.Evaluate("string(/c:dataController/c:views/c:view[1]/@id)")));
                    }
                    XPathNavigator viewNav = c.SelectSingleNode("/c:dataController/c:views/c:view[@id=\'{0}\']", viewId);
                    if (!(Context.User.Identity.IsAuthenticated) && !((viewNav.GetAttribute("access", String.Empty) == "Public")))
                    {
                        return(output);
                    }
                    string roles = null;
                    if (attrs.TryGetValue("data-roles", out roles) && !(new ControllerUtilities().UserIsInRole(roles.Split(','))))
                    {
                        return(output);
                    }
                    tags = (tags
                            + (" " + viewNav.GetAttribute("tags", String.Empty)));
                    bool isForm = (viewNav.GetAttribute("type", String.Empty) == "Form");
                    if (isForm)
                    {
                        _summaryDisabled = true;
                    }
                    if ((String.IsNullOrEmpty(tags) || !(Regex.IsMatch(tags, "\\bprefetch-data-none\\b"))) && (supportGridPrefetch || isForm))
                    {
                        PageRequest request = new PageRequest(-1, 30, null, null);
                        request.Controller      = controllerName;
                        request.View            = viewId;
                        request.Tag             = tags;
                        request.ContextKey      = dataViewId;
                        request.SupportsCaching = true;
                        if (attrs.ContainsKey("data-search-on-start"))
                        {
                            request.DoesNotRequireData = true;
                        }
                        ViewPage response = ControllerFactory.CreateDataController().GetPage(request.Controller, request.View, request);
                        string   result   = String.Format("{{ \"d\": {0} }}", ApplicationServices.CompressViewPageJsonOutput(JsonConvert.SerializeObject(response)));
                        prefetches.Add(String.Format("<script type=\"application/json\" id=\"_{0}_prefetch\">{1}</script>", dataViewId, Regex.Replace(result, "(<(/?\\s*script)(\\s|>))", "]_[$2$3]^[", RegexOptions.IgnoreCase)));
                        if (isForm)
                        {
                            foreach (DataField field in response.Fields)
                            {
                                if (String.IsNullOrEmpty(field.DataViewFilterSource) && (field.Type == "DataView"))
                                {
                                    AttributeDictionary fieldAttr = new AttributeDictionary(String.Empty);
                                    fieldAttr.Add("data-controller", field.DataViewController);
                                    fieldAttr.Add("data-view", field.DataViewId);
                                    fieldAttr.Add("data-tags", field.Tag);
                                    if (field.DataViewSearchOnStart)
                                    {
                                        fieldAttr.Add("data-search-on-start", "true");
                                    }
                                    dataViews.Add(new Tuple <string, AttributeDictionary>(String.Format("{0}_{1}", dataViewId, field.Name), fieldAttr));
                                }
                            }
                        }
                    }
                }
            }
            if (prefetches.Count > 0)
            {
                output = string.Join(String.Empty, prefetches);
            }
            return(output);
        }