Persist state when running and restarting the app in standalone mode

This commit is contained in:
Colin McLeod
2015-06-06 13:25:35 -07:00
parent 5b7a4edab4
commit dbd1060cb3
2 changed files with 59 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
angular.module('app', ['ui.router', 'ct.ui.router.extras.sticky', 'ui.sortable', 'shipyard', 'ngLodash', 'app.templates']) angular.module('app', ['ui.router', 'ct.ui.router.extras.sticky', 'ui.sortable', 'shipyard', 'ngLodash', 'app.templates'])
.run(['$rootScope', '$location', '$window', '$document','$state','commonArray','shipPurpose','shipSize','hardPointClass','internalGroupMap','hardpointsGroupMap', function ($rootScope, $location, $window, $doc, $state, CArr, shipPurpose, sz, hpc, igMap, hgMap) { .run(['$rootScope', '$location', '$window', '$document','$state','commonArray','shipPurpose','shipSize','hardPointClass','internalGroupMap','hardpointsGroupMap', 'Persist', '$state', function ($rootScope, $location, $window, $doc, $state, CArr, shipPurpose, sz, hpc, igMap, hgMap, Persist, $state) {
// App is running as a standalone web app on tablet/mobile
var isStandAlone = $window.navigator.standalone || ($window.external && $window.external.msIsSiteMode && $window.external.msIsSiteMode());
// Redirect any state transition errors to the error controller/state // Redirect any state transition errors to the error controller/state
$rootScope.$on('$stateChangeError', function(e, toState, toParams, fromState, fromParams, error){ $rootScope.$on('$stateChangeError', function(e, toState, toParams, fromState, fromParams, error){
@@ -10,8 +12,17 @@ angular.module('app', ['ui.router', 'ct.ui.router.extras.sticky', 'ui.sortable',
// Track on Google analytics if available // Track on Google analytics if available
$rootScope.$on('$stateChangeSuccess', function(e, to, toParams, from, fromParams) { $rootScope.$on('$stateChangeSuccess', function(e, to, toParams, from, fromParams) {
$rootScope.prevState = { name: from.name, params: fromParams }; $rootScope.prevState = { name: from.name, params: fromParams };
if(to.url && $window.ga) { // Only track states that have a URL
ga('send', 'pageview', {page: $location.path()}); if (to.url) { // Only track states that have a URL
if ($window.ga) {
ga('send', 'pageview', {page: $location.path()});
}
if (isStandAlone) {
// Persist the current state
Persist.setState({name: to.name, params: toParams});
}
} }
}); });
@@ -33,6 +44,14 @@ angular.module('app', ['ui.router', 'ct.ui.router.extras.sticky', 'ui.sortable',
$rootScope.fRPct = d3.format('%'); $rootScope.fRPct = d3.format('%');
$rootScope.fTime = function(d) { return Math.floor(d/60) + ":" + ("00" + Math.floor(d%60)).substr(-2,2); }; $rootScope.fTime = function(d) { return Math.floor(d/60) + ":" + ("00" + Math.floor(d%60)).substr(-2,2); };
if (isStandAlone) {
var state = Persist.getState();
// If a previous state has been stored, load that state
if (state && state.name && state.params) {
$state.go(state.name, state.params, {location:'replace'});
}
}
// Global Event Listeners // Global Event Listeners
$doc.bind('keyup', function (e) { $doc.bind('keyup', function (e) {
if(e.keyCode == 27) { // Escape Key if(e.keyCode == 27) { // Escape Key

View File

@@ -162,6 +162,10 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window,
} }
}; };
/**
* Get the saved insurance type
* @return {string} The name of the saved insurance type of null
*/
this.getInsurance = function () { this.getInsurance = function () {
if (this.lsEnabled) { if (this.lsEnabled) {
return localStorage.getItem('insurance'); return localStorage.getItem('insurance');
@@ -169,12 +173,45 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window,
return null; return null;
}; };
/**
* Persist selected insurance type
* @param {string} name Insurance type name
*/
this.setInsurance = function (name) { this.setInsurance = function (name) {
if (this.lsEnabled) { if (this.lsEnabled) {
return localStorage.setItem('insurance', name); return localStorage.setItem('insurance', name);
} }
}; };
/**
* Retrieve the last router state from local storage
* @param {object} state State object containing state name and params
*/
this.getState = function () {
if (this.lsEnabled) {
var state = localStorage.getItem('state');
if (state) {
return angular.fromJson(state);
}
}
return null;
};
/**
* Save the current router state to localstorage
* @param {object} state State object containing state name and params
*/
this.setState = function (state) {
if (this.lsEnabled) {
console.log('Stand Alone state update:', state);
localStorage.setItem('state',angular.toJson(state));
}
};
/**
* Check if localStorage is enabled/active
* @return {Boolean} True if localStorage is enabled
*/
this.isEnabled = function() { this.isEnabled = function() {
return this.lsEnabled; return this.lsEnabled;
}; };