|
| 1 | +<!-- |
| 2 | + Matomo - free/libre analytics platform |
| 3 | +
|
| 4 | + @link https://matomo.org |
| 5 | + @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 6 | +--> |
| 7 | + |
| 8 | +<template> |
| 9 | + <ActivityIndicator |
| 10 | + v-if="loading" |
| 11 | + :loading="true" |
| 12 | + :loading-message="translate('General_LoadingData')" |
| 13 | + /> |
| 14 | + <Alert |
| 15 | + v-else-if="loadingFailed" |
| 16 | + severity="danger" |
| 17 | + > |
| 18 | + {{ translate('General_ErrorRequest', '', '') }} |
| 19 | + </Alert> |
| 20 | + <component |
| 21 | + v-else-if="componentToRender" |
| 22 | + :is="componentToRender" |
| 23 | + v-bind="componentProps" |
| 24 | + /> |
| 25 | +</template> |
| 26 | + |
| 27 | +<script lang="ts"> |
| 28 | +import { |
| 29 | + Component, |
| 30 | + defineComponent, |
| 31 | + markRaw, |
| 32 | +} from 'vue'; |
| 33 | +import ActivityIndicator from '../ActivityIndicator/ActivityIndicator.vue'; |
| 34 | +import Alert from '../Alert/Alert.vue'; |
| 35 | +import importPluginUmd from '../importPluginUmd'; |
| 36 | +import { Widget as WidgetData } from './types'; |
| 37 | +
|
| 38 | +interface ClientWidgetRendererState { |
| 39 | + componentToRender: Component|null; |
| 40 | + loading: boolean; |
| 41 | + loadingFailed: boolean; |
| 42 | +} |
| 43 | +
|
| 44 | +export default defineComponent({ |
| 45 | + props: { |
| 46 | + widget: { |
| 47 | + type: Object, |
| 48 | + required: true, |
| 49 | + }, |
| 50 | + widgetized: Boolean, |
| 51 | + }, |
| 52 | + components: { |
| 53 | + ActivityIndicator, |
| 54 | + Alert, |
| 55 | + }, |
| 56 | + data(): ClientWidgetRendererState { |
| 57 | + return { |
| 58 | + componentToRender: null, |
| 59 | + loading: false, |
| 60 | + loadingFailed: false, |
| 61 | + }; |
| 62 | + }, |
| 63 | + watch: { |
| 64 | + widget: { |
| 65 | + handler() { |
| 66 | + this.loadComponent(); |
| 67 | + }, |
| 68 | + immediate: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + computed: { |
| 72 | + componentProps() { |
| 73 | + const widget = this.widget as WidgetData; |
| 74 | + return { |
| 75 | + ...(widget.clientComponent?.props || {}), |
| 76 | + uniqueId: widget.uniqueId, |
| 77 | + widgetName: widget.name, |
| 78 | + widgetized: this.widgetized, |
| 79 | + isWidget: this.widgetized, |
| 80 | + isWide: widget.isWide, |
| 81 | + }; |
| 82 | + }, |
| 83 | + }, |
| 84 | + methods: { |
| 85 | + async loadComponent() { |
| 86 | + const widget = this.widget as WidgetData; |
| 87 | + const { clientComponent } = widget; |
| 88 | + this.loading = true; |
| 89 | + this.loadingFailed = false; |
| 90 | + this.componentToRender = null; |
| 91 | +
|
| 92 | + try { |
| 93 | + if (!clientComponent) { |
| 94 | + throw new Error('Missing client-rendered widget metadata'); |
| 95 | + } |
| 96 | +
|
| 97 | + const pluginModule = await importPluginUmd( |
| 98 | + clientComponent.plugin, |
| 99 | + ) as Record<string, unknown>|undefined; |
| 100 | + const component = pluginModule?.[clientComponent.name]; |
| 101 | +
|
| 102 | + if (!component) { |
| 103 | + throw new Error( |
| 104 | + `Unknown widget component ${clientComponent.plugin}.${clientComponent.name}`, |
| 105 | + ); |
| 106 | + } |
| 107 | +
|
| 108 | + this.componentToRender = markRaw(component as Component); |
| 109 | + } catch (e) { |
| 110 | + this.loadingFailed = true; |
| 111 | + } finally { |
| 112 | + this.loading = false; |
| 113 | + } |
| 114 | + }, |
| 115 | + }, |
| 116 | +}); |
| 117 | +</script> |
0 commit comments