tav-ui
Version:
97 lines (94 loc) • 3.29 kB
JavaScript
import { unref, ref, computed, nextTick, watch, toRaw } from 'vue';
import { ROW_KEY } from '../const2.mjs';
function useCheckboxCache(tableRef, tablePropsRef, currentPage, tableEmitter) {
const {
rowConfig: { keyField = ROW_KEY },
api
} = unref(tablePropsRef);
const checkboxCaches = ref({});
const isCheckboxCacheEnabled = computed(() => tablePropsRef.value.checkboxConfig && tablePropsRef.value.checkboxConfig.enabled && tablePropsRef.value.checkboxConfig.cache);
const checkboxCacheList = computed(() => {
if (!isCheckboxCacheEnabled.value)
return [];
let caches = [];
for (const [_, k] of Object.entries(unref(checkboxCaches))) {
caches = [...caches, ...k];
}
return caches;
});
if (api) {
tableEmitter.on("table-pro:api-success", async () => {
await nextTick();
await applyCheckboxCacheByCurrentPage();
});
} else {
watch(() => currentPage.value, async () => {
await nextTick();
await applyCheckboxCacheByCurrentPage();
});
}
async function createCheckboxCache(row) {
if (!isCheckboxCacheEnabled.value)
return;
const cache = !!checkboxCacheList.value.find((cache2) => `${cache2[keyField]}` === `${row[keyField]}`);
if (!cache) {
unref(checkboxCaches)[currentPage.value] = [
...unref(checkboxCaches)[currentPage.value] ?? [],
{ ...toRaw(row), __page: currentPage.value }
];
await unref(tableRef)?.toggleCheckboxRow(row);
}
}
async function createAllCheckboxCache(rows) {
if (!isCheckboxCacheEnabled.value)
return;
unref(checkboxCaches)[currentPage.value] = [
...toRaw(rows).map((row) => {
return { ...toRaw(row), __page: currentPage.value };
})
];
await 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) {
unref(checkboxCaches)[page] = unref(checkboxCaches)[page].filter((cache2) => `${cache2[keyField]}` !== `${row[keyField]}`);
await 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 unref(tableRef)?.clearCheckboxRow();
}
async function applyCheckboxCacheByCurrentPage() {
if (!isCheckboxCacheEnabled.value)
return;
if (unref(checkboxCaches)[currentPage.value]) {
await unref(tableRef)?.clearCheckboxRow();
const promises = unref(checkboxCaches)[currentPage.value].map(async (row) => await unref(tableRef)?.setCheckboxRow(row, true));
await Promise.all(promises);
}
}
return {
checkboxCaches,
isCheckboxCacheEnabled,
checkboxCacheList,
createCheckboxCache,
createAllCheckboxCache,
deleteCheckboxCache,
deleteAllCheckboxCache,
applyCheckboxCacheByCurrentPage
};
}
export { useCheckboxCache };
//# sourceMappingURL=useCheckboxCache2.mjs.map