/Signum.Web/LineHelpers/EntityLineHelper.cs

https://github.com/mutharasank/framework · C# · 167 lines · 136 code · 31 blank · 0 comment · 29 complexity · c74b7fb28b2e072900d18ed834ffebe6 MD5 · raw file

  1. #region usings
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web.Mvc;
  7. using System.Linq.Expressions;
  8. using Signum.Utilities;
  9. using System.Web.Mvc.Html;
  10. using Signum.Entities;
  11. using System.Reflection;
  12. using Signum.Entities.Reflection;
  13. using System.Configuration;
  14. using Signum.Engine;
  15. using Signum.Utilities.Reflection;
  16. using Signum.Web.Controllers;
  17. #endregion
  18. namespace Signum.Web
  19. {
  20. public static class EntityLineHelper
  21. {
  22. internal static MvcHtmlString InternalEntityLine(this HtmlHelper helper, EntityLine entityLine)
  23. {
  24. if (!entityLine.Visible || (entityLine.HideIfNull && entityLine.UntypedValue == null))
  25. return MvcHtmlString.Empty;
  26. HtmlStringBuilder sb = new HtmlStringBuilder();
  27. using (sb.Surround(new HtmlTag("div", entityLine.Prefix).Class("SF-entity-line SF-control-container")))
  28. {
  29. sb.AddLine(helper.HiddenRuntimeInfo(entityLine));
  30. using (sb.Surround(new HtmlTag("div", entityLine.Compose("hidden")).Class("hide")))
  31. {
  32. if (entityLine.UntypedValue != null)
  33. {
  34. sb.AddLine(AutocompleteTextBox(helper, entityLine));
  35. sb.AddLine(EntityButtonHelper.Create(helper, entityLine, btn: true));
  36. sb.AddLine(EntityButtonHelper.Find(helper, entityLine, btn: true));
  37. }
  38. else
  39. {
  40. sb.AddLine(LinkOrSpan(helper, entityLine));
  41. sb.AddLine(EntityButtonHelper.View(helper, entityLine, btn: true));
  42. sb.AddLine(EntityButtonHelper.Remove(helper, entityLine, btn: true));
  43. }
  44. }
  45. using (sb.Surround(new HtmlTag("div", entityLine.Compose("inputGroup")).Class("input-group")))
  46. {
  47. if (entityLine.UntypedValue == null)
  48. sb.AddLine(AutocompleteTextBox(helper, entityLine));
  49. else
  50. sb.AddLine(LinkOrSpan(helper, entityLine));
  51. using (sb.Surround(new HtmlTag("span", entityLine.Compose("shownButton")).Class("input-group-btn")))
  52. {
  53. if (entityLine.UntypedValue == null)
  54. {
  55. sb.AddLine(EntityButtonHelper.Create(helper, entityLine, btn: true));
  56. sb.AddLine(EntityButtonHelper.Find(helper, entityLine, btn: true));
  57. }
  58. else
  59. {
  60. sb.AddLine(EntityButtonHelper.View(helper, entityLine, btn: true));
  61. sb.AddLine(EntityButtonHelper.Remove(helper, entityLine, btn: true));
  62. }
  63. }
  64. }
  65. if (entityLine.Type.IsEmbeddedEntity() && entityLine.Create)
  66. {
  67. TypeContext templateTC = ((TypeContext)entityLine.Parent).Clone((object)Constructor.Construct(entityLine.Type.CleanType()));
  68. sb.AddLine(EntityBaseHelper.EmbeddedTemplate(entityLine, EntityBaseHelper.RenderPopup(helper, templateTC, RenderPopupMode.Popup, entityLine, isTemplate: true), null));
  69. }
  70. if (EntityBaseHelper.EmbeddedOrNew((Modifiable)entityLine.UntypedValue))
  71. sb.AddLine(EntityBaseHelper.RenderPopup(helper, (TypeContext)entityLine.Parent, RenderPopupMode.PopupInDiv, entityLine));
  72. sb.AddLine(entityLine.ConstructorScript(JsFunction.LinesModule, "EntityLine"));
  73. }
  74. return helper.FormGroup(entityLine, entityLine.Prefix, entityLine.LabelText, sb.ToHtml());
  75. }
  76. private static MvcHtmlString LinkOrSpan(HtmlHelper helper, EntityLine entityLine)
  77. {
  78. if (entityLine.ReadOnly)
  79. return helper.FormControlStatic(entityLine.Compose(EntityBaseKeys.Link), entityLine.UntypedValue.TryToString(), null);
  80. if (entityLine.Navigate)
  81. {
  82. var lite = (entityLine.UntypedValue as Lite<IIdentifiable>) ?? (entityLine.UntypedValue as IIdentifiable).Try(i => i.ToLite(i.IsNew));
  83. return helper.Href(entityLine.Compose(EntityBaseKeys.Link),
  84. lite.TryToString(),
  85. lite == null ? null : Navigator.NavigateRoute(lite),
  86. JavascriptMessage.navigate.NiceToString(), "form-control btn-default sf-entity-line-entity", null);
  87. }
  88. else
  89. {
  90. return helper.Span(entityLine.Compose(EntityBaseKeys.Link),
  91. entityLine.UntypedValue.TryToString() ?? " ",
  92. "form-control btn-default sf-entity-line-entity");
  93. }
  94. }
  95. private static MvcHtmlString AutocompleteTextBox(HtmlHelper helper, EntityLine entityLine)
  96. {
  97. if (!entityLine.Autocomplete)
  98. return helper.FormControlStatic(entityLine.Compose(EntityBaseKeys.ToStr), null, null);
  99. if (entityLine.Implementations.Value.IsByAll)
  100. throw new InvalidOperationException("Autocomplete is not possible with ImplementedByAll");
  101. var htmlAttr = new Dictionary<string, object>
  102. {
  103. {"class", "form-control sf-entity-autocomplete"},
  104. { "autocomplete", "off" },
  105. };
  106. if (entityLine.PlaceholderLabels)
  107. htmlAttr.Add("placeholder", entityLine.LabelText);
  108. return helper.TextBox(
  109. entityLine.Compose(EntityBaseKeys.ToStr),
  110. null,
  111. htmlAttr);
  112. }
  113. public static MvcHtmlString EntityLine<T,S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property)
  114. {
  115. return helper.EntityLine<T, S>(tc, property, null);
  116. }
  117. public static MvcHtmlString EntityLine<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property, Action<EntityLine> settingsModifier)
  118. {
  119. TypeContext<S> context = Common.WalkExpression(tc, property);
  120. var vo = tc.ViewOverrides;
  121. if (vo != null && !vo.IsVisible(context.PropertyRoute))
  122. return vo.OnSurroundLine(context.PropertyRoute, helper, tc, null);
  123. EntityLine el = new EntityLine(typeof(S), context.Value, context, null, context.PropertyRoute);
  124. EntityBaseHelper.ConfigureEntityBase(el, el.CleanRuntimeType ?? el.Type.CleanType());
  125. if (el.Implementations == null || el.Implementations.Value.IsByAll)
  126. el.Autocomplete = false;
  127. Common.FireCommonTasks(el);
  128. if (settingsModifier != null)
  129. settingsModifier(el);
  130. var result = helper.InternalEntityLine(el);
  131. if (vo == null)
  132. return result;
  133. return vo.OnSurroundLine(el.PropertyRoute, helper, tc, result);
  134. }
  135. }
  136. }