dash_edit_link.js 4.5 KB

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