plugin_component.ts 8.4 KB

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