/Serenity.Script.UI/Editor/MultipleImageUploadEditor.cs

https://gitlab.com/pgksunilkumar/Serenity · C# · 177 lines · 155 code · 22 blank · 0 comment · 8 complexity · 2022849833734d703f481323da9876ad MD5 · raw file

  1. using jQueryApi;
  2. using Serenity.ComponentModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. namespace Serenity
  9. {
  10. [Editor, DisplayName("MultipleImage Upload"), OptionsType(typeof(ImageUploadEditorOptions))]
  11. [Element("<div/>")]
  12. public class MultipleImageUploadEditor : Widget<ImageUploadEditorOptions>, IGetEditValue, ISetEditValue, IReadOnly
  13. {
  14. protected List<UploadedFile> entities;
  15. protected Toolbar toolbar;
  16. protected jQueryObject fileSymbols;
  17. protected jQueryObject uploadInput;
  18. public MultipleImageUploadEditor(jQueryObject div, ImageUploadEditorOptions opt)
  19. : base(div, opt)
  20. {
  21. entities = new List<UploadedFile>();
  22. div.AddClass("s-MultipleImageUploadEditor");
  23. var self = this;
  24. toolbar = new Toolbar(J("<div/>").AppendTo(this.Element), new ToolbarOptions
  25. {
  26. Buttons = GetToolButtons()
  27. });
  28. var progress = J("<div><div></div></div>").AddClass("upload-progress")
  29. .PrependTo(toolbar.Element);
  30. var addFileButton = toolbar.FindButton("add-file-button");
  31. uploadInput = UploadHelper.AddUploadInput(new UploadInputOptions
  32. {
  33. Container = addFileButton,
  34. InputName = this.uniqueName,
  35. Progress = progress,
  36. FileDone = (response, name, data) =>
  37. {
  38. if (!UploadHelper.CheckImageConstraints(response, options))
  39. return;
  40. var newEntity = new UploadedFile
  41. {
  42. OriginalName = name,
  43. Filename = response.TemporaryFile
  44. };
  45. self.entities.Add(newEntity);
  46. self.Populate();
  47. self.UpdateInterface();
  48. }
  49. });
  50. fileSymbols = jQuery.FromHtml("<ul/>")
  51. .AppendTo(this.element);
  52. this.UpdateInterface();
  53. }
  54. protected virtual string AddFileButtonText()
  55. {
  56. return Q.Text("Controls.ImageUpload.AddFileButton");
  57. }
  58. protected virtual List<ToolButton> GetToolButtons()
  59. {
  60. var self = this;
  61. return new List<ToolButton>
  62. {
  63. new ToolButton
  64. {
  65. Title = AddFileButtonText(),
  66. CssClass = "add-file-button",
  67. OnClick = delegate {
  68. }
  69. }
  70. };
  71. }
  72. protected virtual void Populate()
  73. {
  74. UploadHelper.PopulateFileSymbols(fileSymbols, entities, true, options.UrlPrefix);
  75. fileSymbols.Children().Each((i, e) =>
  76. {
  77. var x = i;
  78. J("<a class='delete'></a>").AppendTo(J(e).Children(".filename"))
  79. .Click(ev =>
  80. {
  81. ev.PreventDefault();
  82. this.entities.RemoveAt(x);
  83. Populate();
  84. });
  85. });
  86. }
  87. protected virtual void UpdateInterface()
  88. {
  89. var addButton = this.toolbar.FindButton("add-file-button");
  90. addButton.ToggleClass("disabled", ReadOnly);
  91. }
  92. public bool ReadOnly
  93. {
  94. get { return !Script.IsNullOrUndefined(uploadInput.GetAttribute("disabled")); }
  95. set
  96. {
  97. if (this.ReadOnly != value)
  98. {
  99. if (value)
  100. uploadInput.Attribute("disabled", "disabled");
  101. else
  102. uploadInput.RemoveAttr("disabled");
  103. UpdateInterface();
  104. }
  105. }
  106. }
  107. public virtual List<UploadedFile> Value
  108. {
  109. get
  110. {
  111. return this.entities.Select(x => jQuery.ExtendObject(new UploadedFile(), x)).ToList();
  112. }
  113. set
  114. {
  115. this.entities = (value ?? new List<UploadedFile>()).Select(x => jQuery.ExtendObject(new UploadedFile(), x)).ToList();
  116. Populate();
  117. UpdateInterface();
  118. }
  119. }
  120. void IGetEditValue.GetEditValue(PropertyItem property, dynamic target)
  121. {
  122. if (JsonEncodeValue)
  123. {
  124. target[property.Name] = Q.ToJSON(this.Value);
  125. }
  126. else
  127. {
  128. target[property.Name] = this.Value;
  129. }
  130. }
  131. void ISetEditValue.SetEditValue(dynamic source, PropertyItem property)
  132. {
  133. var val = source[property.Name];
  134. if (val is string)
  135. {
  136. var json = (source[property.Name] as string).TrimToNull() ?? "[]";
  137. if (json.StartsWith("[") && json.EndsWith("]"))
  138. this.Value = jQuery.ParseJson(json).As<List<UploadedFile>>();
  139. else
  140. this.Value = new List<UploadedFile>
  141. {
  142. new UploadedFile
  143. {
  144. Filename = json,
  145. OriginalName = "UnknownFile"
  146. }
  147. };
  148. }
  149. else
  150. this.Value = (List<UploadedFile>)val;
  151. }
  152. [Option]
  153. public bool JsonEncodeValue { get; set; }
  154. }
  155. }