grid-extend.js 10 KB

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