treeGird.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. ;
  2. layui.define("jquery", function(e) {
  3. "use strict";
  4. var o = layui.jquery,
  5. a = layui.hint(),
  6. r = "layui-tree-enter",
  7. i = function(e) {
  8. this.options = e
  9. },
  10. t = {
  11. arrow: ["", ""],
  12. checkbox: ["", ""],
  13. radio: ["", ""],
  14. branch: ["", ""],
  15. leaf: ""
  16. },
  17. index = 1,
  18. tt = {},
  19. TreeTable = function() {
  20. this.mapping = {};
  21. },
  22. TreeNode = function(item) {
  23. this.item = item;
  24. this.nodes = [];
  25. };
  26. i.prototype.expand = function(treeNode, isOpened, e) {
  27. var o = this;
  28. var subTreeNodes = treeNode.nodes;
  29. if (subTreeNodes && subTreeNodes.length > 0) {
  30. for (var ind = 0; ind < subTreeNodes.length; ind++) {
  31. var subTreeNode = subTreeNodes[ind];
  32. var subTrNode = document.getElementById(subTreeNode.id);
  33. if (subTrNode) {
  34. !isOpened ? (e.data("spread", null), subTrNode.setAttribute('class', 'layui-hide')) : (e.data("spread", !0), subTrNode.setAttribute('class', ''))
  35. }
  36. if (isOpened && !subTreeNode.isOpened) {
  37. continue;
  38. }
  39. o.expand(subTreeNode, isOpened, e);
  40. }
  41. }
  42. },
  43. i.prototype.traverseModel = function(treeTable, parentNode, item, childrenAttrs) {
  44. var o = this;
  45. if (item) {
  46. var treeNode = new TreeNode(item);
  47. if (parentNode) {
  48. treeNode.parentId = parentNode.id;
  49. treeNode.id = item.id;
  50. treeNode.level = parentNode.level + 1;
  51. parentNode.nodes[parentNode.nodes.length] = treeNode;
  52. treeNode.parent = parentNode;
  53. } else {
  54. treeNode.id = item.id;
  55. treeNode.level = 0;
  56. }
  57. treeNode.isOpened = false;
  58. treeTable.mapping[treeNode.id] = treeNode;
  59. var children = item[childrenAttrs];
  60. if (children && children.constructor == Array) {
  61. for (var i = 0; i < children.length; i++) {
  62. o.traverseModel(treeTable, treeNode, children[i], childrenAttrs);
  63. }
  64. }
  65. }
  66. }, i.prototype.init = function(e) {
  67. var o = this;
  68. e.addClass("layui-box layui-tree"), o.options.skin && e.addClass("layui-tree-skin-" + o.options.skin), o.tree(e), o.on(e)
  69. }, i.prototype.initGird = function(e) {
  70. var o = this;
  71. var tableHeaderStr = '<thead><tr><th></th>';
  72. for (var ind = 0; ind < o.options.layout.length; ind++) {
  73. var headerClass = o.options.layout[ind].headerClass ? ' class="' + o.options.layout[ind].headerClass + '"' : '';
  74. tableHeaderStr += '<th' + headerClass + '>' + o.options.layout[ind].name + '</th>';
  75. }
  76. tableHeaderStr += '</tr></thead>';
  77. tt = new TreeTable();
  78. var root = {
  79. id: 'root',
  80. children: o.options.nodes
  81. }
  82. o.traverseModel(tt, null, root, ['children']);
  83. e.addClass("layui-tree"),
  84. o.options.skin && e.addClass("layui-tree-skin-" + o.options.skin),
  85. o.treeGird(e),
  86. e.wrapInner('<tbody></tbody'),
  87. e.prepend(tableHeaderStr),
  88. e.wrapInner('<table class="layui-table"></table>'),
  89. e.wrapInner('<div class="layui-form"></div>'),
  90. o.on(e);
  91. return e;
  92. }, i.prototype.tree = function(e, a) {
  93. var r = this,
  94. i = r.options,
  95. n = a || i.nodes;
  96. layui.each(n, function(a, n) {
  97. if (n.children) {
  98. layui.each(n.children, function(index, item) {
  99. item.pid = n.id;
  100. });
  101. }
  102. var l = n.children && n.children.length > 0,
  103. c = o('<ul class="' + (n.spread ? "layui-show" : "") + '"></ul>'),
  104. s = o(["<li " + (n.spread ? 'data-spread="' + n.spread + '"' : "") + ">", function() {
  105. return l ? '<i class="layui-icon layui-tree-spread">' + (n.spread ? t.arrow[1] : t.arrow[0]) + "</i>" : "";
  106. }(), function() {
  107. return i.check && i.check == "checkbox" ? '<input type="checkbox" name="' + i.checkboxName + '" ' + ((n.checked && n.checked == true) ? 'checked="checked"' : "") + (n.checkboxValue ? ('value="' + n.checkboxValue + '"') : "") + 'data-parent-id="' + n.pid + '"' + 'id="' + n.id + '"' + (i.checkboxStyle ? ('style="' + i.checkboxStyle + '"') : "") + ' />' : "";
  108. }(), function() {
  109. return '<a href="' + (n.href || "javascript:;") + '" ' + (i.target && n.href ? 'target="' + i.target + '"' : "") + ">" + ('<i class="layui-icon layui-tree-' + (l ? "branch" : "leaf") + '">' + (l ? n.spread ? t.branch[1] : t.branch[0] : t.leaf) + "</i>") + ("<cite>" + (n.name || "未命名") + "</cite></a>")
  110. }(), "</li>"].join(""));
  111. l && (s.append(c), r.tree(c, n.children)), e.append(s), "function" == typeof i.click && r.click(s, n), r.spread(s, n), i.drag && r.drag(s, n)
  112. r.changed(s, n)
  113. })
  114. }, i.prototype.treeGird = function(e, a) {
  115. var r = this,
  116. i = r.options,
  117. n = a || i.nodes,
  118. sxa = i.spreadable == true ? true : false;
  119. layui.each(n, function(a, n) {
  120. if (n.children) {
  121. layui.each(n.children, function(index, item) {
  122. item.pid = n.id;
  123. });
  124. }
  125. var treeNode = tt.mapping[n.id];
  126. var indent = "";
  127. if (treeNode.level > 1) {
  128. for (var ind = 1; ind < treeNode.level; ind++) {
  129. indent += '<span style="display: inline-block;width: 20px;"></span>';
  130. }
  131. }
  132. var p;
  133. if (sxa) {
  134. n.spread = true, p = false, treeNode.isOpened = true;
  135. } else {
  136. p = treeNode.parentId == 'root' ? null : treeNode.parentId;
  137. }
  138. var l = n.children && n.children.length > 0,
  139. str = o(['<tr class="' + (p ? "layui-hide" : "") + '" id="' + n.id + '">', function() {
  140. return '<td style="width:2%">' + index + '</td>';
  141. }(), function() {
  142. var ret = ""
  143. for (var ind = 0; ind < i.layout.length; ind++) {
  144. if (i.layout[ind].treeNodes) {
  145. ret += '<td class="' + i.layout[ind].colClass + '" style="' + i.layout[ind].style + '"><li ' + (n.spread ? 'data-spread="' + n.spread + '"' : "") + '>' + (indent + (l ? '<i class="layui-icon layui-tree-spread">' + (n.spread ? t.arrow[1] : t.arrow[0]) + "</i>" : "")) + '<a href="' + (n.href || "javascript:;") + '" ' + (i.target && n.href ? 'target="' + i.target + '"' : "") + ">" + ('<i class="layui-icon layui-tree-' + (l ? "branch" : "leaf") + '">' + (l ? n.spread ? t.branch[1] : t.branch[0] : t.leaf) + "</i>") + ("<cite>" + (n.name || "未命名") + "</cite></a></li></td>");
  146. } else if (i.layout[ind].render) {
  147. ret += '<td class="' + i.layout[ind].colClass + '" style="' + i.layout[ind].style + '">' + i.layout[ind].render(n) + '</td>'
  148. } else {
  149. ret += '<td class="' + i.layout[ind].colClass + '" style="' + i.layout[ind].style + '">' + n[i.layout[ind].field] + '</td>';
  150. }
  151. }
  152. return ret;
  153. }(), "</tr>"].join(""));
  154. e.append(str), index++, l && (r.treeGird(e, n.children)), r.spreadGird(str, n), i.drag && r.drag(str, n)
  155. r.changed(str, n)
  156. })
  157. }, i.prototype.changed = function(e, o) {
  158. var r = this;
  159. if (o.pid == undefined || o.pid == null) {
  160. e.children("input").on("change", function() {
  161. var childUl = e.children("ul"),
  162. checked = this.checked;
  163. childUl.find("input").prop("checked", checked);
  164. })
  165. } else {
  166. e.children("input").on("change", function() {
  167. var that = this;
  168. if (!this.checked) {
  169. if (o.children && o.children.length > 0) {
  170. var childUl = e.children("ul"),
  171. checked = this.checked;
  172. childUl.find("input").prop("checked", checked);
  173. }
  174. r.cancelParentsCheckboxCheck(that);
  175. } else {
  176. r.parentsChecked(this, this.checked);
  177. if (o.children && o.children.length > 0) {
  178. var childUl = e.children("ul"),
  179. checked = this.checked;
  180. childUl.find("input").prop("checked", checked);
  181. }
  182. }
  183. });
  184. }
  185. }, i.prototype.cancelParentsCheckboxCheck = function(ele) {
  186. if (!ele) {
  187. return;
  188. }
  189. var r = this,
  190. siblingInputs = r.siblingInputs(ele),
  191. parentId = ele.getAttribute("data-parent-id"),
  192. parentInput = null,
  193. bool = true,
  194. childrendInputs = null,
  195. hasOneChildrenInputCheck = false;
  196. if (parentId != 'undefined') {
  197. parentInput = document.getElementById(parentId);
  198. childrendInputs = r.currentChildrenInputs(parentInput);
  199. }
  200. for (var i = 0, len = siblingInputs.length; i < len; i++) {
  201. if (siblingInputs[i].checked) {
  202. bool = false;
  203. break;
  204. }
  205. }
  206. if (!childrendInputs || childrendInputs.length == 0) {
  207. hasOneChildrenInputCheck = false;
  208. } else {
  209. for (var j = 0, len2 = childrendInputs.length; j < len2; j++) {
  210. if (childrendInputs[j].getAttribute("data-parent-id") != "undefined") {
  211. if (childrendInputs[j].checked) {
  212. console.log(1158)
  213. hasOneChildrenInputCheck = true;
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. if (bool && !hasOneChildrenInputCheck) {
  220. r.inputChecked(parentInput, false);
  221. }
  222. this.cancelParentsCheckboxCheck(parentInput);
  223. }, i.prototype.siblingInputs = function(ele) {
  224. var that = this;
  225. if (ele) {
  226. var parent = ele.parentElement,
  227. parents = parent.parentElement,
  228. childrens = parents.children,
  229. siblingInputs = [];
  230. } else {
  231. return null;
  232. }
  233. for (var i = 0, len = childrens.length; i < len; i++) {
  234. if (childrens[i] != parent) {
  235. if (childrens[i].children[0].nodeName == "INPUT") {
  236. siblingInputs.push(childrens[i].children[0]);
  237. }
  238. if (childrens[i].children[1].nodeName == "INPUT") {
  239. siblingInputs.push(childrens[i].children[1]);
  240. }
  241. }
  242. }
  243. parent = null;
  244. parents = null;
  245. childrens = null;
  246. return siblingInputs;
  247. }, i.prototype.currentChildrenInputs = function(ele) {
  248. var parent = ele.parentElement,
  249. childrenInputs = [];
  250. if (parent.getElementsByTagName("ul").length > 0) {
  251. var uls = parent.getElementsByTagName("ul");
  252. for (var i = 0, len = uls.length; i < len; i++) {
  253. var inputs = uls[i].getElementsByTagName("input");
  254. for (var j = 0, len2 = inputs.length; j < len2; j++) {
  255. childrenInputs.push(inputs[j]);
  256. }
  257. }
  258. }
  259. return childrenInputs;
  260. }, i.prototype.inputChecked = function(ele, checked) {
  261. ele.checked = checked;
  262. }, i.prototype.parentsChecked = function(e, checked) {
  263. var r = this,
  264. i = r.options,
  265. selector = i.elem,
  266. currentInput = e;
  267. if (currentInput && (currentInput.nodeName == "INPUT")) {
  268. var parentId = currentInput.getAttribute("data-parent-id"),
  269. parentInput = null;
  270. setTimeout(function() {
  271. r.check(currentInput, checked);
  272. if (parentId) {
  273. r.parentsChecked(document.getElementById(parentId), checked);
  274. }
  275. }, 50);
  276. }
  277. }, i.prototype.findParents = function(ele, selector) {
  278. var parent = ele.parentElement,
  279. that = this;
  280. if (selector.substr(0, 1) == "#") {
  281. if (parent) {
  282. if (parent.id != selector.substr(1)) {
  283. that.findParents(parent, selector);
  284. } else {
  285. return parent;
  286. }
  287. }
  288. } else if (selector.substr(0, 1) == ".") {
  289. if (parent) {
  290. var classnameArr = parent.className.split(" "),
  291. len = classnameArr.length,
  292. selectt = selector.substr(1),
  293. hasSelector = false;
  294. if (len > 0) {
  295. for (var i = 0; i < len; i++) {
  296. if (classnameArr[i] == selectt) {
  297. hasSelector = true;
  298. break;
  299. }
  300. }
  301. }
  302. if (!hasSelector) {
  303. that.findParents(parent, selector);
  304. } else if (hasSelector) {
  305. return parent;
  306. }
  307. }
  308. }
  309. }, i.prototype.num = 1, i.prototype.uuid = function() {
  310. var that = this,
  311. randomStr = ['l', 'a', 'y', 'e', 'r', 'n', 'i'],
  312. randomNum = Math.floor(Math.random() * 6);
  313. return function() {
  314. var str = "";
  315. for (var i = 0; i <= randomNum; i++) {
  316. str += randomStr[Math.floor(Math.random() * 6)];
  317. }
  318. return "layer_" + new Date().getTime() + "_" + (that.num++) + "_" + (++that.num) + "_" + str;
  319. }();
  320. }, i.prototype.check = function(input, bool) {
  321. if (bool) {
  322. input.checked = true;
  323. } else {
  324. input.checked = false;
  325. }
  326. }, i.prototype.click = function(e, o) {
  327. var a = this,
  328. r = a.options;
  329. e.children("a").on("click", function(e) {
  330. layui.stope(e), r.click(o)
  331. })
  332. }, i.prototype.spread = function(e, o) {
  333. var a = this,
  334. r = (a.options, e.children(".layui-tree-spread")),
  335. i = e.children("ul"),
  336. n = e.children("a"),
  337. l = function() {
  338. e.data("spread") ? (e.data("spread", null), i.removeClass("layui-show"), r.html(t.arrow[0]), n.find(".layui-icon").html(t.branch[0])) : (e.data("spread", !0), i.addClass("layui-show"), r.html(t.arrow[1]), n.find(".layui-icon").html(t.branch[1]))
  339. };
  340. i[0] && (r.on("click", l), n.on("dblclick", l))
  341. }, i.prototype.spreadGird = function(e, o) {
  342. var a = this,
  343. r = (a.options, e.find(".layui-tree-spread")),
  344. nodeId = e[0].id,
  345. ri = e.find(".layui-tree-branch"),
  346. l = function() {
  347. var treeNode = tt.mapping[nodeId];
  348. var isOpened = treeNode.isOpened;
  349. a.expand(treeNode, !isOpened, e);
  350. isOpened ? (e.data("spread", null), r.html(t.arrow[0]), ri.html(t.branch[0])) : (e.data("spread", !0), r.html(t.arrow[1]), ri.html(t.branch[1]))
  351. treeNode.isOpened = !isOpened;
  352. };
  353. (r.on("click", l), r.on("dblclick", l))
  354. }, i.prototype.on = function(e) {
  355. var a = this,
  356. i = a.options,
  357. t = "layui-tree-drag";
  358. e.find("i").on("selectstart", function(e) {
  359. return !1
  360. }), i.drag && o(document).on("mousemove", function(e) {
  361. var r = a.move;
  362. if (r.from) {
  363. var i = (r.to, o('<div class="layui-box ' + t + '"></div>'));
  364. e.preventDefault(), o("." + t)[0] || o("body").append(i);
  365. var n = o("." + t)[0] ? o("." + t) : i;
  366. n.addClass("layui-show").html(r.from.elem.children("a").html()), n.css({
  367. left: e.pageX + 10,
  368. top: e.pageY + 10
  369. })
  370. }
  371. }).on("mouseup", function() {
  372. var e = a.move;
  373. e.from && (e.from.elem.children("a").removeClass(r), e.to && e.to.elem.children("a").removeClass(r), a.move = {}, o("." + t).remove())
  374. })
  375. }, i.prototype.move = {}, i.prototype.drag = function(e, a) {
  376. var i = this,
  377. t = (i.options, e.children("a")),
  378. n = function() {
  379. var t = o(this),
  380. n = i.move;
  381. n.from && (n.to = {
  382. item: a,
  383. elem: e
  384. }, t.addClass(r))
  385. };
  386. t.on("mousedown", function() {
  387. var o = i.move;
  388. o.from = {
  389. item: a,
  390. elem: e
  391. }
  392. }), t.on("mouseenter", n).on("mousemove", n).on("mouseleave", function() {
  393. var e = o(this),
  394. a = i.move;
  395. a.from && (delete a.to, e.removeClass(r))
  396. })
  397. }, e("tree", function(e) {
  398. var r = new i(e = e || {}),
  399. t = o(e.elem);
  400. return t[0] ? void r.init(t) : a.error("layui.tree 没有找到" + e.elem + "元素");
  401. }), e("treeGird", function(e) {
  402. var r = new i(e = e || {}),
  403. t = o(e.elem);
  404. var v = r.initGird(t);
  405. return t[0] ? v : a.error("layui.tree 没有找到" + e.elem + "元素");
  406. }), e("expand", function(el) {
  407. var a = this,
  408. oi = new i(el = el || {});
  409. for (var key in tt.mapping) {
  410. var treeNode = tt.mapping[key];
  411. if (treeNode.id == 'root') {
  412. return;
  413. }
  414. var isOpened = treeNode.isOpened;
  415. if (isOpened) {
  416. return;
  417. }
  418. var e = o('#' + treeNode.id),
  419. r = (a.options, e.find(".layui-tree-spread")),
  420. ri = e.find(".layui-tree-branch");
  421. oi.expand(treeNode, !isOpened, e);
  422. isOpened ? (e.data("spread", null), r.html(t.arrow[0]), ri.html(t.branch[0])) : (e.data("spread", !0), r.html(t.arrow[1]), ri.html(t.branch[1]))
  423. treeNode.isOpened = !isOpened;
  424. }
  425. }),
  426. e("collapse", function(el) {
  427. var a = this,
  428. oi = new i(el = el || {});
  429. for (var key in tt.mapping) {
  430. var treeNode = tt.mapping[key];
  431. if (treeNode.id == 'root') {
  432. return;
  433. }
  434. var isOpened = treeNode.isOpened;
  435. if (!isOpened) {
  436. return;
  437. }
  438. var e = o('#' + treeNode.id),
  439. r = (a.options, e.find(".layui-tree-spread")),
  440. ri = e.find(".layui-tree-branch");
  441. oi.expand(treeNode, !isOpened, e);
  442. isOpened ? (e.data("spread", null), r.html(t.arrow[0]), ri.html(t.branch[0])) : (e.data("spread", !0), r.html(t.arrow[1]), ri.html(t.branch[1]))
  443. treeNode.isOpened = !isOpened;
  444. }
  445. })
  446. });