| | |
| | | |
| | | this.user = window.auth.getUser(); |
| | | |
| | | |
| | | this.canUpdateUI = true; |
| | | this.isProcessing = false; |
| | | this.isPolling = false; |
| | |
| | | this.api = jvbSettings.api; |
| | | this.endpoint = 'queue'; |
| | | |
| | | this.queueItems = new Map(); |
| | | |
| | | this.init(); |
| | | } |
| | | init() { |
| | |
| | | this.initElements(); |
| | | this.initListeners(); |
| | | this.initStore(); |
| | | if (this.canUpdateUI) { |
| | | if (this.canUpdateUI && this.ui.panel) { |
| | | this.popup = new window.jvbPopup({ |
| | | popup: this.ui.panel, |
| | | toggle: this.ui.toggle.button, |
| | | name: 'Queue Panel', |
| | | }); |
| | | } |
| | | this.defineTemplates(); |
| | | } |
| | | |
| | | initElements() { |
| | |
| | | if (!this.ui.panel) this.canUpdateUI = false; |
| | | } |
| | | |
| | | defineTemplates() { |
| | | const T = window.jvbTemplates; |
| | | |
| | | T.define('emptyState'); |
| | | T.define('queueItem', { |
| | | setup({el, refs, manyRefs, data}) { |
| | | el.dataset.id = data.id; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | initListeners() { |
| | | this.activityListeners = null; |
| | |
| | | this.updatePanel('offline'); |
| | | } |
| | | handleBeforeUnload(e) { |
| | | if (!this.ui.panel) return; |
| | | const total = this.getQueueByStatus(this.pendingStatuses).length; |
| | | if (total > 0) { |
| | | // Modern browsers ignore custom messages, but this triggers the native dialog |
| | |
| | | if (!window.targetCheck(e, this.selectors.panel+', '+this.selectors.toggle.button)) return; |
| | | const refresh = window.targetCheck(e, this.selectors.refresh.button); |
| | | if (refresh) { |
| | | this.ui.refresh.button.classList.add('fetching'); |
| | | this.store.clearCache(); |
| | | this.store.clearFilters(); |
| | | this.store.fetch(); |
| | | this.store.fetch().finally(() => { |
| | | this.ui.refresh.button.classList.remove('fetching'); |
| | | }); |
| | | return; |
| | | } |
| | | |
| | |
| | | {name: 'status', keyPath: 'status'}, |
| | | {name: 'type', keyPath: 'type'}, |
| | | ], |
| | | filters: { |
| | | user: window.auth.getUser() |
| | | }, |
| | | showLoading: false, |
| | | } |
| | | ) |
| | |
| | | } |
| | | if (statusOrId.length ===0) return; |
| | | if (!['cancel', 'dismiss', 'retry'].includes(action)) return; |
| | | |
| | | const shouldRemove = ['cancel', 'dismiss'].includes(action); |
| | | if (shouldRemove) { |
| | | statusOrId.forEach(id => this.removeOperationUI(id)); |
| | | statusOrId.forEach(id => { |
| | | this.removeOperationUI(id) |
| | | }); |
| | | } |
| | | |
| | | try { |
| | |
| | | } |
| | | statusOrId.forEach(id => { |
| | | let item = this.getQueue(id); |
| | | this.notify(`${action}-operation`, item); |
| | | if (item) { |
| | | this.notify(`${action}-operation`, item); |
| | | } |
| | | |
| | | if (shouldRemove) { |
| | | this.clearQueue(id); |
| | | } else { |
| | |
| | | let requestBody; |
| | | if (operation.data instanceof FormData) { |
| | | operation.data.append('id', operation.id); |
| | | operation.data.append('user', this.user); |
| | | operation.data.append('user', window.auth.getUser()); |
| | | requestBody = operation.data; |
| | | } else { |
| | | requestBody = JSON.stringify({ |
| | | ...operation.data, |
| | | id: operation.id, |
| | | user: this.user |
| | | user: window.auth.getUser() |
| | | }); |
| | | operation.headers['Content-Type'] = 'application/json'; |
| | | } |
| | |
| | | if (!this.isPolling) return; |
| | | |
| | | try { |
| | | this.ui.refresh.button.classList.add('fetching'); |
| | | this.store.clearCache(); |
| | | await this.store.fetch(); |
| | | this.ui.refresh.button.classList.remove('fetching'); |
| | | if (!this.maybeStartPolling()) { |
| | | this.stopPolling(); |
| | | this.updatePanel('synced'); |
| | |
| | | } |
| | | |
| | | startCountdown(count, onComplete) { |
| | | if (!this.ui.refresh.countdown) { |
| | | console.warn('Countdown element not found'); |
| | | return; |
| | | } |
| | | this.ui.refresh.countdown.classList.add('counting'); |
| | | this.ui.refresh.countdown.textContent = count; |
| | | |
| | |
| | | |
| | | if (sortedOps.length === 0) { |
| | | window.removeChildren(this.ui.items.container); |
| | | const empty = window.getTemplate('emptyQueue'); |
| | | const empty = window.jvbTemplates.create('emptyQueue'); |
| | | this.ui.items.container.append(empty); |
| | | this.a11y.announce('No items in queue'); |
| | | return; |
| | |
| | | } |
| | | |
| | | createOperationElement(op) { |
| | | const el = window.getTemplate('queueItem'); |
| | | el.dataset.id = op.id; |
| | | |
| | | const el = window.jvbTemplates.create('queueItem', op); |
| | | const item = { |
| | | element: el, |
| | | ui: window.uiFromSelectors(this.selectors.item, el) |
| | |
| | | } |
| | | |
| | | updatePanel(status = 'syncing') { |
| | | if (!this.panelStatuses.includes(status)) return; |
| | | if (!this.ui.panel || !this.panelStatuses.includes(status)) return; |
| | | this.ui.panel.classList.remove(...this.panelStatuses); |
| | | this.ui.panel.classList.add(status); |
| | | } |
| | |
| | | case 'completed': |
| | | return 'Successfully completed'; |
| | | case 'failed': |
| | | return `Failed: ${item.lastError || 'Unknown error'} (Retry ${item.retries}/${this.config.maxRetries})`; |
| | | return `Failed: ${item.lastError || 'Unknown error'} (Retry ${item.retries}/${2})`; |
| | | case 'failed_permanent': |
| | | return `Failed: ${item.lastError || 'Unknown error'}`; |
| | | default: |
| | |
| | | } |
| | | } |
| | | toggleQueue(on = true) { |
| | | if (!this.ui.panel) return; |
| | | this.ui.panel.hidden = !on; |
| | | this.ui.toggle.button.hidden = !on; |
| | | } |