dash_edit_link.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. define([
  2. 'jquery',
  3. '../core_module',
  4. ],
  5. function ($, coreModule) {
  6. 'use strict';
  7. var editViewMap = {
  8. 'settings': { src: 'public/app/features/dashboard/partials/settings.html'},
  9. 'annotations': { src: 'public/app/features/annotations/partials/editor.html'},
  10. 'templating': { src: 'public/app/features/templating/partials/editor.html'},
  11. 'import': { src: '<dash-import></dash-import>' }
  12. };
  13. coreModule.default.directive('dashEditorView', function($compile, $location, $rootScope) {
  14. return {
  15. restrict: 'A',
  16. link: function(scope, elem) {
  17. var editorScope;
  18. var lastEditor;
  19. function hideEditorPane() {
  20. if (editorScope) {
  21. scope.appEvent('dash-editor-hidden', lastEditor);
  22. editorScope.dismiss();
  23. }
  24. }
  25. function showEditorPane(evt, payload, editview) {
  26. if (editview) {
  27. scope.contextSrv.editview = editViewMap[editview];
  28. payload.src = scope.contextSrv.editview.src;
  29. }
  30. if (lastEditor === payload.src) {
  31. hideEditorPane();
  32. return;
  33. }
  34. hideEditorPane();
  35. lastEditor = payload.src;
  36. editorScope = payload.scope ? payload.scope.$new() : scope.$new();
  37. editorScope.dismiss = function() {
  38. editorScope.$destroy();
  39. elem.empty();
  40. lastEditor = null;
  41. editorScope = null;
  42. if (editview) {
  43. var urlParams = $location.search();
  44. if (editview === urlParams.editview) {
  45. delete urlParams.editview;
  46. $location.search(urlParams);
  47. }
  48. }
  49. };
  50. if (editview === 'import') {
  51. var modalScope = $rootScope.$new();
  52. modalScope.$on("$destroy", function() {
  53. editorScope.dismiss();
  54. });
  55. $rootScope.appEvent('show-modal', {
  56. templateHtml: '<dash-import></dash-import>',
  57. scope: modalScope,
  58. backdrop: 'static'
  59. });
  60. return;
  61. }
  62. var view = payload.src;
  63. if (view.indexOf('.html') > 0) {
  64. view = $('<div class="tabbed-view" ng-include="' + "'" + view + "'" + '"></div>');
  65. }
  66. elem.append(view);
  67. $compile(elem.contents())(editorScope);
  68. }
  69. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  70. if (newValue) {
  71. showEditorPane(null, {}, newValue);
  72. } else if (oldValue) {
  73. scope.contextSrv.editview = null;
  74. if (lastEditor === editViewMap[oldValue]) {
  75. hideEditorPane();
  76. }
  77. }
  78. });
  79. scope.contextSrv.editview = null;
  80. scope.$on("$destroy", hideEditorPane);
  81. scope.onAppEvent('hide-dash-editor', hideEditorPane);
  82. scope.onAppEvent('show-dash-editor', showEditorPane);
  83. }
  84. };
  85. });
  86. });