xdesign-vue-next
Version:
XDesign Component for vue-next
706 lines (700 loc) • 21.5 kB
JavaScript
/**
* xdesign v1.0.6
* (c) 2023 xdesign
* @license MIT
*/
import { _ as _classCallCheck, a as _createClass } from '../../../_chunks/dep-72a1dd28.mjs';
import { _ as _defineProperty } from '../../../_chunks/dep-f9e836af.mjs';
import { i as isArray_1 } from '../../../_chunks/dep-a95026f2.mjs';
import { i as isFunction_1 } from '../../../_chunks/dep-1cc1c24f.mjs';
import { i as isNumber_1 } from '../../../_chunks/dep-23f91684.mjs';
import { i as isString_1 } from '../../../_chunks/dep-11fa9c2c.mjs';
import '../../../_chunks/dep-10a947a6.mjs';
import { _ as _baseDifference } from '../../../_chunks/dep-29f616db.mjs';
import { _ as _baseFlatten } from '../../../_chunks/dep-983ce253.mjs';
import { _ as _baseRest, i as isArrayLikeObject_1 } from '../../../_chunks/dep-26bf361a.mjs';
import { c as camelCase_1 } from '../../../_chunks/dep-a628549d.mjs';
import { i as isPlainObject_1 } from '../../../_chunks/dep-ed4e7c50.mjs';
import mitt from 'mitt';
import { TreeNode } from './tree-node.mjs';
import '../../../_chunks/dep-82805301.mjs';
import '../../../_chunks/dep-b75d8d74.mjs';
import '../../../_chunks/dep-6ad18815.mjs';
import '../../../_chunks/dep-addc2a84.mjs';
import '../../../_chunks/dep-068e912d.mjs';
import '../../../_chunks/dep-4265f69a.mjs';
import '../../../_chunks/dep-6aa0223b.mjs';
import '../../../_chunks/dep-db381ece.mjs';
import '../../../_chunks/dep-5755c21c.mjs';
import '../../../_chunks/dep-7f239c43.mjs';
import '../../../_chunks/dep-4903a8a8.mjs';
import '../../../_chunks/dep-7dcfa37a.mjs';
import '../../../_chunks/dep-5f0e0453.mjs';
import '../../../_chunks/dep-205ff58d.mjs';
import '../../../_chunks/dep-dafada74.mjs';
import '../../../_chunks/dep-0e832fc7.mjs';
import '../../../_chunks/dep-e1ab85c5.mjs';
import '../../../_chunks/dep-3ec3335a.mjs';
import '../../../_chunks/dep-d32fbbb3.mjs';
import '../../../_chunks/dep-f4eba04c.mjs';
import '../../../_chunks/dep-735bcd0d.mjs';
import '../../../_chunks/dep-765678ef.mjs';
import '../../../_chunks/dep-8db27830.mjs';
import '../../../_chunks/dep-81c83986.mjs';
import '../../../_chunks/dep-6f04869e.mjs';
import '../../../_chunks/dep-6e7b37b8.mjs';
import '../../../_chunks/dep-71f84cf2.mjs';
import '../../../_chunks/dep-2cefabe0.mjs';
import '../../../_chunks/dep-5bec687c.mjs';
import '../../../_chunks/dep-89b966f4.mjs';
import '../../../_chunks/dep-5a2ce53e.mjs';
import '../../../_chunks/dep-1cab4dbd.mjs';
import '../../../_chunks/dep-7fa157cd.mjs';
import '../../../_chunks/dep-fa6608fd.mjs';
import './tree-node-model.mjs';
import '../../../_chunks/dep-1bae6a97.mjs';
import '../../../_chunks/dep-b9fae426.mjs';
import '../../../_chunks/dep-60f1d7ee.mjs';
import '../../../_chunks/dep-c4737535.mjs';
import '../../../_chunks/dep-69963a8c.mjs';
import '../../../_chunks/dep-8d1c9a23.mjs';
import '../../../_chunks/dep-03412fab.mjs';
import '../../../_chunks/dep-e901316d.mjs';
import '../../../_chunks/dep-b57a2815.mjs';
import '../log/log.mjs';
import '../log/index.mjs';
var baseDifference = _baseDifference,
baseFlatten = _baseFlatten,
baseRest = _baseRest,
isArrayLikeObject = isArrayLikeObject_1;
/**
* Creates an array of `array` values not included in the other given arrays
* using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
* for equality comparisons. The order and references of result values are
* determined by the first array.
*
* **Note:** Unlike `_.pullAll`, this method returns a new array.
*
* @static
* @memberOf _
* @since 0.1.0
* @category Array
* @param {Array} array The array to inspect.
* @param {...Array} [values] The values to exclude.
* @returns {Array} Returns the new array of filtered values.
* @see _.without, _.xor
* @example
*
* _.difference([2, 1], [2, 3]);
* // => [1]
*/
var difference = baseRest(function (array, values) {
return isArrayLikeObject(array) ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true)) : [];
});
var difference_1 = difference;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function nextTick(fn) {
var pm = Promise.resolve();
pm.then(fn);
return pm;
}
var TreeStore = /*#__PURE__*/function () {
function TreeStore(options) {
_classCallCheck(this, TreeStore);
_defineProperty(this, "children", void 0);
_defineProperty(this, "nodes", void 0);
_defineProperty(this, "nodeMap", void 0);
_defineProperty(this, "privateMap", void 0);
_defineProperty(this, "config", void 0);
_defineProperty(this, "activedMap", void 0);
_defineProperty(this, "updatedMap", void 0);
_defineProperty(this, "checkedMap", void 0);
_defineProperty(this, "expandedMap", void 0);
_defineProperty(this, "filterMap", void 0);
_defineProperty(this, "hasFilter", void 0);
_defineProperty(this, "emitter", void 0);
_defineProperty(this, "updateTick", void 0);
_defineProperty(this, "shouldReflow", void 0);
_defineProperty(this, "prevFilter", void 0);
var config = _objectSpread({
prefix: "x",
keys: {},
expandAll: false,
expandLevel: 0,
expandMutex: false,
expandParent: false,
activable: false,
activeMultiple: false,
checkable: false,
checkStrictly: false,
disabled: false,
draggable: false,
load: null,
lazy: false,
valueMode: "onlyLeaf",
filter: null,
allowFoldNodeOnFilter: false,
onLoad: null,
onReflow: null,
onUpdate: null
}, options);
this.config = config;
this.nodes = [];
this.children = [];
this.nodeMap = /* @__PURE__ */new Map();
this.privateMap = /* @__PURE__ */new Map();
this.activedMap = /* @__PURE__ */new Map();
this.expandedMap = /* @__PURE__ */new Map();
this.checkedMap = /* @__PURE__ */new Map();
this.updatedMap = /* @__PURE__ */new Map();
this.filterMap = /* @__PURE__ */new Map();
this.prevFilter = null;
this.updateTick = null;
this.shouldReflow = false;
this.hasFilter = isFunction_1(config.filter);
this.emitter = mitt();
}
_createClass(TreeStore, [{
key: "setConfig",
value: function setConfig(options) {
var config = this.config;
var hasChanged = false;
Object.keys(options).forEach(function (key) {
var val = options[key];
if (val !== config[key]) {
hasChanged = true;
config[key] = val;
}
});
this.hasFilter = isFunction_1(config.filter);
if (hasChanged) {
this.refreshState();
}
}
}, {
key: "getChildren",
value: function getChildren() {
return this.children;
}
}, {
key: "getNode",
value: function getNode(item) {
var node = null;
if (isString_1(item) || isNumber_1(item)) {
node = this.nodeMap.get(item);
} else if (item instanceof TreeNode) {
node = this.nodeMap.get(item.value);
}
if (!node) node = null;
return node;
}
}, {
key: "getIndex",
value: function getIndex(node) {
return this.nodes.indexOf(node);
}
}, {
key: "getParent",
value: function getParent(value) {
var parent = null;
var node = this.getNode(value);
if (node) {
parent = node.getParent();
}
return parent;
}
}, {
key: "getParents",
value: function getParents(value) {
var node = this.getNode(value);
var parents = [];
if (node) {
parents = node.getParents();
}
return parents;
}
}, {
key: "getNodeIndex",
value: function getNodeIndex(value) {
var node = this.getNode(value);
var index = -1;
if (node) {
index = node.getIndex();
}
return index;
}
}, {
key: "getNodes",
value: function getNodes(item, options) {
var nodes = [];
var val = "";
if (isString_1(item) || isNumber_1(item)) {
val = item;
} else if (item instanceof TreeNode) {
val = item.value;
}
if (!val) {
nodes = this.nodes.slice(0);
} else {
var node = this.getNode(val);
if (node) {
nodes = node.walk();
}
}
if (options) {
var conf = _objectSpread({
filter: null,
level: Infinity
}, options);
if (isNumber_1(conf.level) && conf.level !== Infinity) {
nodes = nodes.filter(function (node) {
return node.level <= conf.level;
});
}
if (isFunction_1(conf.filter)) {
nodes = nodes.filter(function (node) {
var nodeModel = node.getModel();
return conf.filter(nodeModel);
});
}
if (isPlainObject_1(conf.props)) {
nodes = nodes.filter(function (node) {
var result = Object.keys(conf.props).every(function (key) {
var propEqual = node[key] === conf.props[key];
return propEqual;
});
return result;
});
}
}
return nodes;
}
}, {
key: "append",
value: function append(list) {
var _this = this;
list.forEach(function (item) {
var node = new TreeNode(_this, item);
_this.children.push(node);
});
this.reflow();
}
}, {
key: "reload",
value: function reload(list) {
this.expandedMap.clear();
this.checkedMap.clear();
this.activedMap.clear();
this.filterMap.clear();
this.removeAll();
this.append(list);
}
}, {
key: "parseNodeData",
value: function parseNodeData(para, item) {
var value = "";
var node = null;
var data = null;
if (isString_1(para) || isNumber_1(para)) {
value = para;
node = this.getNode(value);
data = item;
} else if (para instanceof TreeNode) {
if (item) {
node = para;
data = item;
} else {
data = para;
}
} else {
data = para;
}
var spec = {
node: node,
data: data
};
return spec;
}
}, {
key: "appendNodes",
value: function appendNodes(para, item) {
var spec = this.parseNodeData(para, item);
if (spec.data) {
if (!spec.node) {
if (spec.data instanceof TreeNode) {
spec.data.appendTo(this);
} else if (Array.isArray(spec.data)) {
this.append(spec.data);
} else {
this.append([spec.data]);
}
} else {
if (spec.data instanceof TreeNode) {
spec.data.appendTo(this, spec.node);
} else if (isArray_1(spec.data)) {
spec.node.append(spec.data);
} else {
spec.node.append([spec.data]);
}
spec.node.updateRelated();
}
}
}
}, {
key: "insertBefore",
value: function insertBefore(value, item) {
var node = this.getNode(value);
if (node) {
node.insertBefore(item);
}
}
}, {
key: "insertAfter",
value: function insertAfter(value, item) {
var node = this.getNode(value);
if (node) {
node.insertAfter(item);
}
}
}, {
key: "refreshNodes",
value: function refreshNodes() {
var children = this.children,
nodes = this.nodes;
nodes.length = 0;
children.forEach(function (node) {
var list = node.walk();
Array.prototype.push.apply(nodes, list);
});
}
}, {
key: "reflow",
value: function reflow(node) {
this.shouldReflow = true;
this.updated(node);
}
}, {
key: "updated",
value: function updated(node) {
var _this2 = this;
if (node !== null && node !== void 0 && node.value) {
this.updatedMap.set(node.value, true);
}
if (this.updateTick) return;
this.updateTick = nextTick(function () {
_this2.updateTick = null;
if (_this2.shouldReflow) {
_this2.refreshNodes();
_this2.emit("reflow");
}
_this2.lockFilterPathNodes();
var updatedList = Array.from(_this2.updatedMap.keys());
if (updatedList.length > 0) {
var updatedNodes = updatedList.map(function (value) {
return _this2.getNode(value);
});
_this2.emit("update", {
nodes: updatedNodes,
map: _this2.updatedMap
});
} else if (_this2.shouldReflow) {
_this2.emit("update", {
nodes: [],
map: _this2.updatedMap
});
}
_this2.shouldReflow = false;
_this2.updatedMap.clear();
});
}
}, {
key: "getActived",
value: function getActived(map) {
var activedMap = map || this.activedMap;
return Array.from(activedMap.keys());
}
}, {
key: "getActivedNodes",
value: function getActivedNodes(item) {
var nodes = this.getNodes(item);
nodes = nodes.filter(function (node) {
return node.isActived();
});
return nodes;
}
}, {
key: "replaceActived",
value: function replaceActived(list) {
this.resetActived();
this.setActived(list);
}
}, {
key: "setActived",
value: function setActived(actived) {
var _this3 = this;
var activeMultiple = this.config.activeMultiple;
var list = actived.slice(0);
if (!activeMultiple) {
list.length = 1;
}
list.forEach(function (val) {
_this3.activedMap.set(val, true);
var node = _this3.getNode(val);
if (node) {
node.update();
}
});
}
}, {
key: "resetActived",
value: function resetActived() {
var actived = this.getActived();
this.activedMap.clear();
var relatedNodes = this.getRelatedNodes(actived);
relatedNodes.forEach(function (node) {
node.update();
});
}
}, {
key: "getExpanded",
value: function getExpanded(map) {
var expandedMap = map || this.expandedMap;
return Array.from(expandedMap.keys());
}
}, {
key: "replaceExpanded",
value: function replaceExpanded(list) {
var expanded = this.getExpanded();
var added = difference_1(list, expanded);
var removed = difference_1(expanded, list);
this.setExpandedDirectly(removed, false);
this.updateExpanded(removed);
this.setExpanded(added);
}
}, {
key: "setExpanded",
value: function setExpanded(list) {
this.setExpandedDirectly(list);
this.updateExpanded(list);
}
}, {
key: "setExpandedDirectly",
value: function setExpandedDirectly(list) {
var _this4 = this;
var expanded = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
list.forEach(function (val) {
if (expanded) {
_this4.expandedMap.set(val, true);
} else {
_this4.expandedMap["delete"](val);
}
var node = _this4.getNode(val);
if (node) {
node.afterExpanded();
}
});
}
}, {
key: "resetExpanded",
value: function resetExpanded() {
var expanded = this.getExpanded();
this.expandedMap.clear();
this.updateExpanded(expanded);
}
}, {
key: "updateExpanded",
value: function updateExpanded(list) {
var relatedNodes = this.getRelatedNodes(list, {
withParents: false
});
relatedNodes.forEach(function (node) {
node.update();
});
}
}, {
key: "getChecked",
value: function getChecked(map) {
var nodeMap = this.nodeMap,
config = this.config;
var valueMode = config.valueMode,
checkStrictly = config.checkStrictly;
var list = [];
var checkedMap = map || this.checkedMap;
nodeMap.forEach(function (node) {
if (!node.isChecked(checkedMap)) return;
if (valueMode === "parentFirst" && !checkStrictly) {
if (!node.parent || !node.parent.isChecked(checkedMap)) {
list.push(node.value);
}
} else if (valueMode === "onlyLeaf" && !checkStrictly) {
if (node.isLeaf()) {
list.push(node.value);
}
} else {
list.push(node.value);
}
});
return list;
}
}, {
key: "getCheckedNodes",
value: function getCheckedNodes(item) {
var nodes = this.getNodes(item);
nodes = nodes.filter(function (node) {
return node.isChecked();
});
return nodes;
}
}, {
key: "replaceChecked",
value: function replaceChecked(list) {
this.resetChecked();
this.setChecked(list);
}
}, {
key: "setChecked",
value: function setChecked(list) {
var _this5 = this;
var _this$config = this.config,
checkStrictly = _this$config.checkStrictly,
checkable = _this$config.checkable;
if (!checkable) return;
list.forEach(function (val) {
var node = _this5.getNode(val);
if (!node) return;
if (checkStrictly) {
_this5.checkedMap.set(val, true);
node.updateChecked();
} else {
var childrenNodes = node.walk();
childrenNodes.forEach(function (childNode) {
_this5.checkedMap.set(childNode.value, true);
});
}
});
if (!checkStrictly) {
var checkedValues = this.getChecked();
var relatedNodes = this.getRelatedNodes(checkedValues);
relatedNodes.forEach(function (node) {
node.updateChecked();
});
}
}
}, {
key: "resetChecked",
value: function resetChecked() {
var checked = this.getChecked();
var relatedNodes = this.getRelatedNodes(checked);
this.checkedMap.clear();
relatedNodes.forEach(function (node) {
node.updateChecked();
});
}
}, {
key: "refreshState",
value: function refreshState() {
var nodeMap = this.nodeMap;
nodeMap.forEach(function (node) {
node.update();
});
}
}, {
key: "updateAll",
value: function updateAll() {
var nodeMap = this.nodeMap;
nodeMap.forEach(function (node) {
node.update();
node.updateChecked();
});
}
}, {
key: "remove",
value: function remove(value) {
var node = this.getNode(value);
if (node) {
node.remove();
}
}
}, {
key: "removeAll",
value: function removeAll() {
var nodes = this.getNodes();
nodes.forEach(function (node) {
node.remove();
});
}
}, {
key: "getRelatedNodes",
value: function getRelatedNodes(list, options) {
var _this6 = this;
var conf = _objectSpread({
reverse: false,
withParents: true
}, options);
var map = /* @__PURE__ */new Map();
list.forEach(function (value) {
if (map.get(value)) return;
var node = _this6.getNode(value);
if (node) {
var parents = node.getParents().reverse();
var children = node.walk();
var related = [];
if (conf.withParents) {
related = parents.concat(children);
} else {
related = children;
}
related.forEach(function (relatedNode) {
map.set(relatedNode.value, relatedNode);
});
}
});
var relatedNodes = Array.from(map.values());
if (conf.reverse) {
relatedNodes = relatedNodes.reverse();
}
return relatedNodes;
}
}, {
key: "emit",
value: function emit(name, state) {
var config = this.config,
emitter = this.emitter;
var methodName = camelCase_1("on-".concat(name));
var method = config[methodName];
if (isFunction_1(method)) {
method(state);
}
emitter.emit(name, state);
}
}, {
key: "lockFilterPathNodes",
value: function lockFilterPathNodes() {
var config = this.config;
var allNodes = this.getNodes();
if (this.prevFilter) {
allNodes.forEach(function (node) {
if (node.vmIsLocked) {
node.lock(false);
}
});
}
var currentFilter = config.filter;
if (!currentFilter || !isFunction_1(currentFilter)) return;
this.prevFilter = config.filter;
allNodes.reverse().forEach(function (node) {
var parent = node.getParent();
if (!parent) return;
if (node.vmIsRest || node.vmIsLocked) {
if (!parent.vmIsLocked) {
parent.lock(true);
}
}
});
}
}]);
return TreeStore;
}();
export { TreeStore, TreeStore as default };
//# sourceMappingURL=tree-store.mjs.map