@bic-fe/mds-ui
Version:
A set of enterprise-class Vue UI components.
414 lines (378 loc) • 10.9 kB
JavaScript
'use strict';
exports.__esModule = true;
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var defaultId = 0;
var Node = function () {
function Node(options) {
(0, _classCallCheck3.default)(this, Node);
this.id = defaultId++;
this.checked = false;
this.indeterminate = false;
this.data = null;
this.expanded = false;
this.parent = null;
this.isLeaf = false;
// 是否显示,用于搜索
this.visible = true;
// 搜索关键字,用于标记高亮
this.searchWords = null;
for (var name in options) {
if (options.hasOwnProperty(name)) {
this[name] = options[name];
}
}
this.level = 0;
// 是否被选中
this.selected = false;
// 是否已经加载完毕,用于懒加载
this.loaded = false;
// 是否是加载中状态,用于懒加载
this.loading = false;
this.childNodes = [];
this.parent ? this.level = this.parent.level + 1 : this.level = 0;
this.update();
}
Node.prototype.update = function update() {
var _this = this;
if (!this.data) {
this.handleIsLeaf();
return;
}
var data = this.data;
var prop = this.tree.prop;
// 生成_nodeId
// this.markData(data)
// 非根节点添加到nodesMap中
this.fillNodesMap();
// 处理defaultExpandAll
if (!this.load && this.tree.defaultExpandAll) {
this.expanded = true;
}
// 处理defaultExpandedKeys
if (!this.load && this.tree.defaultExpandedKeys) {
this.tree.defaultExpandedKeys.forEach(function (item) {
if (_this.key === item) {
_this.expanded = true;
if (_this.tree.autoExpandParent) {
_this.getPath(true).forEach(function (node) {
node.expanded = true;
});
}
}
});
}
// 生成childNodes
var children = void 0;
// 根节点的情况把整个data作为children
if (Array.isArray(data) && this.level === 0) {
children = data;
this.id = 'root';
} else if (data.hasOwnProperty(prop.children)) {
children = data[prop.children];
} else {
if (!this.tree.load) this.isLeaf = true;
}
if (Array.isArray(children) && children.length > 0) {
children.forEach(function (data) {
_this.appendChild(data);
});
} else {
if (!this.tree.load) this.isLeaf = true;
}
this.handleIsLeaf();
};
Node.prototype.handleIsLeaf = function handleIsLeaf() {
// 懒加载且未加载的情况
if (this.tree.load && !this.loaded) {
this.isLeaf = this.getTrueData('isLeaf');
}
// 非懒加载或懒加载但已加载的情况
if (!this.tree.load || this.tree.load && this.loaded) {
var childNodes = this.childNodes;
this.isLeaf = !childNodes || childNodes.length === 0;
}
};
Node.prototype.fillNodesMap = function fillNodesMap() {
var key = this.key;
if (!key && this.level !== 0) {
throw new Error('必须指定节点的key');
}
if (key) {
// 处理初始选中的节点
this.handleSelectedKey(key);
if (this.tree.nodesMap.get(key)) {
throw new Error('节点的key不能重复');
}
this.tree.nodesMap.set(key, this);
}
};
Node.prototype.handleSelectedKey = function handleSelectedKey(key) {
this.selected = false;
if (this.tree.selectedKey === key) {
this.selected = true;
}
};
Node.prototype.appendChild = function appendChild(data, index) {
var child = new Node({
parent: this,
data: data,
tree: this.tree
});
if (index) {
this.childNodes.splice(index, 0, child);
} else {
this.childNodes.push(child);
}
};
Node.prototype.getTrueData = function getTrueData(prop) {
var props = this.tree.prop[prop];
var data = this.data;
if (!data) return;
if (data.hasOwnProperty(props)) {
return data[props];
} else if (data.hasOwnProperty(prop)) {
return data[prop];
} else {
return null;
}
};
Node.prototype.markData = function markData(data) {
Object.defineProperty(data, '_node', {
value: this,
enumerable: false,
configurable: false,
writable: false
});
if (!Array.isArray(data)) {
Object.defineProperty(data, '_nodeId', {
value: this.id,
enumerable: false,
configurable: false,
writable: false
});
} else if (this.level === 0) {
Object.defineProperty(data, '_nodeId', {
value: 'root',
enumerable: false,
configurable: false,
writable: false
});
}
};
Node.prototype.changeExpanded = function changeExpanded(value) {
if (this.tree.load && !this.loaded) {
this.loadNode();
}
if (!this.tree.load || this.loaded) {
this.expanded = value;
}
// 手风琴模式展开的时候所有兄弟节点收起
if (this.tree.accordion && value && !this.tree.searchNodeMethod) {
this.siblingsNodes.forEach(function (node) {
node.expanded = false;
});
}
};
Node.prototype.loadNode = function loadNode() {
var _this2 = this;
var node = this;
if (node.loaded || node.loading || node.childNodes.length > 0) {
return;
}
node.loading = true;
node.tree.load(node.data, function (data) {
if (!Array.isArray(data)) throw new Error('传入子元素必须是数组');
data.forEach(function (data) {
node.appendChild(data);
});
node.loading = false;
node.loaded = true;
node.check = false;
node.expanded = true;
_this2.handleIsLeaf();
});
};
Node.prototype.getPath = function getPath(nodeFlag) {
var arr = [];
var n = 0;
var node = this;
// if (Array.isArray(this.tree.root)) {
// n = 1
// }
while (node.level > n) {
arr.push(nodeFlag ? node : node.data);
node = node.parent;
}
return arr;
};
Node.prototype.handleCheckChange = function handleCheckChange() {
var checked = this.checked;
this.childNodes.forEach(function (node) {
node.check = checked;
});
if (this.parent) {
this.parent.handleChildCheckChange();
}
};
Node.prototype.handleChildCheckChange = function handleChildCheckChange() {
if (Array.isArray(this.data) || this.level === 0) {
return;
}
if (this.childNodes.length === this.checkedChildNodes.length && this.childNodes.length !== 0) {
this.checked = true;
this.indeterminate = false;
} else if (this.checkedChildNodes.length > 0) {
this.checked = false;
this.indeterminate = true;
} else {
this.checked = false;
this.indeterminate = false;
for (var i = 0; i < this.childNodes.length; i++) {
if (this.childNodes[i].indeterminate) {
this.indeterminate = true;
break;
}
}
}
this.parent.handleChildCheckChange();
};
Node.prototype.after = function after(node) {
var oldIndex = node.index;
node.parent.childNodes.splice(oldIndex, 1);
node.parent.handleChildCheckChange();
if (node.parent.childNodes.length === 0) {
node.parent.isLeaf = true;
}
node.parent = this.parent;
node.level = this.level;
this.parent.childNodes.splice(this.index + 1, 0, node);
this.handleChildCheckChange();
};
Node.prototype.first = function first(node) {
var oldIndex = node.index;
node.parent.childNodes.splice(oldIndex, 1);
node.parent.handleChildCheckChange();
if (node.parent.childNodes.length === 0) {
node.parent.isLeaf = true;
}
this.isLeaf = false;
node.parent = this;
node.level = this.level + 1;
this.childNodes.splice(0, 0, node);
this.handleChildCheckChange();
};
Node.prototype.append = function append(node) {
var oldIndex = node.index;
node.parent.childNodes.splice(oldIndex, 1);
node.parent.handleChildCheckChange();
if (node.parent.childNodes.length === 0) {
node.parent.isLeaf = true;
}
this.isLeaf = false;
node.parent = this;
node.level = this.level + 1;
this.childNodes.push(node);
this.handleChildCheckChange();
};
Node.prototype.moveNode = function moveNode(node, index) {
var oldIndex = node.index;
node.parent.childNodes.splice(oldIndex, 1);
node.parent.handleChildCheckChange();
if (node.parent.childNodes.length === 0) {
node.parent.isLeaf = true;
}
this.isLeaf = false;
node.parent = this;
node.level = this.level + 1;
if (index !== null) {
this.childNodes.splice(index, 0, node);
} else {
this.childNodes.push(node);
}
this.handleChildCheckChange();
};
Node.prototype.remove = function remove() {
var node = this;
var index = node.index;
node.parent.childNodes.splice(index, 1);
node.parent.handleChildCheckChange();
node.tree.nodesMap.delete(node.id);
node = null;
};
(0, _createClass3.default)(Node, [{
key: 'label',
get: function get() {
return this.getTrueData('label');
}
}, {
key: 'key',
get: function get() {
return this.getTrueData('key');
}
}, {
key: 'disabled',
get: function get() {
return this.getTrueData('disabled');
}
}, {
key: 'check',
get: function get() {
return this.checked;
},
set: function set(val) {
if (val !== this.checked) {
this.checked = val;
this.handleCheckChange();
}
}
}, {
key: 'siblingsNodes',
get: function get() {
var arr = [];
var currentNode = this;
this.parent.childNodes.forEach(function (node) {
if (node.id !== currentNode.id) {
arr.push(node);
}
});
return arr;
}
}, {
key: 'checkedChildNodes',
get: function get() {
var arr = [];
this.childNodes.forEach(function (node) {
if (node.checked) arr.push(node);
});
return arr;
}
}, {
key: 'indeterminateChildNodes',
get: function get() {
var arr = [];
this.childNodes.forEach(function (node) {
if (node.checked || node.indeterminate) arr.push(node);
});
return arr;
}
}, {
key: 'isExpanded',
get: function get() {
return this.expanded;
},
set: function set(value) {
this.changeExpanded(value);
}
}, {
key: 'index',
get: function get() {
return this.parent.childNodes.indexOf(this);
}
}]);
return Node;
}();
exports.default = Node;