Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 1 | import {MDCTooltip} from '@material/tooltip'; |
| 2 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 3 | import {createPlainTooltip} from '../../../common/tooltip.js'; |
| 4 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 5 | export function removeChildNodes(node) { |
| 6 | while (node.firstChild) { |
| 7 | node.removeChild(node.firstChild); |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | export 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 | |
| 17 | export function createExtBadge() { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 18 | let badge = document.createElement('div'); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 19 | badge.classList.add('TWPT-badge'); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 20 | let badgeTooltip = createPlainTooltip( |
| 21 | badge, |
| 22 | chrome.i18n.getMessage( |
| 23 | 'inject_extension_badge_helper', [chrome.i18n.getMessage('appName')]), |
| 24 | false); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 25 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 26 | let badgeI = document.createElement('i'); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 27 | badgeI.classList.add('material-icon-i', 'material-icons-extended'); |
| 28 | badgeI.textContent = 'repeat'; |
| 29 | |
| 30 | badge.append(badgeI); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 31 | return [badge, badgeTooltip]; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 32 | } |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 33 | |
| 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ínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 37 | export function addButtonToThreadListActions( |
| 38 | originalBtn, icon, debugId, tooltip) { |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 39 | 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ínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 63 | |
| 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. |
| 67 | export 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 | } |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 75 | |
| 76 | // Returns the display language set by the user. |
| 77 | export function getDisplayLanguage() { |
| 78 | var startup = |
| 79 | JSON.parse(document.querySelector('html').getAttribute('data-startup')); |
| 80 | return startup?.[1]?.[1]?.[3]?.[6] ?? 'en'; |
| 81 | } |