1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<!--
Matomo - free/libre analytics platform
@link https://matomo.org
@license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
-->
<template>
<div>
<ActivityIndicator
:loading-message="loadingMessage"
:loading="loading"
/>
<div v-show="loadingFailed">
<h2 v-if="widgetName">{{ widgetName }}</h2>
<div class="notification system notification-error">
{{ translate('General_ErrorRequest', '', '') }}
<a
rel="noreferrer noopener"
target="_blank"
href="https://matomo.org/faq/troubleshooting/faq_19489/"
v-if="hasErrorFaqLink"
>
{{ translate('General_ErrorRequestFaqLink') }}
</a>
</div>
</div>
<div class="theWidgetContent" ref="widgetContent" />
</div>
</template>
<script lang="ts">
import { IRootScopeService, IScope } from 'angular';
import { defineComponent } from 'vue';
import ActivityIndicator from '../ActivityIndicator/ActivityIndicator.vue';
import { translate } from '../translate';
import Matomo from '../Matomo/Matomo';
import AjaxHelper from '../AjaxHelper/AjaxHelper';
import { NotificationsStore } from '../Notification';
import MatomoUrl from '../MatomoUrl/MatomoUrl';
import ComparisonsStoreInstance from '../Comparisons/Comparisons.store.instance';
interface WidgetLoaderState {
loading: boolean;
loadingFailed: boolean;
changeCounter: number;
currentScope: null|IScope;
lastWidgetAbortController: null|AbortController;
}
/**
* Loads any custom widget or URL based on the given parameters.
*
* The currently active idSite, period, date and segment (if needed) is automatically
* appended to the parameters. If this widget is removed from the DOM and requests are in
* progress, these requests will be aborted. A loading message or an error message on failure
* is shown as well. It's kinda similar to ng-include but there it is not possible to
* listen to HTTP errors etc.
*
* Example:
* <WidgetLoader :widget-params="{module: '', action: '', ...}"/>
*/
export default defineComponent({
props: {
widgetParams: Object,
widgetName: String,
},
components: {
ActivityIndicator,
},
data(): WidgetLoaderState {
return {
loading: false,
loadingFailed: false,
changeCounter: 0,
currentScope: null,
lastWidgetAbortController: null,
};
},
watch: {
widgetParams(parameters: QueryParameters) {
if (parameters) {
this.loadWidgetUrl(parameters, this.changeCounter += 1);
}
},
},
computed: {
loadingMessage() {
if (!this.widgetName) {
return translate('General_LoadingData');
}
return translate('General_LoadingPopover', this.widgetName);
},
hasErrorFaqLink() {
const isGeneralSettingsAdminEnabled = Matomo.config.enable_general_settings_admin;
const isPluginsAdminEnabled = Matomo.config.enable_plugins_admin;
return Matomo.hasSuperUserAccess
&& (isGeneralSettingsAdminEnabled
|| isPluginsAdminEnabled);
},
},
mounted() {
if (this.widgetParams) {
this.loadWidgetUrl(this.widgetParams as QueryParameters, this.changeCounter += 1);
}
},
unmounted() {
this.cleanupLastWidgetContent();
},
methods: {
abortHttpRequestIfNeeded() {
if (this.lastWidgetAbortController) {
this.lastWidgetAbortController.abort();
this.lastWidgetAbortController = null;
}
},
cleanupLastWidgetContent() {
const widgetContent = this.$refs.widgetContent as HTMLElement;
if (widgetContent) {
widgetContent.innerHTML = '';
}
if (this.currentScope) {
this.currentScope.$destroy();
}
},
getWidgetUrl(parameters?: QueryParameters): QueryParameters {
const urlParams = MatomoUrl.parsed.value;
let fullParameters: QueryParameters = { ...(parameters || {}) };
const paramsToForward = Object.keys({
...MatomoUrl.hashParsed.value,
idSite: '',
period: '',
date: '',
segment: '',
widget: '',
});
paramsToForward.forEach((key) => {
if (key === 'category' || key === 'subcategory') {
return;
}
if (!(key in fullParameters)) {
fullParameters[key] = urlParams[key];
}
});
if (ComparisonsStoreInstance.isComparisonEnabled()) {
fullParameters = {
...fullParameters,
comparePeriods: urlParams.comparePeriods,
compareDates: urlParams.compareDates,
compareSegments: urlParams.compareSegments,
};
}
if (!parameters || !('showtitle' in parameters)) {
fullParameters.showtitle = '1';
}
if (Matomo.shouldPropagateTokenAuth
&& urlParams.token_auth
) {
if (!Matomo.broadcast.isWidgetizeRequestWithoutSession()) {
fullParameters.force_api_session = '1';
}
fullParameters.token_auth = urlParams.token_auth;
}
fullParameters.random = Math.floor(Math.random() * 10000);
return fullParameters;
},
loadWidgetUrl(parameters: QueryParameters, thisChangeId: number) {
this.loading = true;
this.abortHttpRequestIfNeeded();
this.cleanupLastWidgetContent();
this.lastWidgetAbortController = new AbortController();
AjaxHelper.fetch(this.getWidgetUrl(parameters), {
format: 'html',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
abortController: this.lastWidgetAbortController,
}).then((response) => {
if (thisChangeId !== this.changeCounter || !response || typeof response !== 'string') {
// another widget was requested meanwhile, ignore this response
return;
}
this.lastWidgetAbortController = null;
this.loading = false;
this.loadingFailed = false;
const widgetContent = this.$refs.widgetContent as HTMLElement;
window.$(widgetContent).html(response);
const $content = window.$(widgetContent).children();
if (this.widgetName) {
// we need to respect the widget title, which overwrites a possibly set report title
let $title = $content.find('> .card-content .card-title');
if (!$title.length) {
$title = $content.find('> h2');
}
if ($title.length) {
// required to use htmlEntities since it also escapes '{{' format items
$title.html(Matomo.helper.htmlEntities(this.widgetName));
}
}
const $rootScope: IRootScopeService = Matomo.helper.getAngularDependency('$rootScope');
const scope = $rootScope.$new();
this.currentScope = scope;
// compile angularjs first since it will modify all dom nodes, breaking vue bindings
// if they are present
Matomo.helper.compileAngularComponents($content, { scope });
Matomo.helper.compileVueEntryComponents($content);
NotificationsStore.parseNotificationDivs();
setTimeout(() => {
Matomo.postEvent('widget:loaded', {
parameters,
element: $content,
});
});
}).catch((response) => {
if (thisChangeId !== this.changeCounter) {
// another widget was requested meanwhile, ignore this response
return;
}
this.lastWidgetAbortController = null;
this.cleanupLastWidgetContent();
this.loading = false;
if (response.xhrStatus === 'abort') {
return;
}
this.loadingFailed = true;
});
},
},
});
</script>
|