public static HelperResult AttachIcon(this HtmlHelper html, ContentDataItemDecorator dataItem) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { if (dataItem.HasAttachments) { var url = new UrlHelper(html.ViewContext.RequestContext); writer.WriteBeginTag("a"); writer.WriteAttribute("href", url.Content(dataItem) + "#attachments"); writer.WriteAttribute("class", "d-icon d-icon-attach"); writer.Write(HtmlTextWriter.TagRightChar); writer.WriteEndTag("a"); } } }); }
/// <summary> /// Render html element for specified field object. /// </summary> /// <param name="html">The html helper object.</param> /// <param name="field">The field object</param> /// <returns></returns> public static HelperResult Label(this HtmlHelper html, ContentField field) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { try { writer.WriteBeginTag("label"); writer.WriteAttribute("for", field.ClientID); writer.Write(HtmlTextWriter.TagRightChar); if (!string.IsNullOrEmpty(field.Title)) writer.WriteEncodedText(field.Title); else writer.WriteEncodedText(field.Name); writer.WriteEndTag("label"); } catch { } } }); }
/// <summary> /// Render hidden input element for field value. /// </summary> /// <param name="html">The html helper object.</param> /// <param name="field">The field object</param> /// <param name="value">the field value</param> /// <returns></returns> public static HelperResult Hidden(this HtmlHelper html, ContentField field, object value = null) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { writer.WriteBeginTag("input"); writer.WriteAttribute("type", "hidden"); writer.WriteAttribute("id", field.ClientID); writer.WriteAttribute("name", field.Name); if (value != null) { writer.WriteAttribute("value", value.ToString()); } else { if (field.DefaultValue != null) writer.WriteAttribute("value", field.DefaultValue.ToString()); } writer.Write(HtmlTextWriter.SelfClosingTagEnd); } }); }
/// <summary> /// Generate validation data attributes for CurrentField object. /// </summary> /// <param name="field">The CurrencyField object.</param> /// <returns></returns> public static HelperResult GetValidationAttributes(this CurrencyField field) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { writer.WriteAttribute("data-val", "true"); writer.WriteAttribute("data-val-number", string.Format("The {0} must be a vaild number.", field.Title)); //if (field.DecimalDigits > 0) // writer.WriteAttribute("data-val-digits", string.Format("The {0} must be have {1} digits.", field.Title, field.DecimalDigits)); if (field.MaximumValue > 0 || field.MaximumValue > 0 || field.IsRequired) { writer.WriteAttribute("data-val-required", string.Format(RES_REQURIED, field.Title)); if (field.MaximumValue > 0 && field.MaximumValue > 0) { writer.WriteAttribute("data-val-range", string.Format(RangeAttribute_ValidationError, field.MaximumValue, field.MaximumValue)); writer.WriteAttribute("data-val-range-max", field.MaximumValue.ToString()); writer.WriteAttribute("data-val-range-min", field.MaximumValue.ToString()); } else { if (field.MaximumValue > 0) { writer.WriteAttribute("data-val-range", string.Format(RangeAttribute_Max, field.MaximumValue)); writer.WriteAttribute("data-val-range-max", field.MaximumValue.ToString()); } if (field.MinimumValue > 0) { writer.WriteAttribute("data-val-range", string.Format(RangeAttribute_Min, field.MinimumValue)); writer.WriteAttribute("data-val-range-min", field.MinimumValue.ToString()); } } } } }); }
public static HelperResult DraftIcon(this HtmlHelper html, ContentDataItemDecorator dataItem) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { if (!dataItem.IsPublished) { writer.WriteBeginTag("span"); writer.WriteAttribute("title", "Draft"); writer.WriteAttribute("class", "d-icon d-icon-draft"); writer.Write(HtmlTextWriter.TagRightChar); writer.WriteEndTag("span"); } } }); }
/// <summary> /// Generate validation data attributes for TextField object. /// </summary> /// <param name="field">The TextField object.</param> /// <returns></returns> public static HelperResult GetValidationAttributes(this TextField field) { string ValidationErrorIncludingMinimum = "The {0} must be a string with a minimum length of {2} and a maximum length of {1}."; string StringLengthAttribute_ValidationError = "The {0} must be a string with a maximum length of {1}."; return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { if (field.MaxLength > 0 || field.MinLength > 0 || field.IsRequired) { writer.WriteAttribute("data-val", "true"); writer.WriteAttribute("data-val-required", string.Format(RES_REQURIED, field.Title)); if (field.MaxLength > 0 && field.MinLength > 0) { writer.WriteAttribute("data-val-length", string.Format(ValidationErrorIncludingMinimum, field.MinLength, field.MinLength)); writer.WriteAttribute("data-val-length-max", field.MaxLength.ToString()); writer.WriteAttribute("data-val-length-min", field.MinLength.ToString()); } else { if (field.MaxLength > 0) { writer.WriteAttribute("data-val-length-max", field.MaxLength.ToString()); writer.WriteAttribute("data-val-length", string.Format(StringLengthAttribute_ValidationError, field.MaxLength)); } } } } }); }
/// <summary> /// Generate MicroData attributes for specified field object. /// </summary> /// <param name="field">The field object.</param> /// <param name="value">The field value object.</param> /// <returns></returns> public static HelperResult GetMicroDataAttributes(this ContentField field, object value = null) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { if (!string.IsNullOrEmpty(field.ItemType)) { writer.WriteAttribute("itemscope ", "itemscope"); writer.WriteAttribute("itemtype", field.ItemType); } if (!string.IsNullOrEmpty(field.ItemProp)) writer.WriteAttribute("itemprop", field.ItemProp); } }); }
/// <summary> /// Generate the common data attributes for specified field object. /// </summary> /// <param name="field">The field object.</param> /// <param name="value">The field value object.</param> /// <returns></returns> public static HelperResult GetAttributes(this ContentField field, object value = null) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { writer.WriteAttribute("id", field.ClientID); writer.WriteAttribute("name", field.Name); if (value != null) { writer.WriteAttribute("value", value.ToString()); } else { if (field.DefaultValue != null) writer.WriteAttribute("value", field.DefaultValue.ToString()); } if (!string.IsNullOrEmpty(field.Placeholder)) writer.WriteAttribute("placeholder", field.Placeholder); else writer.WriteAttribute("placeholder", field.Title); if (field.IsReadOnly) { writer.WriteAttribute("readonly", "readonly"); writer.WriteAttribute("class", "d-state-disable"); } } }); }
/// <summary> /// Render validation HTML elements for specified field object. /// </summary> /// <param name="html">The html helper object.</param> /// <param name="field">The field object.</param> /// <returns></returns> public static HelperResult ValidationMessage(this HtmlHelper html, ContentField field) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { writer.WriteBeginTag("span"); writer.WriteAttribute("class", "d-field-val-msg d-valmsg"); writer.WriteAttribute("data-valmsg-for", field.Name); writer.WriteAttribute("data-valmsg-replace", "true"); writer.Write(HtmlTextWriter.TagRightChar); //writer.WriteEncodedText(field.Description); writer.WriteEndTag("span"); } }); }
/// <summary> /// Render the tags element for specified data item. /// </summary> /// <param name="helper">The html helper object.</param> /// <param name="dataItem">The data item instance.</param> /// <returns></returns> public static HelperResult Tags(this HtmlHelper helper, ContentDataItem dataItem) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { if (!string.IsNullOrEmpty(dataItem.Tags)) { writer.WriteBeginTag("input"); writer.WriteAttribute("data-role", "tags"); writer.WriteAttribute("readonly", "readonly"); writer.WriteAttribute("value", dataItem.Tags); writer.Write(Html32TextWriter.SelfClosingTagEnd); } } }); }
/// <summary> /// Render an items tree for specified list /// </summary> /// <param name="list">The content list object</param> /// <param name="current">The content data item object.</param> /// <param name="htmlAttributes">The html attributes object.</param> /// <returns></returns> public static HelperResult Tree(ContentListDecorator list, ContentDataItemDecorator current = null, object htmlAttributes = null) { return new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { writer.WriteBeginTag("ul"); writer.WriteAttribute("data-role", "tree"); if (htmlAttributes != null) { var dict = htmlAttributes.ToDictionary(); foreach (var key in dict.Keys) { if (key.StartsWith("data_")) writer.WriteAttribute(key.Replace("_", "-"), (string)dict[key]); else writer.WriteAttribute(key, (string)dict[key]); } } writer.Write(Html32TextWriter.TagRightChar); var items = list.GetItems(i => i.ParentItemID == Guid.Empty && i.IsPublished && i.IsCurrentVersion).OrderBy(i => i.Pos); RenderChildren(writer, list, items, current); writer.WriteEndTag("ul"); } }); }
private static void RenderChildren(Html32TextWriter writer, ContentListDecorator list, IEnumerable<ContentDataItem> items, ContentDataItem current) { var app = App.Get(); var fieldName = list.GetDefaultTitleField().Name; var Url = DNA.Utility.UrlUtility.CreateUrlHelper(); foreach (var item in items) { var itemWrapper = app.Wrap(item); writer.WriteBeginTag("li"); var _class = "d-node"; var children = itemWrapper.Children(); var childrenCount = children.Count(); if (childrenCount > 0) _class += " d-node-hasChildren"; //writer.WriteAttribute("class", "d-node d-node-hasChildren"); var isInPath = false; if (current != null && !string.IsNullOrEmpty(current.Path) && !string.IsNullOrEmpty(item.Path) && current.Path.StartsWith(item.Path)) { isInPath = true; if (childrenCount > 0) writer.WriteAttribute("data-expanded", "true"); } writer.WriteAttribute("data-id", itemWrapper.ID.ToString()); if (current != null && itemWrapper.ID.Equals(current.ID)) writer.WriteAttribute("data-selected", "true"); if (itemWrapper.ParentItemID != Guid.Empty) writer.WriteAttribute("data-parentid", itemWrapper.ParentItemID.ToString()); if (childrenCount > 0) { if (!isInPath) { var urlformat = "~/api/contents/items?name={0}&slug={1}&parentId={2}"; var popupUrl = Url.Content(string.Format(urlformat, list.Name, list.Views.Default.Name, itemWrapper.ID.ToString())); writer.WriteAttribute("data-popupurl", popupUrl); } } writer.WriteAttribute("class", _class); writer.Write(Html32TextWriter.TagRightChar); writer.WriteBeginTag("a"); writer.WriteAttribute("href", itemWrapper.UrlComponent); writer.Write(Html32TextWriter.TagRightChar); writer.Write(itemWrapper.Value(fieldName).Raw); writer.WriteEndTag("a"); if (childrenCount > 0) { if (isInPath) { writer.WriteFullBeginTag("ul"); RenderChildren(writer, list, children, current); writer.WriteEndTag("ul"); } } writer.WriteEndTag("li"); } }