dash_edit_link.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) {
  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. var view = payload.src;
  51. if (view.indexOf('.html') > 0) {
  52. view = $('<div class="tabbed-view" ng-include="' + "'" + view + "'" + '"></div>');
  53. }
  54. elem.append(view);
  55. $compile(elem.contents())(editorScope);
  56. }
  57. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  58. if (newValue) {
  59. showEditorPane(null, {}, newValue);
  60. } else if (oldValue) {
  61. scope.contextSrv.editview = null;
  62. if (lastEditor === editViewMap[oldValue]) {
  63. hideEditorPane();
  64. }
  65. }
  66. });
  67. scope.contextSrv.editview = null;
  68. scope.$on("$destroy", hideEditorPane);
  69. scope.onAppEvent('hide-dash-editor', hideEditorPane);
  70. scope.onAppEvent('show-dash-editor', showEditorPane);
  71. }
  72. };
  73. });
  74. });