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