blob: 78ded47c0e893d2e65f49e7da373cc370368e57d [file] [log] [blame]
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +01001import {MDCTooltip} from '@material/tooltip';
2
3import {shouldImplement} from '../../../../common/commonUtils.js';
4import {createExtBadge} from '../../utils/common.js';
5
6export 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ínez4f5334f2023-11-15 19:38:50 +010024 /**
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ínez0d92a0c2023-11-06 01:37:20 +010031 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ínez4f5334f2023-11-15 19:38:50 +010043 .catch(err => {
44 this.injectOnInfoRetrievalError();
45 throw err;
46 })
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +010047 .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ínez4b0f3ea2023-11-15 23:59:10 +010087 let chip = document.createElement('div');
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +010088 chip.classList.add('TWPT-extrainfo-chip');
89
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +010090 const [badge, badgeTooltip] = createExtBadge();
91
92 let span = document.createElement('span');
93 span.append(chipContent);
94
Adrià Vilanova Martínez4b0f3ea2023-11-15 23:59:10 +010095 chip.append(badge, span);
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +010096 container.append(chip);
97
98 return badgeTooltip;
99 }
100}