tav-ui
Version:
101 lines (96 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var _const = require('../const2.js');
function useCheckboxCache(tableRef, tablePropsRef, currentPage, tableEmitter) {
const {
rowConfig: { keyField = _const.ROW_KEY },
api
} = vue.unref(tablePropsRef);
const checkboxCaches = vue.ref({});
const isCheckboxCacheEnabled = vue.computed(() => tablePropsRef.value.checkboxConfig && tablePropsRef.value.checkboxConfig.enabled && tablePropsRef.value.checkboxConfig.cache);
const checkboxCacheList = vue.computed(() => {
if (!isCheckboxCacheEnabled.value)
return [];
let caches = [];
for (const [_, k] of Object.entries(vue.unref(checkboxCaches))) {
caches = [...caches, ...k];
}
return caches;
});
if (api) {
tableEmitter.on("table-pro:api-success", async () => {
await vue.nextTick();
await applyCheckboxCacheByCurrentPage();
});
} else {
vue.watch(() => currentPage.value, async () => {
await vue.nextTick();
await applyCheckboxCacheByCurrentPage();
});
}
async function createCheckboxCache(row) {
if (!isCheckboxCacheEnabled.value)
return;
const cache = !!checkboxCacheList.value.find((cache2) => `${cache2[keyField]}` === `${row[keyField]}`);
if (!cache) {
vue.unref(checkboxCaches)[currentPage.value] = [
...vue.unref(checkboxCaches)[currentPage.value] ?? [],
{ ...vue.toRaw(row), __page: currentPage.value }
];
await vue.unref(tableRef)?.toggleCheckboxRow(row);
}
}
async function createAllCheckboxCache(rows) {
if (!isCheckboxCacheEnabled.value)
return;
vue.unref(checkboxCaches)[currentPage.value] = [
...vue.toRaw(rows).map((row) => {
return { ...vue.toRaw(row), __page: currentPage.value };
})
];
await vue.unref(tableRef)?.toggleAllCheckboxRow();
}
async function deleteCheckboxCache(row) {
if (!isCheckboxCacheEnabled.value)
return;
const page = row.__page ?? currentPage.value;
const cache = !!checkboxCacheList.value.find((cache2) => `${cache2[keyField]}` === `${row[keyField]}`);
if (cache) {
vue.unref(checkboxCaches)[page] = vue.unref(checkboxCaches)[page].filter((cache2) => `${cache2[keyField]}` !== `${row[keyField]}`);
await vue.unref(tableRef)?.toggleCheckboxRow(row);
}
}
async function deleteAllCheckboxCache(options) {
if (!isCheckboxCacheEnabled.value)
return;
const { deleteByPage } = options;
if (deleteByPage) {
checkboxCaches.value[currentPage.value] = [];
} else {
checkboxCaches.value = {};
}
await vue.unref(tableRef)?.clearCheckboxRow();
}
async function applyCheckboxCacheByCurrentPage() {
if (!isCheckboxCacheEnabled.value)
return;
if (vue.unref(checkboxCaches)[currentPage.value]) {
await vue.unref(tableRef)?.clearCheckboxRow();
const promises = vue.unref(checkboxCaches)[currentPage.value].map(async (row) => await vue.unref(tableRef)?.setCheckboxRow(row, true));
await Promise.all(promises);
}
}
return {
checkboxCaches,
isCheckboxCacheEnabled,
checkboxCacheList,
createCheckboxCache,
createAllCheckboxCache,
deleteCheckboxCache,
deleteAllCheckboxCache,
applyCheckboxCacheByCurrentPage
};
}
exports.useCheckboxCache = useCheckboxCache;
//# sourceMappingURL=useCheckboxCache2.js.map