unsavedChangesSrv.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. modalClass: 'confirm-modal',
  52. persist: true,
  53. show: false,
  54. scope: modalScope,
  55. keyboard: false
  56. });
  57. $q.when(confirmModal).then(function(modalEl) {
  58. modalEl.modal('show');
  59. });
  60. };
  61. this.has_unsaved_changes = function() {
  62. if (!self.original) {
  63. return false;
  64. }
  65. var current = angular.copy(self.current);
  66. var original = self.original;
  67. // ignore timespan changes
  68. current.time = original.time = {};
  69. current.refresh = original.refresh;
  70. // ignore version
  71. current.version = original.version;
  72. // ignore template variable values
  73. _.each(current.templating.list, function(value, index) {
  74. value.current = null;
  75. value.options = null;
  76. if (original.templating.list.length > index) {
  77. original.templating.list[index].current = null;
  78. original.templating.list[index].options = null;
  79. }
  80. });
  81. var currentTimepicker = _.findWhere(current.nav, { type: 'timepicker' });
  82. var originalTimepicker = _.findWhere(original.nav, { type: 'timepicker' });
  83. if (currentTimepicker && originalTimepicker) {
  84. currentTimepicker.now = originalTimepicker.now;
  85. }
  86. var currentJson = angular.toJson(current);
  87. var originalJson = angular.toJson(original);
  88. if (currentJson !== originalJson) {
  89. return true;
  90. }
  91. return false;
  92. };
  93. this.goto_next = function() {
  94. var baseLen = $location.absUrl().length - $location.url().length;
  95. var nextUrl = self.next.substring(baseLen);
  96. $location.url(nextUrl);
  97. };
  98. modalScope.ignore = function() {
  99. self.original = null;
  100. self.goto_next();
  101. };
  102. modalScope.save = function() {
  103. var unregister = $rootScope.$on('dashboard-saved', function() {
  104. self.goto_next();
  105. });
  106. $timeout(unregister, 2000);
  107. $rootScope.$emit('save-dashboard');
  108. };
  109. }).run(function(unsavedChangesSrv) {
  110. unsavedChangesSrv.init();
  111. });
  112. });