plugin_component.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import angular, { ILocationService } from 'angular';
  2. import { each } from 'lodash';
  3. import { DataSourceApi, PanelEvents } from '@grafana/data';
  4. import coreModule from 'app/angular/core_module';
  5. import config from 'app/core/config';
  6. import { importPanelPlugin } from '../../features/plugins/importPanelPlugin';
  7. import { importDataSourcePlugin, importAppPlugin } from '../../features/plugins/plugin_loader';
  8. /** @ngInject */
  9. function pluginDirectiveLoader($compile: any, $http: any, $templateCache: any, $location: ILocationService) {
  10. function getTemplate(component: { template: any; templateUrl: any }) {
  11. if (component.template) {
  12. return Promise.resolve(component.template);
  13. }
  14. const cached = $templateCache.get(component.templateUrl);
  15. if (cached) {
  16. return Promise.resolve(cached);
  17. }
  18. return $http.get(component.templateUrl).then((res: any) => {
  19. return res.data;
  20. });
  21. }
  22. function relativeTemplateUrlToAbs(templateUrl: string, baseUrl: string) {
  23. if (!templateUrl) {
  24. return undefined;
  25. }
  26. if (templateUrl.indexOf('public') === 0) {
  27. return templateUrl;
  28. }
  29. return baseUrl + '/' + templateUrl;
  30. }
  31. function getPluginComponentDirective(options: any) {
  32. // handle relative template urls for plugin templates
  33. options.Component.templateUrl = relativeTemplateUrlToAbs(options.Component.templateUrl, options.baseUrl);
  34. return () => {
  35. return {
  36. templateUrl: options.Component.templateUrl,
  37. template: options.Component.template,
  38. restrict: 'E',
  39. controller: options.Component,
  40. controllerAs: 'ctrl',
  41. bindToController: true,
  42. scope: options.bindings,
  43. link: (scope: any, elem: any, attrs: any, ctrl: any) => {
  44. if (ctrl.link) {
  45. ctrl.link(scope, elem, attrs, ctrl);
  46. }
  47. if (ctrl.init) {
  48. ctrl.init();
  49. }
  50. },
  51. };
  52. };
  53. }
  54. function loadPanelComponentInfo(scope: any, attrs: any) {
  55. const componentInfo: any = {
  56. name: 'panel-plugin-' + scope.panel.type,
  57. bindings: { dashboard: '=', panel: '=', row: '=' },
  58. attrs: {
  59. dashboard: 'dashboard',
  60. panel: 'panel',
  61. class: 'panel-height-helper',
  62. },
  63. };
  64. const panelInfo = config.panels[scope.panel.type];
  65. return importPanelPlugin(panelInfo.id).then((panelPlugin) => {
  66. const PanelCtrl = panelPlugin.angularPanelCtrl;
  67. componentInfo.Component = PanelCtrl;
  68. if (!PanelCtrl || PanelCtrl.registered) {
  69. return componentInfo;
  70. }
  71. if (PanelCtrl.templatePromise) {
  72. return PanelCtrl.templatePromise.then((res: any) => {
  73. return componentInfo;
  74. });
  75. }
  76. if (panelInfo) {
  77. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  78. }
  79. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then((template: any) => {
  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): any {
  88. switch (attrs.type) {
  89. // QueryCtrl
  90. case 'query-ctrl': {
  91. const ds: DataSourceApi = scope.ctrl.datasource as DataSourceApi;
  92. return Promise.resolve({
  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. const baseUrl = scope.ctrl.currentDatasource.meta.baseUrl;
  107. const pluginId = scope.ctrl.currentDatasource.meta.id;
  108. return importDataSourcePlugin(scope.ctrl.currentDatasource.meta).then((dsPlugin) => {
  109. return {
  110. baseUrl,
  111. name: 'annotations-query-ctrl-' + pluginId,
  112. bindings: { annotation: '=', datasource: '=' },
  113. attrs: {
  114. annotation: 'ctrl.currentAnnotation',
  115. datasource: 'ctrl.currentDatasource',
  116. },
  117. Component: dsPlugin.components.AnnotationsQueryCtrl,
  118. };
  119. });
  120. }
  121. // Datasource ConfigCtrl
  122. case 'datasource-config-ctrl': {
  123. const dsMeta = scope.ctrl.datasourceMeta;
  124. const angularUrl = $location.url();
  125. return importDataSourcePlugin(dsMeta).then((dsPlugin) => {
  126. scope.$watch(
  127. 'ctrl.current',
  128. () => {
  129. // This watcher can trigger when we navigate away due to late digests
  130. // This check is to stop onModelChanged from being called when navigating away
  131. // as it triggers a redux action which comes before the angular $routeChangeSucces and
  132. // This makes the bridgeSrv think location changed from redux before detecting it was actually
  133. // changed from angular.
  134. if (angularUrl === $location.url()) {
  135. scope.onModelChanged(scope.ctrl.current);
  136. }
  137. },
  138. true
  139. );
  140. return {
  141. baseUrl: dsMeta.baseUrl,
  142. name: 'ds-config-' + dsMeta.id,
  143. bindings: { meta: '=', current: '=' },
  144. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  145. Component: dsPlugin.angularConfigCtrl,
  146. };
  147. });
  148. }
  149. // AppConfigCtrl
  150. case 'app-config-ctrl': {
  151. const model = scope.ctrl.model;
  152. return importAppPlugin(model).then((appPlugin) => {
  153. return {
  154. baseUrl: model.baseUrl,
  155. name: 'app-config-' + model.id,
  156. bindings: { appModel: '=', appEditCtrl: '=' },
  157. attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
  158. Component: appPlugin.angularConfigCtrl,
  159. };
  160. });
  161. }
  162. // Panel
  163. case 'panel': {
  164. return loadPanelComponentInfo(scope, attrs);
  165. }
  166. default: {
  167. return Promise.reject({
  168. message: 'Could not find component type: ' + attrs.type,
  169. });
  170. }
  171. }
  172. }
  173. function appendAndCompile(scope: any, elem: JQuery, componentInfo: any) {
  174. const child = angular.element(document.createElement(componentInfo.name));
  175. each(componentInfo.attrs, (value, key) => {
  176. child.attr(key, value);
  177. });
  178. $compile(child)(scope);
  179. elem.empty();
  180. // let a binding digest cycle complete before adding to dom
  181. setTimeout(() => {
  182. scope.$applyAsync(() => {
  183. elem.append(child);
  184. setTimeout(() => {
  185. scope.$applyAsync(() => {
  186. scope.$broadcast(PanelEvents.componentDidMount.name);
  187. });
  188. });
  189. });
  190. });
  191. }
  192. function registerPluginComponent(scope: any, elem: JQuery, attrs: any, componentInfo: any) {
  193. if (componentInfo.notFound) {
  194. elem.empty();
  195. return;
  196. }
  197. if (!componentInfo.Component) {
  198. throw {
  199. message: 'Failed to find exported plugin component for ' + componentInfo.name,
  200. };
  201. }
  202. if (!componentInfo.Component.registered) {
  203. const directiveName = attrs.$normalize(componentInfo.name);
  204. const directiveFn = getPluginComponentDirective(componentInfo);
  205. coreModule.directive(directiveName, directiveFn);
  206. componentInfo.Component.registered = true;
  207. }
  208. appendAndCompile(scope, elem, componentInfo);
  209. }
  210. return {
  211. restrict: 'E',
  212. link: (scope: any, elem: JQuery, attrs: any) => {
  213. getModule(scope, attrs)
  214. .then((componentInfo: any) => {
  215. registerPluginComponent(scope, elem, attrs, componentInfo);
  216. })
  217. .catch((err: any) => {
  218. console.error('Plugin component error', err);
  219. });
  220. },
  221. };
  222. }
  223. coreModule.directive('pluginComponent', pluginDirectiveLoader);