dash_edit_link.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. define([
  2. 'jquery',
  3. 'angular',
  4. '../core_module',
  5. ],
  6. function ($, angular, coreModule) {
  7. 'use strict';
  8. var editViewMap = {
  9. 'settings': { src: 'public/app/features/dashboard/partials/settings.html'},
  10. 'annotations': { src: 'public/app/features/annotations/partials/editor.html'},
  11. 'templating': { src: 'public/app/features/templating/partials/editor.html'},
  12. 'history': { html: '<gf-dashboard-history dashboard="dashboard"></gf-dashboard-history>'},
  13. 'timepicker': { src: 'public/app/features/dashboard/timepicker/dropdown.html' },
  14. 'import': { html: '<dash-import></dash-import>' }
  15. };
  16. coreModule.default.directive('dashEditorView', function($compile, $location, $rootScope) {
  17. return {
  18. restrict: 'A',
  19. link: function(scope, elem) {
  20. var editorScope;
  21. var lastEditView;
  22. function hideEditorPane(hideToShowOtherView) {
  23. if (editorScope) {
  24. editorScope.dismiss(hideToShowOtherView);
  25. }
  26. }
  27. function showEditorPane(evt, options) {
  28. if (options.editview) {
  29. options.src = editViewMap[options.editview].src;
  30. options.html = editViewMap[options.editview].html;
  31. }
  32. if (lastEditView && lastEditView === options.editview) {
  33. hideEditorPane(false);
  34. return;
  35. }
  36. hideEditorPane(true);
  37. lastEditView = options.editview;
  38. editorScope = options.scope ? options.scope.$new() : scope.$new();
  39. editorScope.dismiss = function(hideToShowOtherView) {
  40. editorScope.$destroy();
  41. lastEditView = null;
  42. editorScope = null;
  43. elem.removeClass('dash-edit-view--open');
  44. if (!hideToShowOtherView) {
  45. setTimeout(function() {
  46. elem.empty();
  47. }, 250);
  48. }
  49. if (options.editview) {
  50. var urlParams = $location.search();
  51. if (options.editview === urlParams.editview) {
  52. delete urlParams.editview;
  53. // even though we always are in apply phase here
  54. // some angular bug is causing location search updates to
  55. // not happen always so this is a hack fix or that
  56. setTimeout(function() {
  57. $rootScope.$apply(function() {
  58. $location.search(urlParams);
  59. });
  60. });
  61. }
  62. }
  63. };
  64. if (options.editview === 'import') {
  65. var modalScope = $rootScope.$new();
  66. modalScope.$on("$destroy", function() {
  67. editorScope.dismiss();
  68. });
  69. $rootScope.appEvent('show-modal', {
  70. templateHtml: '<dash-import></dash-import>',
  71. scope: modalScope,
  72. backdrop: 'static'
  73. });
  74. return;
  75. }
  76. var view;
  77. if (options.src) {
  78. view = angular.element(document.createElement('div'));
  79. view.html('<div class="tabbed-view" ng-include="' + "'" + options.src + "'" + '"></div>');
  80. } else {
  81. view = angular.element(document.createElement('div'));
  82. view.addClass('tabbed-view');
  83. view.html(options.html);
  84. }
  85. $compile(view)(editorScope);
  86. setTimeout(function() {
  87. elem.empty();
  88. elem.append(view);
  89. setTimeout(function() {
  90. elem.addClass('dash-edit-view--open');
  91. }, 10);
  92. }, 10);
  93. }
  94. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  95. if (newValue) {
  96. showEditorPane(null, {editview: newValue});
  97. } else if (oldValue) {
  98. if (lastEditView === oldValue) {
  99. hideEditorPane();
  100. }
  101. }
  102. });
  103. scope.$on("$destroy", hideEditorPane);
  104. scope.onAppEvent('hide-dash-editor', function() {
  105. hideEditorPane(false);
  106. });
  107. scope.onAppEvent('show-dash-editor', showEditorPane);
  108. scope.onAppEvent('panel-fullscreen-enter', function() {
  109. scope.appEvent('hide-dash-editor');
  110. });
  111. }
  112. };
  113. });
  114. });