Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 1 | import {isOptionEnabled} from '../../common/optionsUtils.js'; |
| 2 | |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 3 | import {addButtonToThreadListActions, removeChildNodes, shouldAddBtnToActionBar} from './utils/common.js'; |
| 4 | |
| 5 | const lockDebugId = 'twpt-batch-lock'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 6 | |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 7 | export var batchLock = { |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 8 | shouldAddButton(node) { |
| 9 | return shouldAddBtnToActionBar(lockDebugId, node); |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 10 | }, |
| 11 | createDialog() { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 12 | var modal = document.querySelector('.pane[pane-id="default-1"]'); |
| 13 | |
| 14 | var dialog = document.createElement('material-dialog'); |
| 15 | dialog.setAttribute('role', 'dialog'); |
| 16 | dialog.setAttribute('aria-modal', 'true'); |
| 17 | dialog.classList.add('TWPT-dialog'); |
| 18 | |
| 19 | var header = document.createElement('header'); |
| 20 | header.setAttribute('role', 'presentation'); |
| 21 | header.classList.add('TWPT-dialog-header'); |
| 22 | |
| 23 | var title = document.createElement('div'); |
| 24 | title.classList.add('TWPT-dialog-header--title', 'title'); |
| 25 | title.textContent = chrome.i18n.getMessage('inject_lockbtn'); |
| 26 | |
| 27 | header.append(title); |
| 28 | |
| 29 | var main = document.createElement('main'); |
| 30 | main.setAttribute('role', 'presentation'); |
| 31 | main.classList.add('TWPT-dialog-main'); |
| 32 | |
| 33 | var p = document.createElement('p'); |
| 34 | p.textContent = chrome.i18n.getMessage('inject_lockdialog_desc'); |
| 35 | |
| 36 | main.append(p); |
| 37 | |
| 38 | dialog.append(header, main); |
| 39 | |
Adrià Vilanova Martínez | d473609 | 2021-08-07 23:43:58 +0200 | [diff] [blame] | 40 | var footers = [['lock', 'unlock', 'cancel'], ['close']]; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 41 | |
| 42 | for (var i = 0; i < footers.length; ++i) { |
| 43 | var footer = document.createElement('footer'); |
| 44 | footer.setAttribute('role', 'presentation'); |
| 45 | footer.classList.add('TWPT-dialog-footer'); |
| 46 | footer.setAttribute('data-footer-id', i); |
| 47 | |
| 48 | if (i > 0) footer.classList.add('is-hidden'); |
| 49 | |
| 50 | footers[i].forEach(action => { |
| 51 | var btn = document.createElement('material-button'); |
| 52 | btn.setAttribute('role', 'button'); |
| 53 | btn.classList.add('TWPT-dialog-footer-btn'); |
| 54 | if (i == 1) btn.classList.add('is-disabled'); |
| 55 | |
| 56 | switch (action) { |
| 57 | case 'lock': |
| 58 | case 'unlock': |
| 59 | btn.addEventListener('click', _ => { |
| 60 | if (btn.classList.contains('is-disabled')) return; |
| 61 | var message = { |
| 62 | action, |
| 63 | prefix: 'TWPT-batchlock', |
| 64 | }; |
| 65 | window.postMessage(message, '*'); |
| 66 | }); |
| 67 | break; |
| 68 | |
Adrià Vilanova Martínez | bddf123 | 2021-08-31 23:23:41 +0200 | [diff] [blame] | 69 | case 'cancel': |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 70 | case 'close': |
| 71 | btn.addEventListener('click', _ => { |
| 72 | if (btn.classList.contains('is-disabled')) return; |
Adrià Vilanova Martínez | bddf123 | 2021-08-31 23:23:41 +0200 | [diff] [blame] | 73 | |
| 74 | if (action == 'close') { |
| 75 | var refreshButton = document.querySelector('.app-title-button'); |
| 76 | if (refreshButton == null) |
| 77 | window.location.reload(); |
| 78 | else |
| 79 | refreshButton.click(); |
| 80 | } |
| 81 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 82 | modal.classList.remove('visible'); |
| 83 | modal.style.display = 'none'; |
| 84 | removeChildNodes(modal); |
| 85 | }); |
| 86 | break; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | var content = document.createElement('div'); |
| 90 | content.classList.add('content', 'TWPT-dialog-footer-btn--content'); |
| 91 | content.textContent = |
| 92 | chrome.i18n.getMessage('inject_lockdialog_btn_' + action); |
| 93 | |
| 94 | btn.append(content); |
| 95 | footer.append(btn); |
| 96 | }); |
| 97 | |
| 98 | var clear = document.createElement('div'); |
| 99 | clear.style.clear = 'both'; |
| 100 | |
| 101 | footer.append(clear); |
| 102 | dialog.append(footer); |
| 103 | } |
| 104 | |
| 105 | removeChildNodes(modal); |
| 106 | modal.append(dialog); |
| 107 | modal.classList.add('visible', 'modal'); |
| 108 | modal.style.display = 'flex'; |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 109 | }, |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 110 | addButtonIfEnabled(readToggle) { |
| 111 | isOptionEnabled('batchlock').then(isEnabled => { |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 112 | if (isEnabled) { |
| 113 | let tooltip = chrome.i18n.getMessage('inject_lockbtn'); |
| 114 | let btn = addButtonToThreadListActions( |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 115 | readToggle, 'lock', lockDebugId, tooltip); |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 116 | btn.addEventListener('click', () => { |
| 117 | this.createDialog(); |
| 118 | }); |
| 119 | } |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 120 | }); |
| 121 | }, |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 122 | }; |