diff --git a/app/js/app.js b/app/js/app.js index 0c49ef36..bb24ae55 100755 --- a/app/js/app.js +++ b/app/js/app.js @@ -1,5 +1,7 @@ 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 $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 $rootScope.$on('$stateChangeSuccess', function(e, to, toParams, from, 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.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 $doc.bind('keyup', function (e) { if(e.keyCode == 27) { // Escape Key diff --git a/app/js/service-persist.js b/app/js/service-persist.js index 4b4f4ff4..c93f1d9e 100755 --- a/app/js/service-persist.js +++ b/app/js/service-persist.js @@ -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 () { if (this.lsEnabled) { return localStorage.getItem('insurance'); @@ -169,12 +173,45 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, return null; }; + /** + * Persist selected insurance type + * @param {string} name Insurance type name + */ this.setInsurance = function (name) { if (this.lsEnabled) { 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() { return this.lsEnabled; };