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