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