dash_edit_link.js 3.0 KB

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