plugin_component.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 { 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: string, baseUrl: string) {
  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.id).then(panelPlugin => {
  65. const PanelCtrl = panelPlugin.angularPanelCtrl;
  66. componentInfo.Component = PanelCtrl;
  67. if (!PanelCtrl || PanelCtrl.registered) {
  68. return componentInfo;
  69. }
  70. if (PanelCtrl.templatePromise) {
  71. return PanelCtrl.templatePromise.then(res => {
  72. return componentInfo;
  73. });
  74. }
  75. if (panelInfo) {
  76. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  77. }
  78. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  79. PanelCtrl.templateUrl = null;
  80. PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
  81. return componentInfo;
  82. });
  83. return PanelCtrl.templatePromise;
  84. });
  85. }
  86. function getModule(scope: any, attrs: any) {
  87. switch (attrs.type) {
  88. // QueryCtrl
  89. case 'query-ctrl': {
  90. const ds: DataSourceApi = scope.ctrl.datasource as DataSourceApi;
  91. return $q.when({
  92. baseUrl: ds.meta.baseUrl,
  93. name: 'query-ctrl-' + ds.meta.id,
  94. bindings: { target: '=', panelCtrl: '=', datasource: '=' },
  95. attrs: {
  96. target: 'ctrl.target',
  97. 'panel-ctrl': 'ctrl',
  98. datasource: 'ctrl.datasource',
  99. },
  100. Component: ds.components.QueryCtrl,
  101. });
  102. }
  103. // Annotations
  104. case 'annotations-query-ctrl': {
  105. return importDataSourcePlugin(scope.ctrl.currentDatasource.meta).then(dsPlugin => {
  106. return {
  107. baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
  108. name: 'annotations-query-ctrl-' + scope.ctrl.currentDatasource.meta.id,
  109. bindings: { annotation: '=', datasource: '=' },
  110. attrs: {
  111. annotation: 'ctrl.currentAnnotation',
  112. datasource: 'ctrl.currentDatasource',
  113. },
  114. Component: dsPlugin.components.AnnotationsQueryCtrl,
  115. };
  116. });
  117. }
  118. // Datasource ConfigCtrl
  119. case 'datasource-config-ctrl': {
  120. const dsMeta = scope.ctrl.datasourceMeta;
  121. return importDataSourcePlugin(dsMeta).then(dsPlugin => {
  122. scope.$watch(
  123. 'ctrl.current',
  124. () => {
  125. scope.onModelChanged(scope.ctrl.current);
  126. },
  127. true
  128. );
  129. return {
  130. baseUrl: dsMeta.baseUrl,
  131. name: 'ds-config-' + dsMeta.id,
  132. bindings: { meta: '=', current: '=' },
  133. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  134. Component: dsPlugin.components.ConfigCtrl,
  135. };
  136. });
  137. }
  138. // AppConfigCtrl
  139. case 'app-config-ctrl': {
  140. const model = scope.ctrl.model;
  141. return importAppPlugin(model).then(appPlugin => {
  142. return {
  143. baseUrl: model.baseUrl,
  144. name: 'app-config-' + model.id,
  145. bindings: { appModel: '=', appEditCtrl: '=' },
  146. attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
  147. Component: appPlugin.angular.ConfigCtrl,
  148. };
  149. });
  150. }
  151. // App Page
  152. case 'app-page': {
  153. const appModel = scope.ctrl.appModel;
  154. return importAppPlugin(appModel).then(appPlugin => {
  155. return {
  156. baseUrl: appModel.baseUrl,
  157. name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
  158. bindings: { appModel: '=' },
  159. attrs: { 'app-model': 'ctrl.appModel' },
  160. Component: appPlugin.angular.pages[scope.ctrl.page.component],
  161. };
  162. });
  163. }
  164. // Panel
  165. case 'panel': {
  166. return loadPanelComponentInfo(scope, attrs);
  167. }
  168. default: {
  169. return $q.reject({
  170. message: 'Could not find component type: ' + attrs.type,
  171. });
  172. }
  173. }
  174. }
  175. function appendAndCompile(scope, elem, componentInfo) {
  176. const child = angular.element(document.createElement(componentInfo.name));
  177. _.each(componentInfo.attrs, (value, key) => {
  178. child.attr(key, value);
  179. });
  180. $compile(child)(scope);
  181. elem.empty();
  182. // let a binding digest cycle complete before adding to dom
  183. setTimeout(() => {
  184. scope.$applyAsync(() => {
  185. elem.append(child);
  186. setTimeout(() => {
  187. scope.$applyAsync(() => {
  188. scope.$broadcast('component-did-mount');
  189. });
  190. });
  191. });
  192. });
  193. }
  194. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  195. if (componentInfo.notFound) {
  196. elem.empty();
  197. return;
  198. }
  199. if (!componentInfo.Component) {
  200. throw {
  201. message: 'Failed to find exported plugin component for ' + componentInfo.name,
  202. };
  203. }
  204. if (!componentInfo.Component.registered) {
  205. const directiveName = attrs.$normalize(componentInfo.name);
  206. const directiveFn = getPluginComponentDirective(componentInfo);
  207. coreModule.directive(directiveName, directiveFn);
  208. componentInfo.Component.registered = true;
  209. }
  210. appendAndCompile(scope, elem, componentInfo);
  211. }
  212. return {
  213. restrict: 'E',
  214. link: (scope, elem, attrs) => {
  215. getModule(scope, attrs)
  216. .then(componentInfo => {
  217. registerPluginComponent(scope, elem, attrs, componentInfo);
  218. })
  219. .catch(err => {
  220. console.log('Plugin component error', err);
  221. });
  222. },
  223. };
  224. }
  225. coreModule.directive('pluginComponent', pluginDirectiveLoader);