DialogForm.js 6.8 KB

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