Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 1 | import {isOptionEnabled} from '../../../common/optionsUtils.js'; |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 2 | import WorkflowsStorage from '../../../workflows/workflowsStorage.js'; |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 3 | import {addElementToThreadListActions, shouldAddBtnToActionBar} from '../utils/common.js'; |
| 4 | |
| 5 | const wfDebugId = 'twpt-workflows'; |
| 6 | |
| 7 | export default class Workflows { |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 8 | constructor() { |
| 9 | this.menu = null; |
| 10 | this.workflows = null; |
| 11 | |
| 12 | // Always keep the workflows list updated |
| 13 | WorkflowsStorage.watch(workflows => { |
| 14 | this.workflows = workflows; |
| 15 | this._emitWorkflowsUpdateEvent(); |
| 16 | }, /* asProtobuf = */ false); |
| 17 | |
| 18 | // Open the workflow manager when instructed to do so. |
| 19 | document.addEventListener('twpt-open-workflow-manager', () => { |
| 20 | chrome.runtime.sendMessage({ |
| 21 | message: 'openWorkflowsManager', |
| 22 | }); |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | _emitWorkflowsUpdateEvent() { |
| 27 | if (!this.menu) return; |
| 28 | const e = new CustomEvent('twpt-workflows-update', { |
| 29 | detail: { |
| 30 | workflows: this.workflows, |
| 31 | } |
| 32 | }); |
| 33 | this.menu?.dispatchEvent?.(e); |
| 34 | } |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 35 | |
| 36 | addThreadListBtnIfEnabled(readToggle) { |
| 37 | isOptionEnabled('workflows').then(isEnabled => { |
| 38 | if (isEnabled) { |
Adrià Vilanova Martínez | 96ae96f | 2022-10-17 23:50:36 +0200 | [diff] [blame] | 39 | this.menu = document.createElement('twpt-workflows-inject'); |
| 40 | this.menu.setAttribute('debugid', wfDebugId); |
| 41 | this._emitWorkflowsUpdateEvent(); |
| 42 | addElementToThreadListActions(readToggle, this.menu); |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 43 | } |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | shouldAddThreadListBtn(node) { |
| 48 | return shouldAddBtnToActionBar(wfDebugId, node); |
| 49 | } |
| 50 | }; |