61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
(function (root, factory) {
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
else root.createTaskOverlayHistory = api;
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
|
|
'use strict';
|
|
|
|
const allowed = new Set(['new', 'find', 'search', 'search-preview', 'plan-today', 'plan-today-preview']);
|
|
|
|
return function createTaskOverlayHistory({ history, eventTarget, onChange }) {
|
|
let active = allowed.has(history.state?.taskOverlay) ? history.state.taskOverlay : null;
|
|
let started = false;
|
|
|
|
function stateKind(state = history.state) {
|
|
return allowed.has(state?.taskOverlay) ? state.taskOverlay : null;
|
|
}
|
|
|
|
function apply(state) {
|
|
const next = stateKind(state);
|
|
if (next === active) return;
|
|
const previous = active;
|
|
active = next;
|
|
onChange(next, previous);
|
|
}
|
|
|
|
return {
|
|
start() {
|
|
if (started) return;
|
|
started = true;
|
|
eventTarget.addEventListener('popstate', event => apply(event.state));
|
|
},
|
|
open(kind) {
|
|
if (!allowed.has(kind)) return false;
|
|
if (active === kind) return true;
|
|
const previous = active;
|
|
const state = { ...(history.state || {}), taskOverlay: kind };
|
|
history.pushState(state, '');
|
|
active = kind;
|
|
onChange(kind, previous);
|
|
return true;
|
|
},
|
|
close() {
|
|
if (!active) return false;
|
|
history.back();
|
|
return true;
|
|
},
|
|
leave() {
|
|
if (!active) return false;
|
|
const previous = active;
|
|
const state = { ...(history.state || {}) };
|
|
delete state.taskOverlay;
|
|
history.replaceState(state, '');
|
|
active = null;
|
|
onChange(null, previous);
|
|
return true;
|
|
},
|
|
current() { return active; },
|
|
};
|
|
};
|
|
});
|