dash_edit_link.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  34. }
  35. function showEditorPane(evt, options) {
  36. if (options.editview) {
  37. _.defaults(options, editViewMap[options.editview]);
  38. }
  39. if (lastEditView && lastEditView === options.editview) {
  40. hideEditorPane(false);
  41. return;
  42. }
  43. hideEditorPane(true);
  44. lastEditView = options.editview;
  45. editorScope = options.scope ? options.scope.$new() : scope.$new();
  46. editorScope.dismiss = function(hideToShowOtherView) {
  47. if (modalScope) {
  48. modalScope.dismiss();
  49. modalScope = null;
  50. }
  51. editorScope.$destroy();
  52. lastEditView = null;
  53. editorScope = null;
  54. elem.removeClass('dash-edit-view--open');
  55. if (!hideToShowOtherView) {
  56. setTimeout(function() {
  57. elem.empty();
  58. }, 250);
  59. }
  60. if (options.editview) {
  61. var urlParams = $location.search();
  62. if (options.editview === urlParams.editview) {
  63. delete urlParams.editview;
  64. // even though we always are in apply phase here
  65. // some angular bug is causing location search updates to
  66. // not happen always so this is a hack fix or that
  67. setTimeout(function() {
  68. $rootScope.$apply(function() {
  69. $location.search(urlParams);
  70. });
  71. });
  72. }
  73. }
  74. };
  75. if (options.isModal) {
  76. modalScope = $rootScope.$new();
  77. modalScope.$on("$destroy", function() {
  78. editorScope.dismiss();
  79. });
  80. $rootScope.appEvent('show-modal', {
  81. templateHtml: options.html,
  82. scope: modalScope,
  83. backdrop: 'static',
  84. modalClass: options.modalClass,
  85. });
  86. return;
  87. }
  88. var view;
  89. if (options.src) {
  90. view = angular.element(document.createElement('div'));
  91. view.html('<div class="tabbed-view" ng-include="' + "'" + options.src + "'" + '"></div>');
  92. } else {
  93. view = angular.element(document.createElement('div'));
  94. view.addClass('tabbed-view');
  95. view.html(options.html);
  96. }
  97. $compile(view)(editorScope);
  98. setTimeout(function() {
  99. elem.empty();
  100. elem.append(view);
  101. setTimeout(function() {
  102. elem.addClass('dash-edit-view--open');
  103. }, 10);
  104. }, 10);
  105. }
  106. scope.$watch("ctrl.dashboardViewState.state.editview", function(newValue, oldValue) {
  107. if (newValue) {
  108. showEditorPane(null, {editview: newValue});
  109. } else if (oldValue) {
  110. if (lastEditView === oldValue) {
  111. hideEditorPane();
  112. }
  113. }
  114. });
  115. scope.$on("$destroy", hideEditorPane);
  116. scope.onAppEvent('hide-dash-editor', function() {
  117. hideEditorPane(false);
  118. });
  119. scope.onAppEvent('show-dash-editor', showEditorPane);
  120. scope.onAppEvent('panel-fullscreen-enter', function() {
  121. scope.appEvent('hide-dash-editor');
  122. });
  123. }
  124. };
  125. });
  126. });