@meve/ui
Version:
Argon design system components library
108 lines (92 loc) • 3.02 kB
JavaScript
function flatVNodes(subTree) {
var vNodes = [];
function flat(subTree) {
subTree.forEach(function (child) {
vNodes.push(child);
if (child.componentInstance) {
flat(child.componentInstance.$children.map(function (item) {
return item.$vnode;
}));
}
if (child.children) {
flat(child.children);
}
});
}
flat(subTree);
return vNodes;
}
export function sortChildren(children, parent) {
var componentOptions = parent.$vnode.componentOptions;
if (!componentOptions || !componentOptions.children) {
return;
}
var vNodes = flatVNodes(componentOptions.children);
children.sort(function (a, b) {
return vNodes.indexOf(a.$vnode) - vNodes.indexOf(b.$vnode);
});
}
export function createParentMixin(parent, options) {
if (options === void 0) {
options = {};
}
var _options = options,
_options$childrenKey = _options.childrenKey,
childrenKey = _options$childrenKey === void 0 ? 'children' : _options$childrenKey;
return {
provide: function provide() {
var _ref;
return _ref = {}, _ref[parent] = this, _ref;
},
data: function data() {
var _ref2;
return _ref2 = {}, _ref2[childrenKey] = [], _ref2;
}
};
}
export function createChildrenMixin(parent, options) {
var _inject, _computed, _methods;
if (options === void 0) {
options = {};
}
var _options2 = options,
_options2$indexKey = _options2.indexKey,
indexKey = _options2$indexKey === void 0 ? 'index' : _options2$indexKey,
_options2$childrenKey = _options2.childrenKey,
childrenKey = _options2$childrenKey === void 0 ? 'children' : _options2$childrenKey,
_options2$parentKey = _options2.parentKey,
parentKey = _options2$parentKey === void 0 ? 'parent' : _options2$parentKey;
return {
inject: (_inject = {}, _inject[parent] = {
default: null
}, _inject),
computed: (_computed = {}, _computed[parentKey] = function () {
return this[parent];
}, _computed[indexKey] = function () {
this["bind" + parentKey + childrenKey + "Relation"]();
if (this[parentKey]) {
return this[parentKey][childrenKey].indexOf(this);
}
return null;
}, _computed),
mounted: function mounted() {
this["bind" + parentKey + childrenKey + "Relation"]();
},
beforeDestroy: function beforeDestroy() {
var _this = this;
if (this[parentKey]) {
this[parentKey][childrenKey] = this[parentKey][childrenKey].filter(function (item) {
return item !== _this;
});
}
},
methods: (_methods = {}, _methods["bind" + parentKey + childrenKey + "Relation"] = function bindRelation() {
if (!this[parentKey] || this[parentKey][childrenKey].includes(this)) {
return;
}
var children = [].concat(this[parentKey][childrenKey], [this]);
sortChildren(children, this[parentKey]);
this[parentKey][childrenKey] = children;
}, _methods)
};
}