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