예제 #1
0
        private static MvcHtmlString DropDownList(HtmlHelper helper, EntityCombo entityCombo)
        {
            List <SelectListItem> items = new List <SelectListItem>();

            items.Add(new SelectListItem()
            {
                Text = "-", Value = ""
            });

            List <Lite <IEntity> > data = entityCombo.Data != null?entityCombo.Data.ToList() :
                                              AutocompleteUtils.FindAllLite(entityCombo.Implementations.Value).Cast <Lite <IEntity> >().ToList();

            if (entityCombo.SortElements)
            {
                data = data.OrderBy(a => a.ToString()).ToList();
            }

            var current = entityCombo.UntypedValue is IEntity ? ((IEntity)entityCombo.UntypedValue)?.ToLite() :
                          entityCombo.UntypedValue as Lite <IEntity>;

            if (current != null && !data.Contains(current))
            {
                data.Add(current);
            }

            items.AddRange(
                data.Select(lite => new SelectListItem()
            {
                Text     = lite.ToString(),
                Value    = lite.Key(),
                Selected = lite.Is(current)
            }));


            entityCombo.ComboHtmlProperties.AddCssClass("form-control");

            if (entityCombo.ComboHtmlProperties.ContainsKey("onchange"))
            {
                throw new InvalidOperationException("EntityCombo cannot have onchange html property, use onEntityChanged instead");
            }

            entityCombo.ComboHtmlProperties.Add("onchange", entityCombo.SFControlThen("combo_selected()"));

            if (entityCombo.Size > 0)
            {
                entityCombo.ComboHtmlProperties.AddCssClass("sf-entity-list");
                entityCombo.ComboHtmlProperties.Add("size", Math.Min(entityCombo.Size, items.Count - 1));
            }

            if (entityCombo.PlaceholderLabels && !entityCombo.ComboHtmlProperties.ContainsKey("placeholder"))
            {
                entityCombo.ComboHtmlProperties.Add("placeholder", entityCombo.LabelText);
            }

            return(helper.SafeDropDownList(
                       entityCombo.Compose(EntityComboKeys.Combo),
                       items,
                       entityCombo.ComboHtmlProperties));
        }
예제 #2
0
        internal static MvcHtmlString InternalEntityCombo(this HtmlHelper helper, EntityCombo entityCombo)
        {
            if (!entityCombo.Visible || entityCombo.HideIfNull && entityCombo.UntypedValue == null)
            {
                return(MvcHtmlString.Empty);
            }

            if (!entityCombo.Type.IsIEntity() && !entityCombo.Type.IsLite())
            {
                throw new InvalidOperationException("EntityCombo can only be done for an identifiable or a lite, not for {0}".FormatWith(entityCombo.Type.CleanType()));
            }

            HtmlStringBuilder sb = new HtmlStringBuilder();

            using (sb.SurroundLine(new HtmlTag("div", entityCombo.Prefix).Class("SF-entity-combo SF-control-container")))
            {
                sb.AddLine(helper.HiddenRuntimeInfo(entityCombo));

                using (sb.SurroundLine(new HtmlTag("div", entityCombo.Compose("hidden")).Class("hide")))
                {
                    if (entityCombo.UntypedValue != null)
                    {
                        sb.AddLine(EntityButtonHelper.Create(helper, entityCombo, btn: true));
                        sb.AddLine(EntityButtonHelper.Find(helper, entityCombo, btn: true));
                    }
                    else
                    {
                        sb.AddLine(EntityButtonHelper.View(helper, entityCombo, btn: true));
                        sb.AddLine(EntityButtonHelper.Remove(helper, entityCombo, btn: true));
                    }
                }

                using (sb.SurroundLine(new HtmlTag("div", entityCombo.Compose("inputGroup")).Class("input-group")))
                {
                    if (entityCombo.ReadOnly)
                    {
                        sb.AddLine(helper.FormControlStatic(entityCombo, entityCombo.Compose(EntityBaseKeys.ToStr), entityCombo.UntypedValue?.ToString()));
                    }
                    else
                    {
                        sb.AddLine(DropDownList(helper, entityCombo));
                    }

                    using (sb.SurroundLine(new HtmlTag("span", entityCombo.Compose("shownButton")).Class("input-group-btn")))
                    {
                        if (entityCombo.UntypedValue == null)
                        {
                            sb.AddLine(EntityButtonHelper.Create(helper, entityCombo, btn: true));
                            sb.AddLine(EntityButtonHelper.Find(helper, entityCombo, btn: true));
                        }
                        else
                        {
                            sb.AddLine(EntityButtonHelper.View(helper, entityCombo, btn: true));
                            sb.AddLine(EntityButtonHelper.Remove(helper, entityCombo, btn: true));
                        }
                    }
                }

                if (entityCombo.Type.IsEmbeddedEntity() && entityCombo.Create)
                {
                    EmbeddedEntity embedded   = (EmbeddedEntity) new ConstructorContext(helper.ViewContext.Controller).ConstructUntyped(entityCombo.Type.CleanType());
                    TypeContext    templateTC = ((TypeContext)entityCombo.Parent).Clone(embedded);
                    sb.AddLine(EntityBaseHelper.EmbeddedTemplate(entityCombo, EntityBaseHelper.RenderPopup(helper, templateTC, RenderPopupMode.Popup, entityCombo, isTemplate: true), null));
                }

                if (EntityBaseHelper.EmbeddedOrNew((Modifiable)entityCombo.UntypedValue))
                {
                    sb.AddLine(EntityBaseHelper.RenderPopup(helper, (TypeContext)entityCombo.Parent, RenderPopupMode.PopupInDiv, entityCombo));
                }

                sb.AddLine(entityCombo.ConstructorScript(JsModule.Lines, "EntityCombo"));
            }

            return(helper.FormGroup(entityCombo, entityCombo.Prefix, entityCombo.LabelHtml ?? entityCombo.LabelText.FormatHtml(), sb.ToHtml()));
        }