DataActions.js 3.8 KB

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