blob: 11b133a1903bc3cbfd38c892ab354381f5530ed1 [file] [log] [blame]
avm999632485a3e2021-09-08 22:18:38 +02001import {MDCTooltip} from '@material/tooltip';
2
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02003import {isOptionEnabled} from '../../common/optionsUtils.js';
avm999632485a3e2021-09-08 22:18:38 +02004import {createPlainTooltip} from '../../common/tooltip.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02005
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02006import {createExtBadge, removeChildNodes} from './utils/common.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02007
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02008export 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ínez3465e772021-07-11 19:18:41 +020022 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ínezd4736092021-08-07 23:43:58 +020050 var footers = [['lock', 'unlock', 'cancel'], ['close']];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020051
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ínezbddf1232021-08-31 23:23:41 +020079 case 'cancel':
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020080 case 'close':
81 btn.addEventListener('click', _ => {
82 if (btn.classList.contains('is-disabled')) return;
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020083
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ínez3465e772021-07-11 19:18:41 +020092 modal.classList.remove('visible');
93 modal.style.display = 'none';
94 removeChildNodes(modal);
95 });
96 break;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020097 }
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ínez462280f2021-08-07 22:59:02 +0200119 },
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ínez462280f2021-08-07 22:59:02 +0200124 clone.querySelector('material-icon').setAttribute('icon', 'lock');
125 clone.querySelector('i.material-icon-i').textContent = 'lock';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200126
avm999632485a3e2021-09-08 22:18:38 +0200127 let badge, badgeTooltip;
128 [badge, badgeTooltip] = createExtBadge();
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200129 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));
avm999632485a3e2021-09-08 22:18:38 +0200143
144 createPlainTooltip(clone, chrome.i18n.getMessage('inject_lockbtn'));
145 new MDCTooltip(badgeTooltip);
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200146 },
147 addButtonIfEnabled(readToggle) {
148 isOptionEnabled('batchlock').then(isEnabled => {
149 if (isEnabled) this.addButton(readToggle);
150 });
151 },
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200152};