panel_loader.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import config from 'app/core/config';
  4. import {UnknownPanel} from '../../plugins/panel/unknown/module';
  5. var directiveModule = angular.module('grafana.directives');
  6. /** @ngInject */
  7. function panelLoader($compile, dynamicDirectiveSrv, $http, $q, $injector) {
  8. return {
  9. restrict: 'E',
  10. scope: {
  11. dashboard: "=",
  12. row: "=",
  13. panel: "="
  14. },
  15. link: function(scope, elem, attrs) {
  16. function getTemplate(directive) {
  17. if (directive.template) {
  18. return $q.when(directive.template);
  19. }
  20. return $http.get(directive.templateUrl).then(res => {
  21. return res.data;
  22. });
  23. }
  24. function addPanelAndCompile(name) {
  25. var child = angular.element(document.createElement(name));
  26. child.attr('dashboard', 'dashboard');
  27. child.attr('panel', 'panel');
  28. child.attr('row', 'row');
  29. $compile(child)(scope);
  30. elem.empty();
  31. elem.append(child);
  32. }
  33. function addPanel(name, Panel) {
  34. if (Panel.registered) {
  35. addPanelAndCompile(name);
  36. return;
  37. }
  38. if (Panel.promise) {
  39. Panel.promise.then(() => {
  40. addPanelAndCompile(name);
  41. });
  42. return;
  43. }
  44. var panelInstance = $injector.instantiate(Panel);
  45. var directive = panelInstance.getDirective();
  46. Panel.promise = getTemplate(directive).then(template => {
  47. directive.templateUrl = null;
  48. directive.template = `<grafana-panel ctrl="ctrl">${template}</grafana-panel>`;
  49. directiveModule.directive(attrs.$normalize(name), function() {
  50. return directive;
  51. });
  52. Panel.registered = true;
  53. addPanelAndCompile(name);
  54. });
  55. }
  56. var panelElemName = 'panel-directive-' + scope.panel.type;
  57. let panelInfo = config.panels[scope.panel.type];
  58. if (!panelInfo) {
  59. addPanel(panelElemName, UnknownPanel);
  60. return;
  61. }
  62. System.import(panelInfo.module).then(function(panelModule) {
  63. addPanel(panelElemName, panelModule.Panel);
  64. }).catch(err => {
  65. console.log('Panel err: ', err);
  66. });
  67. }
  68. };
  69. }
  70. directiveModule.directive('panelLoader', panelLoader);