UNPKG

element-tree-grid

Version:
172 lines (171 loc) 6.31 kB
import { isLoadingRow, isExpandedRow } from "./utils"; var hash = function () { return Math.floor(Math.random() * Math.random() * Math.random() * Math.random() * 1000); }; export var clone = function (data) { return JSON.parse(JSON.stringify(data)); }; var index = function (hash, data) { var i = 0; while (data[i]) { var c = data[i]; if (c.$extra && c.$extra.hash == hash) { break; } i++; } ; return i; }; var indexOf = function (val, arr) { var has = -1; for (var i = 0; i < arr.length; i++) { if (arr[i] == val) { has = i; break; } } return has; }; var descendantsIds = function (id, data, parentKey, treeKey) { var result = [], compare = [id], length = -1; while (length != compare.length) { length = compare.length; data.forEach(function (item) { if (indexOf(item[parentKey], compare) > -1 && indexOf(item[treeKey], compare) == -1) { result.push(item[treeKey]); compare.push(item[treeKey]); } }); } return result; }; var has = function (context, item, list) { var key = context.props.treeKey, parentKey = context.props.parentKey; var uniqueKey = item[key]; var has = false; list.forEach(function (row) { if (row[key] == uniqueKey || row[key] == item[parentKey]) { has = true; } }); return has; }; var toggleExpanded = function (context, scope, isExpended) { var treeKey = context.props.treeKey, states = scope.store.states; states._treeCachedExpanded = states._treeCachedExpanded.filter(function (r) { return r[treeKey] != scope.row[treeKey]; }); if (isExpended) { states._treeRowExpanded = states._treeRowExpanded.filter(function (r) { return r[treeKey] != scope.row[treeKey]; }); } else { states._treeRowExpanded.push(scope.row); } }; var toggleLoading = function (context, scope, isloading) { var treeKey = context.props.treeKey; if (isloading) { scope.store.states._treeRowLoading = scope.store.states._treeRowLoading.filter(function (r) { return r[treeKey] != scope.row[treeKey]; }); } else { scope.store.states._treeRowLoading.push(scope.row); } }; var commit = function (context, scope, list) { var owner = scope.store.table; //methods.owner(context.parent); var states = scope.store.states; // let selection = states.selection; owner.store.commit('setData', list); /* owner.clearSelection(); let data = owner.store.states._data; data.forEach(row => { if (has(context, row, selection)) { owner.toggleRowSelection(row) } }); */ }; var getIndex = function (context, scope, data) { var index = -1, treeKey = context.props.treeKey; data.forEach(function (r, i) { if (r[treeKey] == scope.row[treeKey]) { index = i; } }); return index; }; var Colspand = function (context, scope, data) { var _a = context.props, parentKey = _a.parentKey, treeKey = _a.treeKey, childKey = _a.childKey, states = scope.store.states, row = scope.row, result = []; var removeIds = []; removeIds = descendantsIds(row[treeKey], data, parentKey, treeKey); data = data.filter(function (item) { return !removeIds.some(function (id) { return id == item[treeKey]; }); }); var NeedToCached = states._treeRowExpanded.filter(function (item) { return removeIds.some(function (id) { return id == item[treeKey]; }); }); var _treeCachedExpanded = states._treeCachedExpanded; NeedToCached.forEach(function (item) { if (!_treeCachedExpanded.some(function (i) { return i[treeKey] == item[treeKey]; })) { states._treeCachedExpanded.push(item); } }); states._treeRowExpanded = states._treeRowExpanded.filter(function (item) { return !removeIds.some(function (id) { return id == item[treeKey]; }); }); commit(context, scope, data); }; var insertRows = function (context, scope, children, updateChild) { if (updateChild === void 0) { updateChild = false; } var _data = clone(scope.store.states._data); var _index = getIndex(context, scope, _data); var prefix = _data.slice(0, _index + 1); var i = 0; while (i < _index + 1) { _data.shift(); i++; } if (updateChild) prefix[_index][context.props.childKey] = children; _data = prefix.concat(children).concat(_data); return _data; }; var ExpandRemote = function (context, scope, data) { var _a = context.props, treeKey = _a.treeKey, remote = _a.remote; toggleLoading(context, scope, false); var CallBack = function (children) { var childNumKey = context.props.childNumKey; toggleLoading(context, scope, true); var _data; if (children && children.length > 0) { var updateChild = !context.props.allRemote; _data = insertRows(context, scope, children, updateChild); } else { _data = clone(scope.store.states._data); var _index = getIndex(context, scope, _data); _data[_index][childNumKey] = 0; } commit(context, scope, _data); }; commit(context, scope, data); remote(scope.row, CallBack); // console.info(scope.store.states._treeCachedExpanded) }; var Expand = function (context, scope, data) { var childKey = context.props.childKey; data = insertRows(context, scope, scope.row[childKey]); commit(context, scope, data); }; export function doExpand(context, scope) { var data = clone(scope.store.states._data), childKey = context.props.childKey; // line is loading if (isLoadingRow(context, scope)) return; var isExpended = isExpandedRow(context, scope); toggleExpanded(context, scope, isExpended); if (isExpended) { return Colspand(context, scope, data); } var _a = context.props, remote = _a.remote, allRemote = _a.allRemote; if (remote && allRemote) { return ExpandRemote(context, scope, data); } if (scope.row[childKey]) { return Expand(context, scope, data); } else if (remote) { return ExpandRemote(context, scope, data); } Expand(context, scope, data); }