DataActions.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 (! response.then) {
  25. response.them = {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. keys = Dcat.grid.selected(name),
  39. lang = Dcat.lang;
  40. if (! keys.length) {
  41. return;
  42. }
  43. let msg = 'ID - ' + keys.join(', ');
  44. Dcat.confirm(lang.delete_confirm, msg, function () {
  45. Dcat.NP.start();
  46. $.delete({
  47. url: url + '/' + keys.join(','),
  48. success: function (data) {
  49. Dcat.NP.done();
  50. if (! response.then) {
  51. response.them = {action: 'refresh', value: true}
  52. }
  53. Dcat.handleJsonResponse(data);
  54. }
  55. });
  56. });
  57. });
  58. },
  59. // 图片预览
  60. 'preview-img' (action, Dcat) {
  61. $document.on('click', action, function () {
  62. return Dcat.helpers.previewImage($(this).attr('src'));
  63. });
  64. },
  65. 'popover' (action, Dcat) {
  66. Dcat.onPjaxComplete(function () {
  67. $('.popover').remove();
  68. }, false);
  69. $document.on('click', action, function () {
  70. $(this).popover()
  71. });
  72. },
  73. 'box-actions' () {
  74. $document.on('click', '.box [data-action="collapse"]', function (e) {
  75. e.preventDefault();
  76. $(this).find('i').toggleClass('icon-minus icon-plus');
  77. $(this).closest('.box').find('.box-body').first().collapse("toggle");
  78. });
  79. // Close box
  80. $document.on('click', '.box [data-action="remove"]', function () {
  81. $(this).closest(".box").removeClass().slideUp("fast");
  82. });
  83. },
  84. dropdown () {
  85. function hide() {
  86. $('.dropdown-menu').removeClass('show')
  87. }
  88. $document.off('click', document, hide)
  89. $document.on('click', hide);
  90. function toggle(event) {
  91. var $this = $(this);
  92. $('.dropdown-menu').each(function () {
  93. if ($this.next()[0] !== this) {
  94. $(this).removeClass('show');
  95. }
  96. });
  97. $this.Dropdown('toggleSubmenu')
  98. }
  99. function fix(event) {
  100. event.preventDefault()
  101. event.stopPropagation()
  102. let $this = $(this);
  103. setTimeout(function() {
  104. $this.Dropdown('fixPosition')
  105. }, 1)
  106. }
  107. let selector = '[data-toggle="dropdown"]';
  108. $document.off('click',selector).on('click', selector, toggle).on('click', selector, fix);
  109. },
  110. };
  111. export default class DataActions {
  112. constructor(Dcat) {
  113. let actions = $.extend(defaultActions, Dcat.actions()),
  114. name;
  115. for (name in actions) {
  116. actions[name](`[data-action="${name}"]`, Dcat);
  117. }
  118. }
  119. }