Ejemplo n.º 1
0
        public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null)
        {
            var componentMatches     = regEx.Matches(inputContent);
            var viewComponentManager = DependencyResolver.Resolve <IViewComponentManager>();
            var componentIndexOnPage = 0;

            if (componentMatches.Count == 0)
            {
                foreach (var meta in readFile.GetMeta(nameof(ComponentExpander)))
                {
                    var componentName = meta.Key;
                    var valueAsArray  = (object[])meta.Value;
                    //extract values
                    componentIndexOnPage = (int)valueAsArray[0];
                    var keyValuePairs       = (Dictionary <string, string>)valueAsArray[1];
                    var componentParameters = GetComponentParameters(keyValuePairs, parameters);
                    viewComponentManager.InvokeViewComponent(componentName, componentParameters, out string _, out object model, out string _, true);
                    MergeModel(parameters, model, componentName, componentIndexOnPage, out string _, out string _);
                }
                return(inputContent);
            }


            foreach (Match componentMatch in componentMatches)
            {
                ExtractMatch(componentMatch, out string[] straightParameters, out Dictionary <string, string> keyValuePairs);
                if (!straightParameters.Any())
                {
                    throw new Exception($"A component must be specified with component tag in view {readFile.FileName}");
                }

                var componentName = straightParameters[0];
                //collect the values that are being passed to the component
                var componentParameters = GetComponentParameters(keyValuePairs, parameters);
                viewComponentManager.InvokeViewComponent(componentName,
                                                         componentParameters,
                                                         out string componentContent, out object model, out string viewPath);

                if (!viewPath.IsNullEmptyOrWhiteSpace())
                {
                    readFile.AddChild(ReadFile.From(viewPath));
                }

                //merge models
                MergeModel(parameters, model, componentName, componentIndexOnPage, out var assignString, out var resetAssignString);
                if (!WebHelper.IsAjaxRequest(ApplicationEngine.CurrentHttpContext.Request))
                {
                    //add keyvaluepairs as meta along with the index
                    readFile.AddMeta(componentName, new object[] { componentIndexOnPage, keyValuePairs }, nameof(ComponentExpander));
                }
                var match = componentMatch.Result("$0");
                //replace only first occurance of the pattern result
                readFile.Content = readFile.Content.ReplaceFirstOccurance(match, assignString + componentContent + resetAssignString);
                inputContent     = inputContent.ReplaceFirstOccurance(match, assignString + componentContent + resetAssignString);

                //next component
                componentIndexOnPage++;
            }
            return(inputContent);
        }
Ejemplo n.º 2
0
        public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null)
        {
            var bundleMatches = regEx.Matches(inputContent);

            if (!bundleMatches.Any())
            {
                return(inputContent);
            }
            var generalSettings = DependencyResolver.Resolve <GeneralSettings>();

            foreach (Match match in bundleMatches)
            {
                ExtractMatch(match, out _, out var keyValuePairs);
                if (keyValuePairs == null)
                {
                    throw new Exception($"The bundle tag must have a render parameter set to either 'css' or 'js' in file " + readFile.FileName);
                }
                keyValuePairs.TryGetValue("render", out var render);
                keyValuePairs.TryGetValue("bundle", out var bundle);
                keyValuePairs.TryGetValue("gz", out var gz);
                bundle = bundle ?? "";
                render = render?.ToLower();
                var gzExt = "";
                if (!gz.IsNullEmptyOrWhiteSpace() && gz == "true")
                {
                    gzExt = ".gz";
                }

                if (render.IsNullEmptyOrWhiteSpace() || (render != "css" && render != "js"))
                {
                    throw new Exception($"The bundle tag must have a render parameter set to either 'css' or 'js' in file " + readFile.FileName);
                }

                if (render == "css")
                {
                    if (generalSettings.EnableCssBundling)
                    {
                        var cssBundle  = readFile.GetMeta(nameof(CssExpander)).FirstOrDefault(x => x.Key == CssExpander.BundleKey);
                        var bundleUrls = (Dictionary <string, string>)cssBundle.Value;
                        if (!bundleUrls.ContainsKey(bundle))
                        {
                            throw new Exception($"The bundle with name '{bundle}' was not declared. File name " +
                                                readFile.FileName);
                        }
                        var bundleUrl = bundleUrls[bundle];
                        var link      = $"<link rel=\"stylesheet\" href=\"{bundleUrl}{gzExt}\" />";
                        inputContent     = inputContent.ReplaceFirstOccurance(match.Result("$0"), link);
                        readFile.Content = readFile.Content.ReplaceFirstOccurance(match.Result("$0"), link);
                    }
                    else
                    {
                        inputContent     = inputContent.ReplaceFirstOccurance(match.Result("$0"), "");
                        readFile.Content = readFile.Content.ReplaceFirstOccurance(match.Result("$0"), "");
                    }
                }
                else
                {
                    if (generalSettings.EnableJsBundling)
                    {
                        var jsBundle   = readFile.GetMeta(nameof(JsExpander)).FirstOrDefault(x => x.Key == JsExpander.BundleKey);
                        var bundleUrls = (Dictionary <string, string>)jsBundle.Value;
                        if (!bundleUrls.ContainsKey(bundle))
                        {
                            throw new Exception($"The bundle with name '{bundle}' was not declared. File name " +
                                                readFile.FileName);
                        }
                        var bundleUrl = bundleUrls[bundle];
                        var script    = $"<script type=\"text/javascript\" src=\"{bundleUrl}{gzExt}\"></script>";
                        inputContent     = inputContent.ReplaceFirstOccurance(match.Result("$0"), script);
                        readFile.Content = readFile.Content.ReplaceFirstOccurance(match.Result("$0"), script);
                    }
                    else
                    {
                        inputContent     = inputContent.ReplaceFirstOccurance(match.Result("$0"), "");
                        readFile.Content = readFile.Content.ReplaceFirstOccurance(match.Result("$0"), "");
                    }
                }
            }

            return(inputContent);
        }
Ejemplo n.º 3
0
        public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null)
        {
            if (!ApplicationEngine.IsAdmin())
            {
                return(inputContent);
            }
            var matches      = regEx.Matches(inputContent);
            var paramsAsDict = (IDictionary <string, object>)parameters;

            if (paramsAsDict == null)
            {
                return(inputContent);
            }
            if (matches.Count == 0)
            {
                var navMeta = readFile.GetMeta(nameof(NavigationExpander)).FirstOrDefault(x => x.Key == NavigationKey);
                if (navMeta.Key != null && !paramsAsDict.ContainsKey(NavigationKey))
                {
                    paramsAsDict.Add(NavigationKey, navMeta.Value);
                }
                var groupMeta = readFile.GetMeta(nameof(NavigationExpander)).FirstOrDefault(x => x.Key == NavigationGroupKey);
                if (navMeta.Key != null && !paramsAsDict.ContainsKey(NavigationGroupKey))
                {
                    paramsAsDict.Add(NavigationGroupKey, groupMeta.Value);
                }
                return(inputContent);
            }

            List <Navigation> menuList = null;

            if (!paramsAsDict.ContainsKey(NavigationKey))
            {
                menuList = new List <Navigation>();
                paramsAsDict.Add(NavigationKey, menuList);
            }

            List <NavigationGroup> groupList = null;

            if (!paramsAsDict.ContainsKey(NavigationGroupKey))
            {
                groupList = new List <NavigationGroup>()
                {
                    new NavigationGroup()
                    {
                        Name = "", Id = null, DisplayOrder = 0
                    }
                };
                paramsAsDict.Add(NavigationGroupKey, groupList);
            }

            menuList  = (List <Navigation>)paramsAsDict[NavigationKey];
            groupList = (List <NavigationGroup>)paramsAsDict[NavigationGroupKey];

            foreach (Match match in matches)
            {
                ExtractMatch(match, out var _, out var keyValuePairs);

                //first for groups
                keyValuePairs.TryGetValue("group", out var group);
                keyValuePairs.TryGetValue("order", out var displayOrderValue);
                keyValuePairs.TryGetValue("id", out var id);
                int.TryParse(displayOrderValue, out var displayOrder);

                if (group != null)
                {
                    groupList.Add(new NavigationGroup()
                    {
                        Name         = group,
                        DisplayOrder = displayOrder,
                        Id           = id
                    });

                    continue;
                }
                //do we have route parameter?
                keyValuePairs.TryGetValue("url", out var url);
                keyValuePairs.TryGetValue("title", out var title);
                keyValuePairs.TryGetValue("systemName", out var systemName);
                keyValuePairs.TryGetValue("groupId", out var groupId);
                keyValuePairs.TryGetValue("capability", out var capability);
                keyValuePairs.TryGetValue("iconClass", out var iconClass);
                keyValuePairs.TryGetValue("parent", out var parent);
                if (url.IsNullEmptyOrWhiteSpace())
                {
                    //use the current url if it's empty url
                    url = ApplicationEngine.CurrentHttpContext.Request.Path + ApplicationEngine.CurrentHttpContext.Request.QueryString;
                }

                title      = title ?? "";
                systemName = systemName ?? "";
                menuList.Add(new Navigation()
                {
                    Title        = title,
                    Url          = url,
                    SystemName   = systemName,
                    DisplayOrder = displayOrder,
                    GroupId      = groupId,
                    Capabilities = capability?.Split(" or ", StringSplitOptions.RemoveEmptyEntries),
                    IconClass    = iconClass,
                    Type         = NavigationType,
                    Parent       = parent
                });
            }

            menuList = menuList.OrderBy(x => x.DisplayOrder).ToList();
            paramsAsDict[NavigationKey] = menuList;
            readFile.AddMeta(NavigationKey, menuList, $"{nameof(NavigationExpander)}");

            groupList = groupList.OrderBy(x => x.DisplayOrder).ToList();
            paramsAsDict[NavigationGroupKey] = groupList;
            readFile.AddMeta(NavigationGroupKey, groupList, $"{nameof(NavigationExpander)}");
            //remove the tags
            readFile.Content = regEx.Replace(readFile.Content, "");
            inputContent     = regEx.Replace(inputContent, "");
            return(inputContent);
        }
Ejemplo n.º 4
0
        public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null)
        {
            var viewAccountant = DependencyResolver.Resolve <IViewAccountant>();
            var layoutMatches  = regEx.Matches(inputContent);

            if (layoutMatches.Count == 0)
            {
                if (WebHelper.IsAjaxRequest(ApplicationEngine.CurrentHttpContext.Request))
                {
                    //return content without layout in case of raw request
                    var layoutLessContent = readFile.GetMeta(nameof(LayoutExpander))
                                            .FirstOrDefault(x => x.Key == ContentWithoutLayoutKey)
                                            .Value?.ToString() ?? inputContent;

                    //is there any ajax layout
                    var ajaxLayoutPath = viewAccountant.GetLayoutPath("_LayoutAjax");
                    if (!ajaxLayoutPath.IsNullEmptyOrWhiteSpace())
                    {
                        var ajaxLayout = ReadFile.From(ajaxLayoutPath);
                        return(ajaxLayout.Content.Replace("{% bodyContent %}", layoutLessContent));
                    }
                    return(layoutLessContent);
                }
                return(inputContent);
            }
            if (layoutMatches.Count > 1)
            {
                throw new Exception($"Can't use two layouts in one page");
            }

            ExtractMatch(layoutMatches[0], out string[] straightParameters, out Dictionary <string, string> keyValuePairs);
            if (!straightParameters.Any())
            {
                throw new Exception($"A layout must be specified with layout tag in view {readFile.FileName}");
            }

            if (keyValuePairs != null && keyValuePairs.Any(x => x.Key.Equals("ignoreForAjax") && x.Value == "true"))
            {
                //preserve content without layout
                readFile.AddMeta(ContentWithoutLayoutKey, regEx.Replace(readFile.Content, ""), nameof(LayoutExpander));

                if (WebHelper.IsAjaxRequest(ApplicationEngine.CurrentHttpContext.Request))
                {
                    //return content without layout in case of raw request
                    return(readFile.GetMeta(nameof(LayoutExpander))
                           .FirstOrDefault(x => x.Key == ContentWithoutLayoutKey)
                           .Value.ToString());
                }
            }

            var layoutValue = straightParameters[0];

            //read the layout now
            var layoutPath = viewAccountant.GetLayoutPath(layoutValue);

            if (layoutPath.IsNullEmptyOrWhiteSpace())
            {
                throw new Exception($"Can't find layout {layoutValue} in view {readFile.FileName}");
            }

            var layoutFile = ReadFile.From(layoutPath);

            readFile.AddChild(layoutFile);

            //expand the layout file
            var layoutExpanded = Expand(layoutFile, regEx, layoutFile.Content);

            if (layoutFile.Content == layoutExpanded)
            {
                var bodyMatcher = new Regex(@"{%\s+bodyContent\s+%}");
                //remove the layout tag
                readFile.Content = regEx.Replace(readFile.Content, "");
                //remove the body content tag
                readFile.Content = bodyMatcher.Replace(layoutExpanded, readFile.Content);
                return(readFile.Content);
            }
            return(string.Empty);
        }