DataActions.js 4.2 KB

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