コード例 #1
0
        internal static void CollectionTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput, TemplateHelpers.TemplateHelperDelegate templateHelper)
        {
            ViewDataDictionary viewData = html.ViewData;
            object?            model    = viewData.ModelMetadata.Model;

            if (model is null)
            {
                return;
            }

            IEnumerable collection = model as IEnumerable
                                     ?? throw new InvalidOperationException($"The Collection template was used with an object of type '{model.GetType().FullName}', which does not implement System.IEnumerable.");

            Type typeInCollection      = typeof(string);
            Type?genericEnumerableType = TypeHelpers.ExtractGenericInterface(collection.GetType(), typeof(IEnumerable <>));

            if (genericEnumerableType != null)
            {
                typeInCollection = genericEnumerableType.GetGenericArguments()[0];
            }

            bool typeInCollectionIsNullableValueType = TypeHelpers.IsNullableValueType(typeInCollection);

            string oldPrefix = viewData.TemplateInfo.HtmlFieldPrefix;

            try {
                viewData.TemplateInfo.HtmlFieldPrefix = String.Empty;

                string fieldNameBase = oldPrefix;
                int    index         = 0;

                foreach (object item in collection)
                {
                    Type itemType = typeInCollection;

                    if (item != null &&
                        !typeInCollectionIsNullableValueType)
                    {
                        itemType = item.GetType();
                    }

                    ModelMetadata metadata  = ModelMetadataProviders.Current.GetMetadataForType(() => item, itemType);
                    string        fieldName = String.Format(CultureInfo.InvariantCulture, "{0}[{1}]", fieldNameBase, index++);

                    templateHelper(
                        html,
                        package,
                        seqOutput,
                        metadata,
                        htmlFieldName: fieldName,
                        templateName: null,
                        membersNames: null,
                        DataBoundControlMode.Edit,
                        additionalViewData: null
                        );
                }
            } finally {
                viewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
            }
        }
コード例 #2
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);
        }
コード例 #3
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);
            }
        }
コード例 #4
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);
        }
コード例 #5
0
        public static void HttpPostedFileBaseTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
#if NETCOREAPP
            const string templateName = nameof(IFormFile);
#else
            const string templateName = nameof(HttpPostedFileBase);
#endif

            HtmlInputTemplateHelper(html, package, seqOutput, templateName, inputType: "file");
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
0
        public static void DecimalTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            ViewDataDictionary viewData = html.ViewData;

            if (viewData.TemplateInfo.FormattedModelValue == viewData.ModelMetadata.Model)
            {
                viewData.TemplateInfo.FormattedModelValue =
                    String.Format(CultureInfo.CurrentCulture, "{0:0.00}", viewData.ModelMetadata.Model);
            }

            HtmlInputTemplateHelper(html, package, seqOutput, "Decimal");
        }
コード例 #10
0
        public static void ColorInputTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            ViewDataDictionary viewData = html.ViewData;

            if (viewData.Model is Color color)
            {
                if (viewData.TemplateInfo.FormattedModelValue == viewData.ModelMetadata.Model)
                {
                    viewData.TemplateInfo.FormattedModelValue =
                        String.Format(CultureInfo.InvariantCulture, "#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
                }
            }

            HtmlInputTemplateHelper(html, package, seqOutput, "Color", "color");
        }
コード例 #11
0
        public static void HiddenInputTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            ViewDataDictionary viewData = html.ViewData;

            if (!viewData.ModelMetadata.HideSurroundingHtml)
            {
                DefaultDisplayTemplates.StringTemplate(html, package, seqOutput);
            }

            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            object?model = viewData.Model;

            string?className      = GetEditorCssClass(new EditorInfo("HiddenInput", "input", InputType.Hidden), null);
            var    htmlAttributes = CreateHtmlAttributes(html, className);

            InputInstructions.Input(html, output, String.Empty, model, type: "hidden", htmlAttributes: htmlAttributes);
        }
コード例 #12
0
        public static void DropDownListTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

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

            string?optionLabel = null;

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

            if (optionList?.AddBlankOption == true)
            {
                optionLabel = viewData.ModelMetadata.Watermark ?? String.Empty;
            }

            SelectInstructions.SelectHelper(html, output, viewData.ModelMetadata, String.Empty, options, optionLabel, multiple: false, htmlAttributes: htmlAttributes);
        }