plugin_component.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 { AngularPanelPlugin, DataSourceApi } from '@grafana/ui/src/types';
  6. import { importPanelPlugin, importDataSourcePlugin, importAppPlugin } from './plugin_loader';
  7. /** @ngInject */
  8. function pluginDirectiveLoader($compile, datasourceSrv, $rootScope, $q, $http, $templateCache, $timeout) {
  9. function getTemplate(component) {
  10. if (component.template) {
  11. return $q.when(component.template);
  12. }
  13. const cached = $templateCache.get(component.templateUrl);
  14. if (cached) {
  15. return $q.when(cached);
  16. }
  17. return $http.get(component.templateUrl).then(res => {
  18. return res.data;
  19. });
  20. }
  21. function relativeTemplateUrlToAbs(templateUrl, baseUrl) {
  22. if (!templateUrl) {
  23. return undefined;
  24. }
  25. if (templateUrl.indexOf('public') === 0) {
  26. return templateUrl;
  27. }
  28. return baseUrl + '/' + templateUrl;
  29. }
  30. function getPluginComponentDirective(options) {
  31. // handle relative template urls for plugin templates
  32. options.Component.templateUrl = relativeTemplateUrlToAbs(options.Component.templateUrl, options.baseUrl);
  33. return () => {
  34. return {
  35. templateUrl: options.Component.templateUrl,
  36. template: options.Component.template,
  37. restrict: 'E',
  38. controller: options.Component,
  39. controllerAs: 'ctrl',
  40. bindToController: true,
  41. scope: options.bindings,
  42. link: (scope, elem, attrs, ctrl) => {
  43. if (ctrl.link) {
  44. ctrl.link(scope, elem, attrs, ctrl);
  45. }
  46. if (ctrl.init) {
  47. ctrl.init();
  48. }
  49. },
  50. };
  51. };
  52. }
  53. function loadPanelComponentInfo(scope, attrs) {
  54. const componentInfo: any = {
  55. name: 'panel-plugin-' + scope.panel.type,
  56. bindings: { dashboard: '=', panel: '=', row: '=' },
  57. attrs: {
  58. dashboard: 'dashboard',
  59. panel: 'panel',
  60. class: 'panel-height-helper',
  61. },
  62. };
  63. const panelInfo = config.panels[scope.panel.type];
  64. return importPanelPlugin(panelInfo.module).then(panelPlugin => {
  65. const angularPanelPlugin = panelPlugin as AngularPanelPlugin;
  66. const PanelCtrl = angularPanelPlugin.components.PanelCtrl;
  67. componentInfo.Component = PanelCtrl;
  68. if (!PanelCtrl || PanelCtrl.registered) {
  69. return componentInfo;
  70. }
  71. if (PanelCtrl.templatePromise) {
  72. return PanelCtrl.templatePromise.then(res => {
  73. return componentInfo;
  74. });
  75. }
  76. if (panelInfo) {
  77. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  78. }
  79. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  80. PanelCtrl.templateUrl = null;
  81. PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
  82. return componentInfo;
  83. });
  84. return PanelCtrl.templatePromise;
  85. });
  86. }
  87. function getModule(scope: any, attrs: any) {
  88. switch (attrs.type) {
  89. // QueryCtrl
  90. case 'query-ctrl': {
  91. const ds: DataSourceApi = scope.ctrl.datasource as DataSourceApi;
  92. return $q.when({
  93. baseUrl: ds.meta.baseUrl,
  94. name: 'query-ctrl-' + ds.meta.id,
  95. bindings: { target: '=', panelCtrl: '=', datasource: '=' },
  96. attrs: {
  97. target: 'ctrl.target',
  98. 'panel-ctrl': 'ctrl',
  99. datasource: 'ctrl.datasource',
  100. },
  101. Component: ds.components.QueryCtrl,
  102. });
  103. }
  104. // Annotations
  105. case 'annotations-query-ctrl': {
  106. return importDataSourcePlugin(scope.ctrl.currentDatasource.meta.module).then(dsPlugin => {
  107. return {
  108. baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
  109. name: 'annotations-query-ctrl-' + scope.ctrl.currentDatasource.meta.id,
  110. bindings: { annotation: '=', datasource: '=' },
  111. attrs: {
  112. annotation: 'ctrl.currentAnnotation',
  113. datasource: 'ctrl.currentDatasource',
  114. },
  115. Component: dsPlugin.components.AnnotationsQueryCtrl,
  116. };
  117. });
  118. }
  119. // Datasource ConfigCtrl
  120. case 'datasource-config-ctrl': {
  121. const dsMeta = scope.ctrl.datasourceMeta;
  122. return importDataSourcePlugin(dsMeta.module).then(dsPlugin => {
  123. scope.$watch(
  124. 'ctrl.current',
  125. () => {
  126. scope.onModelChanged(scope.ctrl.current);
  127. },
  128. true
  129. );
  130. return {
  131. baseUrl: dsMeta.baseUrl,
  132. name: 'ds-config-' + dsMeta.id,
  133. bindings: { meta: '=', current: '=' },
  134. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  135. Component: dsPlugin.components.ConfigCtrl,
  136. };
  137. });
  138. }
  139. // AppConfigCtrl
  140. case 'app-config-ctrl': {
  141. const model = scope.ctrl.model;
  142. return importAppPlugin(model).then(appPlugin => {
  143. return {
  144. baseUrl: model.baseUrl,
  145. name: 'app-config-' + model.id,
  146. bindings: { appModel: '=', appEditCtrl: '=' },
  147. attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
  148. Component: appPlugin.angular.ConfigCtrl,
  149. };
  150. });
  151. }
  152. // App Page
  153. case 'app-page': {
  154. const appModel = scope.ctrl.appModel;
  155. return importAppPlugin(appModel).then(appPlugin => {
  156. return {
  157. baseUrl: appModel.baseUrl,
  158. name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
  159. bindings: { appModel: '=' },
  160. attrs: { 'app-model': 'ctrl.appModel' },
  161. Component: appPlugin.angular.pages[scope.ctrl.page.component],
  162. };
  163. });
  164. }
  165. // Panel
  166. case 'panel': {
  167. return loadPanelComponentInfo(scope, attrs);
  168. }
  169. default: {
  170. return $q.reject({
  171. message: 'Could not find component type: ' + attrs.type,
  172. });
  173. }
  174. }
  175. }
  176. function appendAndCompile(scope, elem, componentInfo) {
  177. const child = angular.element(document.createElement(componentInfo.name));
  178. _.each(componentInfo.attrs, (value, key) => {
  179. child.attr(key, value);
  180. });
  181. $compile(child)(scope);
  182. elem.empty();
  183. // let a binding digest cycle complete before adding to dom
  184. setTimeout(() => {
  185. scope.$applyAsync(() => {
  186. elem.append(child);
  187. setTimeout(() => {
  188. scope.$applyAsync(() => {
  189. scope.$broadcast('component-did-mount');
  190. });
  191. });
  192. });
  193. });
  194. }
  195. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  196. if (componentInfo.notFound) {
  197. elem.empty();
  198. return;
  199. }
  200. if (!componentInfo.Component) {
  201. throw {
  202. message: 'Failed to find exported plugin component for ' + componentInfo.name,
  203. };
  204. }
  205. if (!componentInfo.Component.registered) {
  206. const directiveName = attrs.$normalize(componentInfo.name);
  207. const directiveFn = getPluginComponentDirective(componentInfo);
  208. coreModule.directive(directiveName, directiveFn);
  209. componentInfo.Component.registered = true;
  210. }
  211. appendAndCompile(scope, elem, componentInfo);
  212. }
  213. return {
  214. restrict: 'E',
  215. link: (scope, elem, attrs) => {
  216. getModule(scope, attrs)
  217. .then(componentInfo => {
  218. registerPluginComponent(scope, elem, attrs, componentInfo);
  219. })
  220. .catch(err => {
  221. console.log('Plugin component error', err);
  222. });
  223. },
  224. };
  225. }
  226. coreModule.directive('pluginComponent', pluginDirectiveLoader);