unsaved_changes_srv.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. export class Tracker {
  4. current: any;
  5. originalPath: any;
  6. scope: any;
  7. original: any;
  8. next: any;
  9. $window: any;
  10. /** @ngInject */
  11. constructor(
  12. dashboard,
  13. scope,
  14. originalCopyDelay,
  15. private $location,
  16. $window,
  17. private $timeout,
  18. private contextSrv,
  19. private $rootScope
  20. ) {
  21. this.$location = $location;
  22. this.$window = $window;
  23. this.current = dashboard;
  24. this.originalPath = $location.path();
  25. this.scope = scope;
  26. // register events
  27. scope.onAppEvent('dashboard-saved', () => {
  28. this.original = this.current.getSaveModelClone();
  29. this.originalPath = $location.path();
  30. });
  31. $window.onbeforeunload = () => {
  32. if (this.ignoreChanges()) {
  33. return null;
  34. }
  35. if (this.hasChanges()) {
  36. return 'There are unsaved changes to this dashboard';
  37. }
  38. return null;
  39. };
  40. scope.$on('$locationChangeStart', (event, next) => {
  41. // check if we should look for changes
  42. if (this.originalPath === $location.path()) {
  43. return true;
  44. }
  45. if (this.ignoreChanges()) {
  46. return true;
  47. }
  48. if (this.hasChanges()) {
  49. event.preventDefault();
  50. this.next = next;
  51. this.$timeout(() => {
  52. this.open_modal();
  53. });
  54. }
  55. return false;
  56. });
  57. if (originalCopyDelay) {
  58. this.$timeout(() => {
  59. // wait for different services to patch the dashboard (missing properties)
  60. this.original = dashboard.getSaveModelClone();
  61. }, originalCopyDelay);
  62. } else {
  63. this.original = dashboard.getSaveModelClone();
  64. }
  65. }
  66. // for some dashboards and users
  67. // changes should be ignored
  68. ignoreChanges() {
  69. if (!this.original) {
  70. return true;
  71. }
  72. if (!this.contextSrv.isEditor) {
  73. return true;
  74. }
  75. if (!this.current || !this.current.meta) {
  76. return true;
  77. }
  78. var meta = this.current.meta;
  79. return !meta.canSave || meta.fromScript || meta.fromFile;
  80. }
  81. // remove stuff that should not count in diff
  82. cleanDashboardFromIgnoredChanges(dash) {
  83. // ignore time and refresh
  84. dash.time = 0;
  85. dash.refresh = 0;
  86. dash.schemaVersion = 0;
  87. // ignore iteration property
  88. delete dash.iteration;
  89. // filter row and panels properties that should be ignored
  90. dash.rows = _.filter(dash.rows, function(row) {
  91. if (row.repeatRowId) {
  92. return false;
  93. }
  94. row.panels = _.filter(row.panels, function(panel) {
  95. if (panel.repeatPanelId) {
  96. return false;
  97. }
  98. // remove scopedVars
  99. panel.scopedVars = null;
  100. // ignore span changes
  101. panel.span = null;
  102. // ignore panel legend sort
  103. if (panel.legend) {
  104. delete panel.legend.sort;
  105. delete panel.legend.sortDesc;
  106. }
  107. return true;
  108. });
  109. // ignore collapse state
  110. row.collapse = false;
  111. return true;
  112. });
  113. dash.panels = _.filter(dash.panels, panel => {
  114. if (panel.repeatPanelId) {
  115. return false;
  116. }
  117. // remove scopedVars
  118. panel.scopedVars = null;
  119. // ignore panel legend sort
  120. if (panel.legend) {
  121. delete panel.legend.sort;
  122. delete panel.legend.sortDesc;
  123. }
  124. return true;
  125. });
  126. // ignore template variable values
  127. _.each(dash.templating.list, function(value) {
  128. value.current = null;
  129. value.options = null;
  130. value.filters = null;
  131. });
  132. }
  133. hasChanges() {
  134. var current = this.current.getSaveModelClone();
  135. var original = this.original;
  136. this.cleanDashboardFromIgnoredChanges(current);
  137. this.cleanDashboardFromIgnoredChanges(original);
  138. var currentTimepicker = _.find(current.nav, { type: 'timepicker' });
  139. var originalTimepicker = _.find(original.nav, { type: 'timepicker' });
  140. if (currentTimepicker && originalTimepicker) {
  141. currentTimepicker.now = originalTimepicker.now;
  142. }
  143. var currentJson = angular.toJson(current);
  144. var originalJson = angular.toJson(original);
  145. return currentJson !== originalJson;
  146. }
  147. discardChanges() {
  148. this.original = null;
  149. this.gotoNext();
  150. }
  151. open_modal() {
  152. this.$rootScope.appEvent('show-modal', {
  153. templateHtml: '<unsaved-changes-modal dismiss="dismiss()"></unsaved-changes-modal>',
  154. modalClass: 'modal--narrow confirm-modal',
  155. });
  156. }
  157. saveChanges() {
  158. var self = this;
  159. var cancel = this.$rootScope.$on('dashboard-saved', () => {
  160. cancel();
  161. this.$timeout(() => {
  162. self.gotoNext();
  163. });
  164. });
  165. this.$rootScope.appEvent('save-dashboard');
  166. }
  167. gotoNext() {
  168. var baseLen = this.$location.absUrl().length - this.$location.url().length;
  169. var nextUrl = this.next.substring(baseLen);
  170. this.$location.url(nextUrl);
  171. }
  172. }
  173. /** @ngInject */
  174. export function unsavedChangesSrv($rootScope, $q, $location, $timeout, contextSrv, dashboardSrv, $window) {
  175. this.Tracker = Tracker;
  176. this.init = function(dashboard, scope) {
  177. this.tracker = new Tracker(dashboard, scope, 1000, $location, $window, $timeout, contextSrv, $rootScope);
  178. return this.tracker;
  179. };
  180. }
  181. angular.module('grafana.services').service('unsavedChangesSrv', unsavedChangesSrv);