/// <summary> /// Additional attributes to assign to widget on render. /// </summary> /// <param name="widget">Widget attributes are targeted at.</param> /// <param name="attributes">Attributes to append to.</param> public virtual void AppendWidgetAttributes(IWidget widget, ElementAttributesDictionary attributes) { if (this.Required) { attributes.AddCssClass("required"); } }
/// <summary> /// Returns this Widget rendered as HTML. /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="value">Value of the field being rendered.</param> /// <param name="extraAttributes">Attributes to assign to HTML entity.</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { StringBuilder sb = new StringBuilder(); ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); finalAttributes["multiple"] = "multiple"; finalAttributes.SetDefault("name", name); // Create selected choices list IList <object> selectedChoices = value as List <object>; if (selectedChoices == null) { if (value != null && !string.Empty.Equals(value)) { selectedChoices = new object[] { value } } ; } // Render output sb.AppendFormat(CultureInfo.CurrentUICulture, "<select{0}>\n", finalAttributes); RenderOptions(sb, selectedChoices); sb.Append("</select>"); return(sb.ToString()); }
/// <summary> /// Build Element attributes for rendering widget /// </summary> /// <param name="extraAttributes">Additional attributes to apply</param> /// <returns>Attributes for rendering</returns> protected ElementAttributesDictionary BuildAttributes(ElementAttributesDictionary extraAttributes) { var finalAttributes = new ElementAttributesDictionary(GetAttributes()); finalAttributes.Append(extraAttributes); return(finalAttributes); }
/// <summary> /// Constructor /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="value">Value of the field being rendered.</param> /// <param name="attributes">Attributes to assign to HTML entity.</param> /// <param name="choice">Choice for this element.</param> /// <param name="index">This elements position in the radio button collection.</param> public RadioInput(string name, string value, ElementAttributesDictionary attributes, IChoice choice, int index) { this.Name = name; this.Value = value; this.Attributes = new ElementAttributesDictionary(attributes); this.Choice = choice; this.Index = index; }
/// <summary> /// Additional attributes to assign to widget on render. /// </summary> /// <param name="widget">Widget attributes are targeted at.</param> /// <param name="attributes">Attributes to append to.</param> public override void AppendWidgetAttributes(IWidget widget, ElementAttributesDictionary attributes) { base.AppendWidgetAttributes(widget, attributes); if (this.MaxLength != null && widget is Widgets.TextInput) { attributes.SetDefault("maxlength", this.MaxLength.ToString()); } }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { if (!this.RenderValue) { value = null; } return(base.Render(name, value, extraAttributes)); }
/// <summary> /// Element attributes, this can be an ElementAttributesDictionary or an anon object dictionary /// </summary> public ElementAttributesDictionary GetAttributes() { if (this._attributes == null) { this._attributes = new ElementAttributesDictionary(); } return(this._attributes); }
public void ToStringTest() { ElementAttributesDictionary target = new ElementAttributesDictionary(); target.Add("Test1", "Test1"); target.Add("Test2", "Test2"); Assert.AreEqual(" Test1=\"Test1\" Test2=\"Test2\"", target.ToString()); }
/// <summary> /// Render a single choice item into HTML. /// </summary> /// <param name="output">Append output to this string builder.</param> /// <param name="name">Form name of the item being rendered.</param> /// <param name="value">Choice item being rendered.</param> public override void RenderItemChoice(StringBuilder output, string name, object value) { var attributes = new ElementAttributesDictionary(); attributes.SetDefault("class", ItemCssClass); output.AppendFormat(CultureInfo.InvariantCulture, "<li{0}>{1}{2}</li>", attributes, value, new HiddenInput().Render(name, value)); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); finalAttributes["keytype"] = "rsa"; // Override any other value finalAttributes.SetDefault("name", name); return(string.Format(CultureInfo.CurrentUICulture, "<keygen{0} />", finalAttributes)); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { string id = extraAttributes.Get("id", name); return(string.Format(CultureInfo.CurrentUICulture, "{0}\n<div id=\"{1}_ui\"></div>", new HiddenInput().Render(name, value == null ? this.Minimum : value, extraAttributes), id)); }
/// <summary> /// Returns this Widget rendered as HTML. /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="value">Value of the field being rendered.</param> /// <param name="extraAttributes">Attributes to assign to HTML entity.</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { string strValue = value == null ? string.Empty : value.ToString(); if (extraAttributes == null) { extraAttributes = new ElementAttributesDictionary(); } return(this.Renderer(name, strValue, extraAttributes, Choices)); }
public void RenderTest() { Input target = CreateInput(); string name = "testname"; ElementAttributesDictionary extraAttributes = new ElementAttributesDictionary(); extraAttributes.Add("Test1", "Test1"); Assert.AreEqual("<input type=\"test\" name=\"testname\" value=\"123\" />", target.Render(name, "123")); Assert.AreEqual("<input Test1=\"Test1\" type=\"test\" name=\"testname\" value=\"123\" />", target.Render(name, "123", extraAttributes)); }
/// <summary> /// Returns this Widget rendered as HTML. /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="value">Value of the field being rendered.</param> /// <param name="extraAttributes">Attributes to assign to HTML entity.</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { var choices = value as IEnumerable <object>; if (choices != null) { return(RenderCollection(name, choices, extraAttributes)); } else { return(RenderCollection(name, new Collection <object>(), extraAttributes)); } }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { if (DisplayInline) { string id = extraAttributes.Get("id", name); return(string.Format(CultureInfo.CurrentUICulture, "{0}\n<div id=\"{1}_ui\"></div>", new HiddenInput().Render(name, value, extraAttributes), id)); } else { return(base.Render(name, value, extraAttributes)); } }
public void ElementAttributesDictionaryConstructorTest1() { ElementAttributesDictionary source = new ElementAttributesDictionary(); source.Add("Test1", "Test1"); source.Add("Test2", "Test2"); ElementAttributesDictionary target = new ElementAttributesDictionary(source); Assert.AreEqual <int>(2, target.Count); Assert.IsTrue(target.ContainsKey("Test1")); Assert.AreEqual("Test1", target["Test1"]); Assert.IsTrue(target.ContainsKey("Test2")); Assert.AreEqual("Test2", target["Test2"]); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); finalAttributes.SetDefault("cols", "40"); finalAttributes.SetDefault("rows", "10"); finalAttributes.SetDefault("name", name); if (value == null) { value = string.Empty; } return(string.Format(CultureInfo.InstalledUICulture, "<textarea{0}>{1}</textarea>", finalAttributes, value)); }
public void ToStringTest() { var attributes = new ElementAttributesDictionary(); attributes.AppendObject(new { id = "TestID" }); IChoice choice = new Choice("Test1"); int index = 4; RadioInput target = new RadioInput("TestVal", "Test", attributes, choice, index); Assert.AreEqual("<label for=\"TestID_4\">Test1 <input id=\"TestID_4\" type=\"radio\" name=\"TestVal\" value=\"Test1\" /></label>", target.ToString()); target = new RadioInput("TestVal", "Test1", attributes, choice, index); Assert.AreEqual("<label for=\"TestID_4\">Test1 <input id=\"TestID_4\" type=\"radio\" name=\"TestVal\" value=\"Test1\" checked=\"checked\" /></label>", target.ToString()); }
public void RadioInputConstructorTest() { string name = "Test1"; string value = "TestVal"; ElementAttributesDictionary attributes = new ElementAttributesDictionary(); IChoice choice = new Choice("test"); int index = 4; RadioInput target = new RadioInput(name, value, attributes, choice, index); Assert.AreEqual(name, target.Name); Assert.AreEqual(value, target.Value); Assert.AreEqual(index, target.Index); Assert.AreSame(choice, target.Choice); AssertExtras.DictionaryIsEqual(attributes, target.Attributes); }
/// <summary> /// Custom render function. /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="value">Value of the field being rendered.</param> /// <param name="attributes">Attributes to assign to HTML entity.</param> /// <param name="choices">Options to offer user.</param> /// <returns>String represented rendered items.</returns> private static string DefaultRadioFieldRenderer(string name, string value, ElementAttributesDictionary attributes, IEnumerable <Choice> choices) { StringBuilder sb = new StringBuilder(); sb.Append("<ul>\n"); int index = 0; foreach (var choice in choices) { sb.AppendFormat("<li>{0}</li>\n", new RadioInput(name, value, attributes, choice, index++).Render()); } sb.Append("</ul>"); return(sb.ToString()); }
/// <summary> /// Returns this Widget rendered as HTML. /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="value">Value of the field being rendered.</param> /// <param name="extraAttributes">Attributes to assign to HTML entity.</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { const string forFormat = " for=\"{0}\""; const string itemFormat = "<li><label{0}>{1} {2}</label></li>\n"; StringBuilder sb = new StringBuilder(); // Setup attributes ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); finalAttributes.SetDefault("name", name); string id = finalAttributes.Get("id"); // Create selected choices list IList <object> selectedChoices = ConversionHelper.ObjectList(value); // Render choices sb.Append("<ul>\n"); int index = 0; foreach (IChoice choice in Choices) { // Generate label id string labelFor = string.Empty; if (!string.IsNullOrEmpty(id)) { string newId = string.Concat(id, "_", index.ToString(CultureInfo.InvariantCulture)); finalAttributes["id"] = newId; labelFor = string.Format(CultureInfo.CurrentUICulture, forFormat, newId, index++); } // Render checkbox var cb = new CheckBoxInput() { Attributes = finalAttributes, CheckTest = v => selectedChoices == null ? false : selectedChoices.Contains(v) // Lambda expression }; string renderedCb = cb.Render(name, choice.Value); sb.AppendFormat(CultureInfo.CurrentUICulture, itemFormat, labelFor, renderedCb, HttpUtility.HtmlEncode(choice.Label)); } sb.Append("</ul>"); return(sb.ToString()); }
public void RenderTest() { var target = new CheckBoxSelectMultiple(); target.Choices = ChoiceHelper.Range(1, 5); Assert.AreEqual("<ul>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"1\" /> 1</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"2\" /> 2</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"3\" /> 3</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"4\" /> 4</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"5\" /> 5</label></li>\n</ul>", target.Render("Test1", null)); Assert.AreEqual("<ul>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"1\" /> 1</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" checked=\"checked\" value=\"2\" /> 2</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" value=\"3\" /> 3</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" checked=\"checked\" value=\"4\" /> 4</label></li>\n<li><label><input name=\"Test1\" type=\"checkbox\" checked=\"checked\" value=\"5\" /> 5</label></li>\n</ul>", target.Render("Test1", new object [] { 2, 4, 5 })); ElementAttributesDictionary extraAttributes = new ElementAttributesDictionary { { "id", "Test1" } }; Assert.AreEqual("<ul>\n<li><label for=\"Test1_0\"><input id=\"Test1_0\" name=\"Test1\" type=\"checkbox\" value=\"1\" /> 1</label></li>\n<li><label for=\"Test1_1\"><input id=\"Test1_1\" name=\"Test1\" type=\"checkbox\" checked=\"checked\" value=\"2\" /> 2</label></li>\n<li><label for=\"Test1_2\"><input id=\"Test1_2\" name=\"Test1\" type=\"checkbox\" value=\"3\" /> 3</label></li>\n<li><label for=\"Test1_3\"><input id=\"Test1_3\" name=\"Test1\" type=\"checkbox\" checked=\"checked\" value=\"4\" /> 4</label></li>\n<li><label for=\"Test1_4\"><input id=\"Test1_4\" name=\"Test1\" type=\"checkbox\" checked=\"checked\" value=\"5\" /> 5</label></li>\n</ul>", target.Render("Test1", new object[] { 2, 4, 5 }, extraAttributes)); }
/// <summary> /// Renders out the form object and previous field information. /// </summary> /// <param name="stepForm">Form to render.</param> /// <param name="data">Form data posted back to page.</param> /// <param name="stepIndex">Current step</param> /// <returns>View result object.</returns> protected ViewResult Render(IForm stepForm, NameValueCollection data, int stepIndex) { var hiddenFields = new List <string>(); // Add current step hiddenFields.Add(new MvcForms.Widgets.HiddenInput().Render(StepFieldName, stepIndex, ElementAttributesDictionary.Create(new { id = "id_" + StepFieldName }))); for (int idx = 0; idx < _forms.Count; idx++) { if (idx != stepIndex) { // Check if we are dealing with a form var form = _forms[idx] as Form; if (form != null) { // TODO: Add hash support foreach (var boundField in form) { hiddenFields.Add(boundField.AsHidden()); } continue; } // Check if we are dealing with a form group var formGroup = _forms[idx] as FormGroup; if (formGroup != null) { foreach (var boundForm in formGroup) { // TODO: Add hash support foreach (var boundField in boundForm.Form) { hiddenFields.Add(boundField.AsHidden()); } } } } } return(RenderTemplate(stepForm, string.Join("\n", hiddenFields.ToArray()))); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); finalAttributes["type"] = "checkbox"; // Override any other value finalAttributes.SetDefault("name", name); bool result = _checkedTest(value); if (result) { finalAttributes.SetDefault("checked", "checked"); } if (value != null) { finalAttributes.SetDefault("value", value.ToString()); } return(string.Format(CultureInfo.CurrentUICulture, "<input{0} />", finalAttributes)); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); finalAttributes["type"] = this.inputType; // Override any other value finalAttributes.SetDefault("name", name); if (DisableAutoComplete) { finalAttributes.SetDefault("autocomplete", "off"); } if (value != null) { string stringValue = value.ToString(); if (!string.IsNullOrEmpty(stringValue)) { finalAttributes.SetDefault("value", stringValue); } } return(string.Format(CultureInfo.InstalledUICulture, "<input{0} />", finalAttributes.ToString())); }
public void AppendWidgetAttributesTest() { var target = new StringField(); var test1 = new ElementAttributesDictionary(); target.AppendWidgetAttributes(new Widgets.TextInput(), test1); Assert.AreEqual(null, test1.Get("maxlength", null)); target.MaxLength = 10; var test2 = new ElementAttributesDictionary(); target.AppendWidgetAttributes(new Widgets.TextArea(), test2); Assert.AreEqual(null, test2.Get("maxlength", null)); var test3 = new ElementAttributesDictionary(); target.AppendWidgetAttributes(new Widgets.TextInput(), test3); Assert.AreEqual("10", test3.Get("maxlength", null)); }
public void AddCssClassTest() { ElementAttributesDictionary target = new ElementAttributesDictionary(); target.AddCssClass(null); Assert.IsFalse(target.ContainsKey("class")); target.AddCssClass(string.Empty); Assert.IsFalse(target.ContainsKey("class")); target.AddCssClass("class1"); Assert.IsTrue(target.ContainsKey("class")); Assert.AreEqual("class1", target["class"]); target.AddCssClass("class2"); Assert.AreEqual("class1 class2", target["class"]); target.AddCssClass("class1"); Assert.AreEqual("class1 class2", target["class"]); target.AddCssClass("class2"); Assert.AreEqual("class1 class2", target["class"]); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML</returns> public override string Render(string name, object value, ElementAttributesDictionary extraAttributes) { // Correct value Tuple values = value as Tuple; if (values == null) { values = Decompress(value); } // Allow derived classes to make final changes PreRenderWidgets(values); // Build attributes ElementAttributesDictionary finalAttributes = BuildAttributes(extraAttributes); string id = finalAttributes.Get("id", null); // Build output var output = new List <string>(); for (int index = 0; index < Widgets.Count; index++) { // Get value object widgetValue = values.Get(index); // Update ID if (id != null) { finalAttributes["id"] = string.Concat(id, "_", index); } // Render item output.Add(Widgets[index].Render(string.Concat(name, index), widgetValue, finalAttributes)); } return(FormatOutput(output.ToArray())); }
/// <summary> /// Returns this Widget collection rendered as HTML. /// </summary> /// <param name="name">Form name of the widget being renderd.</param> /// <param name="values">Values to be rendered.</param> /// <param name="extraAttributes">Attributes to assign to HTML entity.</param> /// <returns>HTML</returns> public virtual string RenderCollection(string name, IEnumerable <object> values, ElementAttributesDictionary extraAttributes) { var output = new StringBuilder(); var valueName = string.Concat(name, "[]"); output.AppendFormat(CultureInfo.InvariantCulture, "<ul{0}>\n", extraAttributes); foreach (var value in values) { RenderItemChoice(output, valueName, value); } output.AppendFormat(CultureInfo.InvariantCulture, "</ul>"); return(output.ToString()); }
/// <summary> /// Returns this Widget rendered as HTML /// </summary> /// <param name="name">Form name of the widget being renderd</param> /// <param name="value">Value of the field being rendered</param> /// <param name="extraAttributes">Attributes to assign to HTML entity</param> /// <returns>HTML.</returns> public virtual string Render(string name, object value, ElementAttributesDictionary extraAttributes) { throw new NotImplementedException(); }