plugin_component.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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-editor-container">${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 datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  95. return datasourceSrv.get(datasource).then(ds => {
  96. scope.datasource = ds;
  97. return importPluginModule(ds.meta.module).then(dsModule => {
  98. return {
  99. baseUrl: ds.meta.baseUrl,
  100. name: 'query-ctrl-' + ds.meta.id,
  101. bindings: { target: '=', panelCtrl: '=', datasource: '=' },
  102. attrs: {
  103. target: 'target',
  104. 'panel-ctrl': 'ctrl.panelCtrl',
  105. datasource: 'datasource',
  106. },
  107. Component: dsModule.QueryCtrl,
  108. };
  109. });
  110. });
  111. }
  112. // Annotations
  113. case 'annotations-query-ctrl': {
  114. return importPluginModule(scope.ctrl.currentDatasource.meta.module).then(dsModule => {
  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: dsModule.AnnotationsQueryCtrl,
  124. };
  125. });
  126. }
  127. // Datasource ConfigCtrl
  128. case 'datasource-config-ctrl': {
  129. const dsMeta = scope.ctrl.datasourceMeta;
  130. return importPluginModule(dsMeta.module).then((dsModule): any => {
  131. if (!dsModule.ConfigCtrl) {
  132. return { notFound: true };
  133. }
  134. scope.$watch(
  135. 'ctrl.current',
  136. () => {
  137. scope.onModelChanged(scope.ctrl.current);
  138. },
  139. true
  140. );
  141. return {
  142. baseUrl: dsMeta.baseUrl,
  143. name: 'ds-config-' + dsMeta.id,
  144. bindings: { meta: '=', current: '=' },
  145. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  146. Component: dsModule.ConfigCtrl,
  147. };
  148. });
  149. }
  150. // AppConfigCtrl
  151. case 'app-config-ctrl': {
  152. const model = scope.ctrl.model;
  153. return importPluginModule(model.module).then(appModule => {
  154. return {
  155. baseUrl: model.baseUrl,
  156. name: 'app-config-' + model.id,
  157. bindings: { appModel: '=', appEditCtrl: '=' },
  158. attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
  159. Component: appModule.ConfigCtrl,
  160. };
  161. });
  162. }
  163. // App Page
  164. case 'app-page': {
  165. const appModel = scope.ctrl.appModel;
  166. return importPluginModule(appModel.module).then(appModule => {
  167. return {
  168. baseUrl: appModel.baseUrl,
  169. name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
  170. bindings: { appModel: '=' },
  171. attrs: { 'app-model': 'ctrl.appModel' },
  172. Component: appModule[scope.ctrl.page.component],
  173. };
  174. });
  175. }
  176. // Panel
  177. case 'panel': {
  178. return loadPanelComponentInfo(scope, attrs);
  179. }
  180. default: {
  181. return $q.reject({
  182. message: 'Could not find component type: ' + attrs.type,
  183. });
  184. }
  185. }
  186. }
  187. function appendAndCompile(scope, elem, componentInfo) {
  188. const child = angular.element(document.createElement(componentInfo.name));
  189. _.each(componentInfo.attrs, (value, key) => {
  190. child.attr(key, value);
  191. });
  192. $compile(child)(scope);
  193. elem.empty();
  194. // let a binding digest cycle complete before adding to dom
  195. setTimeout(() => {
  196. scope.$applyAsync(() => {
  197. elem.append(child);
  198. setTimeout(() => {
  199. scope.$applyAsync(() => {
  200. scope.$broadcast('component-did-mount');
  201. });
  202. });
  203. });
  204. });
  205. }
  206. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  207. if (componentInfo.notFound) {
  208. elem.empty();
  209. return;
  210. }
  211. if (!componentInfo.Component) {
  212. throw {
  213. message: 'Failed to find exported plugin component for ' + componentInfo.name,
  214. };
  215. }
  216. if (!componentInfo.Component.registered) {
  217. const directiveName = attrs.$normalize(componentInfo.name);
  218. const directiveFn = getPluginComponentDirective(componentInfo);
  219. coreModule.directive(directiveName, directiveFn);
  220. componentInfo.Component.registered = true;
  221. }
  222. appendAndCompile(scope, elem, componentInfo);
  223. }
  224. return {
  225. restrict: 'E',
  226. link: (scope, elem, attrs) => {
  227. getModule(scope, attrs)
  228. .then(componentInfo => {
  229. registerPluginComponent(scope, elem, attrs, componentInfo);
  230. })
  231. .catch(err => {
  232. console.log('Plugin component error', err);
  233. });
  234. },
  235. };
  236. }
  237. coreModule.directive('pluginComponent', pluginDirectiveLoader);