75 lines
2.4 KiB
JavaScript
75 lines
2.4 KiB
JavaScript
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) module.exports = factory;
|
|
else root.createMobileTaskDock = factory;
|
|
})(typeof self !== 'undefined' ? self : this, function createMobileTaskDock(options) {
|
|
const nav = options.nav;
|
|
const buttons = options.buttons;
|
|
const overlays = options.overlays || [];
|
|
let launcher = null;
|
|
let wasHidden = false;
|
|
|
|
function select(name) {
|
|
if (!buttons[name]) return false;
|
|
Object.entries(buttons).forEach(([key, button]) => {
|
|
if (key === name) button.setAttribute('aria-current', 'page');
|
|
else button.removeAttribute('aria-current');
|
|
});
|
|
return true;
|
|
}
|
|
|
|
function refreshVisibility() {
|
|
const hidden = overlays.some(overlay => overlay.classList.contains('open'));
|
|
nav.hidden = hidden;
|
|
if (options.sessionHud) {
|
|
if (hidden) options.sessionHud.setAttribute('data-overlay-hidden', 'true');
|
|
else options.sessionHud.removeAttribute('data-overlay-hidden');
|
|
}
|
|
if (wasHidden && !hidden && launcher) launcher.focus();
|
|
wasHidden = hidden;
|
|
}
|
|
|
|
function start() {
|
|
Object.entries(buttons).forEach(([name, button]) => {
|
|
button.addEventListener('click', event => {
|
|
launcher = event.currentTarget;
|
|
select(name);
|
|
options.actions[name]();
|
|
});
|
|
});
|
|
options.observe(refreshVisibility, overlays);
|
|
refreshVisibility();
|
|
}
|
|
|
|
function updateWork(mode) {
|
|
const labels = {
|
|
continue: ['Continue', 'Continue Today'],
|
|
resume: ['Resume', 'Resume Today'],
|
|
start: ['Start', 'Start Today'],
|
|
plan: ['Plan', 'Plan Today'],
|
|
work: ['Work', 'Work'],
|
|
};
|
|
const workMode = labels[mode] ? mode : 'work';
|
|
if (options.workLabel) options.workLabel.textContent = labels[workMode][0];
|
|
if (buttons.work) {
|
|
buttons.work.setAttribute('aria-label', labels[workMode][1]);
|
|
}
|
|
}
|
|
|
|
function updateAttention(count) {
|
|
const total = Math.max(0, Number(count) || 0);
|
|
const visible = total > 0;
|
|
if (options.attentionBadge) {
|
|
options.attentionBadge.textContent = String(total);
|
|
options.attentionBadge.hidden = !visible;
|
|
}
|
|
if (buttons.attention) {
|
|
buttons.attention.hidden = !visible;
|
|
buttons.attention.setAttribute('aria-label', 'Attention, ' + total + ' items');
|
|
}
|
|
if (visible) nav.setAttribute('data-attention', 'true');
|
|
else nav.removeAttribute('data-attention');
|
|
}
|
|
|
|
return {start, select, refreshVisibility, updateAttention, updateWork};
|
|
});
|