ModalInputViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using Abp.Localization;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.Mvc;
  5. using System.Web.WebPages;
  6. using IwbZero;
  7. namespace WePlatform.Views.Shared.Modals
  8. {
  9. #region ModalBody
  10. public class Input
  11. {
  12. public Input(string id, string displayName = "", InputTypes inputType = InputTypes.Text, string placeholder = "", string name = null, string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "", string help = "")
  13. {
  14. Id = id;
  15. Name = string.IsNullOrEmpty(name) ? id : name;
  16. DisplayName = string.IsNullOrEmpty(displayName) ? L(id) : displayName;
  17. InputType = inputType;
  18. _placeholder = placeholder;
  19. Value = value;
  20. _class = @class;
  21. DataOptions = dataOptions;
  22. Events = events;
  23. Styles = styles;
  24. Other = other;
  25. IsHidden = hide;
  26. IsRequired = !hide;
  27. IsDisabled = false;
  28. IsReadOnly = false;
  29. _label = label;
  30. Help = help;
  31. IsSm = true;
  32. }
  33. #region 方法
  34. public Input SetDataOptions(string dataOptions)
  35. {
  36. DataOptions = dataOptions;
  37. return this;
  38. }
  39. public Input SetEvents(string events)
  40. {
  41. Events = events;
  42. return this;
  43. }
  44. public Input SetName(string name)
  45. {
  46. Name = name;
  47. return this;
  48. }
  49. public Input SetHelp(string help)
  50. {
  51. Help = help;
  52. return this;
  53. }
  54. public Input SetHidden()
  55. {
  56. IsHidden = true;
  57. return this;
  58. }
  59. public Input SetLabel(string label)
  60. {
  61. _label = label;
  62. return this;
  63. }
  64. public Input SetLabelName(string label)
  65. {
  66. _label = "<label class=\"iwb-label col-md-2 form-control-label\" for=\"" + Id + "\">" + label +
  67. "</label>";
  68. return this;
  69. }
  70. public Input SetLabelNameRequired(string label)
  71. {
  72. _label = "<label class=\"iwb-label col-md-2 form-control-label iwb-label-required\" for=\"" + Id + "\">" + label +
  73. "</label>";
  74. return this;
  75. }
  76. public Input SetNotRequired()
  77. {
  78. IsRequired = false;
  79. return this;
  80. }
  81. public Input SetDisabled()
  82. {
  83. IsDisabled = true;
  84. return this;
  85. }
  86. public Input SetReadonly()
  87. {
  88. IsReadOnly = true;
  89. return this;
  90. }
  91. public Input SetSelectOptions(List<SelectListItem> poSelectOptions, bool isMultiple = false, bool isAddBlank = false)
  92. {
  93. string lcRetval = isAddBlank ? "<option value=\"\" selected>请选择</option>" : "";
  94. IsMultiple = isMultiple;
  95. if (poSelectOptions != null && poSelectOptions.Any())
  96. {
  97. foreach (var s in poSelectOptions)
  98. {
  99. lcRetval += "<option value=\"" + s.Value + "\">" + s.Text + "</option>\r\n";
  100. }
  101. }
  102. SelectOptions = lcRetval;
  103. InputType = InputTypes.List;
  104. return this;
  105. }
  106. public Input SetSelectOptions(string poSelectOptions, bool isMultiple = false, bool isAddBlank = false)
  107. {
  108. string lcRetval = isAddBlank ? "<option value=\"\" selected>请选择</option>" : "";
  109. IsMultiple = isMultiple;
  110. if (!string.IsNullOrEmpty(poSelectOptions))
  111. {
  112. lcRetval += poSelectOptions;
  113. }
  114. SelectOptions = lcRetval;
  115. InputType = InputTypes.List;
  116. return this;
  117. }
  118. public Input SetLayout(string labelLayout, string inputLayout)
  119. {
  120. LabelLayoutClass = labelLayout;
  121. InputLayoutClass = inputLayout;
  122. return this;
  123. }
  124. //public Input SetLayout(int labelLayout, int? inputLayout = null, string layoutPrefix = "col-md-")
  125. //{
  126. // inputLayout = inputLayout ?? 12 - labelLayout;
  127. // LabelLayout = $"{layoutPrefix}{labelLayout}";
  128. // InputLayout = $"{layoutPrefix}{inputLayout}";
  129. // return this;
  130. //}
  131. public Input SetOuterDiv(string classStr, string other = "")
  132. {
  133. DivClass = classStr;
  134. DivOther = other;
  135. return this;
  136. }
  137. /// <summary>
  138. /// 外层上面其他标签
  139. /// </summary>
  140. /// <param name="beforeStr"></param>
  141. /// <returns></returns>
  142. public Input SetOuterBefore(string beforeStr)
  143. {
  144. DivOutBefore = beforeStr;
  145. return this;
  146. }
  147. /// <summary>
  148. /// 外层下面其他标签
  149. /// </summary>
  150. /// <param name="afterStr"></param>
  151. /// <returns></returns>
  152. public Input SetOuterAfter(string afterStr)
  153. {
  154. DivOutAfter = afterStr;
  155. return this;
  156. }
  157. /// <summary>
  158. /// 内层上面其他标签
  159. /// </summary>
  160. /// <param name="beforeStr"></param>
  161. /// <returns></returns>
  162. public Input SetInterBefore(string beforeStr)
  163. {
  164. DivInterBefore = beforeStr;
  165. return this;
  166. }
  167. /// <summary>
  168. /// 内层下面其他标签
  169. /// </summary>
  170. /// <param name="afterStr"></param>
  171. /// <returns></returns>
  172. public Input SetInterAfter(string afterStr)
  173. {
  174. DivInterAfter = afterStr;
  175. return this;
  176. }
  177. /// <summary>
  178. /// 添加搜索按钮
  179. /// </summary>
  180. /// <param name="searchModalId"></param>
  181. /// <param name="target"></param>
  182. /// <param name="clear"></param>
  183. /// <returns></returns>
  184. public Input SetSearchIcon(string searchModalId, string target = "",string clear="")
  185. {
  186. SearchModalId = searchModalId;
  187. _target = target;
  188. Clear = clear;
  189. return this;
  190. }
  191. /// <summary>
  192. /// 配置需要清空的INPUT Id
  193. /// </summary>
  194. /// <param name="clear"></param>
  195. /// <param name="isDefault"></param>
  196. /// <returns></returns>
  197. public Input SetSearchClear(string clear,bool isDefault=true)
  198. {
  199. Clear = isDefault ? $"{clear}No,{clear}Name" : clear;
  200. return this;
  201. }
  202. /// <summary>
  203. /// 输入提示
  204. /// </summary>
  205. /// <param name="placeholder"></param>
  206. /// <returns></returns>
  207. public Input SetPlaceholder(string placeholder)
  208. {
  209. _placeholder = placeholder;
  210. return this;
  211. }
  212. public Input SetNotSmall()
  213. {
  214. IsSm = false;
  215. return this;
  216. }
  217. #endregion
  218. #region 字段属性
  219. public string Id { get; set; }
  220. public string Name { get; set; }
  221. public string DisplayName { get; set; }
  222. public string Help { get; set; }
  223. private string PlaceholderPrefix => InputType == InputTypes.List ? L("PlaceholderSelectHeader") : L("PlaceholderHeader");
  224. private string _placeholder;
  225. public string Placeholder => string.IsNullOrEmpty(_placeholder) ? $"{PlaceholderPrefix}{DisplayName}..." : _placeholder;
  226. public InputTypes InputType { get; set; }
  227. public string TypeStr
  228. {
  229. get
  230. {
  231. switch (InputType)
  232. {
  233. case InputTypes.Text:
  234. return "text";
  235. case InputTypes.Password:
  236. return "password";
  237. case InputTypes.Checkbox:
  238. return "checkbox";
  239. case InputTypes.Radio:
  240. return "radio";
  241. case InputTypes.File:
  242. return "file";
  243. case InputTypes.Number:
  244. return "number";
  245. default:
  246. return "text";
  247. }
  248. }
  249. }
  250. public DateTypes DateType { get; set; }
  251. public bool IsSm { get; set; }
  252. public string Number { get; set; }
  253. public int? Min { get; set; }
  254. public int? Max { get; set; }
  255. public string Value { get; set; }
  256. public string SelectOptions { get; private set; }
  257. private string _class;
  258. public string Class
  259. {
  260. get
  261. {
  262. var classStr = $"form-control {_class}";
  263. if (IsDisabled)
  264. {
  265. classStr += " disabled ";
  266. }
  267. if (IsReadOnly)
  268. {
  269. classStr += " readonly ";
  270. }
  271. if (InputType == InputTypes.Number)
  272. {
  273. classStr += $" {Number} ";
  274. }
  275. if (InputType == InputTypes.Date)
  276. {
  277. classStr += DateType == DateTypes.DateTime ? " iwb-date-time " : " iwb-date ";
  278. }
  279. return classStr;
  280. }
  281. set => _class = value;
  282. }
  283. public string DataOptions { get; set; }
  284. public string Events { get; set; }
  285. public string Styles { get; set; }
  286. private string _other;
  287. public string Other
  288. {
  289. get
  290. {
  291. if (Min != null)
  292. {
  293. _other += $" min={Min}";
  294. }
  295. if (Max != null)
  296. {
  297. _other += $" max={Max}";
  298. }
  299. return _other;
  300. }
  301. set => _other = value;
  302. }
  303. public bool IsHidden { get; set; }
  304. public bool IsRequired { get; set; }
  305. public bool IsDisabled { get; set; }
  306. public bool IsReadOnly { get; set; }
  307. public bool IsMultiple { get; private set; }
  308. public string Required => IsHidden ? "" : (IsRequired ? " required" : "");
  309. public string DivClass { get; set; }
  310. public string DivOther { get; set; }
  311. public string DivOutBefore { get; set; }
  312. public string DivOutAfter { get; set; }
  313. public string DivInterBefore { get; set; }
  314. public string DivInterAfter { get; set; }
  315. public string SearchModalId { get; set; }
  316. public string LabelLayoutClass { get; set; }
  317. public string InputLayoutClass { get; set; }
  318. private string _label;
  319. public string Label => IsHidden
  320. ? ""
  321. : string.IsNullOrEmpty(_label)
  322. ? (IsRequired
  323. ? $"<label class=\"{LabelLayoutClass} iwb-label {(IsSm ? "iwb-label-sm" : "col-form-label")} iwb-label-required\" for=\"{Id}\">{DisplayName}</label>"
  324. : $"<label class=\"{LabelLayoutClass} iwb-label {(IsSm ? "iwb-label-sm" : "col-form-label")} \" for=\"{Id}\">{DisplayName}</label>")
  325. : _label;
  326. public string Disabled => IsDisabled ? "disabled=\"disabled\"" : "";
  327. public string ReadOnly => IsReadOnly ? "readonly=\"readonly\"" : "";
  328. public FileInputOption FileOption { get; set; }
  329. private string _target;
  330. public string Target =>
  331. string.IsNullOrEmpty(_target) ? DefaultTarget ?? "" :
  332. _target.StartsWith(".") ? _target :
  333. _target.StartsWith("#") ? _target : $"#{_target}";
  334. public string DefaultTarget { get; set; }
  335. public string Clear { get; set; }
  336. #endregion
  337. private static string L(string name)
  338. {
  339. var str = LocalizationHelper.GetSource(IwbZeroConsts.LocalizationSourceName).GetString(name);
  340. return str;
  341. }
  342. }
  343. public class InputHide : Input
  344. {
  345. public InputHide(string id, string displayName = "", string placeholder = "", string name = "", string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", string label = "")
  346. : base(id, displayName, InputTypes.Textarea, placeholder, name, value, @class, dataOptions, events, styles, other, true, label)
  347. {
  348. }
  349. }
  350. public class InputTextarea : Input
  351. {
  352. public InputTextarea(string id, string displayName = "", string placeholder = "", string name = "", string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "")
  353. : base(id, displayName, InputTypes.Textarea, placeholder, name, value, @class, dataOptions, events, styles, other, hide, label)
  354. {
  355. }
  356. }
  357. public class InputKindeditor : InputTextarea
  358. {
  359. public InputKindeditor(string id, string displayName = "", string placeholder = "", string name = "", string value = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "")
  360. : base(id, displayName, placeholder, name, value, "kindeditor", dataOptions, events, styles, other, hide, label)
  361. {
  362. }
  363. }
  364. public class InputDate : Input
  365. {
  366. public InputDate(string id, string displayName = "", DateTypes date = DateTypes.Date, string placeholder = "", string name = "", string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "")
  367. : base(id, displayName, InputTypes.Date, placeholder, name, value, @class, dataOptions, events, styles, other, hide, label)
  368. {
  369. DateType = date;
  370. }
  371. public InputDate SetDateType(DateTypes date)
  372. {
  373. DateType = date;
  374. return this;
  375. }
  376. }
  377. public class InputDateTime : Input
  378. {
  379. public InputDateTime(string id, string displayName = "", DateTypes date = DateTypes.DateTime, string placeholder = "", string name = "", string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "")
  380. : base(id, displayName, InputTypes.Date, placeholder, name, value, @class, dataOptions, events, styles, other, hide, label)
  381. {
  382. DateType = date;
  383. }
  384. public InputDateTime SetDateType(DateTypes date)
  385. {
  386. DateType = date;
  387. return this;
  388. }
  389. }
  390. public class InputNumber : Input
  391. {
  392. /// <summary>
  393. ///
  394. /// </summary>
  395. /// <param name="id"></param>
  396. /// <param name="displayName"></param>
  397. /// <param name="numberType">0是整数,其他是小数</param>
  398. /// <param name="min"></param>
  399. /// <param name="max"></param>
  400. /// <param name="placeholder"></param>
  401. /// <param name="name"></param>
  402. /// <param name="value"></param>
  403. /// <param name="class"></param>
  404. /// <param name="dataOptions"></param>
  405. /// <param name="events"></param>
  406. /// <param name="styles"></param>
  407. /// <param name="other"></param>
  408. /// <param name="hide"></param>
  409. /// <param name="label"></param>
  410. public InputNumber(string id, string displayName = "", int numberType = 0, int? min = null, int? max = null, string placeholder = "", string name = "", string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "")
  411. : base(id, displayName, InputTypes.Number, placeholder, name, value, @class, dataOptions, events, styles, other, hide, label)
  412. {
  413. Number = numberType == 0 ? "number" : "digits";
  414. Min = min;
  415. Max = max;
  416. }
  417. public InputNumber SetRange(int? min = null, int? max = null, int numberType = 1)
  418. {
  419. Min = min;
  420. Max = max;
  421. Number = numberType == 0 ? "number" : "digits";
  422. Other = Other ?? "";
  423. if (Min != null)
  424. {
  425. Other += $" min={Min}";
  426. }
  427. if (Max != null)
  428. {
  429. Other += $" max={Max}";
  430. }
  431. return this;
  432. }
  433. public InputNumber SetNumberType(int numberType = 1, int? min = null, int? max = null)
  434. {
  435. Number = numberType == 0 ? "number" : "digits";
  436. Min = min;
  437. Max = max;
  438. return this;
  439. }
  440. public InputNumber SetMin(int min)
  441. {
  442. Min = min;
  443. return this;
  444. }
  445. public InputNumber SetMax(int max)
  446. {
  447. Max = max;
  448. return this;
  449. }
  450. }
  451. public class InputFile : Input
  452. {
  453. public InputFile(string id, string displayName = "", string placeholder = "", string name = "", string value = "", string @class = "", string dataOptions = "", string events = "", string styles = "", string other = "", bool hide = false, string label = "")
  454. : base(id, displayName, InputTypes.File, placeholder, name, value, @class, dataOptions, events, styles, other, hide, label)
  455. {
  456. }
  457. /// <summary>
  458. /// 文件选择框设置信息
  459. /// </summary>
  460. /// <param name="opt"></param>
  461. /// <returns></returns>
  462. public InputFile SetFileOption(FileInputOption opt)
  463. {
  464. opt.FileInfoField = string.IsNullOrEmpty(opt.FileInfoField) ? Id : opt.FileInfoField;
  465. FileOption = opt;
  466. InputType = InputTypes.File;
  467. return this;
  468. }
  469. /// <summary>
  470. /// 文件选择框设置信息
  471. /// </summary>
  472. /// <returns></returns>
  473. public InputFile SetFileOption(string fileInfoField = "", string fileNameField = "", string fileExtField = "", int maxSize = 5)
  474. {
  475. fileInfoField = string.IsNullOrEmpty(fileInfoField) ? Id : fileInfoField;
  476. FileOption = new FileInputOption(fileInfoField, fileNameField, fileExtField, false, maxSize);
  477. return this;
  478. }
  479. /// <summary>
  480. /// 图片选择框设置信息
  481. /// </summary>
  482. /// <returns></returns>
  483. public InputFile SetImageOption(string fileInfoField = "", string fileNameField = "", string fileExtField = "", int maxSize = 5)
  484. {
  485. fileInfoField = string.IsNullOrEmpty(fileInfoField) ? Id : fileInfoField;
  486. FileOption = new FileInputOption(fileInfoField, fileNameField, fileExtField, true, maxSize);
  487. return this;
  488. }
  489. }
  490. public class SpecialInputModel
  491. {
  492. public string Id { get; set; }
  493. public string InputStr { get; set; }
  494. }
  495. public class FileInputOption
  496. {
  497. public FileInputOption(string fileInfoField, string fileNameField = "", string fileExtField = "", bool isImage = true, int maxSize = 5, string fileIdField = null)
  498. {
  499. FileInfoField = fileInfoField;
  500. FileNameField = fileNameField;
  501. FileExtField = fileExtField;
  502. IsImage = isImage;
  503. MaxSize = maxSize;
  504. FileIdField = fileIdField;
  505. }
  506. public string FileIdField { get; set; }
  507. public string FileInfoField { get; set; }
  508. public string FileNameField { get; set; }
  509. public string FileExtField { get; set; }
  510. public bool IsImage { get; set; }
  511. /// <summary>
  512. /// 文件最大M(默认5M)
  513. /// </summary>
  514. public int MaxSize { get; set; } = 5;
  515. }
  516. //public class SelectOption
  517. //{
  518. // public SelectOption(string text, string value = "")
  519. // {
  520. // Text = text;
  521. // Value = value;
  522. // }
  523. // public string Value { get; set; }
  524. // public string Text { get; set; }
  525. //}
  526. public enum InputTypes
  527. {
  528. Text, List, Checkbox, Radio, Password, Textarea, File, Number, Date
  529. }
  530. public enum DateTypes
  531. {
  532. Date, DateTime
  533. }
  534. #endregion
  535. }