DataActions.js 4.5 KB

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