DialogForm.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. let w = window;
  2. if (top && w.layer) {
  3. w = top;
  4. }
  5. export default class DialogForm {
  6. constructor(Dcat, options) {
  7. let self = this, nullFun = function () {};
  8. self.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. resetButton: true,
  27. // 执行保存操作后回调
  28. saved: nullFun,
  29. // 保存成功回调
  30. success: nullFun,
  31. // 保存失败回调
  32. error: nullFun,
  33. }, options);
  34. // 表单
  35. self.$form = null;
  36. // 目标按钮
  37. self.$target = null;
  38. self._dialog = w.layer;
  39. self._counter = 1;
  40. self._idx = {};
  41. self._dialogs = {};
  42. self.rendering = 0;
  43. self.submitting = 0;
  44. self.init(options)
  45. }
  46. init(options) {
  47. let self = this,
  48. defUrl = options.defaultUrl,
  49. selector = options.buttonSelector;
  50. selector && $(selector).off('click').click(function () {
  51. self.$target = $(this);
  52. let counter = self.$target.attr('counter'), url;
  53. if (! counter) {
  54. counter = self._counter;
  55. self.$target.attr('counter', counter);
  56. self._counter++;
  57. }
  58. url = self.$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. self._build(url, counter);
  65. });
  66. selector || setTimeout(function () {
  67. self._build(defUrl, self._counter)
  68. }, 400);
  69. }
  70. // 构建表单
  71. _build(url, counter) {
  72. let self = this,
  73. $btn = self.$target;
  74. if (! url || self.rendering) {
  75. return;
  76. }
  77. if (self._dialogs[counter]) { // 阻止同个类型的弹窗弹出多个
  78. self._dialogs[counter].show();
  79. try {
  80. self._dialog.restore(self._idx[counter]);
  81. } catch (e) {
  82. }
  83. return;
  84. }
  85. // 刷新或跳转页面时移除弹窗
  86. Dcat.onPjaxComplete(() => {
  87. self._destroy(counter);
  88. });
  89. self.rendering = 1;
  90. $btn && $btn.buttonLoading();
  91. Dcat.NP.start();
  92. // 请求表单内容
  93. $.ajax({
  94. url: url,
  95. success: function (template) {
  96. self.rendering = 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. self._popup(template, counter);
  105. }
  106. });
  107. }
  108. // 弹出弹窗
  109. _popup(template, counter) {
  110. let self = this,
  111. options = self.options;
  112. // 加载js代码
  113. template = Dcat.assets.resolveHtml(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. self.submit()
  128. },
  129. cancel: function () {
  130. if (options.forceRefresh) { // 是否强制刷新
  131. self._dialogs[counter] = self._idx[counter] = null;
  132. } else {
  133. self._dialogs[counter].hide();
  134. return false;
  135. }
  136. }
  137. };
  138. if (options.resetButton) {
  139. btns.push(options.lang.reset);
  140. dialogOpts.btn2 = function () { // 重置按钮
  141. self.$form.trigger('reset');
  142. return false;
  143. };
  144. }
  145. dialogOpts.btn = btns;
  146. self._idx[counter] = self._dialog.open(dialogOpts);
  147. self._dialogs[counter] = w.$('#layui-layer' + self._idx[counter]);
  148. self.$form = self._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 self = this,
  160. options = self.options,
  161. counter = self.$target.attr('counter'),
  162. $submitBtn = self._dialogs[counter].find('.layui-layer-btn0');
  163. if (self.submitting) {
  164. return;
  165. }
  166. Dcat.Form({
  167. form: self.$form,
  168. redirect: false,
  169. confirm: Dcat.FormConfirm,
  170. before: function () {
  171. // 验证表单
  172. self.$form.validator('validate');
  173. if (self.$form.find('.has-error').length > 0) {
  174. return false;
  175. }
  176. self.submitting = 1;
  177. $submitBtn.buttonLoading();
  178. },
  179. after: function (status, response) {
  180. $submitBtn.buttonLoading(false);
  181. self.submitting = 0;
  182. if (options.saved(status, response) === false) {
  183. return false;
  184. }
  185. if (! status) {
  186. return options.error(status, response);
  187. }
  188. if (response.status) {
  189. let r = options.success(status, response);
  190. self._destroy(counter);
  191. return r;
  192. }
  193. return options.error(status, response);
  194. }
  195. });
  196. return false;
  197. }
  198. }