67 lines
2.0 KiB
JavaScript
67 lines
2.0 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;
|
|
let workMode = 'work';
|
|
|
|
function select(name) {
|
|
Object.entries(buttons).forEach(([key, button]) => {
|
|
if (key === name) button.setAttribute('aria-current', 'page');
|
|
else button.removeAttribute('aria-current');
|
|
});
|
|
}
|
|
|
|
function refreshVisibility() {
|
|
const hidden = overlays.some(overlay => overlay.classList.contains('open'));
|
|
nav.hidden = 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, count) {
|
|
const labels = {
|
|
continue: ['Continue', 'Continue Today'],
|
|
resume: ['Resume', 'Resume Today'],
|
|
start: ['Start', 'Start Today'],
|
|
work: ['Work', 'Work'],
|
|
};
|
|
workMode = labels[mode] ? mode : 'work';
|
|
const total = Math.max(0, Number(count) || 0);
|
|
const badge = options.attentionBadge;
|
|
if (options.workLabel) options.workLabel.textContent = labels[workMode][0];
|
|
if (badge) {
|
|
badge.textContent = String(total);
|
|
badge.hidden = total === 0;
|
|
}
|
|
if (buttons.work) {
|
|
buttons.work.setAttribute(
|
|
'aria-label',
|
|
labels[workMode][1] + (total ? ', ' + total + ' items need attention' : '')
|
|
);
|
|
}
|
|
}
|
|
|
|
function updateAttention(count) {
|
|
updateWork(workMode, count);
|
|
}
|
|
|
|
return {start, refreshVisibility, updateAttention, updateWork};
|
|
});
|