dash_edit_link.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define([
  2. 'jquery',
  3. '../core_module',
  4. ],
  5. function ($, coreModule) {
  6. 'use strict';
  7. var editViewMap = {
  8. 'settings': { src: 'app/features/dashboard/partials/settings.html', title: "Settings" },
  9. 'annotations': { src: 'app/features/annotations/partials/editor.html', title: "Annotations" },
  10. 'templating': { src: 'app/features/templating/partials/editor.html', title: "Templating" }
  11. };
  12. coreModule.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.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) { editorScope.dismiss(); }
  34. }
  35. function showEditorPane(evt, payload, editview) {
  36. if (editview) {
  37. scope.contextSrv.editview = editViewMap[editview];
  38. payload.src = scope.contextSrv.editview.src;
  39. }
  40. if (lastEditor === payload.src) {
  41. hideEditorPane();
  42. return;
  43. }
  44. hideEditorPane();
  45. scope.exitFullscreen();
  46. lastEditor = payload.src;
  47. editorScope = payload.scope ? payload.scope.$new() : scope.$new();
  48. editorScope.dismiss = function() {
  49. editorScope.$destroy();
  50. elem.empty();
  51. lastEditor = null;
  52. editorScope = null;
  53. if (editview) {
  54. var urlParams = $location.search();
  55. if (editview === urlParams.editview) {
  56. delete urlParams.editview;
  57. $location.search(urlParams);
  58. }
  59. }
  60. };
  61. var src = "'" + payload.src + "'";
  62. var cssClass = payload.cssClass || 'gf-box';
  63. var view = $('<div class="' + cssClass + '" ng-include="' + src + '"></div>');
  64. if (payload.cssClass) {
  65. view.addClass(payload.cssClass);
  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. });