plugin_component.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import config from 'app/core/config';
  5. import coreModule from 'app/core/core_module';
  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) { return undefined; }
  23. if (templateUrl.indexOf('public') === 0) { return templateUrl; }
  24. return baseUrl + '/' + templateUrl;
  25. }
  26. function getPluginComponentDirective(options) {
  27. // handle relative template urls for plugin templates
  28. options.Component.templateUrl = relativeTemplateUrlToAbs(options.Component.templateUrl, options.baseUrl);
  29. return function() {
  30. return {
  31. templateUrl: options.Component.templateUrl,
  32. template: options.Component.template,
  33. restrict: 'E',
  34. controller: options.Component,
  35. controllerAs: 'ctrl',
  36. bindToController: true,
  37. scope: options.bindings,
  38. link: (scope, elem, attrs, ctrl) => {
  39. if (ctrl.link) {
  40. ctrl.link(scope, elem, attrs, ctrl);
  41. }
  42. if (ctrl.init) {
  43. ctrl.init();
  44. }
  45. }
  46. };
  47. };
  48. }
  49. function loadPanelComponentInfo(scope, attrs) {
  50. var componentInfo: any = {
  51. name: 'panel-plugin-' + scope.panel.type,
  52. bindings: {dashboard: "=", panel: "=", row: "="},
  53. attrs: {dashboard: "ctrl.dashboard", panel: "panel", row: "ctrl.row"},
  54. };
  55. let panelInfo = config.panels[scope.panel.type];
  56. var panelCtrlPromise = Promise.resolve(UnknownPanelCtrl);
  57. if (panelInfo) {
  58. panelCtrlPromise = System.import(panelInfo.module).then(function(panelModule) {
  59. return panelModule.PanelCtrl;
  60. });
  61. }
  62. return panelCtrlPromise.then(function(PanelCtrl: any) {
  63. componentInfo.Component = PanelCtrl;
  64. if (!PanelCtrl || PanelCtrl.registered) {
  65. return componentInfo;
  66. }
  67. if (PanelCtrl.templatePromise) {
  68. return PanelCtrl.templatePromise.then(res => {
  69. return componentInfo;
  70. });
  71. }
  72. if (panelInfo) {
  73. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  74. }
  75. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  76. PanelCtrl.templateUrl = null;
  77. PanelCtrl.template = `<grafana-panel ctrl="ctrl">${template}</grafana-panel>`;
  78. return componentInfo;
  79. });
  80. return PanelCtrl.templatePromise;
  81. });
  82. }
  83. function getModule(scope, attrs) {
  84. switch (attrs.type) {
  85. // QueryCtrl
  86. case "query-ctrl": {
  87. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  88. return datasourceSrv.get(datasource).then(ds => {
  89. scope.datasource = ds;
  90. return System.import(ds.meta.module).then(dsModule => {
  91. return {
  92. baseUrl: ds.meta.baseUrl,
  93. name: 'query-ctrl-' + ds.meta.id,
  94. bindings: {target: "=", panelCtrl: "=", datasource: "="},
  95. attrs: {"target": "target", "panel-ctrl": "ctrl", datasource: "datasource"},
  96. Component: dsModule.QueryCtrl
  97. };
  98. });
  99. });
  100. }
  101. // QueryOptionsCtrl
  102. case "query-options-ctrl": {
  103. return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
  104. return System.import(ds.meta.module).then((dsModule): any => {
  105. if (!dsModule.QueryOptionsCtrl) {
  106. return {notFound: true};
  107. }
  108. return {
  109. baseUrl: ds.meta.baseUrl,
  110. name: 'query-options-ctrl-' + ds.meta.id,
  111. bindings: {panelCtrl: "="},
  112. attrs: {"panel-ctrl": "ctrl"},
  113. Component: dsModule.QueryOptionsCtrl
  114. };
  115. });
  116. });
  117. }
  118. // Annotations
  119. case "annotations-query-ctrl": {
  120. return System.import(scope.ctrl.currentDatasource.meta.module).then(function(dsModule) {
  121. return {
  122. baseUrl: scope.ctrl.currentDatasource.meta.baseUrl,
  123. name: 'annotations-query-ctrl-' + scope.ctrl.currentDatasource.meta.id,
  124. bindings: {annotation: "=", datasource: "="},
  125. attrs: {"annotation": "ctrl.currentAnnotation", datasource: "ctrl.currentDatasource"},
  126. Component: dsModule.AnnotationsQueryCtrl,
  127. };
  128. });
  129. }
  130. // Datasource ConfigCtrl
  131. case 'datasource-config-ctrl': {
  132. var dsMeta = scope.ctrl.datasourceMeta;
  133. return System.import(dsMeta.module).then(function(dsModule): any {
  134. if (!dsModule.ConfigCtrl) {
  135. return {notFound: true};
  136. }
  137. return {
  138. baseUrl: dsMeta.baseUrl,
  139. name: 'ds-config-' + dsMeta.id,
  140. bindings: {meta: "=", current: "="},
  141. attrs: {meta: "ctrl.datasourceMeta", current: "ctrl.current"},
  142. Component: dsModule.ConfigCtrl,
  143. };
  144. });
  145. }
  146. // AppConfigCtrl
  147. case 'app-config-ctrl': {
  148. let model = scope.ctrl.model;
  149. return System.import(model.module).then(function(appModule) {
  150. return {
  151. baseUrl: model.baseUrl,
  152. name: 'app-config-' + model.id,
  153. bindings: {appModel: "=", appEditCtrl: "="},
  154. attrs: {"app-model": "ctrl.model", "app-edit-ctrl": "ctrl"},
  155. Component: appModule.ConfigCtrl,
  156. };
  157. });
  158. }
  159. // App Page
  160. case 'app-page': {
  161. let appModel = scope.ctrl.appModel;
  162. return System.import(appModel.module).then(function(appModule) {
  163. return {
  164. baseUrl: appModel.baseUrl,
  165. name: 'app-page-' + appModel.id + '-' + scope.ctrl.page.slug,
  166. bindings: {appModel: "="},
  167. attrs: {"app-model": "ctrl.appModel"},
  168. Component: appModule[scope.ctrl.page.component],
  169. };
  170. });
  171. }
  172. // Panel
  173. case 'panel': {
  174. return loadPanelComponentInfo(scope, attrs);
  175. }
  176. default: {
  177. return $q.reject({message: "Could not find component type: " + attrs.type });
  178. }
  179. }
  180. }
  181. function appendAndCompile(scope, elem, componentInfo) {
  182. var 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('refresh');
  193. });
  194. });
  195. }
  196. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  197. if (componentInfo.notFound) {
  198. elem.empty();
  199. return;
  200. }
  201. if (!componentInfo.Component) {
  202. throw {message: 'Failed to find exported plugin component for ' + componentInfo.name};
  203. }
  204. if (!componentInfo.Component.registered) {
  205. var directiveName = attrs.$normalize(componentInfo.name);
  206. var directiveFn = getPluginComponentDirective(componentInfo);
  207. coreModule.directive(directiveName, directiveFn);
  208. componentInfo.Component.registered = true;
  209. }
  210. appendAndCompile(scope, elem, componentInfo);
  211. }
  212. return {
  213. restrict: 'E',
  214. link: function(scope, elem, attrs) {
  215. getModule(scope, attrs).then(function (componentInfo) {
  216. registerPluginComponent(scope, elem, attrs, componentInfo);
  217. }).catch(err => {
  218. $rootScope.appEvent('alert-error', ['Plugin Error', err.message || err]);
  219. console.log('Plugin component error', err);
  220. });
  221. }
  222. };
  223. }
  224. coreModule.directive('pluginComponent', pluginDirectiveLoader);