123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- ;
- layui.define("jquery", function(e) {
- "use strict";
- var o = layui.jquery,
- a = layui.hint(),
- r = "layui-tree-enter",
- i = function(e) {
- this.options = e
- },
- t = {
- arrow: ["", ""],
- checkbox: ["", ""],
- radio: ["", ""],
- branch: ["", ""],
- leaf: ""
- },
- index = 1,
- tt = {},
- TreeTable = function() {
- this.mapping = {};
- },
- TreeNode = function(item) {
- this.item = item;
- this.nodes = [];
- };
- i.prototype.expand = function(treeNode, isOpened, e) {
- var o = this;
- var subTreeNodes = treeNode.nodes;
- if (subTreeNodes && subTreeNodes.length > 0) {
- for (var ind = 0; ind < subTreeNodes.length; ind++) {
- var subTreeNode = subTreeNodes[ind];
- var subTrNode = document.getElementById(subTreeNode.id);
- if (subTrNode) {
- !isOpened ? (e.data("spread", null), subTrNode.setAttribute('class', 'layui-hide')) : (e.data("spread", !0), subTrNode.setAttribute('class', ''))
- }
- if (isOpened && !subTreeNode.isOpened) {
- continue;
- }
- o.expand(subTreeNode, isOpened, e);
- }
- }
- },
- i.prototype.traverseModel = function(treeTable, parentNode, item, childrenAttrs) {
- var o = this;
- if (item) {
- var treeNode = new TreeNode(item);
- if (parentNode) {
- treeNode.parentId = parentNode.id;
- treeNode.id = item.id;
- treeNode.level = parentNode.level + 1;
- parentNode.nodes[parentNode.nodes.length] = treeNode;
- treeNode.parent = parentNode;
- } else {
- treeNode.id = item.id;
- treeNode.level = 0;
- }
- treeNode.isOpened = false;
- treeTable.mapping[treeNode.id] = treeNode;
- var children = item[childrenAttrs];
- if (children && children.constructor == Array) {
- for (var i = 0; i < children.length; i++) {
- o.traverseModel(treeTable, treeNode, children[i], childrenAttrs);
- }
- }
- }
- }, i.prototype.init = function(e) {
- var o = this;
- e.addClass("layui-box layui-tree"), o.options.skin && e.addClass("layui-tree-skin-" + o.options.skin), o.tree(e), o.on(e)
- }, i.prototype.initGird = function(e) {
- var o = this;
- var tableHeaderStr = '<thead><tr><th></th>';
- for (var ind = 0; ind < o.options.layout.length; ind++) {
- var headerClass = o.options.layout[ind].headerClass ? ' class="' + o.options.layout[ind].headerClass + '"' : '';
- tableHeaderStr += '<th' + headerClass + '>' + o.options.layout[ind].name + '</th>';
- }
- tableHeaderStr += '</tr></thead>';
- tt = new TreeTable();
- var root = {
- id: 'root',
- children: o.options.nodes
- }
- o.traverseModel(tt, null, root, ['children']);
- e.addClass("layui-tree"),
- o.options.skin && e.addClass("layui-tree-skin-" + o.options.skin),
- o.treeGird(e),
- e.wrapInner('<tbody></tbody'),
- e.prepend(tableHeaderStr),
- e.wrapInner('<table class="layui-table"></table>'),
- e.wrapInner('<div class="layui-form"></div>'),
- o.on(e);
- return e;
- }, i.prototype.tree = function(e, a) {
- var r = this,
- i = r.options,
- n = a || i.nodes;
- layui.each(n, function(a, n) {
- if (n.children) {
- layui.each(n.children, function(index, item) {
- item.pid = n.id;
- });
- }
- var l = n.children && n.children.length > 0,
- c = o('<ul class="' + (n.spread ? "layui-show" : "") + '"></ul>'),
- s = o(["<li " + (n.spread ? 'data-spread="' + n.spread + '"' : "") + ">", function() {
- return l ? '<i class="layui-icon layui-tree-spread">' + (n.spread ? t.arrow[1] : t.arrow[0]) + "</i>" : "";
- }(), function() {
- 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 + '"') : "") + ' />' : "";
- }(), function() {
- 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>")
- }(), "</li>"].join(""));
- 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)
- r.changed(s, n)
- })
- }, i.prototype.treeGird = function(e, a) {
- var r = this,
- i = r.options,
- n = a || i.nodes,
- sxa = i.spreadable == true ? true : false;
- layui.each(n, function(a, n) {
- if (n.children) {
- layui.each(n.children, function(index, item) {
- item.pid = n.id;
- });
- }
- var treeNode = tt.mapping[n.id];
- var indent = "";
- if (treeNode.level > 1) {
- for (var ind = 1; ind < treeNode.level; ind++) {
- indent += '<span style="display: inline-block;width: 20px;"></span>';
- }
- }
- var p;
- if (sxa) {
- n.spread = true, p = false, treeNode.isOpened = true;
- } else {
- p = treeNode.parentId == 'root' ? null : treeNode.parentId;
- }
- var l = n.children && n.children.length > 0,
- str = o(['<tr class="' + (p ? "layui-hide" : "") + '" id="' + n.id + '">', function() {
- return '<td style="width:2%">' + index + '</td>';
- }(), function() {
- var ret = ""
- for (var ind = 0; ind < i.layout.length; ind++) {
- if (i.layout[ind].treeNodes) {
- 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>");
- } else if (i.layout[ind].render) {
- ret += '<td class="' + i.layout[ind].colClass + '" style="' + i.layout[ind].style + '">' + i.layout[ind].render(n) + '</td>'
- } else {
- ret += '<td class="' + i.layout[ind].colClass + '" style="' + i.layout[ind].style + '">' + n[i.layout[ind].field] + '</td>';
- }
- }
- return ret;
- }(), "</tr>"].join(""));
- e.append(str), index++, l && (r.treeGird(e, n.children)), r.spreadGird(str, n), i.drag && r.drag(str, n)
- r.changed(str, n)
- })
- }, i.prototype.changed = function(e, o) {
- var r = this;
- if (o.pid == undefined || o.pid == null) {
- e.children("input").on("change", function() {
- var childUl = e.children("ul"),
- checked = this.checked;
- childUl.find("input").prop("checked", checked);
- })
- } else {
- e.children("input").on("change", function() {
- var that = this;
- if (!this.checked) {
- if (o.children && o.children.length > 0) {
- var childUl = e.children("ul"),
- checked = this.checked;
- childUl.find("input").prop("checked", checked);
- }
- r.cancelParentsCheckboxCheck(that);
- } else {
- r.parentsChecked(this, this.checked);
- if (o.children && o.children.length > 0) {
- var childUl = e.children("ul"),
- checked = this.checked;
- childUl.find("input").prop("checked", checked);
- }
- }
- });
- }
- }, i.prototype.cancelParentsCheckboxCheck = function(ele) {
- if (!ele) {
- return;
- }
- var r = this,
- siblingInputs = r.siblingInputs(ele),
- parentId = ele.getAttribute("data-parent-id"),
- parentInput = null,
- bool = true,
- childrendInputs = null,
- hasOneChildrenInputCheck = false;
- if (parentId != 'undefined') {
- parentInput = document.getElementById(parentId);
- childrendInputs = r.currentChildrenInputs(parentInput);
- }
- for (var i = 0, len = siblingInputs.length; i < len; i++) {
- if (siblingInputs[i].checked) {
- bool = false;
- break;
- }
- }
- if (!childrendInputs || childrendInputs.length == 0) {
- hasOneChildrenInputCheck = false;
- } else {
- for (var j = 0, len2 = childrendInputs.length; j < len2; j++) {
- if (childrendInputs[j].getAttribute("data-parent-id") != "undefined") {
- if (childrendInputs[j].checked) {
- console.log(1158)
- hasOneChildrenInputCheck = true;
- break;
- }
- }
- }
- }
- if (bool && !hasOneChildrenInputCheck) {
- r.inputChecked(parentInput, false);
- }
- this.cancelParentsCheckboxCheck(parentInput);
- }, i.prototype.siblingInputs = function(ele) {
- var that = this;
- if (ele) {
- var parent = ele.parentElement,
- parents = parent.parentElement,
- childrens = parents.children,
- siblingInputs = [];
- } else {
- return null;
- }
- for (var i = 0, len = childrens.length; i < len; i++) {
- if (childrens[i] != parent) {
- if (childrens[i].children[0].nodeName == "INPUT") {
- siblingInputs.push(childrens[i].children[0]);
- }
- if (childrens[i].children[1].nodeName == "INPUT") {
- siblingInputs.push(childrens[i].children[1]);
- }
- }
- }
- parent = null;
- parents = null;
- childrens = null;
- return siblingInputs;
- }, i.prototype.currentChildrenInputs = function(ele) {
- var parent = ele.parentElement,
- childrenInputs = [];
- if (parent.getElementsByTagName("ul").length > 0) {
- var uls = parent.getElementsByTagName("ul");
- for (var i = 0, len = uls.length; i < len; i++) {
- var inputs = uls[i].getElementsByTagName("input");
- for (var j = 0, len2 = inputs.length; j < len2; j++) {
- childrenInputs.push(inputs[j]);
- }
- }
- }
- return childrenInputs;
- }, i.prototype.inputChecked = function(ele, checked) {
- ele.checked = checked;
- }, i.prototype.parentsChecked = function(e, checked) {
- var r = this,
- i = r.options,
- selector = i.elem,
- currentInput = e;
- if (currentInput && (currentInput.nodeName == "INPUT")) {
- var parentId = currentInput.getAttribute("data-parent-id"),
- parentInput = null;
- setTimeout(function() {
- r.check(currentInput, checked);
- if (parentId) {
- r.parentsChecked(document.getElementById(parentId), checked);
- }
- }, 50);
- }
- }, i.prototype.findParents = function(ele, selector) {
- var parent = ele.parentElement,
- that = this;
- if (selector.substr(0, 1) == "#") {
- if (parent) {
- if (parent.id != selector.substr(1)) {
- that.findParents(parent, selector);
- } else {
- return parent;
- }
- }
- } else if (selector.substr(0, 1) == ".") {
- if (parent) {
- var classnameArr = parent.className.split(" "),
- len = classnameArr.length,
- selectt = selector.substr(1),
- hasSelector = false;
- if (len > 0) {
- for (var i = 0; i < len; i++) {
- if (classnameArr[i] == selectt) {
- hasSelector = true;
- break;
- }
- }
- }
- if (!hasSelector) {
- that.findParents(parent, selector);
- } else if (hasSelector) {
- return parent;
- }
- }
- }
- }, i.prototype.num = 1, i.prototype.uuid = function() {
- var that = this,
- randomStr = ['l', 'a', 'y', 'e', 'r', 'n', 'i'],
- randomNum = Math.floor(Math.random() * 6);
- return function() {
- var str = "";
- for (var i = 0; i <= randomNum; i++) {
- str += randomStr[Math.floor(Math.random() * 6)];
- }
- return "layer_" + new Date().getTime() + "_" + (that.num++) + "_" + (++that.num) + "_" + str;
- }();
- }, i.prototype.check = function(input, bool) {
- if (bool) {
- input.checked = true;
- } else {
- input.checked = false;
- }
- }, i.prototype.click = function(e, o) {
- var a = this,
- r = a.options;
- e.children("a").on("click", function(e) {
- layui.stope(e), r.click(o)
- })
- }, i.prototype.spread = function(e, o) {
- var a = this,
- r = (a.options, e.children(".layui-tree-spread")),
- i = e.children("ul"),
- n = e.children("a"),
- l = function() {
- 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]))
- };
- i[0] && (r.on("click", l), n.on("dblclick", l))
- }, i.prototype.spreadGird = function(e, o) {
- var a = this,
- r = (a.options, e.find(".layui-tree-spread")),
- nodeId = e[0].id,
- ri = e.find(".layui-tree-branch"),
- l = function() {
- var treeNode = tt.mapping[nodeId];
- var isOpened = treeNode.isOpened;
- a.expand(treeNode, !isOpened, e);
- 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]))
- treeNode.isOpened = !isOpened;
- };
- (r.on("click", l), r.on("dblclick", l))
- }, i.prototype.on = function(e) {
- var a = this,
- i = a.options,
- t = "layui-tree-drag";
- e.find("i").on("selectstart", function(e) {
- return !1
- }), i.drag && o(document).on("mousemove", function(e) {
- var r = a.move;
- if (r.from) {
- var i = (r.to, o('<div class="layui-box ' + t + '"></div>'));
- e.preventDefault(), o("." + t)[0] || o("body").append(i);
- var n = o("." + t)[0] ? o("." + t) : i;
- n.addClass("layui-show").html(r.from.elem.children("a").html()), n.css({
- left: e.pageX + 10,
- top: e.pageY + 10
- })
- }
- }).on("mouseup", function() {
- var e = a.move;
- e.from && (e.from.elem.children("a").removeClass(r), e.to && e.to.elem.children("a").removeClass(r), a.move = {}, o("." + t).remove())
- })
- }, i.prototype.move = {}, i.prototype.drag = function(e, a) {
- var i = this,
- t = (i.options, e.children("a")),
- n = function() {
- var t = o(this),
- n = i.move;
- n.from && (n.to = {
- item: a,
- elem: e
- }, t.addClass(r))
- };
- t.on("mousedown", function() {
- var o = i.move;
- o.from = {
- item: a,
- elem: e
- }
- }), t.on("mouseenter", n).on("mousemove", n).on("mouseleave", function() {
- var e = o(this),
- a = i.move;
- a.from && (delete a.to, e.removeClass(r))
- })
- }, e("tree", function(e) {
- var r = new i(e = e || {}),
- t = o(e.elem);
- return t[0] ? void r.init(t) : a.error("layui.tree 没有找到" + e.elem + "元素");
- }), e("treeGird", function(e) {
- var r = new i(e = e || {}),
- t = o(e.elem);
- var v = r.initGird(t);
- return t[0] ? v : a.error("layui.tree 没有找到" + e.elem + "元素");
- }), e("expand", function(el) {
- var a = this,
- oi = new i(el = el || {});
- for (var key in tt.mapping) {
- var treeNode = tt.mapping[key];
- if (treeNode.id == 'root') {
- return;
- }
- var isOpened = treeNode.isOpened;
- if (isOpened) {
- return;
- }
- var e = o('#' + treeNode.id),
- r = (a.options, e.find(".layui-tree-spread")),
- ri = e.find(".layui-tree-branch");
- oi.expand(treeNode, !isOpened, e);
- 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]))
- treeNode.isOpened = !isOpened;
- }
- }),
- e("collapse", function(el) {
- var a = this,
- oi = new i(el = el || {});
- for (var key in tt.mapping) {
- var treeNode = tt.mapping[key];
- if (treeNode.id == 'root') {
- return;
- }
- var isOpened = treeNode.isOpened;
- if (!isOpened) {
- return;
- }
- var e = o('#' + treeNode.id),
- r = (a.options, e.find(".layui-tree-spread")),
- ri = e.find(".layui-tree-branch");
- oi.expand(treeNode, !isOpened, e);
- 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]))
- treeNode.isOpened = !isOpened;
- }
- })
- });
|