grafana/public/app/features/dashboard/unsavedChangesSrv.js

142 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-04-25 08:23:17 -05:00
define([
'angular',
2014-08-07 07:35:19 -05:00
'lodash',
'config',
2014-04-25 08:23:17 -05:00
],
2014-06-06 23:38:33 -05:00
function(angular, _, config) {
2014-04-25 08:23:17 -05:00
'use strict';
if (!config.unsaved_changes_warning) {
return;
}
2014-07-28 11:11:52 -05:00
var module = angular.module('grafana.services');
2014-04-25 08:23:17 -05:00
2014-06-08 07:40:44 -05:00
module.service('unsavedChangesSrv', function($rootScope, $modal, $q, $location, $timeout) {
2014-04-25 08:23:17 -05:00
var self = this;
var modalScope = $rootScope.$new();
2014-06-06 23:38:33 -05:00
$rootScope.$on("dashboard-loaded", function(event, newDashboard) {
2014-06-12 09:35:44 -05:00
// wait for different services to patch the dashboard (missing properties)
$timeout(function() {
self.original = angular.copy(newDashboard);
self.current = newDashboard;
}, 1200);
});
$rootScope.$on("dashboard-saved", function(event, savedDashboard) {
self.original = angular.copy(savedDashboard);
2014-06-08 07:40:44 -05:00
self.current = savedDashboard;
self.orignalPath = $location.path();
});
$rootScope.$on("$routeChangeSuccess", function() {
self.original = null;
self.originalPath = $location.path();
});
2014-06-06 23:38:33 -05:00
window.onbeforeunload = function() {
if (self.has_unsaved_changes()) {
return "There are unsaved changes to this dashboard";
}
};
this.init = function() {
$rootScope.$on("$locationChangeStart", function(event, next) {
if (self.originalPath === $location.path()) {
return;
}
if (self.has_unsaved_changes()) {
event.preventDefault();
self.next = next;
2014-06-12 09:35:44 -05:00
$timeout(self.open_modal);
}
});
};
2014-04-25 08:23:17 -05:00
2014-06-06 23:38:33 -05:00
this.open_modal = function() {
2014-04-25 08:23:17 -05:00
var confirmModal = $modal({
2014-06-12 09:35:44 -05:00
template: './app/partials/unsaved-changes.html',
modalClass: 'confirm-modal',
2014-06-12 09:35:44 -05:00
persist: true,
show: false,
scope: modalScope,
keyboard: false
});
2014-04-25 08:23:17 -05:00
$q.when(confirmModal).then(function(modalEl) {
modalEl.modal('show');
});
};
2014-06-06 23:38:33 -05:00
this.has_unsaved_changes = function() {
if (!self.original) {
2014-04-25 08:23:17 -05:00
return false;
}
2014-06-08 07:40:44 -05:00
var current = angular.copy(self.current);
var original = self.original;
// ignore timespan changes
current.time = original.time = {};
current.refresh = original.refresh;
// ignore version
current.version = original.version;
// ignore template variable values
_.each(current.templating.list, function(value, index) {
value.current = null;
value.options = null;
if (original.templating.list.length > index) {
original.templating.list[index].current = null;
original.templating.list[index].options = null;
}
});
var currentTimepicker = _.findWhere(current.nav, { type: 'timepicker' });
var originalTimepicker = _.findWhere(original.nav, { type: 'timepicker' });
if (currentTimepicker && originalTimepicker) {
currentTimepicker.now = originalTimepicker.now;
}
2014-04-25 08:23:17 -05:00
var currentJson = angular.toJson(current);
var originalJson = angular.toJson(original);
2014-04-25 08:23:17 -05:00
if (currentJson !== originalJson) {
return true;
2014-04-25 08:23:17 -05:00
}
return false;
};
2014-06-06 23:38:33 -05:00
this.goto_next = function() {
var baseLen = $location.absUrl().length - $location.url().length;
var nextUrl = self.next.substring(baseLen);
$location.url(nextUrl);
};
modalScope.ignore = function() {
self.original = null;
self.goto_next();
};
modalScope.save = function() {
var unregister = $rootScope.$on('dashboard-saved', function() {
self.goto_next();
});
$timeout(unregister, 2000);
$rootScope.$emit('save-dashboard');
2014-04-25 08:23:17 -05:00
};
}).run(function(unsavedChangesSrv) {
unsavedChangesSrv.init();
2014-04-25 08:23:17 -05:00
});
});