blob: 3d2bac547b7e7c139ed6b9d601c407c65db8bc9e [file] [log] [blame]
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +01001import {MDCTooltip} from '@material/tooltip';
2
avm999632485a3e2021-09-08 22:18:38 +02003import {createPlainTooltip} from '../../../common/tooltip.js';
4
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02005export function removeChildNodes(node) {
Adrià Vilanova Martínez08a760b2023-02-03 18:47:30 +01006 while (node?.firstChild) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02007 node.removeChild(node.firstChild);
8 }
9}
10
11export function getNParent(node, n) {
12 if (n <= 0) return node;
13 if (!('parentNode' in node)) return null;
14 return getNParent(node.parentNode, n - 1);
15}
16
17export function createExtBadge() {
avm999632485a3e2021-09-08 22:18:38 +020018 let badge = document.createElement('div');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020019 badge.classList.add('TWPT-badge');
avm999632485a3e2021-09-08 22:18:38 +020020 let badgeTooltip = createPlainTooltip(
21 badge,
22 chrome.i18n.getMessage(
23 'inject_extension_badge_helper', [chrome.i18n.getMessage('appName')]),
24 false);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020025
avm999632485a3e2021-09-08 22:18:38 +020026 let badgeI = document.createElement('i');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020027 badgeI.classList.add('material-icon-i', 'material-icons-extended');
28 badgeI.textContent = 'repeat';
29
30 badge.append(badgeI);
avm999632485a3e2021-09-08 22:18:38 +020031 return [badge, badgeTooltip];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020032}
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +010033
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020034// Adds an element to the thread list actions bar next to the button given by
35// |originalBtn|.
36export function addElementToThreadListActions(originalBtn, element) {
37 var duplicateBtn =
38 originalBtn.parentNode.querySelector('[debugid="mark-duplicate-button"]');
39 if (duplicateBtn)
40 duplicateBtn.parentNode.insertBefore(
41 element, (duplicateBtn.nextSibling || duplicateBtn));
42 else
43 originalBtn.parentNode.insertBefore(
44 element, (originalBtn.nextSibling || originalBtn));
45}
46
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +010047// Adds a button to the thread list actions bar next to the button given by
48// |originalBtn|. The button will have icon |icon|, when hovered it will display
49// |tooltip|, and will have a debugid attribute with value |debugId|.
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010050export function addButtonToThreadListActions(
51 originalBtn, icon, debugId, tooltip) {
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +010052 let clone = originalBtn.cloneNode(true);
53 clone.setAttribute('debugid', debugId);
54 clone.classList.add('TWPT-btn--with-badge');
55 clone.querySelector('material-icon').setAttribute('icon', icon);
56 clone.querySelector('i.material-icon-i').textContent = icon;
57
58 let badge, badgeTooltip;
59 [badge, badgeTooltip] = createExtBadge();
60 clone.append(badge);
61
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020062 addElementToThreadListActions(originalBtn, clone);
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +010063
64 createPlainTooltip(clone, tooltip);
65 new MDCTooltip(badgeTooltip);
66
67 return clone;
68}
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010069
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +020070// Returns true if |node| is the "mark as read/unread" button, the parent of the
71// parent of |node| is the actions bar of the thread list, and the button with
72// debugid |debugid| is NOT part of the actions bar.
73export function shouldAddBtnToActionBar(debugid, node) {
74 return node?.tagName == 'MATERIAL-BUTTON' &&
75 (node.getAttribute?.('debugid') == 'mark-read-button' ||
76 node.getAttribute?.('debugid') == 'mark-unread-button') &&
77 node.parentNode?.querySelector('[debugid="' + debugid + '"]') === null &&
78 node.parentNode?.parentNode?.tagName == 'EC-BULK-ACTIONS';
79}
80
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010081// Returns the display language set by the user.
82export function getDisplayLanguage() {
83 var startup =
84 JSON.parse(document.querySelector('html').getAttribute('data-startup'));
85 return startup?.[1]?.[1]?.[3]?.[6] ?? 'en';
86}
Adrià Vilanova Martínez2e633142022-12-28 00:48:41 +010087
88// Refreshes the current view in the Community Console without reloading the
89// whole page if possible.
90export function softRefreshView() {
91 const refreshButton = document.querySelector('.app-title-button');
92 if (refreshButton == null)
93 window.location.reload();
94 else
95 refreshButton.click();
96}