DialogForm.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. selector = options.buttonSelector;
  47. selector && $(selector).off('click').click(function () {
  48. _this.$target = $(this);
  49. let counter = _this.$target.attr('counter'), url;
  50. if (! counter) {
  51. counter = _this._counter;
  52. _this.$target.attr('counter', counter);
  53. _this._counter++;
  54. }
  55. url = _this.$target.data('url') || defUrl; // 给弹窗页面链接追加参数
  56. if (url.indexOf('?') === -1) {
  57. url += '?' + options.query + '=1'
  58. } else if (url.indexOf(options.query) === -1) {
  59. url += '&' + options.query + '=1'
  60. }
  61. _this._build(url, counter);
  62. });
  63. selector || setTimeout(function () {
  64. _this._build(defUrl, _this._counter)
  65. }, 400);
  66. }
  67. // 构建表单
  68. _build(url, counter) {
  69. let _this = this,
  70. $btn = _this.$target;
  71. if (! url || _this.isLoading) {
  72. return;
  73. }
  74. if (_this._dialogs[counter]) { // 阻止同个类型的弹窗弹出多个
  75. _this._dialogs[counter].show();
  76. try {
  77. _this._dialog.restore(_this._idx[counter]);
  78. } catch (e) {
  79. }
  80. return;
  81. }
  82. // 刷新或跳转页面时移除弹窗
  83. Dcat.onPjaxComplete(() => {
  84. _this._destroy(counter);
  85. });
  86. _this.isLoading = 1;
  87. $btn && $btn.buttonLoading();
  88. Dcat.NP.start();
  89. // 请求表单内容
  90. $.ajax({
  91. url: url,
  92. success: function (template) {
  93. _this.isLoading = 0;
  94. Dcat.NP.done();
  95. if ($btn) {
  96. $btn.buttonLoading(false);
  97. setTimeout(function () {
  98. $btn.find('.waves-ripple').remove();
  99. }, 50);
  100. }
  101. _this._popup(template, counter);
  102. }
  103. });
  104. }
  105. // 弹出弹窗
  106. _popup(template, counter) {
  107. let _this = this,
  108. options = _this.options;
  109. // 加载js代码
  110. template = Dcat.assets.filterScriptsAndLoad(template).render();
  111. let btns = [options.lang.submit],
  112. dialogOpts = {
  113. type: 1,
  114. area: (function (v) {
  115. // 屏幕小于800则最大化展示
  116. if (w.screen.width <= 800) {
  117. return ['100%', '100%',];
  118. }
  119. return v;
  120. })(options.area),
  121. content: template,
  122. title: options.title,
  123. yes: function () {
  124. _this._submit()
  125. },
  126. cancel: function () {
  127. if (options.forceRefresh) { // 是否强制刷新
  128. _this._dialogs[counter] = _this._idx[counter] = null;
  129. } else {
  130. _this._dialogs[counter].hide();
  131. return false;
  132. }
  133. }
  134. };
  135. if (! options.disableReset) {
  136. btns.push(options.lang.reset);
  137. dialogOpts.btn2 = function () { // 重置按钮
  138. _this.$form.trigger('reset');
  139. return false;
  140. };
  141. }
  142. dialogOpts.btn = btns;
  143. _this._idx[counter] = _this._dialog.open(dialogOpts);
  144. _this._dialogs[counter] = w.$('#layui-layer' + _this._idx[counter]);
  145. _this.$form = _this._dialogs[counter].find('form').first();
  146. }
  147. // 销毁弹窗
  148. _destroy(counter) {
  149. let dialogs = this._dialogs;
  150. this._dialog.close(this._idx[counter]);
  151. dialogs[counter] && dialogs[counter].remove();
  152. dialogs[counter] = null;
  153. }
  154. // 提交表单
  155. _submit() {
  156. let _this = this,
  157. options = _this.options,
  158. counter = _this.$target.attr('counter'),
  159. $submitBtn = _this._dialogs[counter].find('.layui-layer-btn0');
  160. if (_this.isSubmitting) {
  161. return;
  162. }
  163. Dcat.Form({
  164. form: _this.$form,
  165. redirect: false,
  166. before: function () {
  167. // 验证表单
  168. _this.$form.validator('validate');
  169. if (_this.$form.find('.has-error').length > 0) {
  170. return false;
  171. }
  172. _this.isSubmitting = 1;
  173. $submitBtn.buttonLoading();
  174. },
  175. after: function (success, res) {
  176. $submitBtn.buttonLoading(false);
  177. _this.isSubmitting = 0;
  178. if (options.saved(success, res) === false) {
  179. return false;
  180. }
  181. if (! success) {
  182. return options.error(success, res);
  183. }
  184. if (res.status) {
  185. let r = options.success(success, res);
  186. _this._destroy(counter);
  187. return r;
  188. }
  189. return options.error(success, res);
  190. }
  191. });
  192. return false;
  193. }
  194. }