panelDirective.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'config',
  5. ],
  6. function (angular, $, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.directives');
  9. module.directive('panelLoader', function($compile, $parse) {
  10. return {
  11. restrict: 'E',
  12. link: function(scope, elem, attr) {
  13. var getter = $parse(attr.type), panelType = getter(scope);
  14. var panelPath = config.panels[panelType].path;
  15. scope.require([panelPath + "/module"], function () {
  16. var panelEl = angular.element(document.createElement('grafana-panel-' + panelType));
  17. elem.append(panelEl);
  18. $compile(panelEl)(scope);
  19. });
  20. }
  21. };
  22. });
  23. module.directive('grafanaPanel', function() {
  24. return {
  25. restrict: 'E',
  26. templateUrl: 'app/features/panel/partials/panel.html',
  27. transclude: true,
  28. link: function(scope, elem) {
  29. var panelContainer = elem.find('.panel-container');
  30. scope.$watchGroup(['fullscreen', 'height', 'panel.height', 'row.height'], function() {
  31. panelContainer.css({ minHeight: scope.height || scope.panel.height || scope.row.height, display: 'block' });
  32. elem.toggleClass('panel-fullscreen', scope.fullscreen ? true : false);
  33. });
  34. }
  35. };
  36. });
  37. module.service('dynamicDirectiveSrv', function($compile, $parse, datasourceSrv) {
  38. var self = this;
  39. this.addDirective = function(options, type, editorScope) {
  40. var panelEl = angular.element(document.createElement(options.name + '-' + type));
  41. options.parentElem.append(panelEl);
  42. $compile(panelEl)(editorScope);
  43. };
  44. this.define = function(options) {
  45. var editorScope;
  46. options.scope.$watch(options.datasourceProperty, function(newVal) {
  47. if (editorScope) {
  48. editorScope.$destroy();
  49. options.parentElem.empty();
  50. }
  51. editorScope = options.scope.$new();
  52. datasourceSrv.get(newVal).then(function(ds) {
  53. self.addDirective(options, ds.meta.type, editorScope);
  54. });
  55. });
  56. };
  57. });
  58. module.directive('queryEditorLoader', function($compile, $parse, datasourceSrv) {
  59. return {
  60. restrict: 'E',
  61. link: function(scope, elem) {
  62. var editorScope;
  63. scope.$watch("panel.datasource", function() {
  64. var datasource = scope.target.datasource || scope.panel.datasource;
  65. datasourceSrv.get(datasource).then(function(ds) {
  66. if (editorScope) {
  67. editorScope.$destroy();
  68. elem.empty();
  69. }
  70. editorScope = scope.$new();
  71. editorScope.datasource = ds;
  72. if (!scope.target.refId) {
  73. scope.target.refId = 'A';
  74. }
  75. var panelEl = angular.element(document.createElement('metric-query-editor-' + ds.meta.type));
  76. elem.append(panelEl);
  77. $compile(panelEl)(editorScope);
  78. });
  79. });
  80. }
  81. };
  82. });
  83. module.directive('datasourceEditorView', function(dynamicDirectiveSrv) {
  84. return {
  85. restrict: 'E',
  86. link: function(scope, elem, attrs) {
  87. dynamicDirectiveSrv.define({
  88. datasourceProperty: attrs.datasource,
  89. name: attrs.name,
  90. scope: scope,
  91. parentElem: elem,
  92. });
  93. }
  94. };
  95. });
  96. });