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 | 2e63314 | 2022-12-28 00:48:41 +0100 | [diff] [blame] | 3 | import {addButtonToThreadListActions, removeChildNodes, shouldAddBtnToActionBar, softRefreshView} from './utils/common.js'; |
Adrià Vilanova Martínez | 4107b5e | 2022-10-09 23:11:11 +0200 | [diff] [blame] | 4 | |
| 5 | const kLockDebugId = 'twpt-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 | 4107b5e | 2022-10-09 23:11:11 +0200 | [diff] [blame] | 8 | shouldAddButton(node) { |
| 9 | return shouldAddBtnToActionBar(kLockDebugId, 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 | |
Adrià Vilanova Martínez | 2e63314 | 2022-12-28 00:48:41 +0100 | [diff] [blame] | 74 | if (action == 'close') softRefreshView(); |
Adrià Vilanova Martínez | bddf123 | 2021-08-31 23:23:41 +0200 | [diff] [blame] | 75 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 76 | modal.classList.remove('visible'); |
| 77 | modal.style.display = 'none'; |
| 78 | removeChildNodes(modal); |
| 79 | }); |
| 80 | break; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | var content = document.createElement('div'); |
| 84 | content.classList.add('content', 'TWPT-dialog-footer-btn--content'); |
| 85 | content.textContent = |
| 86 | chrome.i18n.getMessage('inject_lockdialog_btn_' + action); |
| 87 | |
| 88 | btn.append(content); |
| 89 | footer.append(btn); |
| 90 | }); |
| 91 | |
| 92 | var clear = document.createElement('div'); |
| 93 | clear.style.clear = 'both'; |
| 94 | |
| 95 | footer.append(clear); |
| 96 | dialog.append(footer); |
| 97 | } |
| 98 | |
| 99 | removeChildNodes(modal); |
| 100 | modal.append(dialog); |
| 101 | modal.classList.add('visible', 'modal'); |
| 102 | modal.style.display = 'flex'; |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 103 | }, |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 104 | addButtonIfEnabled(readToggle) { |
| 105 | isOptionEnabled('batchlock').then(isEnabled => { |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 106 | if (isEnabled) { |
| 107 | let tooltip = chrome.i18n.getMessage('inject_lockbtn'); |
| 108 | let btn = addButtonToThreadListActions( |
Adrià Vilanova Martínez | 4107b5e | 2022-10-09 23:11:11 +0200 | [diff] [blame] | 109 | readToggle, 'lock', kLockDebugId, tooltip); |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 110 | btn.addEventListener('click', () => { |
| 111 | this.createDialog(); |
| 112 | }); |
| 113 | } |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 114 | }); |
| 115 | }, |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 116 | }; |