dash_edit_link.js 4.3 KB

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