DataActions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. let defaultActions = {
  2. // 刷新按钮
  3. refresh: function ($action, Dcat) {
  4. return function () {
  5. Dcat.reload($(this).data('url'));
  6. };
  7. },
  8. // 删除按钮初始化
  9. delete: function ($action, Dcat) {
  10. let lang = Dcat.lang;
  11. return function() {
  12. let url = $(this).data('url'),
  13. redirect = $(this).data('redirect'),
  14. msg = $(this).data('message');
  15. Dcat.confirm(lang.delete_confirm, msg, function () {
  16. Dcat.NP.start();
  17. $.ajax({
  18. method: 'post',
  19. url: url,
  20. data: {
  21. _method: 'delete',
  22. _token: Dcat.token,
  23. },
  24. success: function (data) {
  25. Dcat.NP.done();
  26. if (data.status) {
  27. Dcat.reload(redirect);
  28. Dcat.swal.success(data.message, msg);
  29. } else {
  30. Dcat.swal.error(data.message, msg);
  31. }
  32. }
  33. });
  34. });
  35. };
  36. },
  37. // 批量删除按钮初始化
  38. 'batch-delete': function ($action, Dcat) {
  39. return function() {
  40. let url = $(this).data('url'),
  41. name = $(this).data('name'),
  42. keys = Dcat.grid.selected(name),
  43. lang = Dcat.lang;
  44. if (! keys.length) {
  45. return;
  46. }
  47. Dcat.confirm(lang.delete_confirm, keys.join(', '), function () {
  48. Dcat.NP.start();
  49. $.ajax({
  50. method: 'post',
  51. url: url + '/' + keys.join(','),
  52. data: {
  53. _method: 'delete',
  54. _token: Dcat.token,
  55. },
  56. success: function (data) {
  57. Dcat.NP.done();
  58. if (data.status) {
  59. Dcat.reload();
  60. Dcat.swal.success(data.message, keys.join(', '));
  61. } else {
  62. Dcat.swal.error(data.message, keys.join(', '));
  63. }
  64. }
  65. });
  66. });
  67. };
  68. },
  69. // 图片预览
  70. 'preview-img': function ($action, Dcat) {
  71. return function () {
  72. return Dcat.helpers.previewImage($(this).attr('src'));
  73. };
  74. },
  75. 'popover': function ($action) {
  76. $('.popover').remove();
  77. return function () {
  78. $action.popover()
  79. };
  80. },
  81. 'box-actions': function () {
  82. $('.box [data-action="collapse"]').click(function (e) {
  83. e.preventDefault();
  84. $(this).find('i').toggleClass('icon-minus icon-plus');
  85. $(this).closest('.box').find('.box-body').first().collapse("toggle");
  86. });
  87. // Close box
  88. $('.box [data-action="remove"]').click(function () {
  89. $(this).closest(".box").removeClass().slideUp("fast");
  90. });
  91. }
  92. };
  93. export default class DataActions {
  94. constructor(Dcat) {
  95. let actions = $.extend(defaultActions, Dcat.actions()),
  96. $action,
  97. name,
  98. func;
  99. for (name in actions) {
  100. $action = $(`[data-action="${name}"]`);
  101. func = actions[name]($action, Dcat);
  102. if (typeof func === 'function') {
  103. // 必须先取消再绑定,否则可能造成重复绑定的效果
  104. $action.off('click').click(func);
  105. }
  106. }
  107. }
  108. }