plugin_component.ts 7.7 KB

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