Orderable.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 行排序
  3. */
  4. export default class Orderable {
  5. constructor(Helper, opts) {
  6. this.options = $.extend({
  7. button: null,
  8. url: '',
  9. }, opts);
  10. this.helper = Helper;
  11. this.direction = this.key = this.depth = this.row = this._req = null;
  12. this._init();
  13. }
  14. _init() {
  15. let _this = this;
  16. $(_this.options.button).off('click').click(function () {
  17. if (_this._req) {
  18. return;
  19. }
  20. _this._req = 1;
  21. Dcat.loading();
  22. let $this = $(this);
  23. _this.key = $this.data('id');
  24. _this.direction = $this.data('direction');
  25. _this.row = $this.closest('tr');
  26. _this.depth = _this.helper.getDepth(_this.row);
  27. _this.request();
  28. })
  29. }
  30. request() {
  31. var _this = this,
  32. helper = _this.helper,
  33. key = _this.key,
  34. row = _this.row,
  35. depth = _this.depth,
  36. direction = _this.direction,
  37. prevAll = row.prevAll(),
  38. nextAll = row.nextAll(),
  39. prev = row.prevAll('tr').first(),
  40. next = row.nextAll('tr').first();
  41. $.put({
  42. url: _this.options.url.replace(':key', key),
  43. data: {_orderable: direction},
  44. success: function(data){
  45. Dcat.loading(false);
  46. _this._req = 0;
  47. if (! data.status) {
  48. return data.data.message && Dcat.warning(data.data.message);
  49. }
  50. Dcat.success(data.data.message);
  51. if (direction) {
  52. var prevRow = helper.sibling(prevAll, depth);
  53. if (helper.swapable(prevRow, depth) && prev.length && helper.getDepth(prev) >= depth) {
  54. prevRow.before(row);
  55. // 把所有子节点上移
  56. helper.getChildren(nextAll, row).forEach(function (v) {
  57. prevRow.before(v)
  58. });
  59. }
  60. } else {
  61. var nextRow = helper.sibling(nextAll, depth),
  62. nextRowChildren = nextRow ? helper.getChildren(nextRow.nextAll(), nextRow) : [];
  63. if (helper.swapable(nextRow, depth) && next.length && helper.getDepth(next) >= depth) {
  64. nextAll = row.nextAll();
  65. if (nextRowChildren.length) {
  66. nextRow = $(nextRowChildren.pop())
  67. }
  68. // 把所有子节点下移
  69. var all = [];
  70. helper.getChildren(nextAll, row).forEach(function (v) {
  71. all.unshift(v)
  72. });
  73. all.forEach(function(v) {
  74. nextRow.after(v)
  75. });
  76. nextRow.after(row);
  77. }
  78. }
  79. },
  80. error: function (a, b, c) {
  81. _this._req = 0;
  82. Dcat.loading(false);
  83. Dcat.handleAjaxError(a, b, c)
  84. }
  85. });
  86. }
  87. }