Esempio n. 1
0
        public static FormViewModel CreateForm()
        {
            var htmlContent = new HtmlElementBuilder().SetName("HtmlContent").SetContent(
                "<h2 style=\"text-align: center;\"><strong>Demo Form<br /></strong></h2>\r\n<p>Test HTML Content embedded in the demo form</p>\r\n<ul>\r\n<li>FirstName / LastName</li>\r\n<li>Years of experiance</li>\r\n<li>Accept Terms and Conditions</li>\r\n<li>Submit</li>\r\n</ul>")
                              .Build();

            var firstNameText = new TextElementBuilder()
                                .SetName("First Name")
                                .SetLabel(ElementLabel.Left("First Name: "))
                                .SetDefaultValue("First Name")
                                .AddRules(
                ValidationRule <ITextElementViewModel> .Create(new TextElement_MaximumLength_Validator(10))
                .WithFailMessage("First Name must be less then 10 characters"),
                ValidationRule <ITextElementViewModel> .Create(new TextElement_MinimunLength_Validator(2))
                .WithFailMessage("First Name must be greater then 2 characters"))
                                .Build();

            var lastNameText = new TextElementBuilder()
                               .SetName("Last Name")
                               .SetLabel(ElementLabel.Left("Last Name: "))
                               .SetDefaultValue("Last Name")
                               .AddRules(
                ValidationRule <ITextElementViewModel> .Create(new TextElement_MaximumLength_Validator(10))
                .WithFailMessage("Last Name must be less then 10 characters"),
                ValidationRule <ITextElementViewModel> .Create(new TextElement_MinimunLength_Validator(2))
                .WithFailMessage("Last Name must be greater then 2 characters"))
                               .Build();

            var personalDetails = new ColumnBuilder().SetName("personalDetails")
                                  .AddChildren(firstNameText, lastNameText).Build();


            var experienceSelection = new SelectElementBuilder().SetName("experienceSelection")
                                      .SetLabel(ElementLabel.Left("How many years experience have you got?")).AddValues("0-1", "1-5", "5+")
                                      .SetDefaultValue("Select a value").Build();
            var termsAndConditions = new CheckElementBuilder().SetName("TermsAndConditions")
                                     .SetLabel(ElementLabel.Left("Do you accept the terms and conditions?")).SetContent("Yes / No").AddRules(
                ValidationRule <ICheckElementViewModel> .Create(new CheckElement_IsChecked_Validator())
                .WithFailMessage("You must accept the terms and conditions")).Build();

            var submitButton = new ButtonElementBuilder().SetName("Submit Button").SetContent("Submit")
                               .SetVerticalAlignment(VerticalAlignment.Bottom).Build();

            var submitEventListener = new EventListener("submit",
                                                        new ElementNameEventSpecification("submit").And(
                                                            new TypeNameEventSpecification(nameof(ButtonElementClickedEvent))),
                                                        new IdentitySpecification <FormViewModel>());

            var rowGroup1 = new RowBuilder().SetName("Data Entry Rows")
                            .AddChildren(htmlContent, personalDetails, experienceSelection, termsAndConditions).Build();

            var rootGroup = new GroupBuilder().SetName("Test Group 1").SetTitle("Demo Form Title")
                            .AddChildren(rowGroup1, submitButton).Build();

            var newForm = new FormBuilder().SetName("Demo Form").SetTitle("Demo Form Title").SetRoot(rootGroup)
                          .AddEventListeners(submitEventListener)
                          .Build();

            return(new FormViewModel(newForm));
        }
Esempio n. 2
0
        private static string InputBuilder(HtmlControlHelper helper,
                                           string type, string name, object value,
                                           IDictionary <string, object> attributes)
        {
            if (attributes == null)
            {
                attributes = new ValueDictionary();
            }

            if (value == null)
            {
                attributes.Remove("value");
            }

            HtmlElementBuilder htb = new HtmlElementBuilder("input");

            htb.Attributes.Merge(attributes, true);
            htb.Attributes.Merge("name", name, true);
            htb.Attributes.Merge("type", type, true);

            if (value != null)
            {
                htb.Attributes.Merge("value", value, true);
            }

            return(htb.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Renders the ScriptManager to the page.
        /// </summary>
        public void Render()
        {
            TextWriter         writer  = _context.Context.Response.Output;
            HtmlElementBuilder builder = new HtmlElementBuilder("script",
                                                                new { Type = "text/javascript" }, String.Empty);

            foreach (string path in _includes.Values)
            {
                builder.Attributes.Merge("src", path, true);
                writer.WriteLine(builder.ToString());
            }

            if (_blocks.Values.Any(b => !b.IsEmpty))
            {
                builder.Attributes.Remove("src");
                writer.WriteLine(builder.ToString(HtmlElementRenderMode.StartTag));
                writer.WriteLine("//<![CDATA[");

                foreach (ScriptBlock script in _blocks.Values)
                {
                    script.Render(writer);
                }

                writer.WriteLine("//]]>");
                writer.WriteLine(builder.ToString(HtmlElementRenderMode.EndTag));
            }
            _blocks.Clear();
            _includes.Clear();
        }
Esempio n. 4
0
        public string Tag(string tagName, object attributes, params string[] content)
        {
            HtmlElementBuilder builder = new HtmlElementBuilder(tagName, attributes,
                                                                (content == null) ? null : String.Join("", content));

            return(builder.ToString());
        }
Esempio n. 5
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !_disposed)
            {
                _disposed = true;

                HtmlElementBuilder builder = new HtmlElementBuilder("form");
                _response.Write(builder.ToString(HtmlElementRenderMode.EndTag));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Returns the string for a hidden input containing a
        /// token used to prevent CSRF attacks.
        /// </summary>
        /// <param name="value">An extended value to store in the token.</param>
        public string Token(string value)
        {
            RequestValidationToken token = RequestValidationToken.Create(Context.Context);

            HtmlElementBuilder builder = new HtmlElementBuilder("input");

            builder.Attributes.Add("type", "hidden");
            builder.Attributes.Add("name", ValidateRequestTokenAttribute.ValidationFieldName);
            builder.Attributes.Add("value", token.Serialize());

            return(builder.ToString());
        }
Esempio n. 7
0
        private static string LinkBuilder(HtmlControlHelper helper,
                                          string url, string text, IDictionary <string, object> attributes)
        {
            HtmlElementBuilder builder = new HtmlElementBuilder("a");

            if (attributes == null)
            {
                attributes = new ValueDictionary();
            }

            builder.Attributes.Merge(attributes);
            builder.Attributes.Merge("href", url, true);
            builder.InnerHtml = text;

            return(builder.ToString());
        }
Esempio n. 8
0
        public static string Serialize(this HtmlHelper helper,
                                       string name, object data, SerializationMode mode)
        {
            Precondition.Require(helper, () => Error.ArgumentNull("helper"));
            Precondition.Defined(name, () => Error.ArgumentNull("name"));

            ModelStateSerializer serializer = new ModelStateSerializer();
            string elementName = helper.Context.ViewData.Template.GetHtmlElementName(name);
            string value       = serializer.Serialize(data ?? helper.Context.ViewData.Model, mode);

            HtmlElementBuilder builder = new HtmlElementBuilder("input");

            builder.Attributes["type"]  = "hidden";
            builder.Attributes["name"]  = elementName;
            builder.Attributes["value"] = value;

            return(builder.ToString());
        }
Esempio n. 9
0
        private static MvcForm FormBuilder(HtmlControlHelper helper,
                                           FormMethod method, string action, IDictionary <string, object> attributes)
        {
            HtmlElementBuilder builder  = new HtmlElementBuilder("form");
            HttpResponseBase   response = helper.Context.Context.Response;

            if (attributes == null)
            {
                attributes = new ValueDictionary();
            }

            builder.Attributes.Merge(attributes);
            builder.Attributes.Merge("action", action, true);
            builder.Attributes.Merge("method", GetFormMethod(method), true);

            response.Write(builder.ToString(HtmlElementRenderMode.StartTag));
            return(new MvcForm(response));
        }
Esempio n. 10
0
        private static string TextAreaBuilder(HtmlControlHelper helper,
                                              string name, string value, int rows, int columns,
                                              IDictionary <string, object> attributes)
        {
            if (attributes == null)
            {
                attributes = new ValueDictionary();
            }

            HtmlElementBuilder htb = new HtmlElementBuilder("textarea");

            htb.Attributes.Merge(attributes, true);
            htb.Attributes.Merge("name", name, true);
            htb.Attributes.Merge("rows", rows, false);
            htb.Attributes.Merge("cols", columns, false);
            htb.InnerText = value ?? String.Empty;

            return(htb.ToString());
        }
Esempio n. 11
0
        private static string SelectBuilder(HtmlControlHelper helper,
                                            string name, MultiSelectList dataSource, bool isMultiple,
                                            IDictionary <string, object> attributes)
        {
            Precondition.Defined(name, () => Error.ArgumentNull("name"));

            if (attributes == null)
            {
                attributes = new ValueDictionary();
            }

            if (isMultiple)
            {
                attributes["multiple"] = "multiple";
            }
            else
            {
                attributes.Remove("multiple");
            }

            HtmlElementBuilder outer = new HtmlElementBuilder("select");

            outer.Attributes.Merge(attributes, true);
            outer.Attributes.Merge("name", name, true);

            if (dataSource != null)
            {
                StringBuilder builder = new StringBuilder();

                IEnumerable <ListItem> listItems = dataSource.GetListItems();
                string[] selectedValues          = null;

                if (helper.DataSource.Keys.Any(k => k.Equals(name, StringComparison.OrdinalIgnoreCase)))
                {
                    selectedValues = helper.DataSource.GetValue <string>(name, String.Empty)
                                     .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }

                builder.AppendLine();

                foreach (ListItem item in listItems)
                {
                    if (selectedValues != null)
                    {
                        item.Selected = selectedValues.Any(s =>
                                                           String.Equals(item.Value, s, StringComparison.OrdinalIgnoreCase));
                    }

                    HtmlElementBuilder inner = new HtmlElementBuilder("option");
                    inner.InnerText = item.Text;

                    if (item.Value != null)
                    {
                        inner.Attributes.Merge("value", item.Value, true);
                    }

                    if (item.Selected)
                    {
                        inner.Attributes.Merge("selected", "selected");
                    }

                    builder.AppendLine(inner.ToString());
                }
                outer.InnerHtml = builder.ToString();
            }
            return(outer.ToString());
        }
Esempio n. 12
0
        public string Tag(string tagName, object attributes)
        {
            HtmlElementBuilder builder = new HtmlElementBuilder(tagName, attributes, null);

            return(builder.ToString());
        }
Esempio n. 13
0
 public void SetUp()
 {
     ServiceLocator.ApplicationPathFinder = new ApplicationPathFinder("http://test");
     _builder = new HtmlElementBuilder(null);
 }