plugin_component.ts 7.7 KB

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