blob: 83af3ce52fa25583461b3e7333b81fb27a81419e [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ínez3465e772021-07-11 19:18:41 +02006
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02007export var batchLock = {
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +02008 shouldAddButton(node) {
9 return shouldAddBtnToActionBar(kLockDebugId, node);
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +020010 },
11 createDialog() {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020012 var modal = document.querySelector('.pane[pane-id="default-1"]');
13
14 var dialog = document.createElement('material-dialog');
15 dialog.setAttribute('role', 'dialog');
16 dialog.setAttribute('aria-modal', 'true');
17 dialog.classList.add('TWPT-dialog');
18
19 var header = document.createElement('header');
20 header.setAttribute('role', 'presentation');
21 header.classList.add('TWPT-dialog-header');
22
23 var title = document.createElement('div');
24 title.classList.add('TWPT-dialog-header--title', 'title');
25 title.textContent = chrome.i18n.getMessage('inject_lockbtn');
26
27 header.append(title);
28
29 var main = document.createElement('main');
30 main.setAttribute('role', 'presentation');
31 main.classList.add('TWPT-dialog-main');
32
33 var p = document.createElement('p');
34 p.textContent = chrome.i18n.getMessage('inject_lockdialog_desc');
35
36 main.append(p);
37
38 dialog.append(header, main);
39
Adrià Vilanova Martínezd4736092021-08-07 23:43:58 +020040 var footers = [['lock', 'unlock', 'cancel'], ['close']];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020041
42 for (var i = 0; i < footers.length; ++i) {
43 var footer = document.createElement('footer');
44 footer.setAttribute('role', 'presentation');
45 footer.classList.add('TWPT-dialog-footer');
46 footer.setAttribute('data-footer-id', i);
47
48 if (i > 0) footer.classList.add('is-hidden');
49
50 footers[i].forEach(action => {
51 var btn = document.createElement('material-button');
52 btn.setAttribute('role', 'button');
53 btn.classList.add('TWPT-dialog-footer-btn');
54 if (i == 1) btn.classList.add('is-disabled');
55
56 switch (action) {
57 case 'lock':
58 case 'unlock':
59 btn.addEventListener('click', _ => {
60 if (btn.classList.contains('is-disabled')) return;
61 var message = {
62 action,
63 prefix: 'TWPT-batchlock',
64 };
65 window.postMessage(message, '*');
66 });
67 break;
68
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020069 case 'cancel':
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020070 case 'close':
71 btn.addEventListener('click', _ => {
72 if (btn.classList.contains('is-disabled')) return;
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020073
Adrià Vilanova Martínez2e633142022-12-28 00:48:41 +010074 if (action == 'close') softRefreshView();
Adrià Vilanova Martínezbddf1232021-08-31 23:23:41 +020075
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020076 modal.classList.remove('visible');
77 modal.style.display = 'none';
78 removeChildNodes(modal);
79 });
80 break;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020081 }
82
83 var content = document.createElement('div');
84 content.classList.add('content', 'TWPT-dialog-footer-btn--content');
85 content.textContent =
86 chrome.i18n.getMessage('inject_lockdialog_btn_' + action);
87
88 btn.append(content);
89 footer.append(btn);
90 });
91
92 var clear = document.createElement('div');
93 clear.style.clear = 'both';
94
95 footer.append(clear);
96 dialog.append(footer);
97 }
98
99 removeChildNodes(modal);
100 modal.append(dialog);
101 modal.classList.add('visible', 'modal');
102 modal.style.display = 'flex';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200103 },
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200104 addButtonIfEnabled(readToggle) {
105 isOptionEnabled('batchlock').then(isEnabled => {
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100106 if (isEnabled) {
107 let tooltip = chrome.i18n.getMessage('inject_lockbtn');
108 let btn = addButtonToThreadListActions(
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +0200109 readToggle, 'lock', kLockDebugId, tooltip);
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100110 btn.addEventListener('click', () => {
111 this.createDialog();
112 });
113 }
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200114 });
115 },
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200116};