blob: ce14d807bc8cd68b11ffde3e64c98581f23febf0 [file] [log] [blame]
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02001import {createExtBadge, removeChildNodes} from './utils/common.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02002
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02003export var batchLock = {
4 nodeIsReadToggleBtn(node) {
5 return ('tagName' in node) && node.tagName == 'MATERIAL-BUTTON' &&
6 node.getAttribute('debugid') !== null &&
7 (node.getAttribute('debugid') == 'mark-read-button' ||
8 node.getAttribute('debugid') == 'mark-unread-button') &&
9 ('parentNode' in node) && node.parentNode !== null &&
10 ('parentNode' in node.parentNode) &&
11 node.parentNode.querySelector('[debugid="batchlock"]') === null &&
12 node.parentNode.parentNode !== null &&
13 ('tagName' in node.parentNode.parentNode) &&
14 node.parentNode.parentNode.tagName == 'EC-BULK-ACTIONS';
15 },
16 createDialog() {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020017 var modal = document.querySelector('.pane[pane-id="default-1"]');
18
19 var dialog = document.createElement('material-dialog');
20 dialog.setAttribute('role', 'dialog');
21 dialog.setAttribute('aria-modal', 'true');
22 dialog.classList.add('TWPT-dialog');
23
24 var header = document.createElement('header');
25 header.setAttribute('role', 'presentation');
26 header.classList.add('TWPT-dialog-header');
27
28 var title = document.createElement('div');
29 title.classList.add('TWPT-dialog-header--title', 'title');
30 title.textContent = chrome.i18n.getMessage('inject_lockbtn');
31
32 header.append(title);
33
34 var main = document.createElement('main');
35 main.setAttribute('role', 'presentation');
36 main.classList.add('TWPT-dialog-main');
37
38 var p = document.createElement('p');
39 p.textContent = chrome.i18n.getMessage('inject_lockdialog_desc');
40
41 main.append(p);
42
43 dialog.append(header, main);
44
Adrià Vilanova Martínezd4736092021-08-07 23:43:58 +020045 var footers = [['lock', 'unlock', 'cancel'], ['close']];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020046
47 for (var i = 0; i < footers.length; ++i) {
48 var footer = document.createElement('footer');
49 footer.setAttribute('role', 'presentation');
50 footer.classList.add('TWPT-dialog-footer');
51 footer.setAttribute('data-footer-id', i);
52
53 if (i > 0) footer.classList.add('is-hidden');
54
55 footers[i].forEach(action => {
56 var btn = document.createElement('material-button');
57 btn.setAttribute('role', 'button');
58 btn.classList.add('TWPT-dialog-footer-btn');
59 if (i == 1) btn.classList.add('is-disabled');
60
61 switch (action) {
62 case 'lock':
63 case 'unlock':
64 btn.addEventListener('click', _ => {
65 if (btn.classList.contains('is-disabled')) return;
66 var message = {
67 action,
68 prefix: 'TWPT-batchlock',
69 };
70 window.postMessage(message, '*');
71 });
72 break;
73
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020074 case 'close':
75 btn.addEventListener('click', _ => {
76 if (btn.classList.contains('is-disabled')) return;
Adrià Vilanova Martínezd4736092021-08-07 23:43:58 +020077 var refreshButton = document.querySelector('.app-title-button');
78 if (refreshButton == null)
79 window.location.reload();
80 else
81 refreshButton.click();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020082 modal.classList.remove('visible');
83 modal.style.display = 'none';
84 removeChildNodes(modal);
85 });
86 break;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020087 }
88
89 var content = document.createElement('div');
90 content.classList.add('content', 'TWPT-dialog-footer-btn--content');
91 content.textContent =
92 chrome.i18n.getMessage('inject_lockdialog_btn_' + action);
93
94 btn.append(content);
95 footer.append(btn);
96 });
97
98 var clear = document.createElement('div');
99 clear.style.clear = 'both';
100
101 footer.append(clear);
102 dialog.append(footer);
103 }
104
105 removeChildNodes(modal);
106 modal.append(dialog);
107 modal.classList.add('visible', 'modal');
108 modal.style.display = 'flex';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200109 },
110 addButton(readToggle) {
111 var clone = readToggle.cloneNode(true);
112 clone.setAttribute('debugid', 'batchlock');
113 clone.classList.add('TWPT-btn--with-badge');
114 clone.setAttribute('title', chrome.i18n.getMessage('inject_lockbtn'));
115 clone.querySelector('material-icon').setAttribute('icon', 'lock');
116 clone.querySelector('i.material-icon-i').textContent = 'lock';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200117
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200118 var badge = createExtBadge();
119 clone.append(badge);
120
121 clone.addEventListener('click', () => {
122 this.createDialog();
123 });
124
125 var duplicateBtn = readToggle.parentNode.querySelector(
126 '[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 }
134};