unsavedChangesSrv.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. ],
  6. function(angular, _, config) {
  7. 'use strict';
  8. if (!config.unsaved_changes_warning) {
  9. return;
  10. }
  11. var module = angular.module('grafana.services');
  12. module.service('unsavedChangesSrv', function($rootScope, $modal, $q, $location, $timeout) {
  13. var self = this;
  14. var modalScope = $rootScope.$new();
  15. $rootScope.$on("dashboard-loaded", function(event, newDashboard) {
  16. // wait for different services to patch the dashboard (missing properties)
  17. $timeout(function() {
  18. self.original = angular.copy(newDashboard);
  19. self.current = newDashboard;
  20. }, 1200);
  21. });
  22. $rootScope.$on("dashboard-saved", function(event, savedDashboard) {
  23. self.original = angular.copy(savedDashboard);
  24. self.current = savedDashboard;
  25. self.orignalPath = $location.path();
  26. });
  27. $rootScope.$on("$routeChangeSuccess", function() {
  28. self.original = null;
  29. self.originalPath = $location.path();
  30. });
  31. window.onbeforeunload = function() {
  32. if (self.has_unsaved_changes()) {
  33. return "There are unsaved changes to this dashboard";
  34. }
  35. };
  36. this.init = function() {
  37. $rootScope.$on("$locationChangeStart", function(event, next) {
  38. if (self.originalPath === $location.path()) {
  39. return;
  40. }
  41. if (self.has_unsaved_changes()) {
  42. event.preventDefault();
  43. self.next = next;
  44. $timeout(self.open_modal);
  45. }
  46. });
  47. };
  48. this.open_modal = function() {
  49. var confirmModal = $modal({
  50. template: './app/partials/unsaved-changes.html',
  51. persist: true,
  52. show: false,
  53. scope: modalScope,
  54. keyboard: false
  55. });
  56. $q.when(confirmModal).then(function(modalEl) {
  57. modalEl.modal('show');
  58. });
  59. };
  60. this.has_unsaved_changes = function() {
  61. if (!self.original) {
  62. return false;
  63. }
  64. var current = angular.copy(self.current);
  65. var original = self.original;
  66. // ignore timespan changes
  67. current.time = original.time = {};
  68. current.refresh = original.refresh;
  69. var currentTimepicker = _.findWhere(current.nav, { type: 'timepicker' });
  70. var originalTimepicker = _.findWhere(original.nav, { type: 'timepicker' });
  71. if (currentTimepicker && originalTimepicker) {
  72. currentTimepicker.now = originalTimepicker.now;
  73. }
  74. var currentJson = angular.toJson(current);
  75. var originalJson = angular.toJson(original);
  76. if (currentJson !== originalJson) {
  77. return true;
  78. }
  79. return false;
  80. };
  81. this.goto_next = function() {
  82. var baseLen = $location.absUrl().length - $location.url().length;
  83. var nextUrl = self.next.substring(baseLen);
  84. $location.url(nextUrl);
  85. };
  86. modalScope.ignore = function() {
  87. self.original = null;
  88. self.goto_next();
  89. };
  90. modalScope.save = function() {
  91. var unregister = $rootScope.$on('dashboard-saved', function() {
  92. self.goto_next();
  93. });
  94. $timeout(unregister, 2000);
  95. $rootScope.$emit('save-dashboard');
  96. };
  97. }).run(function(unsavedChangesSrv) {
  98. unsavedChangesSrv.init();
  99. });
  100. });