Exemplo n.º 1
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);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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();
            }
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        public static void BooleanTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput)
        {
            XcstWriter output = DocumentWriter.CastElement(package, seqOutput);

            ViewDataDictionary viewData = html.ViewData;

            bool?value = null;

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

            if (viewData.ModelMetadata.IsNullableValueType)
            {
                output.WriteStartElement("select");

                string?className = DefaultEditorTemplates.GetEditorCssClass(new EditorInfo("Boolean", "select"), "list-box tri-state");

                HtmlAttributeHelper.WriteClass(className, null, output);
                HtmlAttributeHelper.WriteBoolean("disabled", true, output);

                foreach (SelectListItem item in DefaultEditorTemplates.TriStateValues(value))
                {
                    SelectInstructions.WriteOption(item, output);
                }

                output.WriteEndElement();
            }
            else
            {
                output.WriteStartElement("input");
                output.WriteAttributeString("type", "checkbox");

                string?className = DefaultEditorTemplates.GetEditorCssClass(new EditorInfo("Boolean", "input", InputType.CheckBox), "check-box");

                HtmlAttributeHelper.WriteClass(className, null, output);
                HtmlAttributeHelper.WriteBoolean("disabled", true, output);
                HtmlAttributeHelper.WriteBoolean("checked", value.GetValueOrDefault(), output);

                output.WriteEndElement();
            }
        }
Exemplo n.º 11
0
        static void CheckBoxHelper(
            HtmlHelper htmlHelper, IXcstPackage package, ISequenceWriter <XElement> output, ModelMetadata?metadata, string name, bool?isChecked, HtmlAttribs?htmlAttributes)
        {
            XcstWriter inputWriter  = DocumentWriter.CastElement(package, output);
            XcstWriter hiddenWriter = DocumentWriter.CastElement(package, output);

            bool explicitChecked = isChecked.HasValue;

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

            InputHelper(
                htmlHelper,
                inputWriter,
                InputType.CheckBox,
                metadata,
                name,
                value: "true",
                useViewData: !explicitChecked,
                isChecked: isChecked ?? false,
                setId: true,
                isExplicitValue: false,
                format: null,
                htmlAttributes: htmlAttributes);

            string fullName = Name(htmlHelper, name);

            // Render an additional <input type="hidden".../> for checkboxes. This
            // addresses scenarios where unchecked checkboxes are not sent in the request.
            // Sending a hidden input makes it possible to know that the checkbox was present
            // on the page when the request was submitted.

            hiddenWriter.WriteStartElement("input");
            hiddenWriter.WriteAttributeString("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
            hiddenWriter.WriteAttributeString("name", fullName);
            hiddenWriter.WriteAttributeString("value", "false");
            hiddenWriter.WriteEndElement();
        }
Exemplo n.º 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);
        }
Exemplo n.º 13
0
        internal static void ObjectTemplate(HtmlHelper html, IXcstPackage package, ISequenceWriter <object> seqOutput, TemplateHelpers.TemplateHelperDelegate templateHelper)
        {
            ViewDataDictionary viewData      = html.ViewData;
            ModelMetadata      modelMetadata = viewData.ModelMetadata;

            if (modelMetadata.Model is null ||
                viewData.TemplateInfo.TemplateDepth > 1)
            {
                MetadataInstructions.DisplayTextHelper(html, seqOutput, modelMetadata);
                return;
            }

            var filteredProperties = DisplayInstructions.DisplayProperties(html);
            var groupedProperties  = filteredProperties.GroupBy(p => p.GroupName());

            bool createFieldset = groupedProperties.Any(g => g.Key != null);

            foreach (var group in groupedProperties)
            {
                XcstWriter?fieldsetWriter = null;

                if (createFieldset)
                {
                    fieldsetWriter = DocumentWriter.CastElement(package, seqOutput);

                    fieldsetWriter.WriteStartElement("fieldset");
                    fieldsetWriter.WriteStartElement("legend");
                    fieldsetWriter.WriteString(group.Key);
                    fieldsetWriter.WriteEndElement();
                }

                foreach (ModelMetadata propertyMeta in group)
                {
                    XcstWriter?fieldWriter = null;

                    if (!propertyMeta.HideSurroundingHtml)
                    {
                        XcstDelegate <object>?memberTemplate =
                            DisplayInstructions.MemberTemplate(html, propertyMeta);

                        if (memberTemplate != null)
                        {
                            memberTemplate(null !/* argument is not used */, fieldsetWriter ?? seqOutput);
                            continue;
                        }

                        XcstWriter labelWriter = fieldsetWriter
                                                 ?? DocumentWriter.CastElement(package, seqOutput);

                        labelWriter.WriteStartElement("div");
                        labelWriter.WriteAttributeString("class", "display-label");
                        labelWriter.WriteString(propertyMeta.GetDisplayName() ?? String.Empty);
                        labelWriter.WriteEndElement();

                        fieldWriter = fieldsetWriter
                                      ?? DocumentWriter.CastElement(package, seqOutput);

                        fieldWriter.WriteStartElement("div");
                        fieldWriter.WriteAttributeString("class", "display-field");
                    }

                    templateHelper(
                        html,
                        package,
                        fieldWriter ?? fieldsetWriter ?? seqOutput,
                        propertyMeta,
                        htmlFieldName: propertyMeta.PropertyName,
                        templateName: null,
                        membersNames: null,
                        DataBoundControlMode.ReadOnly,
                        additionalViewData: null
                        );

                    if (!propertyMeta.HideSurroundingHtml)
                    {
                        fieldWriter !.WriteEndElement(); // </div>
                    }
                }

                if (createFieldset)
                {
                    fieldsetWriter !.WriteEndElement(); // </fieldset>
                }
            }
        }