46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
(function (root, factory) {
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
else root.createPlanTodayPreview = api;
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
|
|
'use strict';
|
|
|
|
return function createPlanTodayPreview({
|
|
planner,
|
|
identity,
|
|
getScroll = () => 0,
|
|
setScroll = () => {},
|
|
onOpen = () => {},
|
|
onClose = () => {},
|
|
}) {
|
|
let current = null;
|
|
|
|
function open(item, trigger = null) {
|
|
if (!item || !identity?.(item) || !planner?.snapshot().open) return false;
|
|
current = { item, trigger, scroll:Number(getScroll()) || 0 };
|
|
onOpen(item, trigger);
|
|
return true;
|
|
}
|
|
|
|
function close({ add = false } = {}) {
|
|
if (!current) return 'closed';
|
|
const { item, trigger, scroll } = current;
|
|
let result = 'returned';
|
|
if (add) {
|
|
const id = identity(item);
|
|
result = planner.snapshot().ids.includes(id) ? 'already-added' : planner.toggle(item);
|
|
}
|
|
current = null;
|
|
setScroll(scroll);
|
|
onClose(item, trigger);
|
|
return result;
|
|
}
|
|
|
|
function snapshot() {
|
|
return current ? { open:true, item:current.item, trigger:current.trigger, scroll:current.scroll } : { open:false };
|
|
}
|
|
|
|
return { open, close, snapshot };
|
|
};
|
|
});
|