dash_edit_link.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. scope.appEvent('dash-editor-hidden');
  26. }
  27. }
  28. function showEditorPane(evt, options) {
  29. if (options.editview) {
  30. options.src = editViewMap[options.editview].src;
  31. options.html = editViewMap[options.editview].html;
  32. }
  33. if (lastEditView === options.editview) {
  34. hideEditorPane(false);
  35. return;
  36. }
  37. hideEditorPane(true);
  38. lastEditView = options.editview;
  39. editorScope = options.scope ? options.scope.$new() : scope.$new();
  40. editorScope.dismiss = function(hideToShowOtherView) {
  41. editorScope.$destroy();
  42. lastEditView = null;
  43. editorScope = null;
  44. elem.removeClass('dash-edit-view--open');
  45. if (!hideToShowOtherView) {
  46. setTimeout(function() {
  47. elem.empty();
  48. }, 250);
  49. }
  50. if (options.editview) {
  51. var urlParams = $location.search();
  52. if (options.editview === urlParams.editview) {
  53. delete urlParams.editview;
  54. $location.search(urlParams);
  55. }
  56. }
  57. };
  58. if (options.editview === 'import') {
  59. var modalScope = $rootScope.$new();
  60. modalScope.$on("$destroy", function() {
  61. editorScope.dismiss();
  62. });
  63. $rootScope.appEvent('show-modal', {
  64. templateHtml: '<dash-import></dash-import>',
  65. scope: modalScope,
  66. backdrop: 'static'
  67. });
  68. return;
  69. }
  70. var view;
  71. if (options.src) {
  72. view = angular.element(document.createElement('div'));
  73. view.html('<div class="tabbed-view" ng-include="' + "'" + options.src + "'" + '"></div>');
  74. } else {
  75. view = angular.element(document.createElement('div'));
  76. view.addClass('tabbed-view');
  77. view.html(options.html);
  78. }
  79. $compile(view)(editorScope);
  80. setTimeout(function() {
  81. elem.empty();
  82. elem.append(view);
  83. setTimeout(function() {
  84. elem.addClass('dash-edit-view--open');
  85. }, 10);
  86. }, 10);
  87. }
  88. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  89. if (newValue) {
  90. showEditorPane(null, {editview: newValue});
  91. } else if (oldValue) {
  92. if (lastEditView === oldValue) {
  93. hideEditorPane();
  94. }
  95. }
  96. });
  97. scope.$on("$destroy", hideEditorPane);
  98. scope.onAppEvent('hide-dash-editor', function() {
  99. hideEditorPane(false);
  100. });
  101. scope.onAppEvent('show-dash-editor', showEditorPane);
  102. scope.onAppEvent('panel-fullscreen-enter', function() {
  103. scope.appEvent('hide-dash-editor');
  104. });
  105. }
  106. };
  107. });
  108. });