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) { |
| 87 | let chip = document.createElement('material-chip'); |
| 88 | chip.classList.add('TWPT-extrainfo-chip'); |
| 89 | |
| 90 | let chipContentContainer = document.createElement('div'); |
| 91 | chipContentContainer.classList.add('TWPT-chip-content-container'); |
| 92 | |
| 93 | let content = document.createElement('div'); |
| 94 | content.classList.add('TWPT-content'); |
| 95 | |
| 96 | const [badge, badgeTooltip] = createExtBadge(); |
| 97 | |
| 98 | let span = document.createElement('span'); |
| 99 | span.append(chipContent); |
| 100 | |
| 101 | content.append(badge, span); |
| 102 | chipContentContainer.append(content); |
| 103 | chip.append(chipContentContainer); |
| 104 | container.append(chip); |
| 105 | |
| 106 | return badgeTooltip; |
| 107 | } |
| 108 | } |