dash_edit_link.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.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 cssClass = payload.cssClass || 'gf-box';
  65. var view = $('<div class="' + cssClass + '" ng-include="' + src + '"></div>');
  66. if (payload.cssClass) {
  67. view.addClass(payload.cssClass);
  68. }
  69. elem.append(view);
  70. $compile(elem.contents())(editorScope);
  71. }
  72. scope.$watch("dashboardViewState.state.editview", function(newValue, oldValue) {
  73. if (newValue) {
  74. showEditorPane(null, {}, newValue);
  75. } else if (oldValue) {
  76. scope.contextSrv.editview = null;
  77. if (lastEditor === editViewMap[oldValue]) {
  78. hideEditorPane();
  79. }
  80. }
  81. });
  82. scope.contextSrv.editview = null;
  83. scope.$on("$destroy", hideEditorPane);
  84. scope.onAppEvent('hide-dash-editor', hideEditorPane);
  85. scope.onAppEvent('show-dash-editor', showEditorPane);
  86. }
  87. };
  88. });
  89. });