grid-row-action.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. (function (Dcat) {
  2. class RowAction {
  3. constructor(options) {
  4. this.options = $.extend({
  5. selector: null, // 按钮选择器
  6. event: 'click',
  7. method: 'POST',
  8. key: null, // 行主键
  9. url: null,
  10. data: {}, // 发送到接口的附加参数
  11. calledClass: null,
  12. before: function (data, target) {}, // 发起请求之前回调,返回false可以中断请求
  13. html: function (target, html, data) { // 处理返回的HTML代码
  14. target.html(html);
  15. },
  16. success: function (target, results) {}, // 请求成功回调,返回false可以中断默认的成功处理逻辑
  17. error: function (target, results) {}, // 请求出错回调,返回false可以中断默认的错误处理逻辑
  18. }, options);
  19. this._bind();
  20. }
  21. _bind() {
  22. let _this = this, options = _this.options;
  23. $(options.selector).off(options.event).on(options.event, function (e) {
  24. let data = $(this).data(),
  25. target = $(this);
  26. if (target.attr('loading') > 0) {
  27. return;
  28. }
  29. if (options.before(data, target, _this) === false) {
  30. return;
  31. }
  32. // 发起请求
  33. function request() {
  34. target.attr('loading', 1);
  35. Object.assign(data, options.data);
  36. _this._buildActionPromise(target, data).then(_this._resolver()).catch(_this._reject());
  37. }
  38. var conform = data['confirm'];
  39. delete data['confirm'];
  40. if (conform) {
  41. Dcat.confirm(conform[0], conform[1], request);
  42. } else {
  43. request()
  44. }
  45. });
  46. }
  47. _resolver() {
  48. let _this = this, options = _this.options;
  49. return function (result) {
  50. var response = result[0],
  51. target = result[1];
  52. if (options.success(target, response) === false) {
  53. return;
  54. }
  55. if (typeof response !== 'object') {
  56. return Dcat.error({type: 'error', title: 'Oops!'});
  57. }
  58. var then = function (then) {
  59. switch (then.action) {
  60. case 'refresh':
  61. Dcat.reload();
  62. break;
  63. case 'download':
  64. window.open(then.value, '_blank');
  65. break;
  66. case 'redirect':
  67. Dcat.reload(then.value);
  68. break;
  69. case 'location':
  70. window.location = then.value;
  71. break;
  72. case 'script':
  73. (function () {
  74. eval(then.value);
  75. })();
  76. break;
  77. }
  78. };
  79. if (typeof response.html === 'string' && response.html) {
  80. // 处理api返回的HTML代码
  81. options.html(target, response.html, response);
  82. }
  83. if (typeof response.data.message === 'string' && response.data.type) {
  84. Dcat[response.data.type](response.data.message);
  85. }
  86. if (response.data.then) {
  87. then(response.data.then);
  88. }
  89. };
  90. }
  91. _reject() {
  92. let options = this.options;
  93. return function (result) {
  94. var request = result[0], target = result[1];
  95. if (options.success(target, request) === false) {
  96. return;
  97. }
  98. if (request && typeof request.responseJSON === 'object') {
  99. Dcat.error(request.responseJSON.message)
  100. }
  101. console.error(request);
  102. }
  103. }
  104. _buildActionPromise(target, data) {
  105. let options = this.options;
  106. return new Promise(function (resolve, reject) {
  107. Object.assign(data, {
  108. _token: Dcat.token,
  109. _action: options.calledClass,
  110. _key: options.key,
  111. });
  112. Dcat.NP.start();
  113. $.ajax({
  114. method: options.method,
  115. url: options.url,
  116. data: data,
  117. success: function (data) {
  118. target.attr('loading', 0);
  119. Dcat.NP.done();
  120. resolve([data, target]);
  121. },
  122. error:function(request){
  123. target.attr('loading', 0);
  124. Dcat.NP.done();
  125. reject([request, target]);
  126. }
  127. });
  128. });
  129. }
  130. }
  131. Dcat.grid.RowAction = function (opts) {
  132. return new RowAction(opts);
  133. };
  134. })(Dcat);