import BasicWidget from "./basic_widget.js"; import toastService from "../services/toast.js"; import ws from "../services/ws.js"; import options from "../services/options.js"; import syncService from "../services/sync.js"; const TPL = `
`; export default class SyncStatusWidget extends BasicWidget { constructor() { super(); ws.subscribeToMessages(message => this.processMessage(message)); this.syncState = 'disconnected'; this.allChangesPushed = false; } doRender() { this.$widget = $(TPL); this.$widget.hide(); this.$widget.find('[data-toggle="tooltip"]').tooltip({ html: true }); this.$widget.find('.sync-status-icon:not(.sync-status-in-progress)') .on('click', () => syncService.syncNow()) this.overflowing(); } showIcon(className) { if (!options.get('syncServerHost')) { this.$widget.hide(); return; } this.$widget.show(); this.$widget.find('.sync-status-icon').hide(); this.$widget.find('.sync-status-' + className).show(); } processMessage(message) { if (message.type === 'sync-pull-in-progress') { toastService.showPersistent({ id: 'sync', title: "Sync status", message: "Sync update in progress", icon: "refresh" }); this.syncState = 'in-progress'; this.allChangesPushed = false; } else if (message.type === 'sync-push-in-progress') { this.syncState = 'in-progress'; this.allChangesPushed = false; } else if (message.type === 'sync-finished') { // this gives user a chance to see the toast in case of fast sync finish setTimeout(() => toastService.closePersistent('sync'), 1000); this.syncState = 'connected'; } else if (message.type === 'sync-failed') { this.syncState = 'disconnected'; } else if (message.type === 'frontend-update') { const {lastSyncedPush} = message.data; this.allChangesPushed = lastSyncedPush === ws.getMaxKnownEntityChangeSyncId(); } if (this.syncState === 'in-progress') { this.showIcon('in-progress'); } else { this.showIcon(this.syncState + '-' + (this.allChangesPushed ? 'no-changes' : 'with-changes')); } } }