blob: 29a3dc3d9c2272ce197d7f790a2068637b8cdeb6 [file] [log] [blame]
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02001import {isOptionEnabled} from '../../common/optionsUtils.js';
2
Adrià Vilanova Martínez2e633142022-12-28 00:48:41 +01003import {addButtonToThreadListActions, removeChildNodes, shouldAddBtnToActionBar, softRefreshView} from './utils/common.js';
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +02004
5const kLockDebugId = 'twpt-lock';
Adrià Vilanova Martínez08a760b2023-02-03 18:47:30 +01006export const kModalPaneSelector = '.pane[pane-id="default--1"]';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02007
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02008export var batchLock = {
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +02009 shouldAddButton(node) {
10 return shouldAddBtnToActionBar(kLockDebugId, node);
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +020011 },
12 createDialog() {
Adrià Vilanova Martínez08a760b2023-02-03 18:47:30 +010013 var modal = document.querySelector(kModalPaneSelector);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020014
15 var dialog = document.createElement('material-dialog');
16 dialog.setAttribute('role', 'dialog');
17 dialog.setAttribute('aria-modal', 'true');
18 dialog.classList.add('TWPT-dialog');
19
20 var header = document.createElement('header');
21 header.setAttribute('role', 'presentation');
22 header.classList.add('TWPT-dialog-header');
23
24 var title = document.createElement('div');
25 title.classList.add('TWPT-dialog-header--title', 'title');
26 title.textContent = chrome.i18n.getMessage('inject_lockbtn');
27
28 header.append(title);
29
30 var main = document.createElement('main');
31 main.setAttribute('role', 'presentation');
32 main.classList.add('TWPT-dialog-main');
33
34 var p = document.createElement('p');
35 p.textContent = chrome.i18n.getMessage('inject_lockdialog_desc');
36
37 main.append(p);
38
39 dialog.append(header, main);
40
Adrià Vilanova Martínezd4736092021-08-07 23:43:58 +020041 var footers = [['lock', 'unlock', 'cancel'], ['close']];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020042
43 for (var i = 0; i < footers.length; ++i) {
44 var footer = document.createElement('footer');
45 footer.setAttribute('role', 'presentation');
46 footer.classList.add('TWPT-dialog-footer');
47 footer.setAttribute('data-footer-id', i);
48
49 if (i > 0) footer.classList.add('is-hidden');
50
51 footers[i].forEach(action => {
52 var btn = document.createElement('material-button');
53 btn.setAttribute('role', 'button');
54 btn.classList.add('TWPT-dialog-footer-btn');
55 if (i == 1) btn.classList.add('is-disabled');
56
57 switch (action) {
58 case 'lock':
59 case 'unlock':
60 btn.addEventListener('click', _ => {
61 if (btn.classList.contains('is-disabled')) return;
62 var message = {
63 action,
64 prefix: 'TWPT-batchlock',
65 };
66 window.postMessage(message, '*');
67 });
68 break;
69
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020070 case 'cancel':
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020071 case 'close':
72 btn.addEventListener('click', _ => {
73 if (btn.classList.contains('is-disabled')) return;
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020074
Adrià Vilanova Martínez2e633142022-12-28 00:48:41 +010075 if (action == 'close') softRefreshView();
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020076
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020077 modal.classList.remove('visible');
78 modal.style.display = 'none';
79 removeChildNodes(modal);
80 });
81 break;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020082 }
83
84 var content = document.createElement('div');
85 content.classList.add('content', 'TWPT-dialog-footer-btn--content');
86 content.textContent =
87 chrome.i18n.getMessage('inject_lockdialog_btn_' + action);
88
89 btn.append(content);
90 footer.append(btn);
91 });
92
93 var clear = document.createElement('div');
94 clear.style.clear = 'both';
95
96 footer.append(clear);
97 dialog.append(footer);
98 }
99
100 removeChildNodes(modal);
101 modal.append(dialog);
102 modal.classList.add('visible', 'modal');
103 modal.style.display = 'flex';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200104 },
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200105 addButtonIfEnabled(readToggle) {
106 isOptionEnabled('batchlock').then(isEnabled => {
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100107 if (isEnabled) {
108 let tooltip = chrome.i18n.getMessage('inject_lockbtn');
109 let btn = addButtonToThreadListActions(
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +0200110 readToggle, 'lock', kLockDebugId, tooltip);
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100111 btn.addEventListener('click', () => {
112 this.createDialog();
113 });
114 }
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200115 });
116 },
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200117};