plugin_component.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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: "dashboard", panel: "panel", row: "row"},
  54. };
  55. var panelElemName = 'panel-' + scope.panel.type;
  56. let panelInfo = config.panels[scope.panel.type];
  57. var panelCtrlPromise = Promise.resolve(UnknownPanelCtrl);
  58. if (panelInfo) {
  59. panelCtrlPromise = System.import(panelInfo.module).then(function(panelModule) {
  60. return panelModule.PanelCtrl;
  61. });
  62. }
  63. return panelCtrlPromise.then(function(PanelCtrl: any) {
  64. componentInfo.Component = PanelCtrl;
  65. if (!PanelCtrl || PanelCtrl.registered) {
  66. return componentInfo;
  67. };
  68. if (PanelCtrl.templatePromise) {
  69. return PanelCtrl.templatePromise.then(res => {
  70. return componentInfo;
  71. });
  72. }
  73. if (panelInfo) {
  74. PanelCtrl.templateUrl = relativeTemplateUrlToAbs(PanelCtrl.templateUrl, panelInfo.baseUrl);
  75. }
  76. PanelCtrl.templatePromise = getTemplate(PanelCtrl).then(template => {
  77. PanelCtrl.templateUrl = null;
  78. PanelCtrl.template = `<grafana-panel ctrl="ctrl">${template}</grafana-panel>`;
  79. return componentInfo;
  80. });
  81. return PanelCtrl.templatePromise;
  82. });
  83. }
  84. function getModule(scope, attrs) {
  85. switch (attrs.type) {
  86. // QueryCtrl
  87. case "query-ctrl": {
  88. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  89. return datasourceSrv.get(datasource).then(ds => {
  90. scope.datasource = ds;
  91. return System.import(ds.meta.module).then(dsModule => {
  92. return {
  93. baseUrl: ds.meta.baseUrl,
  94. name: 'query-ctrl-' + ds.meta.id,
  95. bindings: {target: "=", panelCtrl: "=", datasource: "="},
  96. attrs: {"target": "target", "panel-ctrl": "ctrl", datasource: "datasource"},
  97. Component: dsModule.QueryCtrl
  98. };
  99. });
  100. });
  101. }
  102. // QueryOptionsCtrl
  103. case "query-options-ctrl": {
  104. return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
  105. return System.import(ds.meta.module).then((dsModule): any => {
  106. if (!dsModule.QueryOptionsCtrl) {
  107. return {notFound: true};
  108. }
  109. return {
  110. baseUrl: ds.meta.baseUrl,
  111. name: 'query-options-ctrl-' + ds.meta.id,
  112. bindings: {panelCtrl: "="},
  113. attrs: {"panel-ctrl": "ctrl"},
  114. Component: dsModule.QueryOptionsCtrl
  115. };
  116. });
  117. });
  118. }
  119. // Annotations
  120. case "annotations-query-ctrl": {
  121. return System.import(scope.currentDatasource.meta.module).then(function(dsModule) {
  122. return {
  123. baseUrl: scope.currentDatasource.meta.baseUrl,
  124. name: 'annotations-query-ctrl-' + scope.currentDatasource.meta.id,
  125. bindings: {annotation: "=", datasource: "="},
  126. attrs: {"annotation": "currentAnnotation", datasource: "currentDatasource"},
  127. Component: dsModule.AnnotationsQueryCtrl,
  128. };
  129. });
  130. }
  131. // Datasource ConfigCtrl
  132. case 'datasource-config-ctrl': {
  133. var dsMeta = scope.ctrl.datasourceMeta;
  134. return System.import(dsMeta.module).then(function(dsModule): any {
  135. if (!dsModule.ConfigCtrl) {
  136. return {notFound: true};
  137. }
  138. return {
  139. baseUrl: dsMeta.baseUrl,
  140. name: 'ds-config-' + dsMeta.id,
  141. bindings: {meta: "=", current: "="},
  142. attrs: {meta: "ctrl.datasourceMeta", current: "ctrl.current"},
  143. Component: dsModule.ConfigCtrl,
  144. };
  145. });
  146. }
  147. // AppConfigCtrl
  148. case 'app-config-ctrl': {
  149. let model = scope.ctrl.model;
  150. return System.import(model.module).then(function(appModule) {
  151. return {
  152. baseUrl: model.baseUrl,
  153. name: 'app-config-' + model.id,
  154. bindings: {appModel: "=", appEditCtrl: "="},
  155. attrs: {"app-model": "ctrl.model", "app-edit-ctrl": "ctrl"},
  156. Component: appModule.ConfigCtrl,
  157. };
  158. });
  159. }
  160. // App Page
  161. case 'app-page': {
  162. let appModel = scope.ctrl.appModel;
  163. return System.import(appModel.module).then(function(appModule) {
  164. return {
  165. baseUrl: appModel.baseUrl,
  166. name: 'app-page-' + appModel.appId + '-' + scope.ctrl.page.slug,
  167. bindings: {appModel: "="},
  168. attrs: {"app-model": "ctrl.appModel"},
  169. Component: appModule[scope.ctrl.page.component],
  170. };
  171. });
  172. }
  173. // Panel
  174. case 'panel': {
  175. return loadPanelComponentInfo(scope, attrs);
  176. }
  177. default: {
  178. return $q.reject({message: "Could not find component type: " + attrs.type });
  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.$apply(function() {
  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 {message: 'Failed to find exported plugin component for ' + componentInfo.name};
  204. }
  205. if (!componentInfo.Component.registered) {
  206. var directiveName = attrs.$normalize(componentInfo.name);
  207. var directiveFn = getPluginComponentDirective(componentInfo);
  208. coreModule.directive(directiveName, directiveFn);
  209. componentInfo.Component.registered = true;
  210. }
  211. appendAndCompile(scope, elem, componentInfo);
  212. }
  213. return {
  214. restrict: 'E',
  215. link: function(scope, elem, attrs) {
  216. getModule(scope, attrs).then(function (componentInfo) {
  217. registerPluginComponent(scope, elem, attrs, componentInfo);
  218. }).catch(err => {
  219. $rootScope.appEvent('alert-error', ['Plugin Error', err.message || err]);
  220. console.log('Plugin componnet error', err);
  221. });
  222. }
  223. };
  224. }
  225. coreModule.directive('pluginComponent', pluginDirectiveLoader);