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) { |
Adrià Vilanova Martínez | 08a760b | 2023-02-03 18:47:30 +0100 | [diff] [blame] | 6 | while (node?.firstChild) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 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 | |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 34 | // Adds an element to the thread list actions bar next to the button given by |
| 35 | // |originalBtn|. |
| 36 | export 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ínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 47 | // 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ínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 50 | export function addButtonToThreadListActions( |
| 51 | originalBtn, icon, debugId, tooltip) { |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 52 | 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ínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 62 | addElementToThreadListActions(originalBtn, clone); |
Adrià Vilanova Martínez | 5b3590f | 2021-12-26 13:05:10 +0100 | [diff] [blame] | 63 | |
| 64 | createPlainTooltip(clone, tooltip); |
| 65 | new MDCTooltip(badgeTooltip); |
| 66 | |
| 67 | return clone; |
| 68 | } |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 69 | |
Adrià Vilanova Martínez | 4107b5e | 2022-10-09 23:11:11 +0200 | [diff] [blame] | 70 | // 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. |
| 73 | export 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ínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 81 | // Returns the display language set by the user. |
| 82 | export 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ínez | 2e63314 | 2022-12-28 00:48:41 +0100 | [diff] [blame] | 87 | |
| 88 | // Refreshes the current view in the Community Console without reloading the |
| 89 | // whole page if possible. |
| 90 | export function softRefreshView() { |
| 91 | const refreshButton = document.querySelector('.app-title-button'); |
| 92 | if (refreshButton == null) |
| 93 | window.location.reload(); |
| 94 | else |
| 95 | refreshButton.click(); |
| 96 | } |