dashEditLink.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. define([
  2. 'angular',
  3. 'jquery'
  4. ],
  5. function (angular, $) {
  6. 'use strict';
  7. var editViewMap = {
  8. 'settings': { src: 'app/features/dashboard/partials/settings.html', title: "Settings" },
  9. 'annotations': { src: 'app/features/annotations/partials/editor.html', title: "Annotations" },
  10. 'templating': { src: 'app/features/templating/partials/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 hideEditorPane() {
  37. if (editorScope) { editorScope.dismiss(); }
  38. }
  39. function showEditorPane(evt, payload, editview) {
  40. if (editview) {
  41. scope.contextSrv.editview = editViewMap[editview];
  42. payload.src = scope.contextSrv.editview.src;
  43. }
  44. if (lastEditor === payload.src) {
  45. hideEditorPane();
  46. return;
  47. }
  48. hideEditorPane();
  49. scope.exitFullscreen();
  50. lastEditor = payload.src;
  51. editorScope = payload.scope ? payload.scope.$new() : scope.$new();
  52. editorScope.dismiss = function() {
  53. editorScope.$destroy();
  54. elem.empty();
  55. lastEditor = null;
  56. editorScope = null;
  57. if (editview) {
  58. var urlParams = $location.search();
  59. if (editview === urlParams.editview) {
  60. delete urlParams.editview;
  61. $location.search(urlParams);
  62. }
  63. }
  64. };
  65. var src = "'" + payload.src + "'";
  66. var view = $('<div class="gf-box" ng-include="' + src + '"></div>');
  67. if (payload.cssClass) {
  68. view.addClass(payload.cssClass);
  69. }
  70. elem.append(view);
  71. $compile(elem.contents())(editorScope);
  72. }
  73. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  74. if (newValue) {
  75. showEditorPane(null, {}, newValue);
  76. } else if (oldValue) {
  77. scope.contextSrv.editview = null;
  78. if (lastEditor === editViewMap[oldValue]) {
  79. hideEditorPane();
  80. }
  81. }
  82. });
  83. scope.contextSrv.editview = null;
  84. scope.$on("$destroy", hideEditorPane);
  85. scope.onAppEvent('hide-dash-editor', hideEditorPane);
  86. scope.onAppEvent('show-dash-editor', showEditorPane);
  87. }
  88. };
  89. });
  90. });