blob: 87edc6d2c4db796a5776e954123a96c4ef57f711 [file] [log] [blame]
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02001import {isOptionEnabled} from '../../common/optionsUtils.js';
2
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +01003import {addButtonToThreadListActions, removeChildNodes, shouldAddBtnToActionBar} from './utils/common.js';
4
5const lockDebugId = 'twpt-batch-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ínez1e10d192021-12-31 16:01:13 +01008 shouldAddButton(node) {
9 return shouldAddBtnToActionBar(lockDebugId, 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
74 if (action == 'close') {
75 var refreshButton = document.querySelector('.app-title-button');
76 if (refreshButton == null)
77 window.location.reload();
78 else
79 refreshButton.click();
80 }
81
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 },
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200110 addButtonIfEnabled(readToggle) {
111 isOptionEnabled('batchlock').then(isEnabled => {
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100112 if (isEnabled) {
113 let tooltip = chrome.i18n.getMessage('inject_lockbtn');
114 let btn = addButtonToThreadListActions(
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100115 readToggle, 'lock', lockDebugId, tooltip);
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +0100116 btn.addEventListener('click', () => {
117 this.createDialog();
118 });
119 }
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200120 });
121 },
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +0200122};