DialogForm.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. let w = top || window;
  2. export default class DialogForm {
  3. constructor(Dcat, options) {
  4. let _this = this, nullFun = function (a, b) {};
  5. _this.options = $.extend({
  6. // 弹窗标题
  7. title: '',
  8. // 默认地址
  9. defaultUrl: '',
  10. // 需要绑定的按钮选择器
  11. buttonSelector: '',
  12. // 弹窗大小
  13. area: [],
  14. // 语言包
  15. lang: {
  16. submit: Dcat.lang['submit'] || 'Submit',
  17. reset: Dcat.lang['reset'] || 'Reset',
  18. },
  19. // get参数名称
  20. query: '',
  21. // 保存成功后是否刷新页面
  22. forceRefresh: false,
  23. disableReset: false,
  24. // 执行保存操作后回调
  25. saved: nullFun,
  26. // 保存成功回调
  27. success: nullFun,
  28. // 保存失败回调
  29. error: nullFun,
  30. }, options);
  31. // 表单
  32. _this.$form = null;
  33. // 目标按钮
  34. _this.$target = null;
  35. _this._dialog = w.layer;
  36. _this._counter = 1;
  37. _this._idx = {};
  38. _this._dialogs = {};
  39. _this.isLoading = 0;
  40. _this.isSubmitting = 0;
  41. _this._execute(options)
  42. }
  43. _execute(options) {
  44. let _this = this,
  45. defUrl = options.defaultUrl;
  46. (! options.buttonSelector) || $(options.buttonSelector).off('click').click(function () {
  47. _this.$target = $(this);
  48. let counter = _this.$target.attr('counter'), url;
  49. if (! counter) {
  50. counter = _this._counter;
  51. _this.$target.attr('counter', counter);
  52. _this._counter++;
  53. }
  54. url = _this.$target.data('url') || defUrl; // 给弹窗页面链接追加参数
  55. if (url.indexOf('?') === -1) {
  56. url += '?' + options.query + '=1'
  57. } else if (url.indexOf(options.query) === -1) {
  58. url += '&' + options.query + '=1'
  59. }
  60. _this._build(url, counter);
  61. });
  62. options.buttonSelector || setTimeout(function () {
  63. _this._build(defUrl, _this._counter)
  64. }, 400);
  65. }
  66. // 构建表单
  67. _build(url, counter) {
  68. let _this = this,
  69. $btn = _this.$target;
  70. if (! url || _this.isLoading) {
  71. return;
  72. }
  73. if (_this._dialogs[counter]) { // 阻止同个类型的弹窗弹出多个
  74. _this._dialogs[counter].show();
  75. try {
  76. _this._dialog.restore(_this._idx[counter]);
  77. } catch (e) {
  78. }
  79. return;
  80. }
  81. // 刷新或跳转页面时移除弹窗
  82. Dcat.onPjaxComplete(() => {
  83. _this._destroy(counter);
  84. });
  85. _this.isLoading = 1;
  86. $btn && $btn.buttonLoading();
  87. Dcat.NP.start();
  88. // 请求表单内容
  89. $.get(url, function (template) {
  90. _this.isLoading = 0;
  91. Dcat.NP.done();
  92. if ($btn) {
  93. $btn.buttonLoading(false);
  94. setTimeout(function () {
  95. $btn.find('.waves-ripple').remove();
  96. }, 50);
  97. }
  98. _this._popup(template, counter);
  99. });
  100. }
  101. // 弹出弹窗
  102. _popup(template, counter) {
  103. let _this = this,
  104. options = _this.options;
  105. // 加载js代码
  106. template = Dcat.assets.filterScriptsAndLoad(template).render();
  107. let btns = [options.lang.submit],
  108. dialogOpts = {
  109. type: 1,
  110. area: (function (v) {
  111. // 屏幕小于800则最大化展示
  112. if (w.screen.width <= 800) {
  113. return ['100%', '100%',];
  114. }
  115. return v;
  116. })(options.area),
  117. content: template,
  118. title: options.title,
  119. yes: function () {
  120. _this._submit()
  121. },
  122. cancel: function () {
  123. if (options.forceRefresh) { // 是否强制刷新
  124. _this._dialogs[counter] = _this._idx[counter] = null;
  125. } else {
  126. _this._dialogs[counter].hide();
  127. return false;
  128. }
  129. }
  130. };
  131. if (! options.disableReset) {
  132. btns.push(options.lang.reset);
  133. dialogOpts.btn2 = function () { // 重置按钮
  134. _this.$form.trigger('reset');
  135. return false;
  136. };
  137. }
  138. dialogOpts.btn = btns;
  139. _this._idx[counter] = _this._dialog.open(dialogOpts);
  140. _this._dialogs[counter] = w.$('#layui-layer' + _this._idx[counter]);
  141. _this.$form = _this._dialogs[counter].find('form').first();
  142. }
  143. // 销毁弹窗
  144. _destroy(counter) {
  145. let dialogs = this._dialogs;
  146. this._dialog.close(this._idx[counter]);
  147. dialogs[counter] && dialogs[counter].remove();
  148. dialogs[counter] = null;
  149. }
  150. // 提交表单
  151. _submit() {
  152. let _this = this,
  153. options = _this.options,
  154. counter = _this.$target.attr('counter'),
  155. $submitBtn = _this._dialogs[counter].find('.layui-layer-btn0');
  156. if (_this.isSubmitting) {
  157. return;
  158. }
  159. Dcat.Form({
  160. form: _this.$form,
  161. disableRedirect: true,
  162. before: function () {
  163. // 验证表单
  164. _this.$form.validator('validate');
  165. if (_this.$form.find('.has-error').length > 0) {
  166. return false;
  167. }
  168. _this.isSubmitting = 1;
  169. $submitBtn.buttonLoading();
  170. },
  171. after: function (success, res) {
  172. $submitBtn.buttonLoading(false);
  173. _this.isSubmitting = 0;
  174. options.saved(success, res);
  175. if (!success) {
  176. return options.error(success, res);
  177. }
  178. if (res.status) {
  179. options.success(success, res);
  180. _this._destroy(counter);
  181. return;
  182. }
  183. options.error(success, res);
  184. Dcat.error(res.message || 'Save failed.');
  185. }
  186. });
  187. return false;
  188. }
  189. }