2017-12-19 23:06:54 +08:00
|
|
|
import angular from "angular";
|
|
|
|
|
import _ from "lodash";
|
2016-02-02 16:12:58 +08:00
|
|
|
|
2017-12-19 23:06:54 +08:00
|
|
|
import config from "app/core/config";
|
|
|
|
|
import coreModule from "app/core/core_module";
|
|
|
|
|
import { importPluginModule } from "./plugin_loader";
|
2017-10-02 02:02:25 +08:00
|
|
|
|
2017-12-19 23:06:54 +08:00
|
|
|
import { UnknownPanelCtrl } from "app/plugins/panel/unknown/module";
|
|
|
|
|
import { DashboardRowCtrl } from "./row_ctrl";
|
2017-08-18 19:25:51 +08:00
|
|
|
|
2016-02-08 23:57:29 +08:00
|
|
|
/** @ngInject **/
|
2017-12-19 23:06:54 +08:00
|
|
|
function pluginDirectiveLoader(
|
|
|
|
|
$compile,
|
|
|
|
|
datasourceSrv,
|
|
|
|
|
$rootScope,
|
|
|
|
|
$q,
|
|
|
|
|
$http,
|
|
|
|
|
$templateCache
|
|
|
|
|
) {
|
2016-02-04 21:36:19 +08:00
|
|
|
function getTemplate(component) {
|
|
|
|
|
if (component.template) {
|
|
|
|
|
return $q.when(component.template);
|
|
|
|
|
}
|
|
|
|
|
var cached = $templateCache.get(component.templateUrl);
|
|
|
|
|
if (cached) {
|
|
|
|
|
return $q.when(cached);
|
|
|
|
|
}
|
|
|
|
|
return $http.get(component.templateUrl).then(res => {
|
|
|
|
|
return res.data;
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-02 16:12:58 +08:00
|
|
|
|
2016-02-10 01:17:32 +08:00
|
|
|
function relativeTemplateUrlToAbs(templateUrl, baseUrl) {
|
2017-12-19 23:06:54 +08:00
|
|
|
if (!templateUrl) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
if (templateUrl.indexOf("public") === 0) {
|
|
|
|
|
return templateUrl;
|
|
|
|
|
}
|
|
|
|
|
return baseUrl + "/" + templateUrl;
|
2016-02-10 01:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
2016-02-02 16:12:58 +08:00
|
|
|
function getPluginComponentDirective(options) {
|
2016-02-10 01:17:32 +08:00
|
|
|
// handle relative template urls for plugin templates
|
2017-12-19 23:06:54 +08:00
|
|
|
options.Component.templateUrl = relativeTemplateUrlToAbs(
|
|
|
|
|
options.Component.templateUrl,
|
|
|
|
|
options.baseUrl
|
|
|
|
|
);
|
2016-02-10 01:17:32 +08:00
|
|
|
|
2016-02-02 16:12:58 +08:00
|
|
|
return function() {
|
|
|
|
|
return {
|
|
|
|
|
templateUrl: options.Component.templateUrl,
|
2016-02-04 21:36:19 +08:00
|
|
|
template: options.Component.template,
|
2017-12-19 23:06:54 +08:00
|
|
|
restrict: "E",
|
2016-02-02 16:12:58 +08:00
|
|
|
controller: options.Component,
|
2017-12-19 23:06:54 +08:00
|
|
|
controllerAs: "ctrl",
|
2016-02-02 16:12:58 +08:00
|
|
|
bindToController: true,
|
|
|
|
|
scope: options.bindings,
|
|
|
|
|
link: (scope, elem, attrs, ctrl) => {
|
|
|
|
|
if (ctrl.link) {
|
|
|
|
|
ctrl.link(scope, elem, attrs, ctrl);
|
|
|
|
|
}
|
2016-02-04 21:36:19 +08:00
|
|
|
if (ctrl.init) {
|
|
|
|
|
ctrl.init();
|
|
|
|
|
}
|
2016-02-02 16:12:58 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 21:36:19 +08:00
|
|
|
function loadPanelComponentInfo(scope, attrs) {
|
2017-12-19 23:06:54 +08:00
|
|
|
if (scope.panel.type === "row") {
|
2017-08-18 19:25:51 +08:00
|
|
|
return $q.when({
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "dashboard-row",
|
|
|
|
|
bindings: { dashboard: "=", panel: "=" },
|
|
|
|
|
attrs: { dashboard: "ctrl.dashboard", panel: "panel" },
|
|
|
|
|
Component: DashboardRowCtrl
|
2017-08-18 19:25:51 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 22:04:07 +08:00
|
|
|
var componentInfo: any = {
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "panel-plugin-" + scope.panel.type,
|
|
|
|
|
bindings: { dashboard: "=", panel: "=", row: "=" },
|
|
|
|
|
attrs: {
|
|
|
|
|
dashboard: "dashboard",
|
|
|
|
|
panel: "panel",
|
|
|
|
|
class: "panel-height-helper"
|
|
|
|
|
}
|
2016-02-04 22:04:07 +08:00
|
|
|
};
|
|
|
|
|
|
2016-02-04 21:36:19 +08:00
|
|
|
let panelInfo = config.panels[scope.panel.type];
|
2016-02-04 22:04:07 +08:00
|
|
|
var panelCtrlPromise = Promise.resolve(UnknownPanelCtrl);
|
|
|
|
|
if (panelInfo) {
|
2017-12-19 23:06:54 +08:00
|
|
|
panelCtrlPromise = importPluginModule(panelInfo.module).then(function(
|
|
|
|
|
panelModule
|
|
|
|
|
) {
|
2016-02-04 22:04:07 +08:00
|
|
|
return panelModule.PanelCtrl;
|
|
|
|
|
});
|
2016-02-04 21:36:19 +08:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 22:04:07 +08:00
|
|
|
return panelCtrlPromise.then(function(PanelCtrl: any) {
|
|
|
|
|
componentInfo.Component = PanelCtrl;
|
2016-02-04 21:36:19 +08:00
|
|
|
|
|
|
|
|
if (!PanelCtrl || PanelCtrl.registered) {
|
|
|
|
|
return componentInfo;
|
2017-04-20 17:16:37 +08:00
|
|
|
}
|
2016-02-04 21:36:19 +08:00
|
|
|
|
|
|
|
|
if (PanelCtrl.templatePromise) {
|
|
|
|
|
return PanelCtrl.templatePromise.then(res => {
|
|
|
|
|
return componentInfo;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 01:17:32 +08:00
|
|
|
if (panelInfo) {
|
2017-12-19 23:06:54 +08:00
|
|
|
PanelCtrl.templateUrl = relativeTemplateUrlToAbs(
|
|
|
|
|
PanelCtrl.templateUrl,
|
|
|
|
|
panelInfo.baseUrl
|
|
|
|
|
);
|
2016-02-10 01:17:32 +08:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 21:36:19 +08:00
|
|
|
PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
|
|
|
|
|
PanelCtrl.templateUrl = null;
|
2017-11-21 21:30:33 +08:00
|
|
|
PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
|
2016-02-04 21:36:19 +08:00
|
|
|
return componentInfo;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return PanelCtrl.templatePromise;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 16:12:58 +08:00
|
|
|
function getModule(scope, attrs) {
|
|
|
|
|
switch (attrs.type) {
|
2016-02-02 22:15:20 +08:00
|
|
|
// QueryCtrl
|
|
|
|
|
case "query-ctrl": {
|
2016-02-02 16:12:58 +08:00
|
|
|
let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
|
|
|
|
|
return datasourceSrv.get(datasource).then(ds => {
|
|
|
|
|
scope.datasource = ds;
|
|
|
|
|
|
2017-10-02 02:02:25 +08:00
|
|
|
return importPluginModule(ds.meta.module).then(dsModule => {
|
2016-02-02 16:12:58 +08:00
|
|
|
return {
|
2016-02-10 01:17:32 +08:00
|
|
|
baseUrl: ds.meta.baseUrl,
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "query-ctrl-" + ds.meta.id,
|
|
|
|
|
bindings: { target: "=", panelCtrl: "=", datasource: "=" },
|
|
|
|
|
attrs: {
|
|
|
|
|
target: "target",
|
|
|
|
|
"panel-ctrl": "ctrl.panelCtrl",
|
|
|
|
|
datasource: "datasource"
|
|
|
|
|
},
|
2016-02-02 19:52:43 +08:00
|
|
|
Component: dsModule.QueryCtrl
|
2016-02-02 16:12:58 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-02-02 22:15:20 +08:00
|
|
|
}
|
|
|
|
|
// QueryOptionsCtrl
|
|
|
|
|
case "query-options-ctrl": {
|
|
|
|
|
return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
|
2017-10-02 02:02:25 +08:00
|
|
|
return importPluginModule(ds.meta.module).then((dsModule): any => {
|
2016-02-02 22:15:20 +08:00
|
|
|
if (!dsModule.QueryOptionsCtrl) {
|
2017-12-19 23:06:54 +08:00
|
|
|
return { notFound: true };
|
2016-02-02 22:15:20 +08:00
|
|
|
}
|
2016-02-02 16:12:58 +08:00
|
|
|
|
2016-02-02 22:15:20 +08:00
|
|
|
return {
|
2016-02-10 01:17:32 +08:00
|
|
|
baseUrl: ds.meta.baseUrl,
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "query-options-ctrl-" + ds.meta.id,
|
|
|
|
|
bindings: { panelCtrl: "=" },
|
|
|
|
|
attrs: { "panel-ctrl": "ctrl.panelCtrl" },
|
2016-02-02 22:15:20 +08:00
|
|
|
Component: dsModule.QueryOptionsCtrl
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-03 05:58:37 +08:00
|
|
|
// Annotations
|
2016-02-03 01:16:30 +08:00
|
|
|
case "annotations-query-ctrl": {
|
2017-12-19 23:06:54 +08:00
|
|
|
return importPluginModule(
|
|
|
|
|
scope.ctrl.currentDatasource.meta.module
|
|
|
|
|
).then(function(dsModule) {
|
2016-02-03 01:16:30 +08:00
|
|
|
return {
|
2016-09-08 17:25:45 +08:00
|
|
|
baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
|
2017-12-19 23:06:54 +08:00
|
|
|
name:
|
|
|
|
|
"annotations-query-ctrl-" + scope.ctrl.currentDatasource.meta.id,
|
|
|
|
|
bindings: { annotation: "=", datasource: "=" },
|
|
|
|
|
attrs: {
|
|
|
|
|
annotation: "ctrl.currentAnnotation",
|
|
|
|
|
datasource: "ctrl.currentDatasource"
|
|
|
|
|
},
|
|
|
|
|
Component: dsModule.AnnotationsQueryCtrl
|
2016-02-03 01:16:30 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-03-23 01:21:21 +08:00
|
|
|
// Datasource ConfigCtrl
|
2017-12-19 23:06:54 +08:00
|
|
|
case "datasource-config-ctrl": {
|
2016-03-10 16:47:29 +08:00
|
|
|
var dsMeta = scope.ctrl.datasourceMeta;
|
2017-10-02 02:02:25 +08:00
|
|
|
return importPluginModule(dsMeta.module).then(function(dsModule): any {
|
2016-03-23 01:21:21 +08:00
|
|
|
if (!dsModule.ConfigCtrl) {
|
2017-12-19 23:06:54 +08:00
|
|
|
return { notFound: true };
|
2016-03-14 20:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
2016-02-02 16:12:58 +08:00
|
|
|
return {
|
2016-03-10 16:47:29 +08:00
|
|
|
baseUrl: dsMeta.baseUrl,
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "ds-config-" + dsMeta.id,
|
|
|
|
|
bindings: { meta: "=", current: "=" },
|
|
|
|
|
attrs: { meta: "ctrl.datasourceMeta", current: "ctrl.current" },
|
|
|
|
|
Component: dsModule.ConfigCtrl
|
2016-02-02 16:12:58 +08:00
|
|
|
};
|
|
|
|
|
});
|
2016-02-02 22:15:20 +08:00
|
|
|
}
|
2016-02-05 19:13:59 +08:00
|
|
|
// AppConfigCtrl
|
2017-12-19 23:06:54 +08:00
|
|
|
case "app-config-ctrl": {
|
2016-02-27 01:55:17 +08:00
|
|
|
let model = scope.ctrl.model;
|
2017-10-02 02:02:25 +08:00
|
|
|
return importPluginModule(model.module).then(function(appModule) {
|
2016-02-05 19:13:59 +08:00
|
|
|
return {
|
2016-02-27 01:55:17 +08:00
|
|
|
baseUrl: model.baseUrl,
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "app-config-" + model.id,
|
|
|
|
|
bindings: { appModel: "=", appEditCtrl: "=" },
|
|
|
|
|
attrs: { "app-model": "ctrl.model", "app-edit-ctrl": "ctrl" },
|
|
|
|
|
Component: appModule.ConfigCtrl
|
2016-02-05 19:13:59 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-09 18:17:49 +08:00
|
|
|
// App Page
|
2017-12-19 23:06:54 +08:00
|
|
|
case "app-page": {
|
2016-02-09 18:17:49 +08:00
|
|
|
let appModel = scope.ctrl.appModel;
|
2017-10-02 02:02:25 +08:00
|
|
|
return importPluginModule(appModel.module).then(function(appModule) {
|
2016-02-09 18:17:49 +08:00
|
|
|
return {
|
2016-02-10 01:17:32 +08:00
|
|
|
baseUrl: appModel.baseUrl,
|
2017-12-19 23:06:54 +08:00
|
|
|
name: "app-page-" + appModel.id + "-" + scope.ctrl.page.slug,
|
|
|
|
|
bindings: { appModel: "=" },
|
|
|
|
|
attrs: { "app-model": "ctrl.appModel" },
|
|
|
|
|
Component: appModule[scope.ctrl.page.component]
|
2016-02-09 18:17:49 +08:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-02-04 21:36:19 +08:00
|
|
|
// Panel
|
2017-12-19 23:06:54 +08:00
|
|
|
case "panel": {
|
2016-02-04 21:36:19 +08:00
|
|
|
return loadPanelComponentInfo(scope, attrs);
|
|
|
|
|
}
|
2016-02-02 22:15:20 +08:00
|
|
|
default: {
|
2017-12-19 23:06:54 +08:00
|
|
|
return $q.reject({
|
|
|
|
|
message: "Could not find component type: " + attrs.type
|
|
|
|
|
});
|
2016-02-02 22:15:20 +08:00
|
|
|
}
|
2016-02-02 16:12:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appendAndCompile(scope, elem, componentInfo) {
|
|
|
|
|
var child = angular.element(document.createElement(componentInfo.name));
|
|
|
|
|
_.each(componentInfo.attrs, (value, key) => {
|
|
|
|
|
child.attr(key, value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$compile(child)(scope);
|
|
|
|
|
elem.empty();
|
2016-04-24 18:50:33 +08:00
|
|
|
|
|
|
|
|
// let a binding digest cycle complete before adding to dom
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
elem.append(child);
|
2016-06-06 23:18:18 +08:00
|
|
|
scope.$applyAsync(function() {
|
2017-12-19 23:06:54 +08:00
|
|
|
scope.$broadcast("component-did-mount");
|
|
|
|
|
scope.$broadcast("refresh");
|
2016-04-26 20:51:06 +08:00
|
|
|
});
|
2016-04-24 18:50:33 +08:00
|
|
|
});
|
2016-02-02 16:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerPluginComponent(scope, elem, attrs, componentInfo) {
|
2016-02-02 22:15:20 +08:00
|
|
|
if (componentInfo.notFound) {
|
|
|
|
|
elem.empty();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-03 23:31:07 +08:00
|
|
|
if (!componentInfo.Component) {
|
2017-12-19 23:06:54 +08:00
|
|
|
throw {
|
|
|
|
|
message:
|
|
|
|
|
"Failed to find exported plugin component for " + componentInfo.name
|
|
|
|
|
};
|
2016-02-03 23:31:07 +08:00
|
|
|
}
|
|
|
|
|
|
2016-02-02 16:12:58 +08:00
|
|
|
if (!componentInfo.Component.registered) {
|
|
|
|
|
var directiveName = attrs.$normalize(componentInfo.name);
|
|
|
|
|
var directiveFn = getPluginComponentDirective(componentInfo);
|
|
|
|
|
coreModule.directive(directiveName, directiveFn);
|
|
|
|
|
componentInfo.Component.registered = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appendAndCompile(scope, elem, componentInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2017-12-19 23:06:54 +08:00
|
|
|
restrict: "E",
|
2016-02-02 16:12:58 +08:00
|
|
|
link: function(scope, elem, attrs) {
|
2017-12-19 23:06:54 +08:00
|
|
|
getModule(scope, attrs)
|
|
|
|
|
.then(function(componentInfo) {
|
|
|
|
|
registerPluginComponent(scope, elem, attrs, componentInfo);
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
$rootScope.appEvent("alert-error", [
|
|
|
|
|
"Plugin Error",
|
|
|
|
|
err.message || err
|
|
|
|
|
]);
|
|
|
|
|
console.log("Plugin component error", err);
|
|
|
|
|
});
|
2016-02-02 16:12:58 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-19 23:06:54 +08:00
|
|
|
coreModule.directive("pluginComponent", pluginDirectiveLoader);
|