blob: 20af6dfca07bede9cf0aa3a15c89a0536a259ece [file] [log] [blame]
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02001import {isOptionEnabled} from '../../common/optionsUtils.js';
2
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02003import {createExtBadge, removeChildNodes} from './utils/common.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02004
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02005export var batchLock = {
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="batchlock"]') === null &&
14 node.parentNode.parentNode !== null &&
15 ('tagName' in node.parentNode.parentNode) &&
16 node.parentNode.parentNode.tagName == 'EC-BULK-ACTIONS';
17 },
18 createDialog() {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020019 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ínezd4736092021-08-07 23:43:58 +020047 var footers = [['lock', 'unlock', 'cancel'], ['close']];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020048
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ínezbddf1232021-08-31 23:23:41 +020076 case 'cancel':
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020077 case 'close':
78 btn.addEventListener('click', _ => {
79 if (btn.classList.contains('is-disabled')) return;
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020080
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ínez3465e772021-07-11 19:18:41 +020089 modal.classList.remove('visible');
90 modal.style.display = 'none';
91 removeChildNodes(modal);
92 });
93 break;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020094 }
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ínez462280f2021-08-07 22:59:02 +0200116 },
117 addButton(readToggle) {
118 var clone = readToggle.cloneNode(true);
119 clone.setAttribute('debugid', 'batchlock');
120 clone.classList.add('TWPT-btn--with-badge');
121 clone.setAttribute('title', chrome.i18n.getMessage('inject_lockbtn'));
122 clone.querySelector('material-icon').setAttribute('icon', 'lock');
123 clone.querySelector('i.material-icon-i').textContent = 'lock';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200124
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200125 var badge = createExtBadge();
126 clone.append(badge);
127
128 clone.addEventListener('click', () => {
129 this.createDialog();
130 });
131
132 var duplicateBtn = readToggle.parentNode.querySelector(
133 '[debugid="mark-duplicate-button"]');
134 if (duplicateBtn)
135 duplicateBtn.parentNode.insertBefore(
136 clone, (duplicateBtn.nextSibling || duplicateBtn));
137 else
138 readToggle.parentNode.insertBefore(
139 clone, (readToggle.nextSibling || readToggle));
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200140 },
141 addButtonIfEnabled(readToggle) {
142 isOptionEnabled('batchlock').then(isEnabled => {
143 if (isEnabled) this.addButton(readToggle);
144 });
145 },
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200146};