Adrià Vilanova Martínez | 0d92a0c | 2023-11-06 01:37:20 +0100 | [diff] [blame] | 1 | import {MDCTooltip} from '@material/tooltip'; |
| 2 | |
| 3 | import {shouldImplement} from '../../../../common/commonUtils.js'; |
| 4 | import {createExtBadge} from '../../utils/common.js'; |
| 5 | |
| 6 | export default class BaseExtraInfoInjection { |
| 7 | constructor(infoHandler, optionsWatcher) { |
| 8 | if (this.constructor == BaseExtraInfoInjection) { |
| 9 | throw new Error('The base class cannot be instantiated.'); |
| 10 | } |
| 11 | |
| 12 | this.infoHandler = infoHandler; |
| 13 | this.optionsWatcher = optionsWatcher; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Method which actually injects the extra information. It should be |
| 18 | * implemented by the extending class. |
| 19 | */ |
| 20 | inject() { |
| 21 | shouldImplement('inject'); |
| 22 | } |
| 23 | |
Adrià Vilanova Martínez | 4f5334f | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 24 | /** |
| 25 | * Overridable method which is called when an error ocurred while retrieving |
| 26 | * the info needed to inject the extra information. This is useful to show an |
| 27 | * error component in the screen. |
| 28 | */ |
| 29 | injectOnInfoRetrievalError() {} |
| 30 | |
Adrià Vilanova Martínez | 0d92a0c | 2023-11-06 01:37:20 +0100 | [diff] [blame] | 31 | async isEnabled() { |
| 32 | return await this.optionsWatcher.isEnabled('extrainfo'); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * This is the method which should be called when injecting extra information. |
| 37 | */ |
| 38 | async injectIfEnabled(injectionDetails) { |
| 39 | const isEnabled = await this.isEnabled(); |
| 40 | if (!isEnabled) return; |
| 41 | |
| 42 | return this.infoHandler.getCurrentInfo(injectionDetails) |
Adrià Vilanova Martínez | 4f5334f | 2023-11-15 19:38:50 +0100 | [diff] [blame] | 43 | .catch(err => { |
| 44 | this.injectOnInfoRetrievalError(); |
| 45 | throw err; |
| 46 | }) |
Adrià Vilanova Martínez | 0d92a0c | 2023-11-06 01:37:20 +0100 | [diff] [blame] | 47 | .then(info => this.inject(info, injectionDetails)) |
| 48 | .catch(err => { |
| 49 | console.error( |
| 50 | `${this.constructor.name}: error while injecting extra info: `, |
| 51 | err); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Add chips which contain |chipContentList| to |node|. If |withContainer| is |
| 57 | * set to true, a container will contain all the chips. |
| 58 | */ |
| 59 | addExtraInfoChips(chipContentList, node, withContainer = false) { |
| 60 | if (chipContentList.length == 0) return; |
| 61 | |
| 62 | let container; |
| 63 | if (withContainer) { |
| 64 | container = document.createElement('div'); |
| 65 | container.classList.add('TWPT-extrainfo-container'); |
| 66 | } else { |
| 67 | container = node; |
| 68 | } |
| 69 | |
| 70 | let tooltips = []; |
| 71 | |
| 72 | for (const content of chipContentList) { |
| 73 | const tooltip = this.addChipToContainer(content, container); |
| 74 | tooltips.push(tooltip); |
| 75 | } |
| 76 | |
| 77 | if (withContainer) node.append(container); |
| 78 | |
| 79 | for (const tooltip of tooltips) new MDCTooltip(tooltip); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Adds a chip to the container and returns a tooltip element to be |
| 84 | * instantiated. |
| 85 | */ |
| 86 | addChipToContainer(chipContent, container) { |
Adrià Vilanova Martínez | 4b0f3ea | 2023-11-15 23:59:10 +0100 | [diff] [blame] | 87 | let chip = document.createElement('div'); |
Adrià Vilanova Martínez | 0d92a0c | 2023-11-06 01:37:20 +0100 | [diff] [blame] | 88 | chip.classList.add('TWPT-extrainfo-chip'); |
| 89 | |
Adrià Vilanova Martínez | 0d92a0c | 2023-11-06 01:37:20 +0100 | [diff] [blame] | 90 | const [badge, badgeTooltip] = createExtBadge(); |
| 91 | |
| 92 | let span = document.createElement('span'); |
| 93 | span.append(chipContent); |
| 94 | |
Adrià Vilanova Martínez | 4b0f3ea | 2023-11-15 23:59:10 +0100 | [diff] [blame] | 95 | chip.append(badge, span); |
Adrià Vilanova Martínez | 0d92a0c | 2023-11-06 01:37:20 +0100 | [diff] [blame] | 96 | container.append(chip); |
| 97 | |
| 98 | return badgeTooltip; |
| 99 | } |
| 100 | } |