2012-09-03 03:24:55 +04:00
|
|
|
/*
|
|
|
|
* @provide pskl.LocalStrageService
|
|
|
|
*
|
|
|
|
* @require Constants
|
|
|
|
* @require Events
|
|
|
|
*/
|
|
|
|
$.namespace("pskl");
|
|
|
|
|
|
|
|
pskl.LocalStorageService = (function() {
|
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
var frameSheet_;
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var localStorageThrottler_ = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var persistToLocalStorageRequest_ = function() {
|
2012-09-03 03:24:55 +04:00
|
|
|
// Persist to localStorage when drawing. We throttle localStorage accesses
|
|
|
|
// for high frequency drawing (eg mousemove).
|
2012-09-11 02:52:09 +04:00
|
|
|
if(localStorageThrottler_ !== null) {
|
2012-09-03 03:24:55 +04:00
|
|
|
window.clearTimeout(localStorageThrottler_);
|
|
|
|
}
|
|
|
|
localStorageThrottler_ = window.setTimeout(function() {
|
|
|
|
persistToLocalStorage_();
|
|
|
|
localStorageThrottler_ = null;
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-09-11 02:52:09 +04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var persistToLocalStorage_ = function() {
|
|
|
|
console.log('[LocalStorage service]: Snapshot stored');
|
|
|
|
window.localStorage.snapShot = frameSheet_.serialize();
|
2012-09-03 03:24:55 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-09-11 02:52:09 +04:00
|
|
|
* @private
|
2012-09-12 14:01:47 +04:00
|
|
|
* TODO(vince): Move that away from LocalStorageService
|
2012-09-11 02:52:09 +04:00
|
|
|
*/
|
|
|
|
var restoreFromLocalStorage_ = function() {
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
frameSheet_.deserialize(window.localStorage.snapShot);
|
2012-09-12 14:01:47 +04:00
|
|
|
frameSheet_.setCurrentFrameIndex(0);
|
2012-09-03 03:24:55 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-09-11 02:52:09 +04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var cleanLocalStorage_ = function() {
|
|
|
|
console.log('[LocalStorage service]: Snapshot removed');
|
|
|
|
delete window.localStorage.snapShot;
|
2012-09-03 03:24:55 +04:00
|
|
|
};
|
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
return {
|
|
|
|
init: function(frameSheet) {
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
if(frameSheet === undefined) {
|
|
|
|
throw "Bad LocalStorageService initialization: <undefined frameSheet>";
|
|
|
|
}
|
|
|
|
frameSheet_ = frameSheet;
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
$.subscribe(Events.LOCALSTORAGE_REQUEST, persistToLocalStorageRequest_);
|
|
|
|
},
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
// TODO(vincz): Find a good place to put this UI rendering, a service should not render UI.
|
|
|
|
displayRestoreNotification: function() {
|
2012-09-11 02:54:06 +04:00
|
|
|
if(window.localStorage && window.localStorage.snapShot) {
|
2012-09-11 02:52:09 +04:00
|
|
|
var reloadLink = "<a href='#' class='localstorage-restore onclick='piskel.restoreFromLocalStorage()'>reload</a>";
|
|
|
|
var discardLink = "<a href='#' class='localstorage-discard' onclick='piskel.cleanLocalStorage()'>discard</a>";
|
|
|
|
var content = "Non saved version found. " + reloadLink + " or " + discardLink;
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-09-11 02:52:09 +04:00
|
|
|
$.publish(Events.SHOW_NOTIFICATION, [{
|
|
|
|
"content": content,
|
|
|
|
"behavior": function(rootNode) {
|
|
|
|
rootNode = $(rootNode);
|
|
|
|
rootNode.click(function(evt) {
|
|
|
|
var target = $(evt.target);
|
|
|
|
if(target.hasClass("localstorage-restore")) {
|
|
|
|
restoreFromLocalStorage_();
|
|
|
|
}
|
|
|
|
else if (target.hasClass("localstorage-discard")) {
|
|
|
|
cleanLocalStorage_();
|
|
|
|
}
|
|
|
|
$.publish(Events.HIDE_NOTIFICATION);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-09-03 03:24:55 +04:00
|
|
|
})();
|