private ThemeVariableInfo MaterializeVariable(XmlElement xel) { string name = xel.GetAttribute("name"); string value = xel.InnerText; if (name.IsEmpty()) { throw new SmartException("The name attribute is required for the 'Var' element. Affected: '{0}' - element: {1}", _manifest.FullPath, xel.OuterXml); } string type = xel.GetAttribute("type").ToSafe("String"); var varType = ConvertVarType(type, xel, out var selectRef); if (varType != ThemeVariableType.String && value.IsEmpty()) { throw new SmartException("A value is required for non-string 'Var' elements. Affected: '{0}' - element: {1}", _manifest.FullPath, xel.OuterXml); } var info = new ThemeVariableInfo { Name = name, DefaultValue = value, Type = varType, SelectRef = selectRef, Manifest = _manifest }; return(info); }
public static MvcHtmlString ThemeVarEditor(this HtmlHelper html, ThemeVariableInfo info, object value) { Guard.ArgumentNotNull(info, "info"); string expression = html.NameForThemeVar(info); var strValue = string.Empty; var arrValue = value as string[]; if (arrValue != null) { strValue = arrValue.Length > 0 ? arrValue[0] : value.ToString(); } else { strValue = value.ToString(); } var currentTheme = ThemeHelper.ResolveCurrentTheme(); var isDefault = strValue.IsCaseInsensitiveEqual(info.DefaultValue); MvcHtmlString control; if (info.Type == ThemeVariableType.Color) { control = html.ColorBox(expression, strValue, info.DefaultValue); } else if (info.Type == ThemeVariableType.Boolean) { control = html.CheckBox(expression, strValue.ToBool()); } else if (info.Type == ThemeVariableType.Select) { control = ThemeVarSelectEditor(html, info, expression, strValue); } else { control = html.TextBox(expression, isDefault ? "" : strValue, new { placeholder = info.DefaultValue }); } if (currentTheme != info.Manifest) { // the variable is inherited from a base theme: display an info badge var chainInfo = "<span class='themevar-chain-info'><i class='fa fa-chain fa-flip-horizontal'></i> {0}</span>".FormatCurrent(info.Manifest.ThemeName); return MvcHtmlString.Create(control.ToString() + chainInfo); } else { return control; } }
public static MvcHtmlString ThemeVarLabel(this HtmlHelper html, ThemeVariableInfo info) { Guard.ArgumentNotNull(info, "info"); var result = new StringBuilder(); var resKey = "ThemeVar.{0}.{1}".FormatInvariant(info.Manifest.ThemeName, info.Name); var langId = EngineContext.Current.Resolve<IWorkContext>().WorkingLanguage.Id; var locService = EngineContext.Current.Resolve<ILocalizationService>(); var displayName = locService.GetResource(resKey, langId, false, Inflector.Titleize(info.Name)); var hint = locService.GetResource(resKey + ".Hint", langId, false, "", true); hint = "@(var_){0}{1}".FormatInvariant(info.Name, hint.HasValue() ? "\n" + hint : ""); result.Append("<div class='ctl-label'>"); result.Append(html.Label(html.NameForThemeVar(info), displayName)); result.Append(html.Hint(hint).ToHtmlString()); result.Append("</div>"); return MvcHtmlString.Create(result.ToString()); }
public static MvcHtmlString ThemeVarEditor(this HtmlHelper html, ThemeVariableInfo info, object value) { Guard.ArgumentNotNull(info, "info"); string expression = html.NameForThemeVar(info); if (info.Type == ThemeVariableType.Color) { return html.ColorBox(expression, value.ToString()); } else if (info.Type == ThemeVariableType.Boolean) { return html.CheckBox(expression, value.ToString().ToBool()); } else if (info.Type == ThemeVariableType.Select) { return ThemeVarSelectEditor(html, info, expression, value.ToString()); } return html.TextBox(expression, value); }
private ThemeVariableInfo MaterializeVariable(XmlElement xel) { string name = xel.GetAttribute("name"); string value = xel.InnerText; if (name.IsEmpty()) { throw new SmartException("The name attribute is required for the 'Var' element. Affected: '{0}' - element: {1}", _manifest.FullPath, xel.OuterXml); } if (value.IsEmpty()) { throw new SmartException("A value is required for the 'Var' element. Affected: '{0}' - element: {1}", _manifest.FullPath, xel.OuterXml); } string type = xel.GetAttribute("type").ToSafe("String"); string selectRef = null; var varType = ConvertVarType(type, xel, out selectRef); var info = new ThemeVariableInfo { Name = name, DefaultValue = value, Type = varType, SelectRef = selectRef, Manifest = _manifest }; return info; }
private static MvcHtmlString ThemeVarSelectEditor(HtmlHelper html, ThemeVariableInfo info, string expression, string value) { var manifest = info.Manifest; if (!manifest.Selects.ContainsKey(info.SelectRef)) { throw new SmartException("A select list with id '{0}' was not specified. Please specify a 'Select' element with at least one 'Option' child.", info.SelectRef); } //var isDefault = value.IsCaseInsensitiveEqual(info.DefaultValue); var selectList = from x in manifest.Selects[info.SelectRef] select new SelectListItem { Value = x, Text = x, // TODO: (MC) Localize Selected = x.IsCaseInsensitiveEqual(value) }; return html.DropDownList(expression, selectList, new { placeholder = info.DefaultValue }); }
public static string IdForThemeVar(this HtmlHelper html, ThemeVariableInfo info) { return TagBuilder.CreateSanitizedId(html.NameForThemeVar(info)); }
public static string NameForThemeVar(this HtmlHelper html, ThemeVariableInfo info) { return "values[{0}]".FormatInvariant(info.Name); }