plugin_component.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // ConfigCtrl
  132. case 'datasource-config-ctrl': {
  133. return System.import(scope.datasourceMeta.module).then(function(dsModule) {
  134. return {
  135. baseUrl: scope.datasourceMeta.baseUrl,
  136. name: 'ds-config-' + scope.datasourceMeta.id,
  137. bindings: {meta: "=", current: "="},
  138. attrs: {meta: "datasourceMeta", current: "current"},
  139. Component: dsModule.ConfigCtrl,
  140. };
  141. });
  142. }
  143. // AppConfigCtrl
  144. case 'app-config-ctrl': {
  145. let appModel = scope.ctrl.appModel;
  146. return System.import(appModel.module).then(function(appModule) {
  147. return {
  148. baseUrl: appModel.baseUrl,
  149. name: 'app-config-' + appModel.appId,
  150. bindings: {appModel: "=", appEditCtrl: "="},
  151. attrs: {"app-model": "ctrl.appModel", "app-edit-ctrl": "ctrl"},
  152. Component: appModule.ConfigCtrl,
  153. };
  154. });
  155. }
  156. // App Page
  157. case 'app-page': {
  158. let appModel = scope.ctrl.appModel;
  159. return System.import(appModel.module).then(function(appModule) {
  160. return {
  161. baseUrl: appModel.baseUrl,
  162. name: 'app-page-' + appModel.appId + '-' + scope.ctrl.page.slug,
  163. bindings: {appModel: "="},
  164. attrs: {"app-model": "ctrl.appModel"},
  165. Component: appModule[scope.ctrl.page.component],
  166. };
  167. });
  168. }
  169. // Panel
  170. case 'panel': {
  171. return loadPanelComponentInfo(scope, attrs);
  172. }
  173. default: {
  174. return $q.reject({message: "Could not find component type: " + attrs.type });
  175. }
  176. }
  177. }
  178. function appendAndCompile(scope, elem, componentInfo) {
  179. var child = angular.element(document.createElement(componentInfo.name));
  180. _.each(componentInfo.attrs, (value, key) => {
  181. child.attr(key, value);
  182. });
  183. $compile(child)(scope);
  184. elem.empty();
  185. elem.append(child);
  186. }
  187. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  188. if (componentInfo.notFound) {
  189. elem.empty();
  190. return;
  191. }
  192. if (!componentInfo.Component) {
  193. throw {message: 'Failed to find exported plugin component for ' + componentInfo.name};
  194. }
  195. if (!componentInfo.Component.registered) {
  196. var directiveName = attrs.$normalize(componentInfo.name);
  197. var directiveFn = getPluginComponentDirective(componentInfo);
  198. coreModule.directive(directiveName, directiveFn);
  199. componentInfo.Component.registered = true;
  200. }
  201. appendAndCompile(scope, elem, componentInfo);
  202. }
  203. return {
  204. restrict: 'E',
  205. link: function(scope, elem, attrs) {
  206. getModule(scope, attrs).then(function (componentInfo) {
  207. registerPluginComponent(scope, elem, attrs, componentInfo);
  208. }).catch(err => {
  209. $rootScope.appEvent('alert-error', ['Plugin Error', err.message || err]);
  210. console.log('Plugin componnet error', err);
  211. });
  212. }
  213. };
  214. }
  215. coreModule.directive('pluginComponent', pluginDirectiveLoader);