grid-extend.js 10 KB

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