2016-02-02 16:12:58 +08:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
import angular from 'angular';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import coreModule from '../core_module';
|
|
|
|
|
2016-02-02 22:15:20 +08:00
|
|
|
function pluginDirectiveLoader($compile, datasourceSrv, $rootScope) {
|
2016-02-02 16:12:58 +08:00
|
|
|
|
|
|
|
function getPluginComponentDirective(options) {
|
|
|
|
return function() {
|
|
|
|
return {
|
|
|
|
templateUrl: options.Component.templateUrl,
|
|
|
|
restrict: 'E',
|
|
|
|
controller: options.Component,
|
|
|
|
controllerAs: 'ctrl',
|
|
|
|
bindToController: true,
|
|
|
|
scope: options.bindings,
|
|
|
|
link: (scope, elem, attrs, ctrl) => {
|
|
|
|
if (ctrl.link) {
|
|
|
|
ctrl.link(scope, elem, attrs, ctrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return System.import(ds.meta.module).then(dsModule => {
|
|
|
|
return {
|
2016-02-02 22:15:20 +08:00
|
|
|
name: 'query-ctrl-' + ds.meta.id,
|
2016-02-02 17:19:15 +08:00
|
|
|
bindings: {target: "=", panelCtrl: "=", datasource: "="},
|
|
|
|
attrs: {"target": "target", "panel-ctrl": "ctrl", 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 => {
|
|
|
|
return System.import(ds.meta.module).then((dsModule): any => {
|
|
|
|
if (!dsModule.QueryOptionsCtrl) {
|
|
|
|
return {notFound: true};
|
|
|
|
}
|
2016-02-02 16:12:58 +08:00
|
|
|
|
2016-02-02 22:15:20 +08:00
|
|
|
return {
|
|
|
|
name: 'query-options-ctrl-' + ds.meta.id,
|
|
|
|
bindings: {panelCtrl: "="},
|
|
|
|
attrs: {"panel-ctrl": "ctrl"},
|
|
|
|
Component: dsModule.QueryOptionsCtrl
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-02-03 01:16:30 +08:00
|
|
|
// QueryOptionsCtrl
|
|
|
|
case "annotations-query-ctrl": {
|
|
|
|
return System.import(scope.currentDatasource.meta.module).then(function(dsModule) {
|
|
|
|
return {
|
|
|
|
name: 'annotations-query-ctrl-' + scope.currentDatasource.meta.id,
|
|
|
|
bindings: {annotation: "=", datasource: "="},
|
|
|
|
attrs: {"annotation": "currentAnnotation", datasource: "currentDatasource"},
|
|
|
|
Component: dsModule.AnnotationsQueryCtrl,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2016-02-02 22:15:20 +08:00
|
|
|
// ConfigCtrl
|
|
|
|
case 'datasource-config-ctrl': {
|
2016-02-02 16:12:58 +08:00
|
|
|
return System.import(scope.datasourceMeta.module).then(function(dsModule) {
|
|
|
|
return {
|
|
|
|
name: 'ds-config-' + scope.datasourceMeta.id,
|
|
|
|
bindings: {meta: "=", current: "="},
|
|
|
|
attrs: {meta: "datasourceMeta", current: "current"},
|
2016-02-02 22:15:20 +08:00
|
|
|
Component: dsModule.ConfigCtrl,
|
2016-02-02 16:12:58 +08:00
|
|
|
};
|
|
|
|
});
|
2016-02-02 22:15:20 +08:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
$rootScope.appEvent('alert-error', ['Plugin component error', 'could not find component '+ attrs.type]);
|
|
|
|
}
|
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();
|
|
|
|
elem.append(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerPluginComponent(scope, elem, attrs, componentInfo) {
|
2016-02-02 22:15:20 +08:00
|
|
|
if (componentInfo.notFound) {
|
|
|
|
elem.empty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
restrict: 'E',
|
|
|
|
link: function(scope, elem, attrs) {
|
|
|
|
getModule(scope, attrs).then(function (componentInfo) {
|
|
|
|
registerPluginComponent(scope, elem, attrs, componentInfo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
coreModule.directive('pluginComponent', pluginDirectiveLoader);
|