plugin_component.ts 7.8 KB

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