public static MvcHtmlString EntityCombo <T, S>(this HtmlHelper helper, TypeContext <T> tc, Expression <Func <T, S> > property, Action <EntityCombo> settingsModifier) { TypeContext <S> context = Common.WalkExpression(tc, property); var vo = tc.ViewOverrides; if (vo != null && !vo.IsVisible(context.PropertyRoute)) { return(vo.OnSurroundLine(context.PropertyRoute, helper, tc, null)); } EntityCombo ec = new EntityCombo(typeof(S), context.Value, context, null, context.PropertyRoute); EntityBaseHelper.ConfigureEntityBase(ec, ec.CleanRuntimeType ?? ec.Type.CleanType()); Common.FireCommonTasks(ec); if (settingsModifier != null) { settingsModifier(ec); } var result = helper.InternalEntityCombo(ec); if (vo == null) { return(result); } return(vo.OnSurroundLine(ec.PropertyRoute, helper, tc, result)); }
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)); }
private static MvcHtmlString PrintValueField(HtmlHelper helper, Context parent, FilterOption filterOption) { var implementations = filterOption.Token.GetImplementations(); if (filterOption.Token.Type.IsLite()) { Lite <IEntity> lite = (Lite <IEntity>)Common.Convert(filterOption.Value, filterOption.Token.Type); if (lite != null && string.IsNullOrEmpty(lite.ToString())) { Database.FillToString(lite); } Type cleanType = Lite.Extract(filterOption.Token.Type); if (EntityKindCache.IsLowPopulation(cleanType) && !cleanType.IsInterface && !implementations.Value.IsByAll) { EntityCombo ec = new EntityCombo(filterOption.Token.Type, lite, parent, "", filterOption.Token.GetPropertyRoute()) { Implementations = implementations.Value, }; EntityBaseHelper.ConfigureEntityButtons(ec, filterOption.Token.Type.CleanType()); ec.FormGroupStyle = FormGroupStyle.None; ec.Create = false; ec.ReadOnly = filterOption.Frozen; return(EntityComboHelper.InternalEntityCombo(helper, ec)); } else { EntityLine el = new EntityLine(filterOption.Token.Type, lite, parent, "", filterOption.Token.GetPropertyRoute()) { Implementations = implementations.Value, }; if (el.Implementations.Value.IsByAll) { el.Autocomplete = false; } EntityBaseHelper.ConfigureEntityButtons(el, filterOption.Token.Type.CleanType()); el.FormGroupStyle = FormGroupStyle.None; el.Create = false; el.ReadOnly = filterOption.Frozen; return(EntityLineHelper.InternalEntityLine(helper, el)); } } else if (filterOption.Token.Type.IsEmbeddedEntity()) { EmbeddedEntity lite = (EmbeddedEntity)Common.Convert(filterOption.Value, filterOption.Token.Type); EntityLine el = new EntityLine(filterOption.Token.Type, lite, parent, "", filterOption.Token.GetPropertyRoute()) { Implementations = null, Autocomplete = false, }; EntityBaseHelper.ConfigureEntityButtons(el, filterOption.Token.Type.CleanType()); el.FormGroupStyle = FormGroupStyle.None; el.Create = false; el.ReadOnly = filterOption.Frozen; return(EntityLineHelper.InternalEntityLine(helper, el)); } else { var vl = new ValueLine(filterOption.Token.Type, filterOption.Value, parent, "", filterOption.Token.GetPropertyRoute()) { FormGroupStyle = FormGroupStyle.None, ReadOnly = filterOption.Frozen, Format = filterOption.Token.Format, UnitText = filterOption.Token.Unit, }; if (filterOption.Token.Type.UnNullify().IsEnum) { vl.EnumComboItems = ValueLine.CreateComboItems(EnumEntity.GetValues(vl.Type.UnNullify()), vl.UntypedValue == null || vl.Type.IsNullable()); } return(ValueLineHelper.ValueLine(helper, vl)); } throw new InvalidOperationException("Invalid filter for type {0}".FormatWith(filterOption.Token.Type.Name)); }
private static MvcHtmlString PrintValueField(HtmlHelper helper, Context parent, FilterOption filterOption) { var implementations = filterOption.Token.GetImplementations(); if (filterOption.Token.Type.IsLite()) { Lite<IEntity> lite = (Lite<IEntity>)Common.Convert(filterOption.Value, filterOption.Token.Type); if (lite != null && string.IsNullOrEmpty(lite.ToString())) Database.FillToString(lite); Type cleanType = Lite.Extract(filterOption.Token.Type); if (EntityKindCache.IsLowPopulation(cleanType) && !cleanType.IsInterface && !implementations.Value.IsByAll) { EntityCombo ec = new EntityCombo(filterOption.Token.Type, lite, parent, "", filterOption.Token.GetPropertyRoute()) { Implementations = implementations.Value, }; EntityBaseHelper.ConfigureEntityButtons(ec, filterOption.Token.Type.CleanType()); ec.FormGroupStyle = FormGroupStyle.None; ec.Create = false; ec.ReadOnly = filterOption.Frozen; return EntityComboHelper.InternalEntityCombo(helper, ec); } else { EntityLine el = new EntityLine(filterOption.Token.Type, lite, parent, "", filterOption.Token.GetPropertyRoute()) { Implementations = implementations.Value, }; if (el.Implementations.Value.IsByAll) el.Autocomplete = false; EntityBaseHelper.ConfigureEntityButtons(el, filterOption.Token.Type.CleanType()); el.FormGroupStyle = FormGroupStyle.None; el.Create = false; el.ReadOnly = filterOption.Frozen; return EntityLineHelper.InternalEntityLine(helper, el); } } else if (filterOption.Token.Type.IsEmbeddedEntity()) { EmbeddedEntity lite = (EmbeddedEntity)Common.Convert(filterOption.Value, filterOption.Token.Type); EntityLine el = new EntityLine(filterOption.Token.Type, lite, parent, "", filterOption.Token.GetPropertyRoute()) { Implementations = null, Autocomplete = false, }; EntityBaseHelper.ConfigureEntityButtons(el, filterOption.Token.Type.CleanType()); el.FormGroupStyle = FormGroupStyle.None; el.Create = false; el.ReadOnly = filterOption.Frozen; return EntityLineHelper.InternalEntityLine(helper, el); } else { var vl = new ValueLine(filterOption.Token.Type, filterOption.Value, parent, "", filterOption.Token.GetPropertyRoute()) { FormGroupStyle = FormGroupStyle.None, ReadOnly = filterOption.Frozen, Format = filterOption.Token.Format, UnitText = filterOption.Token.Unit, }; if (filterOption.Token.Type.UnNullify().IsEnum) { vl.EnumComboItems = ValueLine.CreateComboItems(EnumEntity.GetValues(vl.Type.UnNullify()), vl.UntypedValue == null || vl.Type.IsNullable()); } return ValueLineHelper.ValueLine(helper, vl); } throw new InvalidOperationException("Invalid filter for type {0}".FormatWith(filterOption.Token.Type.Name)); }
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())); }