DataActions.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. },
  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. },
  55. success: function (data) {
  56. Dcat.NP.done();
  57. if (data.status) {
  58. Dcat.reload();
  59. Dcat.swal.success(data.message, keys.join(', '));
  60. } else {
  61. Dcat.swal.error(data.message, keys.join(', '));
  62. }
  63. }
  64. });
  65. });
  66. };
  67. },
  68. // 图片预览
  69. 'preview-img': function ($action, Dcat) {
  70. return function () {
  71. return Dcat.helpers.previewImage($(this).attr('src'));
  72. };
  73. },
  74. 'popover': function ($action) {
  75. $('.popover').remove();
  76. return function () {
  77. $action.popover()
  78. };
  79. },
  80. 'box-actions': function () {
  81. $('.box [data-action="collapse"]').click(function (e) {
  82. e.preventDefault();
  83. $(this).find('i').toggleClass('icon-minus icon-plus');
  84. $(this).closest('.box').find('.box-body').first().collapse("toggle");
  85. });
  86. // Close box
  87. $('.box [data-action="remove"]').click(function () {
  88. $(this).closest(".box").removeClass().slideUp("fast");
  89. });
  90. },
  91. dropdown: function () {
  92. function hide() {
  93. $('.dropdown-menu').removeClass('show')
  94. }
  95. $(document).off('click', document, hide)
  96. $(document).on('click', hide);
  97. function toggle(event) {
  98. var $this = $(this);
  99. $('.dropdown-menu').each(function () {
  100. if ($this.next()[0] !== this) {
  101. $(this).removeClass('show');
  102. }
  103. });
  104. $this.Dropdown('toggleSubmenu')
  105. }
  106. function fix(event) {
  107. event.preventDefault()
  108. event.stopPropagation()
  109. // console.log(666);
  110. setTimeout(function() {
  111. $(this).Dropdown('fixPosition')
  112. }, 1)
  113. }
  114. $('[data-toggle="dropdown"]').off('click').on("click", toggle).on("click", fix);
  115. }
  116. };
  117. export default class DataActions {
  118. constructor(Dcat) {
  119. let actions = $.extend(defaultActions, Dcat.actions()),
  120. $action,
  121. name,
  122. func;
  123. for (name in actions) {
  124. $action = $(`[data-action="${name}"]`);
  125. func = actions[name]($action, Dcat);
  126. if (typeof func === 'function') {
  127. // 必须先取消再绑定,否则可能造成重复绑定的效果
  128. $action.off('click').click(func);
  129. }
  130. }
  131. }
  132. }