protected void rptShortcodes_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (!canAddEditDelete) { e.Item.FindControl("btnEdit").Visible = false; e.Item.FindControl("btnDelete").Visible = false; } LavaShortcode dataItem = (LavaShortcode)e.Item.DataItem; e.Item.FindControl("divEditPanel").Visible = !dataItem.IsSystem; e.Item.FindControl("divViewPanel").Visible = dataItem.IsSystem; // Add special logic for shortcodes in c# assemblies var shortcode = e.Item.DataItem as LavaShortcode; if (shortcode.Id == -1) { // This is a shortcode from a c# assembly e.Item.FindControl("btnView").Visible = false; var lMessages = (Literal)e.Item.FindControl("lMessages"); if (lMessages != null) { lMessages.Text = "<div class='margin-t-md alert alert-info'>This shortcode is defined in code (verses being stored in the database) and therefore can not be modified.</div>"; } } } }
/// <summary> /// Shows the edit details. /// </summary> /// <param name="LavaShortcode">The lavaShortcode.</param> private void ShowEditDetails(LavaShortcode lavaShortcode) { if (lavaShortcode.Id.Equals(0)) { lActionTitle.Text = ActionTitle.Add(LavaShortcode.FriendlyTypeName).FormatAsHtmlTitle(); } else { lActionTitle.Text = ActionTitle.Edit(LavaShortcode.FriendlyTypeName).FormatAsHtmlTitle(); } SetEditMode(true); hlInactive.Visible = !lavaShortcode.IsActive; tbLavaShortcodeName.Text = lavaShortcode.Name; cbIsActive.Checked = lavaShortcode.IsActive; tbDescription.Text = lavaShortcode.Description; htmlDocumentation.Text = lavaShortcode.Documentation; ceMarkup.Text = lavaShortcode.Markup; tbTagName.Text = lavaShortcode.TagName; kvlParameters.Value = lavaShortcode.Parameters; hfOriginalTagName.Value = lavaShortcode.TagName; if (lavaShortcode.EnabledLavaCommands.IsNotNullOrWhitespace()) { lcpLavaCommands.SetValues(lavaShortcode.EnabledLavaCommands.Split(',').ToList()); } rblTagType.BindToEnum <TagType>(); rblTagType.SetValue(( int )lavaShortcode.TagType); }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { LavaShortcode lavaShortcode; var rockContext = new RockContext(); var lavaShortCodeService = new LavaShortcodeService(rockContext); int lavaShortCode = hfLavaShortcodeId.ValueAsInt(); if (lavaShortCodeService.Queryable().Any(a => a.TagName == tbTagName.Text && a.Id != lavaShortCode)) { Page.ModelState.AddModelError("DuplicateTag", "Tag with the same name is already in use."); return; } if (lavaShortCode == 0) { lavaShortcode = new LavaShortcode(); lavaShortCodeService.Add(lavaShortcode); } else { lavaShortcode = lavaShortCodeService.Get(lavaShortCode); } lavaShortcode.Name = tbLavaShortcodeName.Text; lavaShortcode.IsActive = cbIsActive.Checked; lavaShortcode.Description = tbDescription.Text; lavaShortcode.Documentation = htmlDocumentation.Text; lavaShortcode.TagType = rblTagType.SelectedValueAsEnum <TagType>(); lavaShortcode.TagName = tbTagName.Text; lavaShortcode.Markup = ceMarkup.Text; lavaShortcode.Parameters = kvlParameters.Value; lavaShortcode.EnabledLavaCommands = String.Join(",", lcpLavaCommands.SelectedLavaCommands); rockContext.SaveChanges(); // unregister shortcode if (hfOriginalTagName.Value.IsNotNullOrWhiteSpace()) { Template.UnregisterShortcode(hfOriginalTagName.Value); } // register shortcode if (lavaShortcode.TagType == TagType.Block) { Template.RegisterShortcode <DynamicShortcodeBlock>(lavaShortcode.TagName); } else { Template.RegisterShortcode <DynamicShortcodeInline>(lavaShortcode.TagName); } // (bug fix) Now we have to clear the entire LavaTemplateCache because it's possible that some other // usage of this shortcode is cached with a key we can't predict. LavaTemplateCache.Clear(); NavigateToParentPage(); }
/// <summary> /// Shows the detail. /// </summary> /// <param name="lavaShortcodeId">The Lava Shortcode identifier.</param> public void ShowDetail(int lavaShortcodeId) { pnlDetails.Visible = true; // Load depending on Add(0) or Edit LavaShortcode lavaShortcode = null; if (!lavaShortcodeId.Equals(0)) { lavaShortcode = new LavaShortcodeService(new RockContext()).Get(lavaShortcodeId); lActionTitle.Text = ActionTitle.Edit(LavaShortcode.FriendlyTypeName).FormatAsHtmlTitle(); pdAuditDetails.SetEntity(lavaShortcode, ResolveRockUrl("~")); } if (lavaShortcode == null) { lavaShortcode = new LavaShortcode { Id = 0 }; lActionTitle.Text = ActionTitle.Add(LavaShortcode.FriendlyTypeName).FormatAsHtmlTitle(); // hide the panel drawer that show created and last modified dates pdAuditDetails.Visible = false; } hfLavaShortcodeId.Value = lavaShortcode.Id.ToString(); // render UI based on Authorized and IsSystem bool readOnly = false; nbEditModeMessage.Text = string.Empty; if (!IsUserAuthorized(Authorization.EDIT)) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(Campus.FriendlyTypeName); } if (lavaShortcode.IsSystem) { readOnly = true; nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem(Campus.FriendlyTypeName); } if (readOnly) { ShowReadonlyDetails(lavaShortcode); } else { ShowEditDetails(lavaShortcode); } }
protected void rptShortcodes_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (!canAddEditDelete) { e.Item.FindControl("btnEdit").Visible = false; e.Item.FindControl("btnDelete").Visible = false; } LavaShortcode dataItem = (LavaShortcode)e.Item.DataItem; e.Item.FindControl("divEditPanel").Visible = !dataItem.IsSystem; } }
/// <summary> /// Shows the readonly details. /// </summary> /// <param name="LavaShortcode">The lavaShortcode.</param> private void ShowReadonlyDetails(LavaShortcode lavaShortcode) { SetEditMode(false); lActionTitle.Text = lavaShortcode.Name.FormatAsHtmlTitle(); hlInactive.Visible = !lavaShortcode.IsActive; lLayoutDescription.Text = lavaShortcode.Description; DescriptionList descriptionList = new DescriptionList(); descriptionList.Add("System", lavaShortcode.IsSystem.ToYesNo()); descriptionList.Add("Tag Name", lavaShortcode.TagName); descriptionList.Add("Tag Type", lavaShortcode.TagType); lblMainDetails.Text = descriptionList.Html; }
/// <summary> /// Shows the readonly details. /// </summary> /// <param name="LavaShortcode">The lavaShortcode.</param> private void ShowReadonlyDetails(LavaShortcode lavaShortcode) { SetEditMode(false); lActionTitle.Text = lavaShortcode.Name.FormatAsHtmlTitle(); hlInactive.Visible = !lavaShortcode.IsActive; lLayoutDescription.Text = lavaShortcode.Description; var list = lavaShortcode.Markup.ToKeyValuePairList(); DescriptionList headerMarkup = new DescriptionList(); headerMarkup.Add("System", lavaShortcode.IsSystem.ToYesNo()); headerMarkup.Add("Tag Name", lavaShortcode.TagName); headerMarkup.Add("Tag Type", lavaShortcode.TagType); lblHeaderFields.Text = headerMarkup.Html; ceView.Text = lavaShortcode.Markup; if (!string.IsNullOrEmpty(lavaShortcode.Parameters)) { var values = new List <string>(); string[] nameValues = lavaShortcode.Parameters.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); foreach (string nameValue in nameValues) { string[] nameAndValue = nameValue.Split(new char[] { '^' }); // url decode array items just in case they were UrlEncoded (in the KeyValueList controls) nameAndValue = nameAndValue.Select(s => HttpUtility.UrlDecode(s)).ToArray(); if (nameAndValue.Length == 2) { values.Add(string.Format("<strong>{0}:</strong> {1}", nameAndValue[0], nameAndValue[1])); } else { values.Add(nameValue); } } lblParameters.Text = string.Join("<br/>", values); } lblEnabledCommands.Text = lavaShortcode.EnabledLavaCommands; }
/// <summary> /// Handles the Click event of the btnDelete control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnDelete_Click(object sender, EventArgs e) { LinkButton btn = ( LinkButton )sender; RepeaterItem item = ( RepeaterItem )btn.NamingContainer; HiddenField hfShortcodeId = ( HiddenField )item.FindControl("hfShortcodeId"); var rockContext = new RockContext(); LavaShortcodeService lavaShortcodeService = new LavaShortcodeService(rockContext); LavaShortcode lavaShortcode = lavaShortcodeService.Get(hfShortcodeId.ValueAsInt()); if (lavaShortcode != null) { // unregister the shortcode Template.UnregisterShortcode(lavaShortcode.TagName); lavaShortcodeService.Delete(lavaShortcode); rockContext.SaveChanges(); } LoadShortcodes(); }
private DynamicShortcodeDefinition GetShortcodeDefinitionFromShortcodeEntity(LavaShortcode shortcode) { if (shortcode == null) { return(null); } var newShortcode = new DynamicShortcodeDefinition(); newShortcode.Name = shortcode.Name; newShortcode.TemplateMarkup = shortcode.Markup; var parameters = RockSerializableDictionary.FromUriEncodedString(shortcode.Parameters); newShortcode.Parameters = new Dictionary <string, string>(parameters.Dictionary); newShortcode.EnabledLavaCommands = shortcode.EnabledLavaCommands.SplitDelimitedValues(",", StringSplitOptions.RemoveEmptyEntries).ToList(); if (shortcode.TagType == TagType.Block) { newShortcode.ElementType = LavaShortcodeTypeSpecifier.Block; } else { newShortcode.ElementType = LavaShortcodeTypeSpecifier.Inline; } return(newShortcode); }
/// <summary> /// Register the specified shortcodes with the Lava Engine. /// </summary> /// <param name="engine"></param> public void RegisterShortcode(ILavaEngine engine, LavaShortcode shortcode) { engine.RegisterShortcode(shortcode.TagName, (shortcodeName) => GetShortcodeDefinition(shortcodeName)); }
public void WebsiteLavaShortcodeProvider_ModifiedShortcode_ReturnsCorrectVersionAfterModification() { var options = new LavaEngineConfigurationOptions(); ILavaTemplateCacheService cacheService = new WebsiteLavaTemplateCacheService(); options.CacheService = cacheService; TestHelper.ExecuteForActiveEngines((defaultEngineInstance) => { if (defaultEngineInstance.GetType() == typeof(DotLiquidEngine) || defaultEngineInstance.GetType() == typeof(RockLiquidEngine)) { Debug.Write("Shortcode caching is not currently implemented for RockLiquid/DotLiquid."); return; } var engine = LavaService.NewEngineInstance(defaultEngineInstance.GetType(), options); var shortcodeProvider = new TestLavaDynamicShortcodeProvider(); var rockContext = new RockContext(); var lavaShortCodeService = new LavaShortcodeService(rockContext); // Create a new Shortcode. var shortcodeGuid1 = TestGuids.Shortcodes.ShortcodeTest1.AsGuid(); var lavaShortcode = lavaShortCodeService.Queryable().FirstOrDefault(x => x.Guid == shortcodeGuid1); if (lavaShortcode == null) { lavaShortcode = new LavaShortcode(); lavaShortCodeService.Add(lavaShortcode); } lavaShortcode.Guid = shortcodeGuid1; lavaShortcode.TagName = "TestShortcode1"; lavaShortcode.Name = "Test Shortcode 1"; lavaShortcode.IsActive = true; lavaShortcode.Description = "Test shortcode"; lavaShortcode.TagType = TagType.Inline; lavaShortcode.Markup = "Hello!"; rockContext.SaveChanges(); shortcodeProvider.RegisterShortcode(engine, lavaShortcode); // Resolve a template using the new shortcode and verify the result. engine.ClearTemplateCache(); shortcodeProvider.ClearCache(); LavaService.SetCurrentEngine(engine); TestHelper.AssertTemplateOutput(engine, "Hello!", "{[ TestShortcode1 ]}"); lavaShortcode.Markup = "Goodbye!"; rockContext.SaveChanges(); shortcodeProvider.ClearCache(); engine.ClearTemplateCache(); TestHelper.AssertTemplateOutput(engine, "Goodbye!", "{[ TestShortcode1 ]}"); }); }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { LavaShortcode lavaShortcode; var rockContext = new RockContext(); var lavaShortCodeService = new LavaShortcodeService(rockContext); int lavaShortCode = hfLavaShortcodeId.ValueAsInt(); if (lavaShortCodeService.Queryable().Any(a => a.TagName == tbTagName.Text && a.Id != lavaShortCode)) { Page.ModelState.AddModelError("DuplicateTag", "Tag with the same name is already in use."); return; } if (lavaShortCode == 0) { lavaShortcode = new LavaShortcode(); lavaShortCodeService.Add(lavaShortcode); } else { lavaShortcode = lavaShortCodeService.Get(lavaShortCode); } var oldTagName = hfOriginalTagName.Value; lavaShortcode.Name = tbLavaShortcodeName.Text; lavaShortcode.IsActive = cbIsActive.Checked; lavaShortcode.Description = tbDescription.Text; lavaShortcode.Documentation = htmlDocumentation.Text; lavaShortcode.TagType = rblTagType.SelectedValueAsEnum <TagType>(); lavaShortcode.TagName = tbTagName.Text.Trim(); lavaShortcode.Markup = ceMarkup.Text; lavaShortcode.Parameters = kvlParameters.Value; lavaShortcode.EnabledLavaCommands = String.Join(",", lcpLavaCommands.SelectedLavaCommands); rockContext.SaveChanges(); if (LavaService.RockLiquidIsEnabled) { // unregister shortcode if (oldTagName.IsNotNullOrWhiteSpace()) { Template.UnregisterShortcode(oldTagName); } // Register the new shortcode definition. Note that RockLiquid shortcode tags are case-sensitive. if (lavaShortcode.TagType == TagType.Block) { Template.RegisterShortcode <Rock.Lava.Shortcodes.DynamicShortcodeBlock>(lavaShortcode.TagName); } else { Template.RegisterShortcode <Rock.Lava.Shortcodes.DynamicShortcodeInline>(lavaShortcode.TagName); } // (bug fix) Now we have to clear the entire LavaTemplateCache because it's possible that some other // usage of this shortcode is cached with a key we can't predict. #pragma warning disable CS0618 // Type or member is obsolete // This obsolete code can be deleted when support for the DotLiquid Lava implementation is removed. LavaTemplateCache.Clear(); #pragma warning restore CS0618 // Type or member is obsolete } if (oldTagName.IsNotNullOrWhiteSpace()) { LavaService.DeregisterShortcode(oldTagName); } // Register the new shortcode definition. LavaService.RegisterShortcode(lavaShortcode.TagName, (shortcodeName) => WebsiteLavaShortcodeProvider.GetShortcodeDefinition(shortcodeName)); LavaService.ClearTemplateCache(); NavigateToParentPage(); }