plugin_component.ts 7.8 KB

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