grid-extend.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. (function (w, $) {
  2. function Tree(opts) {
  3. this.options = $.extend({
  4. button: null,
  5. table: null,
  6. url: '',
  7. perPage: '',
  8. showNextPage: '',
  9. pageQueryName: '',
  10. parentIdQueryName: '',
  11. levelQueryName: '',
  12. loadMoreText: '<svg style="fill:currentColor" t="1582877365167" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="32874" width="24" height="24"><path d="M162.8 515m-98.3 0a98.3 98.3 0 1 0 196.6 0 98.3 98.3 0 1 0-196.6 0Z" p-id="32875"></path><path d="M511.9 515m-98.3 0a98.3 98.3 0 1 0 196.6 0 98.3 98.3 0 1 0-196.6 0Z" p-id="32876"></path><path d="M762.8 515a98.3 98.3 0 1 0 196.6 0 98.3 98.3 0 1 0-196.6 0Z" p-id="32877"></path></svg>',
  13. }, opts);
  14. this.key = this.level = this.row = this.data = this._req = null;
  15. this._init();
  16. }
  17. Tree.prototype = {
  18. _init: function () {
  19. this._bindClick();
  20. },
  21. _bindClick: function () {
  22. var _this = this;
  23. $(this.options.button).off('click').click(function () {
  24. if (_this._req) {
  25. return;
  26. }
  27. var $this = $(this),
  28. _i = $("i", this),
  29. data = {
  30. _token: LA.token,
  31. };
  32. _this.key = $this.data('key');
  33. _this.level = $this.data('level');
  34. _this.row = $this.closest('tr');
  35. _this.data = data;
  36. data[_this.options.parentIdQueryName] = _this.key;
  37. data[_this.options.levelQueryName] = _this.level + 1;
  38. if ($this.data('inserted') == '0') {
  39. _this._request(1);
  40. $this.data('inserted', 1);
  41. }
  42. _i.toggleClass("fa-angle-right fa-angle-down");
  43. getChildren(_this.row.nextAll(), _this.row).forEach(function (v) {
  44. if (_i.hasClass('fa-angle-right')) {
  45. $(v).hide();
  46. } else {
  47. $(v).show();
  48. }
  49. });
  50. })
  51. },
  52. _request: function (page, after) {
  53. var _this = this,
  54. row = _this.row,
  55. key = _this.key,
  56. level = _this.level,
  57. tableSelector = _this.options.table;
  58. if (_this._req) {
  59. return;
  60. }
  61. _this._req = 1;
  62. LA.loading();
  63. _this.data[_this.options.pageQueryName.replace(':key', key)] = page;
  64. $.ajax({
  65. url: _this.options.url,
  66. type: 'GET',
  67. data: _this.data,
  68. headers: {'X-PJAX': true},
  69. success: function (resp) {
  70. after && after();
  71. LA.loading(false);
  72. _this._req = 0;
  73. // 获取最后一行
  74. var children = getChildren(row.nextAll(), row);
  75. row = children.length ? $(children.pop()) : row;
  76. var _body = $('<div>'+resp+'</div>'),
  77. _tbody = _body.find(tableSelector + ' tbody'),
  78. lastPage = _body.find('last-page').text(),
  79. nextPage = _body.find('next-page').text();
  80. // 标记子节点行
  81. _tbody.find('tr').each(function (_, v) {
  82. $(v).attr('data-level', level + 1)
  83. });
  84. if (
  85. _this.options.showNextPage
  86. && _tbody.find('tr').length == _this.options.perPage
  87. && lastPage >= page
  88. ) {
  89. // 加载更多
  90. var loadMore = $("<tr data-level='" + (level + 1) + "' data-page='" + nextPage
  91. + "'><td colspan='"+(row.find('td').length)
  92. + "' align='center' style='cursor: pointer'> <a>" + _this.options.loadMoreText + "</a> </td></tr>");
  93. row.after(loadMore);
  94. // 加载更多
  95. loadMore.click(function () {
  96. var _t = $(this);
  97. _this._request(_t.data('page'), function () {
  98. _t.remove();
  99. });
  100. });
  101. }
  102. // 附加子节点
  103. row.after(_tbody.html());
  104. // 附加子节点js脚本以及触发子节点js脚本执行
  105. _body.find('script').each(function (_, v) {
  106. row.after(v);
  107. });
  108. $(document).trigger('pjax:script')
  109. },
  110. error:function(a, b, c){
  111. after && after();
  112. LA.loading(false);
  113. _this._req = 0;
  114. if (a.status != 404) {
  115. LA.ajaxError(a, b, c);
  116. }
  117. }
  118. });
  119. }
  120. };
  121. function Orderable(opts) {
  122. this.options = $.extend({
  123. button: null,
  124. url: '',
  125. }, opts);
  126. this.direction = this.key = this.level = this.row = this._req = null;
  127. this._init();
  128. }
  129. Orderable.prototype = {
  130. _init: function () {
  131. this._bindClick()
  132. },
  133. _bindClick: function () {
  134. var _this = this;
  135. $(_this.options.button).off('click').click(function () {
  136. if (_this._req) {
  137. return;
  138. }
  139. _this._req = 1;
  140. LA.loading();
  141. var $this = $(this);
  142. _this.key = $this.data('id');
  143. _this.direction = $this.data('direction');
  144. _this.row = $this.closest('tr');
  145. _this.level = getLevel(_this.row);
  146. _this._request();
  147. })
  148. },
  149. _request: function () {
  150. var _this = this,
  151. key = _this.key,
  152. row = _this.row,
  153. level = _this.level,
  154. direction = _this.direction,
  155. prevAll = row.prevAll(),
  156. nextAll = row.nextAll(),
  157. prev = row.prevAll('tr').first(),
  158. next = row.nextAll('tr').first();
  159. $.ajax({
  160. type: 'POST',
  161. url: _this.options.url.replace(':key', key),
  162. data: {_method:'PUT', _token:LA.token, _orderable:direction},
  163. success: function(data){
  164. LA.loading(false);
  165. _this._req = 0;
  166. if (data.status) {
  167. LA.success(data.message);
  168. if (direction) {
  169. var prevRow = sibling(prevAll, level);
  170. if (swapable(prevRow, level) && prev.length && getLevel(prev) >= level) {
  171. prevRow.before(row);
  172. // 把所有子节点上移
  173. getChildren(nextAll, row).forEach(function (v) {
  174. prevRow.before(v)
  175. });
  176. }
  177. } else {
  178. var nextRow = sibling(nextAll, level),
  179. nextRowChildren = nextRow ? getChildren(nextRow.nextAll(), nextRow) : [];
  180. if (swapable(nextRow, level) && next.length && getLevel(next) >= level) {
  181. nextAll = row.nextAll();
  182. if (nextRowChildren.length) {
  183. nextRow = $(nextRowChildren.pop())
  184. }
  185. // 把所有子节点下移
  186. var all = [];
  187. getChildren(nextAll, row).forEach(function (v) {
  188. all.unshift(v)
  189. });
  190. all.forEach(function(v) {
  191. nextRow.after(v)
  192. });
  193. nextRow.after(row);
  194. }
  195. }
  196. }
  197. },
  198. error: function (a, b, c) {
  199. _this._req = 0;
  200. LA.loading(false);
  201. LA.ajaxError(a, b, c)
  202. }
  203. });
  204. },
  205. };
  206. function isTr(v) {
  207. return $(v).prop('tagName').toLocaleLowerCase() === 'tr'
  208. }
  209. function getLevel(v) {
  210. return parseInt($(v).data('level') || 0);
  211. }
  212. function isChildren(parent, child) {
  213. return getLevel(child) > getLevel(parent);
  214. }
  215. function getChildren(all, parent) {
  216. var arr = [], isBreak = false, firstTr;
  217. all.each(function (_, v) {
  218. // 过滤非tr标签
  219. if (! isTr(v) || isBreak) return;
  220. firstTr || (firstTr = $(v));
  221. // 非连续的子节点
  222. if (firstTr && ! isChildren(parent, firstTr)) {
  223. return;
  224. }
  225. if (isChildren(parent, v)) {
  226. arr.push(v)
  227. } else {
  228. isBreak = true;
  229. }
  230. });
  231. return arr;
  232. }
  233. function swapable(_o, level) {
  234. if (
  235. _o
  236. && _o.length
  237. && level === getLevel(_o)
  238. ) {
  239. return true
  240. }
  241. }
  242. function sibling(all, level) {
  243. var next;
  244. all.each(function (_, v) {
  245. if (getLevel(v) === level && ! next && isTr(v)) {
  246. next = $(v);
  247. }
  248. });
  249. return next;
  250. }
  251. w.LA.grid.tree = function (opts) {
  252. return new Tree(opts);
  253. };
  254. w.LA.grid.orderable = function (opts) {
  255. return new Orderable(opts);
  256. };
  257. })(window, $);