panel_directive.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'app/core/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 module = config.panels[panelType].module;
  15. System.import(module).then(function() {
  16. var panelEl = angular.element(document.createElement('grafana-panel-' + panelType));
  17. elem.append(panelEl);
  18. $compile(panelEl)(scope);
  19. }).catch(function(err) {
  20. console.log('Failed to load panel:', err);
  21. scope.appEvent('alert-error', ['Panel Load Error', 'Failed to load panel ' + panelType + ', ' + err]);
  22. });
  23. }
  24. };
  25. });
  26. module.directive('grafanaPanel', function() {
  27. return {
  28. restrict: 'E',
  29. templateUrl: 'app/features/panel/partials/panel.html',
  30. transclude: true,
  31. link: function(scope, elem) {
  32. var panelContainer = elem.find('.panel-container');
  33. scope.$watchGroup(['fullscreen', 'height', 'panel.height', 'row.height'], function() {
  34. panelContainer.css({ minHeight: scope.height || scope.panel.height || scope.row.height, display: 'block' });
  35. elem.toggleClass('panel-fullscreen', scope.fullscreen ? true : false);
  36. });
  37. }
  38. };
  39. });
  40. module.directive('datasourceCustomSettingsView', function($compile) {
  41. return {
  42. restrict: 'E',
  43. scope: {
  44. dsMeta: "=",
  45. current: "=",
  46. },
  47. link: function(scope, elem) {
  48. scope.$watch("dsMeta.module", function() {
  49. if (!scope.dsMeta) {
  50. return;
  51. }
  52. System.import(scope.dsMeta.module).then(function() {
  53. elem.empty();
  54. var panelEl = angular.element(document.createElement('datasource-custom-settings-view-' + scope.dsMeta.id));
  55. elem.append(panelEl);
  56. $compile(panelEl)(scope);
  57. }).catch(function(err) {
  58. console.log('Failed to load plugin:', err);
  59. scope.appEvent('alert-error', ['Plugin Load Error', 'Failed to load plugin ' + scope.dsMeta.id + ', ' + err]);
  60. });
  61. });
  62. }
  63. };
  64. });
  65. module.service('dynamicDirectiveSrv', function($compile, $parse, datasourceSrv) {
  66. var self = this;
  67. this.addDirective = function(options, type, editorScope) {
  68. var panelEl = angular.element(document.createElement(options.name + '-' + type));
  69. options.parentElem.append(panelEl);
  70. $compile(panelEl)(editorScope);
  71. };
  72. this.define = function(options) {
  73. var editorScope;
  74. options.scope.$watch(options.datasourceProperty, function(newVal) {
  75. if (editorScope) {
  76. editorScope.$destroy();
  77. options.parentElem.empty();
  78. }
  79. editorScope = options.scope.$new();
  80. datasourceSrv.get(newVal).then(function(ds) {
  81. self.addDirective(options, ds.meta.id, editorScope);
  82. });
  83. });
  84. };
  85. });
  86. module.directive('datasourceEditorView', function(dynamicDirectiveSrv) {
  87. return {
  88. restrict: 'E',
  89. link: function(scope, elem, attrs) {
  90. dynamicDirectiveSrv.define({
  91. datasourceProperty: attrs.datasource,
  92. name: attrs.name,
  93. scope: scope,
  94. parentElem: elem,
  95. });
  96. }
  97. };
  98. });
  99. module.directive('queryEditorLoader', function($compile, $parse, datasourceSrv) {
  100. return {
  101. restrict: 'E',
  102. link: function(scope, elem) {
  103. var editorScope;
  104. scope.$watch("panel.datasource", function() {
  105. var datasource = scope.target.datasource || scope.panel.datasource;
  106. datasourceSrv.get(datasource).then(function(ds) {
  107. if (editorScope) {
  108. editorScope.$destroy();
  109. elem.empty();
  110. }
  111. editorScope = scope.$new();
  112. editorScope.datasource = ds;
  113. if (!scope.target.refId) {
  114. scope.target.refId = 'A';
  115. }
  116. var panelEl = angular.element(document.createElement('metric-query-editor-' + ds.meta.id));
  117. elem.append(panelEl);
  118. $compile(panelEl)(editorScope);
  119. });
  120. });
  121. }
  122. };
  123. });
  124. module.directive('panelResizer', function($rootScope) {
  125. return {
  126. restrict: 'E',
  127. template: '<span class="resize-panel-handle"></span>',
  128. link: function(scope, elem) {
  129. var resizing = false;
  130. var lastPanel = false;
  131. var handleOffset;
  132. var originalHeight;
  133. var originalWidth;
  134. var maxWidth;
  135. function dragStartHandler(e) {
  136. e.preventDefault();
  137. resizing = true;
  138. handleOffset = $(e.target).offset();
  139. originalHeight = parseInt(scope.row.height);
  140. originalWidth = scope.panel.span;
  141. maxWidth = $(document).width();
  142. lastPanel = scope.row.panels[scope.row.panels.length - 1];
  143. $('body').on('mousemove', moveHandler);
  144. $('body').on('mouseup', dragEndHandler);
  145. }
  146. function moveHandler(e) {
  147. scope.row.height = originalHeight + (e.pageY - handleOffset.top);
  148. scope.panel.span = originalWidth + (((e.pageX - handleOffset.left) / maxWidth) * 12);
  149. scope.panel.span = Math.min(Math.max(scope.panel.span, 1), 12);
  150. var rowSpan = scope.dashboard.rowSpan(scope.row);
  151. // auto adjust other panels
  152. if (Math.floor(rowSpan) < 14) {
  153. // last panel should not push row down
  154. if (lastPanel === scope.panel && rowSpan > 12) {
  155. lastPanel.span -= rowSpan - 12;
  156. }
  157. // reduce width of last panel so total in row is 12
  158. else if (lastPanel !== scope.panel) {
  159. lastPanel.span = lastPanel.span - (rowSpan - 12);
  160. lastPanel.span = Math.min(Math.max(lastPanel.span, 1), 12);
  161. }
  162. }
  163. scope.$apply(function() {
  164. scope.$broadcast('render');
  165. });
  166. }
  167. function dragEndHandler() {
  168. // if close to 12
  169. var rowSpan = scope.dashboard.rowSpan(scope.row);
  170. if (rowSpan < 12 && rowSpan > 11) {
  171. lastPanel.span += 12 - rowSpan;
  172. }
  173. scope.$apply(function() {
  174. $rootScope.$broadcast('render');
  175. });
  176. $('body').off('mousemove', moveHandler);
  177. $('body').off('mouseup', dragEndHandler);
  178. }
  179. elem.on('mousedown', dragStartHandler);
  180. scope.$on("$destroy", function() {
  181. elem.off('mousedown', dragStartHandler);
  182. });
  183. }
  184. };
  185. });
  186. });