blob: 68fb7362edb838fffa35335ffc94d2da554ee20f [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) {
6 while (node.firstChild) {
7 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
34// Adds a button to the thread list actions bar next to the button given by
35// |originalBtn|. The button will have icon |icon|, when hovered it will display
36// |tooltip|, and will have a debugid attribute with value |debugId|.
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010037export function addButtonToThreadListActions(
38 originalBtn, icon, debugId, tooltip) {
Adrià Vilanova Martínez5b3590f2021-12-26 13:05:10 +010039 let clone = originalBtn.cloneNode(true);
40 clone.setAttribute('debugid', debugId);
41 clone.classList.add('TWPT-btn--with-badge');
42 clone.querySelector('material-icon').setAttribute('icon', icon);
43 clone.querySelector('i.material-icon-i').textContent = icon;
44
45 let badge, badgeTooltip;
46 [badge, badgeTooltip] = createExtBadge();
47 clone.append(badge);
48
49 var duplicateBtn =
50 originalBtn.parentNode.querySelector('[debugid="mark-duplicate-button"]');
51 if (duplicateBtn)
52 duplicateBtn.parentNode.insertBefore(
53 clone, (duplicateBtn.nextSibling || duplicateBtn));
54 else
55 originalBtn.parentNode.insertBefore(
56 clone, (originalBtn.nextSibling || originalBtn));
57
58 createPlainTooltip(clone, tooltip);
59 new MDCTooltip(badgeTooltip);
60
61 return clone;
62}
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010063
64// Returns true if |node| is the "mark as read/unread" button, the parent of the
65// parent of |node| is the actions bar of the thread list, and the button with
66// debugid |debugid| is NOT part of the actions bar.
67export function shouldAddBtnToActionBar(debugid, node) {
68 return node?.tagName == 'MATERIAL-BUTTON' &&
69 (node.getAttribute?.('debugid') == 'mark-read-button' ||
70 node.getAttribute?.('debugid') == 'mark-unread-button') &&
71 node.getAttribute?.('debugid') !== null &&
72 node.parentNode?.querySelector('[debugid="' + debugid + '"]') === null &&
73 node.parentNode?.parentNode?.tagName == 'EC-BULK-ACTIONS';
74}