96 lines
3.5 KiB
JavaScript
96 lines
3.5 KiB
JavaScript
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) module.exports = factory;
|
|
else root.createMobileFirstTask = factory;
|
|
})(typeof self !== 'undefined' ? self : this, function createMobileFirstTask(options) {
|
|
const doc = typeof document !== 'undefined' ? document : null;
|
|
const win = typeof window !== 'undefined' ? window : null;
|
|
options = Object.assign({
|
|
storage: typeof localStorage !== 'undefined' ? localStorage : null,
|
|
isOnline: () => typeof navigator === 'undefined' || navigator.onLine,
|
|
mediaQuery: win?.matchMedia('(max-width: 600px)') || {matches:false},
|
|
eventTarget: win,
|
|
sheet: doc?.querySelector('#mobile-first-task'),
|
|
findButton: doc?.querySelector('#mobile-first-task-find'),
|
|
createButton: doc?.querySelector('#mobile-first-task-create'),
|
|
setupButton: doc?.querySelector('#mobile-first-task-setup'),
|
|
closeButton: doc?.querySelector('#close-mobile-first-task'),
|
|
status: doc?.querySelector('#mobile-first-task-status'),
|
|
onFind: () => doc?.querySelector('#find-work')?.click(),
|
|
onCreate: () => doc?.querySelector('#new-issue')?.click(),
|
|
onSetup: () => doc?.querySelector('#open-device-setup')?.click(),
|
|
}, options);
|
|
const prefix = 'stackchain.first-task.v1:';
|
|
|
|
function account() {
|
|
return String(options.getLogin?.() || '').trim().toLowerCase();
|
|
}
|
|
|
|
function key() {
|
|
const login = account();
|
|
return login ? prefix + login : '';
|
|
}
|
|
|
|
function completed() {
|
|
const currentKey = key();
|
|
if (!currentKey) return false;
|
|
try { return options.storage.getItem(currentKey) === 'complete'; }
|
|
catch (_error) { return false; }
|
|
}
|
|
|
|
function required() {
|
|
return Boolean(options.mediaQuery.matches && account() && !options.hasWork() && !completed());
|
|
}
|
|
|
|
function markComplete() {
|
|
const currentKey = key();
|
|
if (!currentKey) return;
|
|
try { options.storage.setItem(currentKey, 'complete'); }
|
|
catch (_error) {}
|
|
}
|
|
|
|
function render() {
|
|
const online = options.isOnline();
|
|
options.findButton.disabled = !online;
|
|
options.status.textContent = online ?
|
|
'Choose a task to claim or create one of your own.' :
|
|
'You are offline. Create a task now and it will stay in Drafts until you reconnect.';
|
|
}
|
|
|
|
function open() {
|
|
if (!required()) return false;
|
|
render();
|
|
if (!options.sheet.open) options.sheet.showModal();
|
|
(options.findButton.disabled ? options.createButton : options.findButton).focus();
|
|
return true;
|
|
}
|
|
|
|
function refresh() {
|
|
if (account() && options.hasWork() && !completed()) {
|
|
markComplete();
|
|
if (options.sheet.open) options.sheet.close();
|
|
return 'completed';
|
|
}
|
|
if (options.sheet.open) render();
|
|
return required() ? 'required' : 'inactive';
|
|
}
|
|
|
|
function handoff(callback, requiresOnline = false) {
|
|
if (requiresOnline && !options.isOnline()) return;
|
|
if (options.sheet.open) options.sheet.close();
|
|
callback();
|
|
}
|
|
|
|
function start() {
|
|
options.findButton.addEventListener('click', () => handoff(options.onFind, true));
|
|
options.createButton.addEventListener('click', () => handoff(options.onCreate));
|
|
options.setupButton.addEventListener('click', () => handoff(options.onSetup));
|
|
options.closeButton.addEventListener('click', () => {
|
|
if (options.sheet.open) options.sheet.close();
|
|
});
|
|
options.eventTarget?.addEventListener('online', render);
|
|
options.eventTarget?.addEventListener('offline', render);
|
|
}
|
|
|
|
return {required, open, refresh, render, start};
|
|
});
|