-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
plugin_component.ts
275 lines (248 loc) · 8.56 KB
/
plugin_component.ts
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import angular, { ILocationService } from 'angular';
import _ from 'lodash';
import config from 'app/core/config';
import coreModule from 'app/core/core_module';
import { DataSourceApi } from '@grafana/data';
import { importPanelPlugin, importDataSourcePlugin, importAppPlugin } from './plugin_loader';
import DatasourceSrv from './datasource_srv';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
/** @ngInject */
function pluginDirectiveLoader(
$compile: any,
datasourceSrv: DatasourceSrv,
$rootScope: GrafanaRootScope,
$http: any,
$templateCache: any,
$timeout: any,
$location: ILocationService
) {
function getTemplate(component: { template: any; templateUrl: any }) {
if (component.template) {
return Promise.resolve(component.template);
}
const cached = $templateCache.get(component.templateUrl);
if (cached) {
return Promise.resolve(cached);
}
return $http.get(component.templateUrl).then((res: any) => {
return res.data;
});
}
function relativeTemplateUrlToAbs(templateUrl: string, baseUrl: string) {
if (!templateUrl) {
return undefined;
}
if (templateUrl.indexOf('public') === 0) {
return templateUrl;
}
return baseUrl + '/' + templateUrl;
}
function getPluginComponentDirective(options: any) {
// handle relative template urls for plugin templates
options.Component.templateUrl = relativeTemplateUrlToAbs(options.Component.templateUrl, options.baseUrl);
return () => {
return {
templateUrl: options.Component.templateUrl,
template: options.Component.template,
restrict: 'E',
controller: options.Component,
controllerAs: 'ctrl',
bindToController: true,
scope: options.bindings,
link: (scope: any, elem: any, attrs: any, ctrl: any) => {
if (ctrl.link) {
ctrl.link(scope, elem, attrs, ctrl);
}
if (ctrl.init) {
ctrl.init();
}
},
};
};
}
function loadPanelComponentInfo(scope: any, attrs: any) {
const componentInfo: any = {
name: 'panel-plugin-' + scope.panel.type,
bindings: { dashboard: '=', panel: '=', row: '=' },
attrs: {
dashboard: 'dashboard',
panel: 'panel',
class: 'panel-height-helper',
},
};
const panelInfo = config.panels[scope.panel.type];
return importPanelPlugin(panelInfo.id).then(panelPlugin => {
const PanelCtrl = panelPlugin.angularPanelCtrl;
componentInfo.Component = PanelCtrl;
if (!PanelCtrl || PanelCtrl.registered) {
return componentInfo;
}
if (PanelCtrl.templatePromise) {
return PanelCtrl.templatePromise.then((res: any) => {
return componentInfo;
});
}
if (panelInfo) {
PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
}
PanelCtrl.templatePromise = getTemplate(PanelCtrl).then((template: any) => {
PanelCtrl.templateUrl = null;
PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
return componentInfo;
});
return PanelCtrl.templatePromise;
});
}
function getModule(scope: any, attrs: any): any {
switch (attrs.type) {
// QueryCtrl
case 'query-ctrl': {
const ds: DataSourceApi = scope.ctrl.datasource as DataSourceApi;
return Promise.resolve({
baseUrl: ds.meta.baseUrl,
name: 'query-ctrl-' + ds.meta.id,
bindings: { target: '=', panelCtrl: '=', datasource: '=' },
attrs: {
target: 'ctrl.target',
'panel-ctrl': 'ctrl',
datasource: 'ctrl.datasource',
},
Component: ds.components!.QueryCtrl,
});
}
// Annotations
case 'annotations-query-ctrl': {
const baseUrl = scope.ctrl.currentDatasource.meta.baseUrl;
const pluginId = scope.ctrl.currentDatasource.meta.id;
return importDataSourcePlugin(scope.ctrl.currentDatasource.meta).then(dsPlugin => {
return {
baseUrl,
name: 'annotations-query-ctrl-' + pluginId,
bindings: { annotation: '=', datasource: '=' },
attrs: {
annotation: 'ctrl.currentAnnotation',
datasource: 'ctrl.currentDatasource',
},
Component: dsPlugin.components.AnnotationsQueryCtrl,
};
});
}
// Datasource ConfigCtrl
case 'datasource-config-ctrl': {
const dsMeta = scope.ctrl.datasourceMeta;
const angularUrl = $location.url();
return importDataSourcePlugin(dsMeta).then(dsPlugin => {
scope.$watch(
'ctrl.current',
() => {
// This watcher can trigger when we navigate away due to late digests
// This check is to stop onModelChanged from being called when navigating away
// as it triggers a redux action which comes before the angular $routeChangeSucces and
// This makes the bridgeSrv think location changed from redux before detecting it was actually
// changed from angular.
if (angularUrl === $location.url()) {
scope.onModelChanged(scope.ctrl.current);
}
},
true
);
return {
baseUrl: dsMeta.baseUrl,
name: 'ds-config-' + dsMeta.id,
bindings: { meta: '=', current: '=' },
attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
Component: dsPlugin.angularConfigCtrl,
};
});
}
// AppConfigCtrl
case 'app-config-ctrl': {
const model = scope.ctrl.model;
return importAppPlugin(model).then(appPlugin => {
return {
baseUrl: model.baseUrl,
name: 'app-config-' + model.id,
bindings: { appModel: '=', appEditCtrl: '=' },
attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
Component: appPlugin.angularConfigCtrl,
};
});
}
// App Page
case 'app-page': {
const appModel = scope.ctrl.appModel;
return importAppPlugin(appModel).then(appPlugin => {
if (!appPlugin.angularPages) {
throw new Error('Plugin has no page components');
}
return {
baseUrl: appModel.baseUrl,
name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
bindings: { appModel: '=' },
attrs: { 'app-model': 'ctrl.appModel' },
Component: appPlugin.angularPages[scope.ctrl.page.component],
};
});
}
// Panel
case 'panel': {
return loadPanelComponentInfo(scope, attrs);
}
default: {
return Promise.reject({
message: 'Could not find component type: ' + attrs.type,
});
}
}
}
function appendAndCompile(scope: any, elem: JQuery, componentInfo: any) {
const child = angular.element(document.createElement(componentInfo.name));
_.each(componentInfo.attrs, (value, key) => {
child.attr(key, value);
});
$compile(child)(scope);
elem.empty();
// let a binding digest cycle complete before adding to dom
setTimeout(() => {
scope.$applyAsync(() => {
elem.append(child);
setTimeout(() => {
scope.$applyAsync(() => {
scope.$broadcast('component-did-mount');
});
});
});
});
}
function registerPluginComponent(scope: any, elem: JQuery, attrs: any, componentInfo: any) {
if (componentInfo.notFound) {
elem.empty();
return;
}
if (!componentInfo.Component) {
throw {
message: 'Failed to find exported plugin component for ' + componentInfo.name,
};
}
if (!componentInfo.Component.registered) {
const directiveName = attrs.$normalize(componentInfo.name);
const directiveFn = getPluginComponentDirective(componentInfo);
coreModule.directive(directiveName, directiveFn);
componentInfo.Component.registered = true;
}
appendAndCompile(scope, elem, componentInfo);
}
return {
restrict: 'E',
link: (scope: any, elem: JQuery, attrs: any) => {
getModule(scope, attrs)
.then((componentInfo: any) => {
registerPluginComponent(scope, elem, attrs, componentInfo);
})
.catch((err: any) => {
console.error('Plugin component error', err);
});
},
};
}
coreModule.directive('pluginComponent', pluginDirectiveLoader);