dash_edit_link.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 'add-panel': { html: '<add-panel></add-panel>' },
  16. 'import': { html: '<dash-import dismiss="dismiss()"></dash-import>', isModal: true },
  17. 'permissions': { html: '<dash-acl-modal dismiss="dismiss()"></dash-acl-modal>', isModal: true },
  18. 'new-folder': {
  19. isModal: true,
  20. html: '<folder-modal dismiss="dismiss()"></folder-modal>',
  21. modalClass: 'modal--narrow'
  22. }
  23. };
  24. coreModule.default.directive('dashEditorView', function($compile, $location, $rootScope) {
  25. return {
  26. restrict: 'A',
  27. link: function(scope, elem) {
  28. var editorScope;
  29. var modalScope;
  30. var lastEditView;
  31. function hideEditorPane(hideToShowOtherView) {
  32. if (editorScope) {
  33. editorScope.dismiss(hideToShowOtherView);
  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. // even though we always are in apply phase here
  66. // some angular bug is causing location search updates to
  67. // not happen always so this is a hack fix or that
  68. setTimeout(function() {
  69. $rootScope.$apply(function() {
  70. $location.search(urlParams);
  71. });
  72. });
  73. }
  74. }
  75. };
  76. if (options.isModal) {
  77. modalScope = $rootScope.$new();
  78. modalScope.$on("$destroy", function() {
  79. editorScope.dismiss();
  80. });
  81. $rootScope.appEvent('show-modal', {
  82. templateHtml: options.html,
  83. scope: modalScope,
  84. backdrop: 'static',
  85. modalClass: options.modalClass,
  86. });
  87. return;
  88. }
  89. var view;
  90. if (options.src) {
  91. view = angular.element(document.createElement('div'));
  92. view.html('<div class="tabbed-view" ng-include="' + "'" + options.src + "'" + '"></div>');
  93. } else {
  94. view = angular.element(document.createElement('div'));
  95. view.addClass('tabbed-view');
  96. view.html(options.html);
  97. }
  98. $compile(view)(editorScope);
  99. setTimeout(function() {
  100. elem.empty();
  101. elem.append(view);
  102. setTimeout(function() {
  103. elem.addClass('dash-edit-view--open');
  104. }, 10);
  105. }, 10);
  106. }
  107. scope.$watch("ctrl.dashboardViewState.state.editview", function(newValue, oldValue) {
  108. if (newValue) {
  109. showEditorPane(null, {editview: newValue});
  110. } else if (oldValue) {
  111. if (lastEditView === oldValue) {
  112. hideEditorPane();
  113. }
  114. }
  115. });
  116. scope.$on("$destroy", hideEditorPane);
  117. scope.onAppEvent('hide-dash-editor', function() {
  118. hideEditorPane(false);
  119. });
  120. scope.onAppEvent('show-dash-editor', showEditorPane);
  121. scope.onAppEvent('panel-fullscreen-enter', function() {
  122. scope.appEvent('hide-dash-editor');
  123. });
  124. }
  125. };
  126. });
  127. });