framework /Signum.Web/LineHelpers/EntityLineHelper.cs

Language C# Lines 168
MD5 Hash c74b7fb28b2e072900d18ed834ffebe6
Repository https://github.com/mutharasank/framework.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;
using Signum.Utilities;
using System.Web.Mvc.Html;
using Signum.Entities;
using System.Reflection;
using Signum.Entities.Reflection;
using System.Configuration;
using Signum.Engine;
using Signum.Utilities.Reflection;
using Signum.Web.Controllers;
#endregion

namespace Signum.Web
{
    public static class EntityLineHelper
    {
        internal static MvcHtmlString InternalEntityLine(this HtmlHelper helper, EntityLine entityLine)
        {
            if (!entityLine.Visible || (entityLine.HideIfNull && entityLine.UntypedValue == null))
                return MvcHtmlString.Empty;

            HtmlStringBuilder sb = new HtmlStringBuilder();
            using (sb.Surround(new HtmlTag("div", entityLine.Prefix).Class("SF-entity-line SF-control-container")))
            {
                 sb.AddLine(helper.HiddenRuntimeInfo(entityLine));

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

                 using (sb.Surround(new HtmlTag("div", entityLine.Compose("inputGroup")).Class("input-group")))
                 {
                     if (entityLine.UntypedValue == null)
                         sb.AddLine(AutocompleteTextBox(helper, entityLine));
                     else
                         sb.AddLine(LinkOrSpan(helper, entityLine));

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

                 if (entityLine.Type.IsEmbeddedEntity() && entityLine.Create)
                {
                    TypeContext templateTC = ((TypeContext)entityLine.Parent).Clone((object)Constructor.Construct(entityLine.Type.CleanType()));
                    sb.AddLine(EntityBaseHelper.EmbeddedTemplate(entityLine, EntityBaseHelper.RenderPopup(helper, templateTC, RenderPopupMode.Popup, entityLine, isTemplate: true), null));
                }

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


                sb.AddLine(entityLine.ConstructorScript(JsFunction.LinesModule, "EntityLine"));
            }

            return helper.FormGroup(entityLine, entityLine.Prefix, entityLine.LabelText, sb.ToHtml());
        }

        private static MvcHtmlString LinkOrSpan(HtmlHelper helper, EntityLine entityLine)
        {
            if (entityLine.ReadOnly)
                return helper.FormControlStatic(entityLine.Compose(EntityBaseKeys.Link), entityLine.UntypedValue.TryToString(), null);

            if (entityLine.Navigate)
            {
                var lite = (entityLine.UntypedValue as Lite<IIdentifiable>) ?? (entityLine.UntypedValue as IIdentifiable).Try(i => i.ToLite(i.IsNew));

                return helper.Href(entityLine.Compose(EntityBaseKeys.Link),
                        lite.TryToString(),
                        lite == null ? null : Navigator.NavigateRoute(lite),
                        JavascriptMessage.navigate.NiceToString(), "form-control  btn-default sf-entity-line-entity", null);
            }
            else
            {
                return helper.Span(entityLine.Compose(EntityBaseKeys.Link),
                        entityLine.UntypedValue.TryToString() ?? " ",
                        "form-control btn-default sf-entity-line-entity");
            }
        }

        private static MvcHtmlString AutocompleteTextBox(HtmlHelper helper, EntityLine entityLine)
        {
            if (!entityLine.Autocomplete)
                return helper.FormControlStatic(entityLine.Compose(EntityBaseKeys.ToStr), null, null);

            if (entityLine.Implementations.Value.IsByAll)
                throw new InvalidOperationException("Autocomplete is not possible with ImplementedByAll");

            var htmlAttr = new Dictionary<string, object>
            {
                {"class", "form-control sf-entity-autocomplete"},
                { "autocomplete", "off" }, 
            };

            if (entityLine.PlaceholderLabels)
                htmlAttr.Add("placeholder", entityLine.LabelText);

            return helper.TextBox(
                entityLine.Compose(EntityBaseKeys.ToStr),
                null,
                htmlAttr);
        }

        public static MvcHtmlString EntityLine<T,S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property) 
        {
            return helper.EntityLine<T, S>(tc, property, null);
        }

        public static MvcHtmlString EntityLine<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property, Action<EntityLine> 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);


            EntityLine el = new EntityLine(typeof(S), context.Value, context, null, context.PropertyRoute);

            EntityBaseHelper.ConfigureEntityBase(el, el.CleanRuntimeType ?? el.Type.CleanType());

            if (el.Implementations == null || el.Implementations.Value.IsByAll)
                el.Autocomplete = false;

            Common.FireCommonTasks(el);

            if (settingsModifier != null)
                settingsModifier(el);

            var result = helper.InternalEntityLine(el); 

            if (vo == null)
                return result;

            return vo.OnSurroundLine(el.PropertyRoute, helper, tc, result);
        }
    }
}
Back to Top