grid-row-action.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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) === 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 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. return function (result) {
  93. var request = result[0], target = result[1];
  94. if (options.success(target, request) === false) {
  95. return;
  96. }
  97. if (request && typeof request.responseJSON === 'object') {
  98. Dcat.error(request.responseJSON.message)
  99. }
  100. console.error(request);
  101. }
  102. }
  103. _buildActionPromise(target, data) {
  104. let options = this.options;
  105. return new Promise(function (resolve, reject) {
  106. Object.assign(data, {
  107. _token: Dcat.token,
  108. _action: options.calledClass,
  109. _key: options.key,
  110. });
  111. Dcat.NP.start();
  112. $.ajax({
  113. method: options.method,
  114. url: options.url,
  115. data: data,
  116. success: function (data) {
  117. target.attr('loading', 0);
  118. Dcat.NP.done();
  119. resolve([data, target]);
  120. },
  121. error:function(request){
  122. target.attr('loading', 0);
  123. Dcat.NP.done();
  124. reject([request, target]);
  125. }
  126. });
  127. });
  128. }
  129. }
  130. Dcat.grid.RowAction = function (opts) {
  131. return new RowAction(opts);
  132. };
  133. })(Dcat);