plugin_component.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import coreModule from 'app/core/core_module';
  5. import { importPluginModule } from './plugin_loader';
  6. import { UnknownPanelCtrl } from 'app/plugins/panel/unknown/module';
  7. import { DashboardRowCtrl } from './row_ctrl';
  8. /** @ngInject **/
  9. function pluginDirectiveLoader(
  10. $compile,
  11. datasourceSrv,
  12. $rootScope,
  13. $q,
  14. $http,
  15. $templateCache
  16. ) {
  17. function getTemplate(component) {
  18. if (component.template) {
  19. return $q.when(component.template);
  20. }
  21. var cached = $templateCache.get(component.templateUrl);
  22. if (cached) {
  23. return $q.when(cached);
  24. }
  25. return $http.get(component.templateUrl).then(res => {
  26. return res.data;
  27. });
  28. }
  29. function relativeTemplateUrlToAbs(templateUrl, baseUrl) {
  30. if (!templateUrl) {
  31. return undefined;
  32. }
  33. if (templateUrl.indexOf('public') === 0) {
  34. return templateUrl;
  35. }
  36. return baseUrl + '/' + templateUrl;
  37. }
  38. function getPluginComponentDirective(options) {
  39. // handle relative template urls for plugin templates
  40. options.Component.templateUrl = relativeTemplateUrlToAbs(
  41. options.Component.templateUrl,
  42. options.baseUrl
  43. );
  44. return function() {
  45. return {
  46. templateUrl: options.Component.templateUrl,
  47. template: options.Component.template,
  48. restrict: 'E',
  49. controller: options.Component,
  50. controllerAs: 'ctrl',
  51. bindToController: true,
  52. scope: options.bindings,
  53. link: (scope, elem, attrs, ctrl) => {
  54. if (ctrl.link) {
  55. ctrl.link(scope, elem, attrs, ctrl);
  56. }
  57. if (ctrl.init) {
  58. ctrl.init();
  59. }
  60. },
  61. };
  62. };
  63. }
  64. function loadPanelComponentInfo(scope, attrs) {
  65. if (scope.panel.type === 'row') {
  66. return $q.when({
  67. name: 'dashboard-row',
  68. bindings: { dashboard: '=', panel: '=' },
  69. attrs: { dashboard: 'ctrl.dashboard', panel: 'panel' },
  70. Component: DashboardRowCtrl,
  71. });
  72. }
  73. var componentInfo: any = {
  74. name: 'panel-plugin-' + scope.panel.type,
  75. bindings: { dashboard: '=', panel: '=', row: '=' },
  76. attrs: {
  77. dashboard: 'dashboard',
  78. panel: 'panel',
  79. class: 'panel-height-helper',
  80. },
  81. };
  82. let panelInfo = config.panels[scope.panel.type];
  83. var panelCtrlPromise = Promise.resolve(UnknownPanelCtrl);
  84. if (panelInfo) {
  85. panelCtrlPromise = importPluginModule(panelInfo.module).then(function(
  86. panelModule
  87. ) {
  88. return panelModule.PanelCtrl;
  89. });
  90. }
  91. return panelCtrlPromise.then(function(PanelCtrl: any) {
  92. componentInfo.Component = PanelCtrl;
  93. if (!PanelCtrl || PanelCtrl.registered) {
  94. return componentInfo;
  95. }
  96. if (PanelCtrl.templatePromise) {
  97. return PanelCtrl.templatePromise.then(res => {
  98. return componentInfo;
  99. });
  100. }
  101. if (panelInfo) {
  102. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(
  103. PanelCtrl.templateUrl,
  104. panelInfo.baseUrl
  105. );
  106. }
  107. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  108. PanelCtrl.templateUrl = null;
  109. PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
  110. return componentInfo;
  111. });
  112. return PanelCtrl.templatePromise;
  113. });
  114. }
  115. function getModule(scope, attrs) {
  116. switch (attrs.type) {
  117. // QueryCtrl
  118. case 'query-ctrl': {
  119. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  120. return datasourceSrv.get(datasource).then(ds => {
  121. scope.datasource = ds;
  122. return importPluginModule(ds.meta.module).then(dsModule => {
  123. return {
  124. baseUrl: ds.meta.baseUrl,
  125. name: 'query-ctrl-' + ds.meta.id,
  126. bindings: { target: '=', panelCtrl: '=', datasource: '=' },
  127. attrs: {
  128. target: 'target',
  129. 'panel-ctrl': 'ctrl.panelCtrl',
  130. datasource: 'datasource',
  131. },
  132. Component: dsModule.QueryCtrl,
  133. };
  134. });
  135. });
  136. }
  137. // QueryOptionsCtrl
  138. case 'query-options-ctrl': {
  139. return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
  140. return importPluginModule(ds.meta.module).then((dsModule): any => {
  141. if (!dsModule.QueryOptionsCtrl) {
  142. return { notFound: true };
  143. }
  144. return {
  145. baseUrl: ds.meta.baseUrl,
  146. name: 'query-options-ctrl-' + ds.meta.id,
  147. bindings: { panelCtrl: '=' },
  148. attrs: { 'panel-ctrl': 'ctrl.panelCtrl' },
  149. Component: dsModule.QueryOptionsCtrl,
  150. };
  151. });
  152. });
  153. }
  154. // Annotations
  155. case 'annotations-query-ctrl': {
  156. return importPluginModule(
  157. scope.ctrl.currentDatasource.meta.module
  158. ).then(function(dsModule) {
  159. return {
  160. baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
  161. name:
  162. 'annotations-query-ctrl-' + scope.ctrl.currentDatasource.meta.id,
  163. bindings: { annotation: '=', datasource: '=' },
  164. attrs: {
  165. annotation: 'ctrl.currentAnnotation',
  166. datasource: 'ctrl.currentDatasource',
  167. },
  168. Component: dsModule.AnnotationsQueryCtrl,
  169. };
  170. });
  171. }
  172. // Datasource ConfigCtrl
  173. case 'datasource-config-ctrl': {
  174. var dsMeta = scope.ctrl.datasourceMeta;
  175. return importPluginModule(dsMeta.module).then(function(dsModule): any {
  176. if (!dsModule.ConfigCtrl) {
  177. return { notFound: true };
  178. }
  179. return {
  180. baseUrl: dsMeta.baseUrl,
  181. name: 'ds-config-' + dsMeta.id,
  182. bindings: { meta: '=', current: '=' },
  183. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  184. Component: dsModule.ConfigCtrl,
  185. };
  186. });
  187. }
  188. // AppConfigCtrl
  189. case 'app-config-ctrl': {
  190. let model = scope.ctrl.model;
  191. return importPluginModule(model.module).then(function(appModule) {
  192. return {
  193. baseUrl: model.baseUrl,
  194. name: 'app-config-' + model.id,
  195. bindings: { appModel: '=', appEditCtrl: '=' },
  196. attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
  197. Component: appModule.ConfigCtrl,
  198. };
  199. });
  200. }
  201. // App Page
  202. case 'app-page': {
  203. let appModel = scope.ctrl.appModel;
  204. return importPluginModule(appModel.module).then(function(appModule) {
  205. return {
  206. baseUrl: appModel.baseUrl,
  207. name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
  208. bindings: { appModel: '=' },
  209. attrs: { 'app-model': 'ctrl.appModel' },
  210. Component: appModule[scope.ctrl.page.component],
  211. };
  212. });
  213. }
  214. // Panel
  215. case 'panel': {
  216. return loadPanelComponentInfo(scope, attrs);
  217. }
  218. default: {
  219. return $q.reject({
  220. message: 'Could not find component type: ' + attrs.type,
  221. });
  222. }
  223. }
  224. }
  225. function appendAndCompile(scope, elem, componentInfo) {
  226. var child = angular.element(document.createElement(componentInfo.name));
  227. _.each(componentInfo.attrs, (value, key) => {
  228. child.attr(key, value);
  229. });
  230. $compile(child)(scope);
  231. elem.empty();
  232. // let a binding digest cycle complete before adding to dom
  233. setTimeout(function() {
  234. elem.append(child);
  235. scope.$applyAsync(function() {
  236. scope.$broadcast('component-did-mount');
  237. scope.$broadcast('refresh');
  238. });
  239. });
  240. }
  241. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  242. if (componentInfo.notFound) {
  243. elem.empty();
  244. return;
  245. }
  246. if (!componentInfo.Component) {
  247. throw {
  248. message:
  249. 'Failed to find exported plugin component for ' + componentInfo.name,
  250. };
  251. }
  252. if (!componentInfo.Component.registered) {
  253. var directiveName = attrs.$normalize(componentInfo.name);
  254. var directiveFn = getPluginComponentDirective(componentInfo);
  255. coreModule.directive(directiveName, directiveFn);
  256. componentInfo.Component.registered = true;
  257. }
  258. appendAndCompile(scope, elem, componentInfo);
  259. }
  260. return {
  261. restrict: 'E',
  262. link: function(scope, elem, attrs) {
  263. getModule(scope, attrs)
  264. .then(function(componentInfo) {
  265. registerPluginComponent(scope, elem, attrs, componentInfo);
  266. })
  267. .catch(err => {
  268. $rootScope.appEvent('alert-error', [
  269. 'Plugin Error',
  270. err.message || err,
  271. ]);
  272. console.log('Plugin component error', err);
  273. });
  274. },
  275. };
  276. }
  277. coreModule.directive('pluginComponent', pluginDirectiveLoader);