dash_edit_link.js 2.9 KB

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