blob: 86a70577e37622499297c2b9a3daefb9cbd9bba1 [file] [log] [blame]
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02001import {isOptionEnabled} from '../../common/optionsUtils.js';
2
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +00003import {addButtonToThreadListActions, 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 = {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +00006 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="twpt-lock"]') === null &&
14 node.parentNode.parentNode !== null &&
15 ('tagName' in node.parentNode.parentNode) &&
16 node.parentNode.parentNode.tagName == 'EC-BULK-ACTIONS';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +020017 },
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 },
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200117 addButtonIfEnabled(readToggle) {
118 isOptionEnabled('batchlock').then(isEnabled => {
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100119 if (isEnabled) {
120 let tooltip = chrome.i18n.getMessage('inject_lockbtn');
121 let btn = addButtonToThreadListActions(
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000122 readToggle, 'lock', 'twpt-lock', tooltip);
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100123 btn.addEventListener('click', () => {
124 this.createDialog();
125 });
126 }
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200127 });
128 },
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200129};