dashEditLink.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. define([
  2. 'angular',
  3. 'jquery'
  4. ],
  5. function (angular, $) {
  6. 'use strict';
  7. var editViewMap = {
  8. 'settings': { src: 'app/partials/dasheditor.html', title: "Settings" },
  9. 'annotations': { src: 'app/features/annotations/partials/editor.html', title: "Annotations" },
  10. 'templating': { src: 'app/partials/templating_editor.html', title: "Templating" }
  11. };
  12. angular
  13. .module('grafana.directives')
  14. .directive('dashEditorLink', function($timeout) {
  15. return {
  16. restrict: 'A',
  17. link: function(scope, elem, attrs) {
  18. var partial = attrs.dashEditorLink;
  19. elem.bind('click',function() {
  20. $timeout(function() {
  21. var editorScope = attrs.editorScope === 'isolated' ? null : scope;
  22. scope.appEvent('show-dash-editor', { src: partial, scope: editorScope });
  23. });
  24. });
  25. }
  26. };
  27. });
  28. angular
  29. .module('grafana.directives')
  30. .directive('dashEditorView', function($compile, $location) {
  31. return {
  32. restrict: 'A',
  33. link: function(scope, elem) {
  34. var editorScope;
  35. var lastEditor;
  36. function hideScrollbars(value) {
  37. if (value) {
  38. window.scrollTo(0,0);
  39. document.documentElement.style.overflow = 'hidden'; // firefox, chrome
  40. document.body.scroll = "no"; // ie only
  41. } else {
  42. document.documentElement.style.overflow = 'auto';
  43. document.body.scroll = "yes";
  44. }
  45. }
  46. function hideEditorPane() {
  47. hideScrollbars(false);
  48. if (editorScope) { editorScope.dismiss(); }
  49. }
  50. function showEditorPane(evt, payload, editview) {
  51. if (editview) {
  52. scope.contextSrv.editview = editViewMap[editview];
  53. payload.src = scope.contextSrv.editview.src;
  54. }
  55. if (lastEditor === payload.src) {
  56. hideEditorPane();
  57. return;
  58. }
  59. hideEditorPane();
  60. scope.exitFullscreen();
  61. lastEditor = payload.src;
  62. editorScope = payload.scope ? payload.scope.$new() : scope.$new();
  63. editorScope.dismiss = function() {
  64. editorScope.$destroy();
  65. elem.empty();
  66. lastEditor = null;
  67. editorScope = null;
  68. hideScrollbars(false);
  69. if (editview) {
  70. var urlParams = $location.search();
  71. if (editview === urlParams.editview) {
  72. delete urlParams.editview;
  73. $location.search(urlParams);
  74. }
  75. }
  76. };
  77. // hide page scrollbars while edit pane is visible
  78. hideScrollbars(true);
  79. var src = "'" + payload.src + "'";
  80. var view = $('<div class="gf-box" ng-include="' + src + '"></div>');
  81. if (payload.cssClass) {
  82. view.addClass(payload.cssClass);
  83. }
  84. elem.append(view);
  85. $compile(elem.contents())(editorScope);
  86. }
  87. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  88. if (newValue) {
  89. showEditorPane(null, {}, newValue);
  90. } else if (oldValue) {
  91. scope.contextSrv.editview = null;
  92. if (lastEditor === editViewMap[oldValue]) {
  93. hideEditorPane();
  94. }
  95. }
  96. });
  97. scope.contextSrv.editview = null;
  98. scope.$on("$destroy", hideEditorPane);
  99. scope.onAppEvent('hide-dash-editor', hideEditorPane);
  100. scope.onAppEvent('show-dash-editor', showEditorPane);
  101. }
  102. };
  103. });
  104. });