Exemple #1
0
        public static void RadioButtonForModel(
            HtmlHelper htmlHelper, XcstWriter output, object value, bool isChecked, HtmlAttribs?htmlAttributes = null)
        {
            ModelMetadata metadata = htmlHelper.ViewData.ModelMetadata;

            RadioButtonForMetadata(htmlHelper, output, metadata, String.Empty, value, /*isChecked: */ isChecked, htmlAttributes);
        }
 public void WriteTo(XcstWriter output)
 {
     foreach (var item in this)
     {
         HtmlAttributeHelper.WriteAttribute(item.Key, item.Value, output);
     }
 }
Exemple #3
0
        static void RadioButtonHelper(
            HtmlHelper htmlHelper, XcstWriter output, ModelMetadata?metadata, string name, object value, bool?isChecked, HtmlAttribs?htmlAttributes)
        {
            bool explicitChecked = isChecked.HasValue;

            if (explicitChecked &&
                htmlAttributes != null &&
                htmlAttributes.ContainsKey("checked"))
            {
                htmlAttributes = htmlAttributes.Clone();
                htmlAttributes.Remove("checked"); // Explicit value must override dictionary
            }

            InputHelper(
                htmlHelper,
                output,
                InputType.Radio,
                metadata,
                name,
                value,
                useViewData: false,
                isChecked: isChecked.GetValueOrDefault(),
                setId: true,
                isExplicitValue: true,
                format: null,
                htmlAttributes: htmlAttributes);
        }
Exemple #4
0
        public static void Label(
            HtmlHelper html, XcstWriter output, string expression, string?labelText = null, HtmlAttribs?htmlAttributes = null)
        {
            ModelMetadata metadata = ModelMetadata.FromStringExpression(expression, html.ViewData);

            LabelHelper(html, output, metadata, expression, labelText, htmlAttributes);
        }
Exemple #5
0
        public static void InputForModel(
            HtmlHelper htmlHelper, XcstWriter output, object?value = null, string?type = null, string?format = null, HtmlAttribs?htmlAttributes = null)
        {
            ModelMetadata metadata = htmlHelper.ViewData.ModelMetadata;

            InputForMetadata(htmlHelper, output, type, metadata, /*expression: */ String.Empty, value, format, htmlAttributes);
        }
        internal static IList <SelectListItem> EnumOptions(Type enumType, XcstWriter output, string?formatString = null, bool applyFormatInEdit = false)
        {
            Debug.Assert(enumType.IsEnum);

            var selectList = new List <SelectListItem>();

            const BindingFlags BindingFlags = BindingFlags.DeclaredOnly
                                              | BindingFlags.GetField
                                              | BindingFlags.Public
                                              | BindingFlags.Static;

            foreach (FieldInfo field in enumType.GetFields(BindingFlags))
            {
                object enumValue = field.GetValue(null);

                string value = (formatString != null && applyFormatInEdit) ?
                               String.Format(CultureInfo.CurrentCulture, formatString, enumValue)
               : field.Name;

                string text = (formatString != null && !applyFormatInEdit) ?
                              output.SimpleContent.Format(formatString, enumValue)
               : DisplayNameUtil.GetFieldDisplayName(field);

                selectList.Add(new SelectListItem {
                    Value = value,
                    Text  = text,
                });
            }

            return(selectList);
        }
        public static void EnumTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            string?            className      = GetEditorCssClass(new EditorInfo("Enum", "select"), null);
            var                htmlAttributes = CreateHtmlAttributes(html, className);
            ViewDataDictionary viewData       = html.ViewData;

            Type modelType = viewData.ModelMetadata.ModelType;
            Type enumType  = Nullable.GetUnderlyingType(modelType) ?? modelType;

            if (!enumType.IsEnum)
            {
                throw new InvalidOperationException("Enum template can only be used on Enum members.");
            }

            string?formatString = viewData.ModelMetadata.EditFormatString
                                  ?? viewData.ModelMetadata.DisplayFormatString;

            bool applyFormatInEdit = viewData.ModelMetadata.EditFormatString != null;

            IList <SelectListItem> options = EnumOptions(enumType, output, formatString, applyFormatInEdit);
            string optionLabel             = viewData.ModelMetadata.Watermark ?? String.Empty;

            SelectInstructions.SelectHelper(html, output, viewData.ModelMetadata, String.Empty, options, optionLabel, multiple: false, htmlAttributes: htmlAttributes);
        }
        public static void BooleanTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            ViewDataDictionary viewData = html.ViewData;

            bool?value = null;

            if (viewData.Model != null)
            {
                value = Convert.ToBoolean(viewData.Model, CultureInfo.InvariantCulture);
            }

            if (viewData.ModelMetadata.IsNullableValueType)
            {
                XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

                string?className      = GetEditorCssClass(new EditorInfo("Boolean", "select"), "list-box tri-state");
                var    htmlAttributes = CreateHtmlAttributes(html, className);

                SelectInstructions.Select(html, output, String.Empty, TriStateValues(value), htmlAttributes: htmlAttributes);
            }
            else
            {
                string?className      = GetEditorCssClass(new EditorInfo("Boolean", "input", InputType.CheckBox), "check-box");
                var    htmlAttributes = CreateHtmlAttributes(html, className);

                InputInstructions.CheckBox(html, package, seqOutput, String.Empty, value.GetValueOrDefault(), htmlAttributes: htmlAttributes);
            }
        }
Exemple #9
0
 public static void WriteBoolean(string key, bool value, XcstWriter output)
 {
     if (value)
     {
         output.WriteAttributeString(key, key);
     }
 }
Exemple #10
0
        public static void LabelFor <TModel, TValue>(
            HtmlHelper <TModel> html, XcstWriter output, Expression <Func <TModel, TValue> > expression, string?labelText = null, HtmlAttribs?htmlAttributes = null)
        {
            ModelMetadata metadata         = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
            string        expressionString = ExpressionHelper.GetExpressionText(expression);

            LabelHelper(html, output, metadata, expressionString, labelText, htmlAttributes);
        }
Exemple #11
0
        public static void InputFor <TModel, TProperty>(
            HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, string?type = null, string?format = null, HtmlAttribs?htmlAttributes = null)
        {
            ModelMetadata metadata   = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string        exprString = ExpressionHelper.GetExpressionText(expression);

            InputForMetadata(htmlHelper, output, type, metadata, exprString, /*value: */ null, format, htmlAttributes);
        }
        public static void PasswordTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            string?className      = GetEditorCssClass(new EditorInfo("Password", "input", InputType.Password), "text-box single-line password");
            var    htmlAttributes = CreateHtmlAttributes(html, className, addMetadataAttributes: true);

            InputInstructions.Input(html, output, String.Empty, value: null, type: "password", htmlAttributes: htmlAttributes);
        }
Exemple #13
0
        internal static void WriteId(string name, XcstWriter output)
        {
            string?sanitizedId = TagBuilder.CreateSanitizedId(name);

            if (!String.IsNullOrEmpty(sanitizedId))
            {
                output.WriteAttributeString("id", sanitizedId);
            }
        }
        public static void ValidationMessageFor <TModel, TProperty>(
            HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, string?validationMessage = null,
            HtmlAttribs?htmlAttributes = null, string?tag = null)
        {
            ModelMetadata metadata         = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string        expressionString = ExpressionHelper.GetExpressionText(expression);

            ValidationMessageHelper(htmlHelper, output, metadata, expressionString, validationMessage, htmlAttributes, tag);
        }
        internal static void TextAreaHelper(
            HtmlHelper htmlHelper, XcstWriter output, ModelMetadata metadata, string name, IDictionary <string, object> rowsAndColumns,
            HtmlAttribs?htmlAttributes, string?innerHtmlPrefix = null)
        {
            string fullName = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (String.IsNullOrEmpty(fullName))
            {
                throw new ArgumentNullException(nameof(name));
            }

            output.WriteStartElement("textarea");
            HtmlAttributeHelper.WriteId(fullName, output);
            output.WriteAttributeString("name", fullName);
            HtmlAttributeHelper.WriteAttributes(rowsAndColumns, output);

            bool explicitRowsAndCols = rowsAndColumns != implicitRowsAndColumns;

            // If there are any errors for a named field, we add the css attribute.

            string?cssClass = (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out ModelState modelState) &&
                               modelState.Errors.Count > 0) ? HtmlHelper.ValidationInputCssClassName : null;

            HtmlAttributeHelper.WriteClass(cssClass, htmlAttributes, output);
            HtmlAttributeHelper.WriteAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata), output);

            // name cannnot be overridden, and class was already written
            // explicit rows and cols cannot be overridden

            HtmlAttributeHelper.WriteAttributes(
                htmlAttributes,
                output,
                excludeFn: n => n == "name" || n == "class" || (explicitRowsAndCols && (n == "rows" || n == "cols")));

            string?value;

            if (modelState?.Value != null)
            {
                value = modelState.Value.AttemptedValue;
            }
            else if (metadata.Model != null)
            {
                value = Convert.ToString(metadata.Model, CultureInfo.CurrentCulture);
            }
            else
            {
                value = String.Empty;
            }

            // The first newline is always trimmed when a TextArea is rendered, so we add an extra one
            // in case the value being rendered is something like "\r\nHello".

            output.WriteString((innerHtmlPrefix ?? Environment.NewLine) + value);

            output.WriteEndElement();
        }
Exemple #16
0
        static void InputImpl(
            HtmlHelper htmlHelper, XcstWriter output, string?type, ModelMetadata?metadata, string expression, object?value,
            bool?useViewData, string?format, HtmlAttribs?htmlAttributes)
        {
            InputType?inputType       = GetInputType(type);
            bool      checkBoxOrRadio = inputType == InputType.CheckBox ||
                                        inputType == InputType.Radio;

            if (type != null &&
                (inputType is null || checkBoxOrRadio) &&
                (htmlAttributes is null || !htmlAttributes.ContainsKey("type")))
            {
                htmlAttributes         = htmlAttributes?.Clone() ?? new HtmlAttributeDictionary();
                htmlAttributes["type"] = type;
            }

            if (checkBoxOrRadio)
            {
                // Don't want Input() to behave like RadioButton() or CheckBox()
                inputType = null;
            }

            if (inputType == InputType.Hidden)
            {
#if ASPNETMVC
                if (value is Binary binaryValue)
                {
                    value = binaryValue.ToArray();
                }
#endif

                if (value is byte[] byteArrayValue)
                {
                    value = Convert.ToBase64String(byteArrayValue);
                }
            }

            if (useViewData is null)
            {
                useViewData = (value is null);
            }

            InputHelper(
                htmlHelper,
                output,
                inputType ?? InputType.Text,
                metadata: metadata,
                name: expression,
                value: value,
                useViewData: useViewData.Value,
                isChecked: false,
                setId: true,
                isExplicitValue: true,
                format: format,
                htmlAttributes: htmlAttributes);
        }
        public static void MultilineTextTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            object value          = html.ViewData.TemplateInfo.FormattedModelValue;
            string?className      = GetEditorCssClass(new EditorInfo("MultilineText", "textarea"), "text-box multi-line");
            var    htmlAttributes = CreateHtmlAttributes(html, className, addMetadataAttributes: true);

            TextAreaInstructions.TextArea(html, output, String.Empty, value, 0, 0, htmlAttributes);
        }
Exemple #18
0
        public static void RadioButton(
            HtmlHelper htmlHelper, XcstWriter output, string name, object value, bool isChecked, HtmlAttribs?htmlAttributes = null)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            RadioButtonHelper(htmlHelper, output, /*metadata: */ null, name, value, isChecked, htmlAttributes);
        }
Exemple #19
0
        public static void UrlTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            ViewDataDictionary viewData = html.ViewData;

            output.WriteStartElement("a");
            output.WriteAttributeString("href", Convert.ToString(viewData.Model, CultureInfo.InvariantCulture));
            output.WriteString(output.SimpleContent.Convert(viewData.TemplateInfo.FormattedModelValue));
            output.WriteEndElement();
        }
        static void HtmlInputTemplateHelper(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput, string templateName, string?inputType = null)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            object value = html.ViewData.TemplateInfo.FormattedModelValue;

            string?className      = GetEditorCssClass(new EditorInfo(templateName, "input", InputType.Text), "text-box single-line");
            var    htmlAttributes = CreateHtmlAttributes(html, className, inputType: inputType, addMetadataAttributes: true);

            InputInstructions.Input(html, output, name: String.Empty, value: value, htmlAttributes: htmlAttributes);
        }
        public static void ListBoxTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            string?            className      = GetEditorCssClass(new EditorInfo("ListBox", "select"), null);
            var                htmlAttributes = CreateHtmlAttributes(html, className);
            ViewDataDictionary viewData       = html.ViewData;

            IEnumerable <SelectListItem>?options = Options(viewData);

            SelectInstructions.SelectHelper(html, output, viewData.ModelMetadata, String.Empty, options, optionLabel: null, multiple: true, htmlAttributes: htmlAttributes);
        }
Exemple #22
0
        GetHtml(HttpContext httpContext, XcstWriter output)
        {
            IAntiforgery antiforgery = GetAntiforgeryService(httpContext);

            AntiforgeryTokenSet tokenSet = antiforgery.GetAndStoreTokens(httpContext);

            output.WriteStartElement("input");
            output.WriteAttributeString("type", "hidden");
            output.WriteAttributeString("name", tokenSet.FormFieldName);
            output.WriteAttributeString("value", tokenSet.RequestToken);
            output.WriteEndElement();
        }
        public static void TextArea(
            HtmlHelper htmlHelper, XcstWriter output, string name, object?value = null, HtmlAttribs?htmlAttributes = null)
        {
            ModelMetadata metadata = ModelMetadata.FromStringExpression(name, htmlHelper.ViewData);

            if (value != null)
            {
                metadata.Model = value;
            }

            TextAreaHelper(htmlHelper, output, metadata, name, implicitRowsAndColumns, htmlAttributes);
        }
Exemple #24
0
        static void InputForMetadata(
            HtmlHelper htmlHelper, XcstWriter output, string?type, ModelMetadata?metadata, string expression, object?value,
            string?format, HtmlAttribs?htmlAttributes)
        {
            if (value is null &&
                metadata != null &&
                GetInputType(type) != InputType.Password)
            {
                value = metadata.Model;
            }

            InputImpl(htmlHelper, output, type, metadata, expression, value, /*useViewData: */ false, format, htmlAttributes);
        }
Exemple #25
0
        public static void ImageUrlTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            ViewDataDictionary viewData = html.ViewData;

            if (viewData.Model != null)
            {
                XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

                output.WriteStartElement("img");
                output.WriteAttributeString("src", Convert.ToString(viewData.Model, CultureInfo.InvariantCulture));
                output.WriteEndElement();
            }
        }
Exemple #26
0
        public static void RadioButtonFor <TModel, TProperty>(
            HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, object value, HtmlAttribs?htmlAttributes = null)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            ModelMetadata metadata         = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string        expressionString = ExpressionHelper.GetExpressionText(expression);

            RadioButtonForMetadata(htmlHelper, output, metadata, expressionString, value, /*isChecked: */ null, htmlAttributes);
        }
Exemple #27
0
        public static void GetHtml(HttpContextBase httpContext, XcstWriter output)
        {
            if (httpContext is null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }
            if (output is null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            _worker.GetFormInputElement(httpContext, output);
        }
        public static void TextAreaFor <TModel, TProperty>(
            HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, HtmlAttribs?htmlAttributes = null)
        {
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            ModelMetadata metadata         = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string        expressionString = ExpressionHelper.GetExpressionText(expression);

            TextAreaHelper(htmlHelper, output, metadata, expressionString, implicitRowsAndColumns, htmlAttributes);
        }
        public static void ValidationMessage(
            HtmlHelper htmlHelper, XcstWriter output, string modelName, string?validationMessage = null, HtmlAttribs?htmlAttributes = null,
            string?tag = null)
        {
            if (modelName is null)
            {
                throw new ArgumentNullException(nameof(modelName));
            }

            ModelMetadata metadata = ModelMetadata.FromStringExpression(modelName, htmlHelper.ViewData);

            ValidationMessageHelper(htmlHelper, output, metadata, modelName, validationMessage, htmlAttributes, tag);
        }
Exemple #30
0
        public static void SelectFor <TModel, TProperty>(
            HtmlHelper <TModel> htmlHelper, XcstWriter output, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem>?selectList = null,
            bool multiple = false, HtmlAttribs?htmlAttributes = null)
        {
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            ModelMetadata metadata         = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string        expressionString = ExpressionHelper.GetExpressionText(expression);

            SelectHelper(htmlHelper, output, metadata, expressionString, selectList, default(string), multiple, htmlAttributes);
        }