unsavedChangesSrv.js 3.0 KB

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