Example #1
0
        protected MvcHtmlString BaseButton(string displayName,
                                           ManipulationButtonStyle buttonStyle, IDictionary <string, object> htmlAttributes)
        {
            switch (buttonStyle)
            {
            case ManipulationButtonStyle.Button:
                return(MvcHtmlString.Create
                       (
                           string.Format("<input type=\"button\" value=\"{0}\" {1} />",
                                         CurrHtmlHelper.Encode(displayName),
                                         BasicHtmlHelper.GetAttributesString(htmlAttributes))
                       ));

            case ManipulationButtonStyle.Image:
                htmlAttributes["src"] = displayName;
                BasicHtmlHelper.SetDefaultStyle(htmlAttributes, "cursor", "pointer");
                return(MvcHtmlString.Create
                       (
                           string.Format("<img {1} />",
                                         displayName,
                                         BasicHtmlHelper.GetAttributesString(htmlAttributes))
                       ));

            default:
                htmlAttributes["href"] = "javascript:void(0);";
                return(MvcHtmlString.Create
                       (
                           string.Format("<a {1}>{0}</a>",
                                         CurrHtmlHelper.Encode(displayName),
                                         BasicHtmlHelper.GetAttributesString(htmlAttributes))
                       ));
            }
        }
Example #2
0
        public static MvcHtmlString GenericInputFor <VM, T>(
            this HtmlHelper <VM> htmlHelper,
            InputType inputType,
            Expression <Func <VM, T> > expression,
            IDictionary <string, object> htmlAttributes)
        {
            if (expression == null)
            {
                throw (new ArgumentNullException("expression"));
            }

            var name =
                htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(
                    ExpressionHelper.GetExpressionText(expression));
            string type  = GetInputTypeString(inputType);
            T      value = default(T);

            try
            {
                value = expression.Compile().Invoke(htmlHelper.ViewData.Model);
            }
            catch { }

            return(MvcHtmlString.Create(
                       string.Format("<input type='{4}' name='{0}' id='{1}' value='{2}'  {3}/>",
                                     name, BasicHtmlHelper.IdFromName(name),
                                     htmlHelper.Encode(Convert.ToString(value, CultureInfo.InvariantCulture)),
                                     BasicHtmlHelper.GetAttributesString(htmlAttributes), type)));
        }
Example #3
0
        protected string RenderBasicScripts(CultureInfo format)
        {
            if (baseScriptsRendered)
            {
                return(string.Empty);
            }
            baseScriptsRendered = true;

            StringBuilder sb = new StringBuilder();

            sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper, this.GetType(), prefix));
            bool isClientBlock = htmlHelper.ViewData["ClientBindings"] != null ||
                                 htmlHelper.ViewContext.HttpContext.Items.Contains("ClientTemplateOn");

            sb.Append(string.Format(startScriptFormat,
                                    BasicHtmlHelper.IdFromName(BasicHtmlHelper.AddField(this.totalPrefix, "$")),
                                    BasicHtmlHelper.JavaScriptDate(min, isClientBlock),
                                    BasicHtmlHelper.JavaScriptDate(max, isClientBlock),
                                    clientOnChanged != null ? clientOnChanged + " return" : " return;",
                                    BasicHtmlHelper.JavaScriptDate(curr),
                                    clientMinScript,
                                    clientMaxScript,
                                    yearCombo ? "true" : "false",
                                    dateHidden ? "true" : "false",
                                    clientRefreshRegistrations,
                                    this.dateInCalendar ? "true" : "false",
                                    BasicHtmlHelper.GetAttributesString(attributeExtensions),
                                    totalPrefix
                                    ));
            sb.Append(BasicHtmlHelper.TrueValue(htmlHelper, this.totalPrefix, curr));
            FormatAttribute[] formatAttributes = pAccesor[typeof(FormatAttribute)] as FormatAttribute[];
            if (formatAttributes != null && formatAttributes.Length > 0)
            {
                sb.Append(BasicHtmlHelper.FormattedValueString(htmlHelper, this.totalPrefix, formatAttributes[0].HtmlEncode ? htmlHelper.Encode(formatAttributes[0].GetDisplay(curr)) : formatAttributes[0].GetDisplay(curr)));
            }
            else
            {
                DisplayFormatAttribute[] dFormatAttributes = pAccesor[typeof(DisplayFormatAttribute)] as DisplayFormatAttribute[];
                if (dFormatAttributes != null && dFormatAttributes.Length > 0)
                {
                    sb.Append(BasicHtmlHelper.FormattedValueString(htmlHelper, this.totalPrefix, dFormatAttributes[0].HtmlEncode ? htmlHelper.Encode(
                                                                       new FormatAttribute(dFormatAttributes[0]).GetDisplay(curr)) : new FormatAttribute(dFormatAttributes[0]).GetDisplay(curr)));
                }
            }
            if (format != null)
            {
                WriteMonthes(sb, format);
            }
            return(sb.ToString());
        }
Example #4
0
        protected string RenderPageButton(string textOrUrl, string disabledTexOrUrl, int index,
                                          PageButtonStyle pageButtonStyle, PageButtonType?pageButtonType, IDictionary <string, object> htmlAttributes = null, bool encoded = true)
        {
            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }
            string buttonType = getPageButtonClass(pageButtonType);
            string buttonId   = fieldId + "_" + buttonType;

            if (!pageButtonType.HasValue)
            {
                if (index < 0)
                {
                    buttonId = buttonId + "_" + (-index).ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    buttonId = buttonId + index.ToString(CultureInfo.InvariantCulture);
                }
                htmlAttributes["data-pager-index"] = index.ToString(CultureInfo.InvariantCulture);
            }
            htmlAttributes["id"] = buttonId;
            htmlAttributes["data-pager-button"] = buttonType;
            BasicHtmlHelper.AddClass(htmlAttributes, fieldId + "_class");
            BasicHtmlHelper.SetDefaultStyle(htmlAttributes, "cursor", "pointer");
            switch (pageButtonStyle)
            {
            case PageButtonStyle.Button:
                htmlAttributes["type"]  = "button";
                htmlAttributes["value"] = htmlHelper.Encode(textOrUrl);
                return(string.Format(buttonSchema, BasicHtmlHelper.GetAttributesString(htmlAttributes)));

            case PageButtonStyle.Image:
                htmlAttributes["src"] = textOrUrl;
                if (disabledTexOrUrl != null)
                {
                    htmlAttributes["data-disabled-src"] = disabledTexOrUrl;
                    htmlAttributes["data-enabled-src"]  = textOrUrl;
                }
                return(string.Format(imgSchema, BasicHtmlHelper.GetAttributesString(htmlAttributes)));

            default:
                htmlAttributes["href"] = "javascript:void(0);";
                return(string.Format(linkSchema, BasicHtmlHelper.GetAttributesString(htmlAttributes), encoded ? htmlHelper.Encode(textOrUrl): textOrUrl));
            }
        }
Example #5
0
        public static MvcHtmlString GenericInput <VM>(
            this HtmlHelper <VM> htmlHelper,
            InputType inputType,
            string name,
            object value,
            IDictionary <string, object> htmlAttributes)
        {
            if (name == null)
            {
                throw (new ArgumentNullException("name"));
            }
            name =
                htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(
                    name);
            string type = GetInputTypeString(inputType);

            return(MvcHtmlString.Create(
                       string.Format("<input type='{4}' name='{0}' id='{1}' value='{2}'  {3}/>",
                                     name, BasicHtmlHelper.IdFromName(name),
                                     htmlHelper.Encode(Convert.ToString(value, CultureInfo.InvariantCulture)),
                                     BasicHtmlHelper.GetAttributesString(htmlAttributes), type)));
        }
Example #6
0
        public static MvcHtmlString ManipulationButton <VM>(
            this HtmlHelper <VM> htmlHelper,
            ManipulationButtonType manipulationButtonType,
            string textOrUrl,
            string targetName,
            string name = null,
            ManipulationButtonStyle manipulationButtonStyle = ManipulationButtonStyle.Button,
            IDictionary <string, object> htmlAttributes     = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = getManipulationButtonClass(manipulationButtonType);
            }
            if (textOrUrl == null)
            {
                throw new ArgumentNullException("textOrUrl");
            }
            if (targetName == null)
            {
                throw new ArgumentNullException("targetName");
            }

            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }

            string buttonId = BasicHtmlHelper.IdFromName(
                BasicHtmlHelper.AddField(htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix, name));

            htmlAttributes["id"] = buttonId;
            switch (manipulationButtonStyle)
            {
            case ManipulationButtonStyle.Button:
                htmlAttributes["type"]  = "button";
                htmlAttributes["value"] = htmlHelper.Encode(textOrUrl);
                return(MvcHtmlString.Create(
                           string.Format(buttonSchema,
                                         BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                         processTarget(manipulationButtonType, targetName),
                                         getManipulationButtonClass(manipulationButtonType),
                                         buttonId)));

            case ManipulationButtonStyle.Link:
                htmlAttributes["href"] = "javascript:void(0);";
                return(MvcHtmlString.Create(
                           string.Format(linkSchema,
                                         BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                         processTarget(manipulationButtonType, targetName),
                                         getManipulationButtonClass(manipulationButtonType),
                                         buttonId,
                                         htmlHelper.Encode(textOrUrl))));

            default:
                htmlAttributes["src"] = textOrUrl;
                BasicHtmlHelper.SetDefaultStyle(htmlAttributes, "cursor", "pointer");
                return(MvcHtmlString.Create(
                           string.Format(imgSchema,
                                         BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                         processTarget(manipulationButtonType, targetName),
                                         getManipulationButtonClass(manipulationButtonType),
                                         buttonId)));
            }
        }/*
Example #7
0
        public static MvcHtmlString MutualExclusionListFor <TModel, TValue, TChoiceItem, TDisplay>
        (
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TValue> > expression,
            ChoiceList <TChoiceItem, TValue, TDisplay> choiceList,
            bool useTemplate    = false,
            string templateName = null)
            where TValue : IComparable, IConvertible
        {
            if (expression == null)
            {
                throw (new ArgumentNullException("expression"));
            }
            if (choiceList == null)
            {
                throw (new ArgumentNullException("choiceList"));
            }
            TValue value = default(TValue);

            try
            {
                value = expression.Compile().Invoke(htmlHelper.ViewData.Model);
            }
            catch { }
            var fullPropertyPath =
                htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(
                    ExpressionHelper.GetExpressionText(expression));

            MutualExclusionList <TChoiceItem, TValue, TDisplay> displayModel =
                new MutualExclusionList <TChoiceItem, TValue, TDisplay>();

            displayModel.ImportFromModel(value, new object[] { choiceList });

            StringBuilder sb = new StringBuilder();

            sb.Append(htmlHelper.Hidden(fullPropertyPath + ".$$",
                                        (typeof(MutualExclusionList <TChoiceItem, TValue, TDisplay>)).AssemblyQualifiedName).ToString());
            int index = 0;

            foreach (MutualExclusionListItem <TValue> item in displayModel.Items)
            {
                sb.Append(htmlHelper.Hidden
                              (EnumerableHelper.CreateSubIndexName(fullPropertyPath + ".Items", index) + ".Value",
                              item.Value));
                index++;
            }
            if (useTemplate)
            {
                if (templateName == null)
                {
                    templateName = typeof(MutualExclusionList <TChoiceItem, TValue, TDisplay>).Name;
                }

                ViewDataDictionary <MutualExclusionList <TChoiceItem, TValue, TDisplay> > dataDictionary =
                    new ViewDataDictionary <MutualExclusionList <TChoiceItem, TValue, TDisplay> >(displayModel);
                dataDictionary.TemplateInfo.HtmlFieldPrefix = fullPropertyPath;

                sb.Append(htmlHelper.Partial(templateName, dataDictionary).ToString());
            }
            else
            {
                index = 0;
                foreach (MutualExclusionListItem <TValue> item in displayModel.Items)
                {
                    sb.Append("<div>");
                    sb.Append(htmlHelper.RadioButton(
                                  EnumerableHelper.CreateSubIndexName(fullPropertyPath + ".Items", index) + ".Selected",
                                  item.Selected,
                                  item.DisplayAttributes));
                    sb.Append("&nbsp;");
                    sb.Append(string.Format(CultureInfo.InvariantCulture,
                                            "<span {0}>{1}</span>",
                                            BasicHtmlHelper.GetAttributesString(item.LabelAttributes),
                                            item.Label));
                    sb.Append("</div>");
                    index++;
                }
            }
            return(MvcHtmlString.Create(sb.ToString()));
        }
Example #8
0
        public MvcHtmlString DateCalendar(CalendarOptions calendarOptions = null, bool inLine = true, IDictionary <string, object> containerHtmlAttributes = null)
        {
            if (this.dateRendered)
            {
                return(MvcHtmlString.Create(string.Empty));
            }
            if (!this.dateInCalendar)
            {
                throw new ArgumentException(ControlsResources.DateTimeInput_Calendar);
            }

            dateRendered = true;
            string newPrefix = BasicHtmlHelper.AddField(prefix, "$");



            StringBuilder sb = new StringBuilder();

            sb.Append(htmlHelper.Hidden(
                          BasicHtmlHelper.AddField(newPrefix, "Year"), Year).ToString());
            sb.Append(htmlHelper.Hidden(
                          BasicHtmlHelper.AddField(newPrefix, "Month"), Month).ToString());
            sb.Append(htmlHelper.Hidden(
                          BasicHtmlHelper.AddField(newPrefix, "Day"), Day).ToString());
            if (calendarOptions == null)
            {
                calendarOptions = new CalendarOptions();
            }
            string clientId = BasicHtmlHelper.IdFromName(BasicHtmlHelper.AddField(totalPrefix, "$"));

            if (string.IsNullOrWhiteSpace(calendarOptions.DateFormat))
            {
                calendarOptions.DateFormat = clientFormat;
            }
            clientFormat = calendarOptions.DateFormat;
            switch (clientFormat)
            {
            case "d": calendarOptions.DateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
                break;

            case "f": calendarOptions.DateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
                break;

            case "F": calendarOptions.DateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
                break;

            case "D": calendarOptions.DateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
                break;

            case "M": calendarOptions.DateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
                break;

            case "Y": calendarOptions.DateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
                break;

            case "S": calendarOptions.DateFormat = "yyyy-MM-dd";
                break;

            case "t": calendarOptions.DateFormat = null;
                break;

            case "T": calendarOptions.DateFormat = null;
                break;

            default:
                calendarOptions.DateFormat = null;
                break;
            }
            if (calendarOptions.DateFormat != null)
            {
                calendarOptions.DateFormat = calendarOptions.DateFormat
                                             .Replace("yy", "y")
                                             .Replace("dddd", "DD")
                                             .Replace("ddd", "D")
                                             .Replace("M", "m")
                                             .Replace("mmmm", "MM")
                                             .Replace("mmm", "M");
            }
            //AddFunctions(ref containerHtmlAttributes, true);
            if (inLine)
            {
                sb.Append(string.Format(calendarSchema,
                                        clientId,
                                        calendarOptions.Render(clientId, inLine), BasicHtmlHelper.GetAttributesString(containerHtmlAttributes)));
            }
            else
            {
                sb.Append(string.Format(calendarSchemaOffLine,
                                        clientId,
                                        calendarOptions.Render(clientId, inLine), BasicHtmlHelper.GetAttributesString(containerHtmlAttributes)));
            }
            sb.Append(RenderBasicScripts(null));
            return(MvcHtmlString.Create(sb.ToString()));
        }
Example #9
0
        protected void GetItemContainerTags(
            ItemContainerType itemContainerType,
            int index,
            Func <TItem, int, string> getDynamicContainer,
            IDictionary <string, object> htmlattributes,
            string prefix,
            bool hidden,
            out string openTag,
            out string closureTag
            )
        {
            if (htmlattributes == null)
            {
                htmlattributes = new Dictionary <string, object>();
            }
            BasicHtmlHelper.SetAttribute(htmlattributes, "id", BasicHtmlHelper.IdFromName(prefix) + "_Container");

            if (hidden)
            {
                BasicHtmlHelper.SetDefaultStyle(htmlattributes, "display", "none");
            }
            switch (itemContainerType)
            {
            case ItemContainerType.div:
                openTag    = string.Format("<div {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</div>";
                break;

            case ItemContainerType.span:
                openTag    = string.Format("<span {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</span>";
                break;

            case ItemContainerType.tr:
                openTag    = string.Format("<tr {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</tr>";
                break;

            case ItemContainerType.td:
                openTag    = string.Format("<td {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</td>";
                break;

            case ItemContainerType.li:
                openTag    = string.Format("<li {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</li>";
                break;

            case ItemContainerType.section:
                openTag    = string.Format("<section {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</section>";
                break;

            case ItemContainerType.article:
                openTag    = string.Format("<article {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</article>";
                break;

            case ItemContainerType.p:
                openTag    = string.Format("<p {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                closureTag = "</p>";
                break;

            default:
                if (getDynamicContainer != null)
                {
                    string tagName = getDynamicContainer(Item.OldValue, index);
                    openTag    = string.Format("<{1} {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes), tagName);
                    closureTag = string.Format("</{0}>", tagName);
                }
                else
                {
                    openTag    = string.Format("<div {0}>", BasicHtmlHelper.GetAttributesString(htmlattributes));
                    closureTag = "</div>";
                }
                break;
            }
        }
Example #10
0
        public static MvcHtmlString Menu <T>(this HtmlHelper htmlHelper, string name, IEnumerable <T> collection, IDictionary <string, object> htmlAttributes = null, Expression <Func <T, IEnumerable <T> > > itemCollection = null, object itemTemplate = null, Func <HtmlHelper, T, bool> selected = null,
                                             Func <T, int> templateChoice = null, string selectedClass = "item-selected", string selectedPathClass = "selected-path", string hasChildrenClass = "has-children", Func <T, string> allItemsClass = null, string innerUlClass = null, Func <T, string> liAttributesString = null)
            where T : class, new()
        {
            if (name == null)
            {
                throw (new ArgumentNullException("name"));
            }
            if (collection == null)
            {
                throw (new ArgumentNullException("collection"));
            }
            if (itemCollection == null && typeof(T) != typeof(SimpleMenuItem))
            {
                throw (new ArgumentNullException("itemCollection"));
            }
            if (itemTemplate == null)
            {
                if (typeof(T) == typeof(SimpleMenuItem))
                {
                    Func <HtmlHelper <SimpleMenuItem>, string> template = x =>
                    {
                        if (x.ViewData.Model.Link == null)
                        {
                            return(string.Format("<a href='#'>{0}</a>", x.ViewData.Model.Text));
                        }
                        else
                        {
                            if (x.ViewData.Model.Target == null)
                            {
                                return(string.Format("<a href='{0}'>{1}</a>", x.ViewData.Model.Link, x.ViewData.Model.Text));
                            }
                            else
                            {
                                return(string.Format("<a href='{0}' target='{2}'>{1}</a>", x.ViewData.Model.Link, x.ViewData.Model.Text, x.ViewData.Model.Target));
                            }
                        }
                    };
                    itemTemplate = template;
                }
                else
                {
                    throw (new ArgumentNullException("itemTemplate"));
                }
            }
            StringBuilder sb                    = new StringBuilder();
            string        partialPrefix         = name;
            string        prefix                = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(partialPrefix);
            string        addPrefix             = null;
            Func <T, IEnumerable <T> > accessor = null;

            if (itemCollection == null)
            {
                addPrefix = "Children";
            }
            else
            {
                addPrefix = ExpressionHelper.GetExpressionText(itemCollection);
                accessor  = itemCollection.Compile();
            }
            sb.AppendFormat("<ul {0}>", BasicHtmlHelper.GetAttributesString(htmlAttributes));
            if (collection != null)
            {
                int i = 0;
                foreach (T item in collection)
                {
                    renderMenuItem <T>(htmlHelper, sb, item, accessor, itemTemplate, string.Format("{0}[{1}]", prefix, i), addPrefix, selected,
                                       templateChoice, selectedClass, selectedPathClass, hasChildrenClass, allItemsClass, innerUlClass, liAttributesString);
                    i++;
                }
            }
            sb.Append("</ul>");
            return(MvcHtmlString.Create(sb.ToString()));
        }
Example #11
0
        protected string RenderPackedList()
        {
            PackedList <IEnumerable <TValue>, TValue> displayModel = new PackedList <IEnumerable <TValue>, TValue>();

            displayModel.ImportFromModel(Value, new object[] { DualSelect_Separator });

            StringBuilder sb = new StringBuilder();

            sb.Append(BasicHtmlHelper.RenderDisplayInfo(CurrHtmlHelper,
                                                        typeof(PackedList <IEnumerable <TValue>, TValue>),
                                                        Prefix));
            sb.AppendFormat("<span {2} style='display:none' id='{1}' is-item-control='true' pname='{0}' data-element-type = 'DualSelect' ></span>", _fullprefix, BasicHtmlHelper.IdFromName(_fullprefix), BasicHtmlHelper.GetAttributesString(AttributeExtensions));
            sb.Append(
                CurrHtmlHelper.Hidden(BasicHtmlHelper.AddField(Prefix, "$.Separator"),
                                      DualSelect_Separator, new RouteValueDictionary {
                { "data-elementispart", "true" }
            })).ToString();

            sb.Append(CurrHtmlHelper.Hidden(
                          BasicHtmlHelper.AddField(Prefix, "$.PackedValue"),
                          displayModel.PackedValue, new RouteValueDictionary {
                { "data-elementispart", "true" }
            }));
            return(sb.ToString());
        }
Example #12
0
        protected string RenderPageButton(string name, string textOrUrl, int page,
                                          PageButtonStyle pageButtonStyle, IDictionary <string, object> htmlAttributes = null, bool encoded = true)
        {
            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }

            string buttonId = null;

            if (!string.IsNullOrEmpty(fieldName))
            {
                buttonId = BasicHtmlHelper.IdFromName(
                    BasicHtmlHelper.AddField(fieldName, name));
            }
            else
            {
                if (string.IsNullOrWhiteSpace(controllerName))
                {
                    buttonId = BasicHtmlHelper.IdFromName(
                        BasicHtmlHelper.AddField("CurrentController_" + actionName + "_" + parameterName, name));
                }
                else
                {
                    buttonId = BasicHtmlHelper.IdFromName(
                        BasicHtmlHelper.AddField(controllerName + "_" + actionName + "_" + parameterName, name));
                }
            }

            string pageUrl = string.Empty;

            if (parameterName != null)
            {
                System.Web.Routing.RouteValueDictionary routeDictionary = null;
                if (otherParameters != null)
                {
                    routeDictionary = otherParameters as System.Web.Routing.RouteValueDictionary;
                    if (routeDictionary == null)
                    {
                        routeDictionary = new System.Web.Routing.RouteValueDictionary(otherParameters);
                    }
                }
                else
                {
                    routeDictionary = new System.Web.Routing.RouteValueDictionary();
                }
                if (page < 0)
                {
                    routeDictionary.Add(parameterName, templateSymbol);
                }
                else
                {
                    routeDictionary.Add(parameterName, page);
                }
                pageUrl = UrlHelper.GenerateUrl(
                    routeName,
                    actionName,
                    controllerName,
                    protocol,
                    hostname,
                    fragment,
                    routeDictionary,
                    htmlHelper.RouteCollection,
                    htmlHelper.ViewContext.RequestContext,
                    true);
            }

            htmlAttributes["id"] = buttonId;
            string res;

            BasicHtmlHelper.SetDefaultStyle(htmlAttributes, "cursor", "pointer");
            switch (pageButtonStyle)
            {
            case PageButtonStyle.Button:
                htmlAttributes["type"]  = "button";
                htmlAttributes["value"] = htmlHelper.Encode(textOrUrl);
                return
                    (string.Format(buttonSchema,
                                   BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                   fieldName,
                                   pageString(page),
                                   buttonId,
                                   clientUrl(page, pageUrl),
                                   targetIdName,
                                   validationType));

            case PageButtonStyle.Link:
                htmlAttributes["href"] = "javascript:void(0);";

                if (htmlAttributes.ContainsKey("disabled") && htmlAttributes["disabled"].ToString() == "disabled" ||
                    htmlAttributes.ContainsKey("data-selected-page") && htmlAttributes["data-selected-page"].ToString() == "selected")
                {
                    string prevstyle = BasicHtmlHelper.SetStyle(htmlAttributes, "color", "gray");
                    res =
                        string.Format(linkSchemaDisabled,
                                      BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                      fieldName,
                                      page,
                                      buttonId,
                                      encoded ? htmlHelper.Encode(textOrUrl) : textOrUrl,
                                      pageUrl,
                                      targetIdName,
                                      validationType);
                    BasicHtmlHelper.SetAttribute(htmlAttributes, "style", prevstyle);
                }
                else
                {
                    if (pageUrl.Length != 0 && targetIdName.Length == 0 && page >= 0)
                    {
                        htmlAttributes["href"] = pageUrl;
                        res = string.Format(linkSchemaGet, BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                            encoded ? htmlHelper.Encode(textOrUrl) : textOrUrl);
                    }
                    else
                    {
                        res =
                            string.Format(linkSchema,
                                          BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                          fieldName,
                                          pageString(page),
                                          buttonId,
                                          encoded ? htmlHelper.Encode(textOrUrl) : textOrUrl,
                                          clientUrl(page, pageUrl),
                                          targetIdName,
                                          validationType);
                    }
                }
                return(res);

            default:
                htmlAttributes["src"] = textOrUrl;

                if (htmlAttributes.ContainsKey("disabled") && htmlAttributes["disabled"].ToString() == "disabled")
                {
                    string prevstyle = BasicHtmlHelper.SetStyle(htmlAttributes, "color", "gray");
                    res =
                        string.Format(imgSchemaDisabled,
                                      BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                      fieldName,
                                      page,
                                      buttonId,
                                      pageUrl,
                                      targetIdName,
                                      validationType);
                    BasicHtmlHelper.SetAttribute(htmlAttributes, "style", prevstyle);
                }
                else
                {
                    res =
                        string.Format(imgSchema,
                                      BasicHtmlHelper.GetAttributesString(htmlAttributes),
                                      fieldName,
                                      pageString(page),
                                      buttonId,
                                      clientUrl(page, pageUrl),
                                      targetIdName,
                                      validationType);
                }
                return(res);
            }
        }
Example #13
0
        public static MvcHtmlString CheckBoxListFor <TModel, TChoiceItem, TValue, TDisplay>
        (
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, IEnumerable <TValue> > > expression,
            ChoiceList <TChoiceItem, TValue, TDisplay> choiceList,
            bool useTemplate    = false,
            object template     = null,
            object itemTemplate = null)
            where TValue : IComparable
        {
            IEnumerable <TValue> values = default(IEnumerable <TValue>);

            if (expression == null)
            {
                throw (new ArgumentNullException("expression"));
            }
            try
            {
                values = expression.Compile().Invoke(htmlHelper.ViewData.Model);
            }
            catch {}
            if (values == null)
            {
                values = new List <TValue>();
            }
            if (choiceList == null)
            {
                throw (new ArgumentNullException("choiceList"));
            }

            var fullPropertyPath =
                htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(
                    ExpressionHelper.GetExpressionText(expression));
            var propertyPath =

                ExpressionHelper.GetExpressionText(expression);
            CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> displayModel =
                new CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay>();

            displayModel.ImportFromModel(values, new object[] { choiceList });

            StringBuilder sb = new StringBuilder();

            sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper,
                                                        typeof(CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay>),
                                                        propertyPath));

            int index = 0;

            foreach (CheckBoxListItem <TValue> item in displayModel.Items)
            {
                sb.Append(htmlHelper.Hidden
                              (EnumerableHelper.CreateSubIndexName(BasicHtmlHelper.AddField(propertyPath, "$.Items"), index) + ".Code",
                              item.Code));
                index++;
            }
            if (useTemplate)
            {
                if (template == null && itemTemplate == null)
                {
                    template = typeof(CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay>).Name;
                }
                if (template != null)
                {
                    ViewDataDictionary <CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> > dataDictionary =
                        new ViewDataDictionary <CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> >(displayModel);
                    dataDictionary.TemplateInfo.HtmlFieldPrefix = (BasicHtmlHelper.AddField(fullPropertyPath, "$"));

                    sb.Append(new TemplateInvoker <CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> >(template).Invoke <TModel>(htmlHelper, dataDictionary));
                }
                else
                {
                    int itemIndex = 0;
                    foreach (CheckBoxListItem <TValue> item in displayModel.Items)
                    {
                        ViewDataDictionary <CheckBoxListItem <TValue> > dataDictionary =
                            new ViewDataDictionary <CheckBoxListItem <TValue> >(item);
                        dataDictionary.TemplateInfo.HtmlFieldPrefix = (BasicHtmlHelper.AddField(fullPropertyPath, string.Format("$.Items[{0}]", itemIndex)));
                        sb.Append(new TemplateInvoker <CheckBoxListItem <TValue> >(itemTemplate)
                                  .Invoke(htmlHelper, dataDictionary));
                        itemIndex++;
                    }
                }
            }
            else
            {
                index = 0;
                foreach (CheckBoxListItem <TValue> item in displayModel.Items)
                {
                    sb.Append("<div>");
                    sb.Append(htmlHelper.CheckBox(
                                  EnumerableHelper.CreateSubIndexName(BasicHtmlHelper.AddField(propertyPath, "$.Items"), index) + ".Selected",
                                  item.Selected,
                                  item.DisplayAttributes));
                    sb.Append("&nbsp;");
                    sb.Append(string.Format(CultureInfo.InvariantCulture,
                                            "<span {0}>{1}</span>",
                                            BasicHtmlHelper.GetAttributesString(item.LabelAttributes),
                                            item.Label));
                    sb.Append("</div>");
                    index++;
                }
            }
            return(MvcHtmlString.Create(sb.ToString()));
        }
Example #14
0
        internal static MvcHtmlString DropDownbase <VM, TItem, TDisplay, TValue>(
            this HtmlHelper <VM> htmlHelper, string name, object value, List <ExtendedSelectListItem> items, IDictionary <string, object> htmlAttributes, bool allowsMultipleValues, bool useGroups, ModelMetadata metaData = null)
        {
            string partialName = name;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            object defaultValue = (allowsMultipleValues) ? htmlHelper.GetModelStateValue(name, typeof(string[])) : htmlHelper.GetModelStateValue(name, typeof(string));

            if (defaultValue == null)
            {
                defaultValue = value;
            }
            if (defaultValue != null)
            {
                IEnumerable          defaultValues  = (allowsMultipleValues) ? defaultValue as IEnumerable : new[] { defaultValue };
                IEnumerable <string> values         = (from object currValue in defaultValues select Convert.ToString(currValue, CultureInfo.CurrentCulture)).ToList();
                HashSet <string>     selectedValues = new HashSet <string>(values, StringComparer.OrdinalIgnoreCase);

                foreach (ExtendedSelectListItem item in items)
                {
                    item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
                }
            }
            StringBuilder listItemBuilder = new StringBuilder();

            if (useGroups)
            {
                foreach (var group in items.GroupBy(i => i.GroupKey))
                {
                    var groupProps = group.Select(it => new { Name = it.GroupName, Attrs = it.GroupAttributes }).FirstOrDefault();
                    IDictionary <string, object> dictionary = groupProps.Attrs as IDictionary <string, object>;
                    if (group.Key != null && groupProps.Name != null)
                    {
                        if (dictionary != null)
                        {
                            listItemBuilder.AppendLine(string.Format("<optgroup label=\"{0}\" value=\"{1}\" {2}>",
                                                                     htmlHelper.Encode(groupProps.Name),
                                                                     htmlHelper.Encode(group.Key),
                                                                     BasicHtmlHelper.GetAttributesString(dictionary)));
                        }
                        else
                        {
                            listItemBuilder.AppendLine(string.Format("<optgroup label=\"{0}\" value=\"{1}\" {2}>",
                                                                     htmlHelper.Encode(groupProps.Name),
                                                                     htmlHelper.Encode(group.Key),
                                                                     BasicHtmlHelper.GetAttributesString(groupProps.Attrs)));
                        }
                    }
                    foreach (ExtendedSelectListItem item in group)
                    {
                        listItemBuilder.AppendLine(ListItemToOption(item));
                    }
                    if (group.Key != null && groupProps.Name != null)
                    {
                        listItemBuilder.AppendLine("</optgroup>");
                    }
                }
            }
            else
            {
                foreach (ExtendedSelectListItem item in items)
                {
                    listItemBuilder.AppendLine(ListItemToOption(item));
                }
            }

            TagBuilder tagBuilder = new TagBuilder("select")
            {
                InnerHtml = listItemBuilder.ToString()
            };

            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", name, true /* replaceExisting */);
            tagBuilder.GenerateId(name);
            if (allowsMultipleValues)
            {
                tagBuilder.MergeAttribute("multiple", "multiple");
            }

            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }
            tagBuilder.MergeAttributes(MvcEnvironment.GetUnobtrusiveValidation(htmlHelper, partialName, metaData));
            return(MvcHtmlString.Create(tagBuilder.ToString()));
        }
Example #15
0
        private static MvcHtmlString ClientDropDownListForBase <VM, M, TItem, TDisplay, TValue>(
            HtmlHelper <VM> htmlHelper, Expression <Func <VM, M> > expression, IDictionary <string, object> htmlAttributes, ChoiceList <TItem, TValue, TDisplay> items, bool isList, ModelMetadata metaData)
        {
            MVCControlsToolkit.Controls.Bindings.IBindingsBuilder <VM> bindings = htmlHelper.ClientBindings();
            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }
            Expression <Func <VM, IEnumerable <TItem> > > itemsExpression = items.origin as Expression <Func <VM, IEnumerable <TItem> > >;
            string prefix  = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
            string caption = null;

            if (!isList)
            {
                caption = items.OverridePrompt;
                if (caption == null)
                {
                    caption = metaData.Watermark;
                }
            }
            if (typeof(TItem) == typeof(TValue))
            {
                bindings.Options <VM, IEnumerable <TItem> >(itemsExpression, caption);
            }
            else
            {
                bindings.Options <VM, TItem, TValue, TDisplay>(itemsExpression, items._evalueSelector, items._edisplaySelector, caption);
            }

            if (isList)
            {
                htmlAttributes["multiple"] = "multiple";
                bindings.SelectedOptions(expression);
            }
            else
            {
                bindings.Value(expression);
            }
            string prevBind = null;

            if (htmlAttributes.ContainsKey("data-bind"))
            {
                prevBind = htmlAttributes["data-bind"] as string;
            }
            if (string.IsNullOrWhiteSpace(prevBind))
            {
                htmlAttributes["data-bind"] = bindings.Get();
            }
            else
            {
                htmlAttributes["data-bind"] = prevBind + ", " + bindings.Get();
            }
            htmlAttributes["data-nobinding"] = true;
            return(MvcHtmlString.Create(string.Format("<select id = '{0}' name = '{1}' {2} ></select>", BasicHtmlHelper.IdFromName(prefix), prefix, BasicHtmlHelper.GetAttributesString(htmlAttributes))));
        }