ivue-material-plus
Version:
A high quality UI components Library with Vue.js
180 lines (177 loc) • 5.31 kB
JavaScript
import { getCurrentInstance, unref, nextTick } from 'vue';
import useWatcher from './watcher.mjs';
import { throwError } from '../../../utils/error.mjs';
function sortColumn(array) {
array.forEach((item) => {
var _a, _b;
item.currentIndex = (_a = item.getColumnIndex) == null ? void 0 : _a.call(item);
if ((_b = item.children) == null ? void 0 : _b.length) {
sortColumn(item.children);
}
});
array.sort((cur, pre) => cur.currentIndex - pre.currentIndex);
}
function replaceColumn(array, column) {
return array.map((item) => {
var _a;
if (item.id === column.id) {
return column;
} else if ((_a = item.children) == null ? void 0 : _a.length) {
item.children = replaceColumn(item.children, column);
}
return item;
});
}
function useStore() {
const vm = getCurrentInstance();
const watcher = useWatcher();
const mutations = {
setData(states, data) {
const dataInstanceChanged = unref(states._data) !== data;
states.data.value = data;
states._data.value = data;
vm.store.handleExecQueryData();
vm.store.updateCurrentRowData();
vm.store.updateExpandRows();
vm.store.updateTreeData(vm.store.states.defaultExpandAll.value);
if (unref(states.reserveSelection)) {
vm.store.assertRowKey();
vm.store.updateSelectionByRowKey();
} else {
if (dataInstanceChanged) {
vm.store.clearSelection();
} else {
vm.store.cleanRedundantSelection();
}
}
vm.store.updateAllSelected();
if (vm.$ready) {
vm.store.scheduleLayout();
}
},
toggleAllSelection() {
vm.store.toggleAllSelection();
},
setHoverRow(states, row) {
states.hoverRow.value = row;
},
insertColumn(states, column, parent) {
const array = unref(states._columns);
let newColumns = [];
if (!parent) {
array.push(column);
newColumns = array;
} else {
if (parent && !parent.children) {
parent.children = [];
}
parent.children && parent.children.push(column);
newColumns = replaceColumn(array, parent);
}
sortColumn(newColumns);
states._columns.value = newColumns;
if (column.type === "selection") {
states.selectable.value = column.selectable;
states.reserveSelection.value = column.reserveSelection;
}
if (vm.$ready) {
vm.store.updateColumns();
vm.store.scheduleLayout();
}
},
removeColumn(states, column, parent) {
const array = unref(states._columns) || [];
if (parent && parent.children) {
const findIndex = parent.children.findIndex(
(item) => item.id === column.id
);
parent.children.splice(findIndex, 1);
if (parent.children.length === 0) {
delete parent.children;
}
states._columns.value = replaceColumn(array, parent);
} else {
const index = array.indexOf(column);
if (index > -1) {
array.splice(index, 1);
states._columns.value = array;
}
}
if (vm.$ready) {
vm.store.updateColumns();
vm.store.scheduleLayout();
}
},
setCurrentRow(_states, row) {
vm.store.updateCurrentRow(row);
},
rowSelectedChanged(_states, row) {
vm.store.toggleRowSelection(row);
vm.store.updateAllSelected();
},
changeSortCondition(states, options) {
const { sortingColumn, sortProp, sortOrder } = states;
if (unref(sortOrder) === null) {
states.sortingColumn.value = null;
states.sortProp.value = null;
}
vm.store.handleExecQueryData(false);
if (!options || !(options.silent || options.init)) {
vm.emit("on-sort-change", {
column: unref(sortingColumn),
prop: unref(sortProp),
order: unref(sortOrder)
});
}
vm.store.updateTableScrollY();
},
sort(states, options) {
const { prop, order, init } = options;
if (prop) {
const column = unref(states.columns).find(
(column2) => column2.property === prop
);
if (column) {
column.order = order;
vm.store.updateSort(column, prop, order);
vm.store.commit("changeSortCondition", { init });
}
}
},
filterChange(states, options) {
const { column, values, silent } = options;
const newFilters = vm.store.updateFilters(column, values);
vm.store.handleExecQueryData();
if (!silent) {
vm.emit("on-filter-change", newFilters);
}
vm.store.updateTableScrollY();
}
};
const commit = (name, ...args) => {
const mutations2 = vm.store.mutations;
if (mutations2[name]) {
mutations2[name].apply(vm, [vm.store.states].concat(args));
} else {
throwError("ivue-table", `Action not found: ${String(name)}`);
}
};
const updateTableScrollY = () => {
nextTick(() => {
vm.layout.updateScrollY.apply(vm.layout);
});
};
return {
...watcher,
mutations,
commit,
updateTableScrollY
};
}
class HelperStore {
constructor() {
this.Return = useStore();
}
}
export { useStore as default };
//# sourceMappingURL=index.mjs.map