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); }
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); }
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); }
public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null) { var jsMatches = regEx.Matches(inputContent); if (!jsMatches.Any()) { return(inputContent); } var generalSettings = DependencyResolver.Resolve <GeneralSettings>(); var jsFiles = new Dictionary <string, List <string> >(); foreach (Match match in jsMatches) { ExtractMatch(match, out var parts, out var keyValuePairs); if (!parts.Any()) { throw new Exception("No file provided for bundling"); } var bundle = ""; keyValuePairs?.TryGetValue("bundle", out bundle); if (jsFiles.ContainsKey(bundle)) { jsFiles[bundle] = jsFiles[bundle].Concat(parts).ToList(); } else { jsFiles.Add(bundle, parts.ToList()); } } var canBundle = generalSettings.EnableJsBundling && jsFiles.Any(); string bundleUrl = null; Dictionary <string, string> bundleUrls = null; if (canBundle) { bundleUrls = new Dictionary <string, string>(); var bundleService = DependencyResolver.Resolve <IBundleService>(); foreach (var jsFile in jsFiles) { bundleUrl = bundleService.GenerateJsBundle(jsFile.Value.ToArray()); bundleUrls.Add(jsFile.Key, bundleUrl); } } if (bundleUrls != null) { inputContent = regEx.Replace(inputContent, ""); readFile.Content = regEx.Replace(readFile.Content, ""); //add to readfile to be picked up by the renderer tag readFile.AddMeta(BundleKey, bundleUrls, nameof(JsExpander)); } else { var fileIndex = 0; //render the link tags on page var alljsFiles = jsFiles.Values.SelectMany(x => x).ToArray(); foreach (Match match in jsMatches) { var script = $"<script type=\"text/javascript\" src=\"{alljsFiles[fileIndex++]}\"></script>"; inputContent = inputContent.ReplaceFirstOccurance(match.Result("$0"), script); } } return(inputContent); }