tav-ui
Version:
88 lines (85 loc) • 2.99 kB
JavaScript
import { toRaw } from 'vue';
import { sleep } from '../../utils/index2.mjs';
function useCardActions(options) {
const { dataSource: _dataSource, setDataSource, loading } = options;
async function cardCreateRows(_options) {
const { rows, position = null, useLoading } = _options;
const dataSource = JSON.parse(JSON.stringify(_dataSource.value));
if (useLoading !== void 0 && useLoading)
loading.value.value = true;
let promiseAll;
if (Array.isArray(position)) {
promiseAll = toRaw(rows).map(async (row, idx) => {
const _idx = dataSource.findIndex((d) => d.id === position[idx].id);
dataSource.splice(_idx, 0, row);
return Promise.resolve();
});
} else {
promiseAll = toRaw(rows).map(async (row) => {
if (position === null) {
dataSource.splice(0, 0, row);
} else if (position === -1) {
dataSource.splice(dataSource.length, 0, row);
} else {
const idx = dataSource.findIndex((d) => d.id === position.id);
dataSource.splice(idx, 0, row);
}
return Promise.resolve();
});
}
await Promise.all(promiseAll);
setDataSource(dataSource);
if (useLoading !== void 0 && useLoading)
loading.value.value = false;
}
async function cardReadRows(_options = {}) {
const { useLoading } = _options;
const dataSource = JSON.parse(JSON.stringify(_dataSource.value));
if (useLoading !== void 0 && useLoading)
loading.value.value = true;
await sleep(150);
if (useLoading !== void 0 && useLoading)
loading.value.value = false;
return dataSource;
}
async function cardUpdateRows(_options) {
const { rows, deleteRows, useLoading } = _options;
const dataSource = JSON.parse(JSON.stringify(_dataSource.value));
if (useLoading !== void 0 && useLoading)
loading.value.value = true;
const promiseAll = toRaw(rows).map((row, idx) => {
const _idx = dataSource.findIndex((d) => d.id === deleteRows[idx].id);
dataSource.splice(_idx, 1, row);
return Promise.resolve();
});
await Promise.all(promiseAll);
setDataSource(dataSource);
if (useLoading !== void 0 && useLoading)
loading.value.value = false;
}
async function cardDeleteRows(_options) {
const { rows, useLoading } = _options;
let dataSource = JSON.parse(JSON.stringify(_dataSource.value));
if (useLoading !== void 0 && useLoading)
loading.value.value = true;
if (rows === void 0) {
dataSource = [];
} else {
toRaw(rows).forEach((row) => {
const idx = dataSource.findIndex((d) => d.id === row.id);
dataSource.splice(idx, 1);
});
}
setDataSource(dataSource);
if (useLoading !== void 0 && useLoading)
loading.value.value = false;
}
return {
cardCreateRows,
cardReadRows,
cardUpdateRows,
cardDeleteRows
};
}
export { useCardActions };
//# sourceMappingURL=use-card-actions2.mjs.map