plugin_component.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. /** @ngInject */
  7. function pluginDirectiveLoader($compile, datasourceSrv, $rootScope, $q, $http, $templateCache, $timeout) {
  8. function getTemplate(component) {
  9. if (component.template) {
  10. return $q.when(component.template);
  11. }
  12. const cached = $templateCache.get(component.templateUrl);
  13. if (cached) {
  14. return $q.when(cached);
  15. }
  16. return $http.get(component.templateUrl).then(res => {
  17. return res.data;
  18. });
  19. }
  20. function relativeTemplateUrlToAbs(templateUrl, baseUrl) {
  21. if (!templateUrl) {
  22. return undefined;
  23. }
  24. if (templateUrl.indexOf('public') === 0) {
  25. return templateUrl;
  26. }
  27. return baseUrl + '/' + templateUrl;
  28. }
  29. function getPluginComponentDirective(options) {
  30. // handle relative template urls for plugin templates
  31. options.Component.templateUrl = relativeTemplateUrlToAbs(options.Component.templateUrl, options.baseUrl);
  32. return () => {
  33. return {
  34. templateUrl: options.Component.templateUrl,
  35. template: options.Component.template,
  36. restrict: 'E',
  37. controller: options.Component,
  38. controllerAs: 'ctrl',
  39. bindToController: true,
  40. scope: options.bindings,
  41. link: (scope, elem, attrs, ctrl) => {
  42. if (ctrl.link) {
  43. ctrl.link(scope, elem, attrs, ctrl);
  44. }
  45. if (ctrl.init) {
  46. ctrl.init();
  47. }
  48. },
  49. };
  50. };
  51. }
  52. function loadPanelComponentInfo(scope, attrs) {
  53. const componentInfo: any = {
  54. name: 'panel-plugin-' + scope.panel.type,
  55. bindings: { dashboard: '=', panel: '=', row: '=' },
  56. attrs: {
  57. dashboard: 'dashboard',
  58. panel: 'panel',
  59. class: 'panel-height-helper',
  60. },
  61. };
  62. const panelInfo = config.panels[scope.panel.type];
  63. let panelCtrlPromise = Promise.resolve(null);
  64. if (panelInfo) {
  65. panelCtrlPromise = importPluginModule(panelInfo.module).then(panelModule => {
  66. return panelModule.PanelCtrl;
  67. });
  68. }
  69. return panelCtrlPromise.then((PanelCtrl: any) => {
  70. componentInfo.Component = PanelCtrl;
  71. if (!PanelCtrl || PanelCtrl.registered) {
  72. return componentInfo;
  73. }
  74. if (PanelCtrl.templatePromise) {
  75. return PanelCtrl.templatePromise.then(res => {
  76. return componentInfo;
  77. });
  78. }
  79. if (panelInfo) {
  80. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  81. }
  82. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  83. PanelCtrl.templateUrl = null;
  84. PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
  85. return componentInfo;
  86. });
  87. return PanelCtrl.templatePromise;
  88. });
  89. }
  90. function getModule(scope, attrs) {
  91. switch (attrs.type) {
  92. // QueryCtrl
  93. case 'query-ctrl': {
  94. const ds = scope.ctrl.datasource;
  95. return $q.when({
  96. baseUrl: ds.meta.baseUrl,
  97. name: 'query-ctrl-' + ds.meta.id,
  98. bindings: { target: '=', panelCtrl: '=', datasource: '=' },
  99. attrs: {
  100. target: 'ctrl.target',
  101. 'panel-ctrl': 'ctrl',
  102. datasource: 'ctrl.datasource',
  103. },
  104. Component: ds.pluginExports.QueryCtrl,
  105. });
  106. }
  107. // Annotations
  108. case 'annotations-query-ctrl': {
  109. return importPluginModule(scope.ctrl.currentDatasource.meta.module).then(dsModule => {
  110. return {
  111. baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
  112. name: 'annotations-query-ctrl-' + scope.ctrl.currentDatasource.meta.id,
  113. bindings: { annotation: '=', datasource: '=' },
  114. attrs: {
  115. annotation: 'ctrl.currentAnnotation',
  116. datasource: 'ctrl.currentDatasource',
  117. },
  118. Component: dsModule.AnnotationsQueryCtrl,
  119. };
  120. });
  121. }
  122. // Datasource ConfigCtrl
  123. case 'datasource-config-ctrl': {
  124. const dsMeta = scope.ctrl.datasourceMeta;
  125. return importPluginModule(dsMeta.module).then(
  126. (dsModule): any => {
  127. if (!dsModule.ConfigCtrl) {
  128. return { notFound: true };
  129. }
  130. scope.$watch(
  131. 'ctrl.current',
  132. () => {
  133. scope.onModelChanged(scope.ctrl.current);
  134. },
  135. true
  136. );
  137. return {
  138. baseUrl: dsMeta.baseUrl,
  139. name: 'ds-config-' + dsMeta.id,
  140. bindings: { meta: '=', current: '=' },
  141. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  142. Component: dsModule.ConfigCtrl,
  143. };
  144. }
  145. );
  146. }
  147. // AppConfigCtrl
  148. case 'app-config-ctrl': {
  149. const model = scope.ctrl.model;
  150. return importPluginModule(model.module).then(appModule => {
  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: appModule.ConfigCtrl,
  157. };
  158. });
  159. }
  160. // App Page
  161. case 'app-page': {
  162. const appModel = scope.ctrl.appModel;
  163. return importPluginModule(appModel.module).then(appModule => {
  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: appModule[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, elem, componentInfo) {
  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, elem, attrs, componentInfo) {
  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, elem, attrs) => {
  224. getModule(scope, attrs)
  225. .then(componentInfo => {
  226. registerPluginComponent(scope, elem, attrs, componentInfo);
  227. })
  228. .catch(err => {
  229. console.log('Plugin component error', err);
  230. });
  231. },
  232. };
  233. }
  234. coreModule.directive('pluginComponent', pluginDirectiveLoader);