コード例 #1
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #2
0
 public void WriteTo(XcstWriter output)
 {
     foreach (var item in this)
     {
         HtmlAttributeHelper.WriteAttribute(item.Key, item.Value, output);
     }
 }
コード例 #3
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #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);
        }
コード例 #5
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
 public static void WriteBoolean(string key, bool value, XcstWriter output)
 {
     if (value)
     {
         output.WriteAttributeString(key, key);
     }
 }
コード例 #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);
        }
コード例 #11
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        internal static void WriteId(string name, XcstWriter output)
        {
            string?sanitizedId = TagBuilder.CreateSanitizedId(name);

            if (!String.IsNullOrEmpty(sanitizedId))
            {
                output.WriteAttributeString("id", sanitizedId);
            }
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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();
        }
コード例 #16
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #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();
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
        }
コード例 #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();
        }
コード例 #23
0
        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);
        }
コード例 #24
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #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();
            }
        }
コード例 #26
0
ファイル: InputInstructions.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #27
0
ファイル: AntiForgery.cs プロジェクト: maxtoroq/XCST-a
        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);
        }
コード例 #28
0
        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);
        }
コード例 #29
0
        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);
        }
コード例 #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);
        }