dashEditLink.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. define([
  2. 'angular',
  3. 'jquery'
  4. ],
  5. function (angular, $) {
  6. 'use strict';
  7. angular
  8. .module('grafana.directives')
  9. .directive('dashEditorLink', function($timeout) {
  10. return {
  11. restrict: 'A',
  12. link: function(scope, elem, attrs) {
  13. var partial = attrs.dashEditorLink;
  14. elem.bind('click',function() {
  15. $timeout(function() {
  16. var editorScope = attrs.editorScope === 'isolated' ? null : scope;
  17. scope.appEvent('show-dash-editor', { src: partial, scope: editorScope });
  18. });
  19. });
  20. }
  21. };
  22. });
  23. angular
  24. .module('grafana.directives')
  25. .directive('dashEditorView', function($compile) {
  26. return {
  27. restrict: 'A',
  28. link: function(scope, elem) {
  29. var editorScope;
  30. var lastEditor;
  31. function hideScrollbars(value) {
  32. if (value) {
  33. window.scrollTo(0,0);
  34. document.documentElement.style.overflow = 'hidden'; // firefox, chrome
  35. document.body.scroll = "no"; // ie only
  36. } else {
  37. document.documentElement.style.overflow = 'auto';
  38. document.body.scroll = "yes";
  39. }
  40. }
  41. function hideEditorPane() {
  42. hideScrollbars(false);
  43. if (editorScope) { editorScope.dismiss(); }
  44. }
  45. scope.$on("$destroy", hideEditorPane);
  46. scope.onAppEvent('hide-dash-editor', hideEditorPane);
  47. scope.onAppEvent('show-dash-editor', function(evt, payload) {
  48. if (lastEditor === payload.src) {
  49. hideEditorPane();
  50. return;
  51. }
  52. hideEditorPane();
  53. scope.exitFullscreen();
  54. lastEditor = payload.src;
  55. editorScope = payload.scope ? payload.scope.$new() : scope.$new();
  56. editorScope.dismiss = function() {
  57. console.log('dismiss: ');
  58. editorScope.$destroy();
  59. elem.empty();
  60. lastEditor = null;
  61. editorScope = null;
  62. hideScrollbars(false);
  63. };
  64. // hide page scrollbars while edit pane is visible
  65. hideScrollbars(true);
  66. var src = "'" + payload.src + "'";
  67. var view = $('<div class="dashboard-edit-view" ng-include="' + src + '"></div>');
  68. elem.append(view);
  69. $compile(elem.contents())(editorScope);
  70. });
  71. }
  72. };
  73. });
  74. });