plugin_component.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. import { DashboardRowCtrl } from './row_ctrl';
  8. /** @ngInject **/
  9. function pluginDirectiveLoader($compile, datasourceSrv, $rootScope, $q, $http, $templateCache) {
  10. function getTemplate(component) {
  11. if (component.template) {
  12. return $q.when(component.template);
  13. }
  14. var cached = $templateCache.get(component.templateUrl);
  15. if (cached) {
  16. return $q.when(cached);
  17. }
  18. return $http.get(component.templateUrl).then(res => {
  19. return res.data;
  20. });
  21. }
  22. function relativeTemplateUrlToAbs(templateUrl, baseUrl) {
  23. if (!templateUrl) {
  24. return undefined;
  25. }
  26. if (templateUrl.indexOf('public') === 0) {
  27. return templateUrl;
  28. }
  29. return baseUrl + '/' + templateUrl;
  30. }
  31. function getPluginComponentDirective(options) {
  32. // handle relative template urls for plugin templates
  33. options.Component.templateUrl = relativeTemplateUrlToAbs(options.Component.templateUrl, options.baseUrl);
  34. return function() {
  35. return {
  36. templateUrl: options.Component.templateUrl,
  37. template: options.Component.template,
  38. restrict: 'E',
  39. controller: options.Component,
  40. controllerAs: 'ctrl',
  41. bindToController: true,
  42. scope: options.bindings,
  43. link: (scope, elem, attrs, ctrl) => {
  44. if (ctrl.link) {
  45. ctrl.link(scope, elem, attrs, ctrl);
  46. }
  47. if (ctrl.init) {
  48. ctrl.init();
  49. }
  50. },
  51. };
  52. };
  53. }
  54. function loadPanelComponentInfo(scope, attrs) {
  55. if (scope.panel.type === 'row') {
  56. return $q.when({
  57. name: 'dashboard-row',
  58. bindings: { dashboard: '=', panel: '=' },
  59. attrs: { dashboard: 'ctrl.dashboard', panel: 'panel' },
  60. Component: DashboardRowCtrl,
  61. });
  62. }
  63. var componentInfo: any = {
  64. name: 'panel-plugin-' + scope.panel.type,
  65. bindings: { dashboard: '=', panel: '=', row: '=' },
  66. attrs: {
  67. dashboard: 'dashboard',
  68. panel: 'panel',
  69. class: 'panel-height-helper',
  70. },
  71. };
  72. let panelInfo = config.panels[scope.panel.type];
  73. var panelCtrlPromise = Promise.resolve(UnknownPanelCtrl);
  74. if (panelInfo) {
  75. panelCtrlPromise = importPluginModule(panelInfo.module).then(function(panelModule) {
  76. return panelModule.PanelCtrl;
  77. });
  78. }
  79. return panelCtrlPromise.then(function(PanelCtrl: any) {
  80. componentInfo.Component = PanelCtrl;
  81. if (!PanelCtrl || PanelCtrl.registered) {
  82. return componentInfo;
  83. }
  84. if (PanelCtrl.templatePromise) {
  85. return PanelCtrl.templatePromise.then(res => {
  86. return componentInfo;
  87. });
  88. }
  89. if (panelInfo) {
  90. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  91. }
  92. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  93. PanelCtrl.templateUrl = null;
  94. PanelCtrl.template = `<grafana-panel ctrl="ctrl" class="panel-height-helper">${template}</grafana-panel>`;
  95. return componentInfo;
  96. });
  97. return PanelCtrl.templatePromise;
  98. });
  99. }
  100. function getModule(scope, attrs) {
  101. switch (attrs.type) {
  102. // QueryCtrl
  103. case 'query-ctrl': {
  104. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  105. return datasourceSrv.get(datasource).then(ds => {
  106. scope.datasource = ds;
  107. return importPluginModule(ds.meta.module).then(dsModule => {
  108. return {
  109. baseUrl: ds.meta.baseUrl,
  110. name: 'query-ctrl-' + ds.meta.id,
  111. bindings: { target: '=', panelCtrl: '=', datasource: '=' },
  112. attrs: {
  113. target: 'target',
  114. 'panel-ctrl': 'ctrl.panelCtrl',
  115. datasource: 'datasource',
  116. },
  117. Component: dsModule.QueryCtrl,
  118. };
  119. });
  120. });
  121. }
  122. // QueryOptionsCtrl
  123. case 'query-options-ctrl': {
  124. return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
  125. return importPluginModule(ds.meta.module).then((dsModule): any => {
  126. if (!dsModule.QueryOptionsCtrl) {
  127. return { notFound: true };
  128. }
  129. return {
  130. baseUrl: ds.meta.baseUrl,
  131. name: 'query-options-ctrl-' + ds.meta.id,
  132. bindings: { panelCtrl: '=' },
  133. attrs: { 'panel-ctrl': 'ctrl.panelCtrl' },
  134. Component: dsModule.QueryOptionsCtrl,
  135. };
  136. });
  137. });
  138. }
  139. // Annotations
  140. case 'annotations-query-ctrl': {
  141. return importPluginModule(scope.ctrl.currentDatasource.meta.module).then(function(dsModule) {
  142. return {
  143. baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
  144. name: 'annotations-query-ctrl-' + scope.ctrl.currentDatasource.meta.id,
  145. bindings: { annotation: '=', datasource: '=' },
  146. attrs: {
  147. annotation: 'ctrl.currentAnnotation',
  148. datasource: 'ctrl.currentDatasource',
  149. },
  150. Component: dsModule.AnnotationsQueryCtrl,
  151. };
  152. });
  153. }
  154. // Datasource ConfigCtrl
  155. case 'datasource-config-ctrl': {
  156. var dsMeta = scope.ctrl.datasourceMeta;
  157. return importPluginModule(dsMeta.module).then(function(dsModule): any {
  158. if (!dsModule.ConfigCtrl) {
  159. return { notFound: true };
  160. }
  161. return {
  162. baseUrl: dsMeta.baseUrl,
  163. name: 'ds-config-' + dsMeta.id,
  164. bindings: { meta: '=', current: '=' },
  165. attrs: { meta: 'ctrl.datasourceMeta', current: 'ctrl.current' },
  166. Component: dsModule.ConfigCtrl,
  167. };
  168. });
  169. }
  170. // AppConfigCtrl
  171. case 'app-config-ctrl': {
  172. let model = scope.ctrl.model;
  173. return importPluginModule(model.module).then(function(appModule) {
  174. return {
  175. baseUrl: model.baseUrl,
  176. name: 'app-config-' + model.id,
  177. bindings: { appModel: '=', appEditCtrl: '=' },
  178. attrs: { 'app-model': 'ctrl.model', 'app-edit-ctrl': 'ctrl' },
  179. Component: appModule.ConfigCtrl,
  180. };
  181. });
  182. }
  183. // App Page
  184. case 'app-page': {
  185. let appModel = scope.ctrl.appModel;
  186. return importPluginModule(appModel.module).then(function(appModule) {
  187. return {
  188. baseUrl: appModel.baseUrl,
  189. name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
  190. bindings: { appModel: '=' },
  191. attrs: { 'app-model': 'ctrl.appModel' },
  192. Component: appModule[scope.ctrl.page.component],
  193. };
  194. });
  195. }
  196. // Panel
  197. case 'panel': {
  198. return loadPanelComponentInfo(scope, attrs);
  199. }
  200. default: {
  201. return $q.reject({
  202. message: 'Could not find component type: ' + attrs.type,
  203. });
  204. }
  205. }
  206. }
  207. function appendAndCompile(scope, elem, componentInfo) {
  208. var child = angular.element(document.createElement(componentInfo.name));
  209. _.each(componentInfo.attrs, (value, key) => {
  210. child.attr(key, value);
  211. });
  212. $compile(child)(scope);
  213. elem.empty();
  214. // let a binding digest cycle complete before adding to dom
  215. setTimeout(function() {
  216. elem.append(child);
  217. scope.$applyAsync(function() {
  218. scope.$broadcast('component-did-mount');
  219. scope.$broadcast('refresh');
  220. });
  221. });
  222. }
  223. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  224. if (componentInfo.notFound) {
  225. elem.empty();
  226. return;
  227. }
  228. if (!componentInfo.Component) {
  229. throw {
  230. message: 'Failed to find exported plugin component for ' + componentInfo.name,
  231. };
  232. }
  233. if (!componentInfo.Component.registered) {
  234. var directiveName = attrs.$normalize(componentInfo.name);
  235. var directiveFn = getPluginComponentDirective(componentInfo);
  236. coreModule.directive(directiveName, directiveFn);
  237. componentInfo.Component.registered = true;
  238. }
  239. appendAndCompile(scope, elem, componentInfo);
  240. }
  241. return {
  242. restrict: 'E',
  243. link: function(scope, elem, attrs) {
  244. getModule(scope, attrs)
  245. .then(function(componentInfo) {
  246. registerPluginComponent(scope, elem, attrs, componentInfo);
  247. })
  248. .catch(err => {
  249. $rootScope.appEvent('alert-error', ['Plugin Error', err.message || err]);
  250. console.log('Plugin component error', err);
  251. });
  252. },
  253. };
  254. }
  255. coreModule.directive('pluginComponent', pluginDirectiveLoader);