From 286985d2f156d4d4f7f3df187d900195fb41ff32 Mon Sep 17 00:00:00 2001 From: windy <1374721899@qq.com> Date: Mon, 19 Aug 2019 15:11:25 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-20730=20feat:=20=E9=83=A8=E4=BB=B6?= =?UTF-8?q?=E6=A0=91=E6=96=B0=E5=A2=9Eopen=E5=B1=9E=E6=80=A7=E5=85=A8?= =?UTF-8?q?=E5=B1=95=E5=BC=80=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../abstract.treevaluechooser.js | 64 +++++++++++++++++-- .../abstract.treevaluechooser.list.js | 31 +++++++-- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/src/component/treevaluechooser/abstract.treevaluechooser.js b/src/component/treevaluechooser/abstract.treevaluechooser.js index 5bd5b7875..98081dbef 100644 --- a/src/component/treevaluechooser/abstract.treevaluechooser.js +++ b/src/component/treevaluechooser/abstract.treevaluechooser.js @@ -7,7 +7,8 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { _defaultConfig: function () { return BI.extend(BI.AbstractTreeValueChooser.superclass._defaultConfig.apply(this, arguments), { items: null, - itemsCreator: BI.emptyFn + itemsCreator: BI.emptyFn, + open: false }); }, @@ -515,7 +516,7 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { }, _reqTreeNode: function (op, callback) { - var self = this; + var self = this, o = this.options; var result = []; var times = op.times; var checkState = op.checkState || {}; @@ -523,7 +524,7 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { var selectedValues = op.selectedValues || {}; var valueMap = {}; // if (judgeState(parentValues, selectedValues, checkState)) { - valueMap = dealWidthSelectedValue(parentValues, selectedValues); + valueMap = dealWithSelectedValue(parentValues, selectedValues); // } var nodes = this._getChildren(parentValues); for (var i = (times - 1) * this._const.perPage; nodes[i] && i < times * this._const.perPage; i++) { @@ -536,7 +537,31 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { times: 1, isParent: nodes[i].getChildrenLength() > 0, checked: state[0], - halfCheck: state[1] + halfCheck: state[1], + open: o.open + }); + } + // 如果指定节点全部打开 + if (o.open) { + var allNodes = []; + // 获取所有节点 + BI.each(nodes, function (idx, node) { + allNodes = BI.concat(allNodes, self._getAllChildren(parentValues.concat([node.value]))); + }); + BI.each(allNodes, function (idx, node) { + var valueMap = dealWithSelectedValue(node.parentValues, selectedValues); + var state = getCheckState(node.value, node.parentValues, valueMap, checkState); + result.push({ + id: node.id, + pId: node.pId, + value: node.value, + text: node.text, + times: 1, + isParent: node.getChildrenLength() > 0, + checked: state[0], + halfCheck: state[1], + open: self.options.open + }); }); } // 深层嵌套的比较麻烦,这边先实现的是在根节点添加 @@ -558,7 +583,7 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { return (parentValues.length === 0 || (checked && half) && !BI.isEmpty(selected_value)); } - function dealWidthSelectedValue(parentValues, selectedValues) { + function dealWithSelectedValue(parentValues, selectedValues) { var valueMap = {}; BI.each(parentValues, function (i, v) { selectedValues = selectedValues[v] || {}; @@ -578,12 +603,14 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { nextNames[t] = true; } }); + // valueMap的数组第一个参数为不选: 0, 半选: 1, 全选:2, 第二个参数为改节点下选中的子节点个数(子节点全选或者不存在) valueMap[value] = [1, BI.size(nextNames)]; }); return valueMap; } function getCheckState(current, parentValues, valueMap, checkState) { + // 节点本身的checked和half优先级最高 var checked = checkState.checked, half = checkState.half; var tempCheck = false, halfCheck = false; if (BI.has(valueMap, current)) { @@ -601,8 +628,10 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { } var check; if (!checked && !halfCheck && !tempCheck) { + // 当节点自身是不选的,且通过selectedValues没有得到全选, 则check状态取决于valueMap check = BI.has(valueMap, current); } else { + // 不是上面那种情况就先看在节点没有带有明确半选的时候,通过节点自身的checked和valueMap的状态能都得到选中信息 check = ((tempCheck || checked) && !half) || BI.has(valueMap, current); } return [check, halfCheck]; @@ -707,6 +736,31 @@ BI.AbstractTreeValueChooser = BI.inherit(BI.Widget, { return parent.getChildren(); }, + _getAllChildren: function(parentValues) { + var children = this._getChildren(parentValues); + var nodes = [].concat(children); + BI.each(nodes, function (idx, node) { + node.parentValues = parentValues; + }); + var queue = BI.map(children, function (idx, node) { + return { + parentValues: parentValues, + value: node.value + }; + }); + while (BI.isNotEmptyArray(queue)) { + var node = queue.shift(); + var pValues = (node.parentValues).concat(node.value); + var childNodes = this._getChildren(pValues); + BI.each(childNodes, function (idx, node) { + node.parentValues = pValues; + }); + queue = queue.concat(childNodes); + nodes = nodes.concat(childNodes); + } + return nodes; + }, + _getChildCount: function (parentValues) { return this._getChildren(parentValues).length; } diff --git a/src/component/treevaluechooser/abstract.treevaluechooser.list.js b/src/component/treevaluechooser/abstract.treevaluechooser.list.js index da4d21b74..182b47af2 100644 --- a/src/component/treevaluechooser/abstract.treevaluechooser.list.js +++ b/src/component/treevaluechooser/abstract.treevaluechooser.list.js @@ -200,12 +200,12 @@ BI.AbstractListTreeValueChooser = BI.inherit(BI.AbstractTreeValueChooser, { }, _reqTreeNode: function (op, callback) { - var self = this; + var self = this, o = this.options; var result = []; var times = op.times; var parentValues = op.parentValues || []; var selectedValues = op.selectedValues || []; - var valueMap = dealWidthSelectedValue(selectedValues); + var valueMap = dealWithSelectedValue(selectedValues); var nodes = this._getChildren(parentValues); for (var i = (times - 1) * this._const.perPage; nodes[i] && i < times * this._const.perPage; i++) { var checked = BI.has(valueMap, nodes[i].value); @@ -217,7 +217,30 @@ BI.AbstractListTreeValueChooser = BI.inherit(BI.AbstractTreeValueChooser, { times: 1, isParent: nodes[i].getChildrenLength() > 0, checked: checked, - halfCheck: false + halfCheck: false, + open: o.open + }); + } + // 如果指定节点全部打开 + if (o.open) { + var allNodes = []; + // 获取所有节点 + BI.each(nodes, function (idx, node) { + allNodes = BI.concat(allNodes, self._getAllChildren(parentValues.concat([node.value]))); + }); + BI.each(allNodes, function (idx, node) { + var checked = BI.has(valueMap, node.value); + result.push({ + id: node.id, + pId: node.pId, + value: node.value, + text: node.text, + times: 1, + isParent: node.getChildrenLength() > 0, + checked: checked, + halfCheck: false, + open: o.open + }); }); } // 深层嵌套的比较麻烦,这边先实现的是在根节点添加 @@ -231,7 +254,7 @@ BI.AbstractListTreeValueChooser = BI.inherit(BI.AbstractTreeValueChooser, { }); }); - function dealWidthSelectedValue(selectedValues) { + function dealWithSelectedValue(selectedValues) { var valueMap = {}; BI.each(selectedValues, function (idx, v) { valueMap[BI.last(v)] = [2, 0];