Last active
August 29, 2015 13:56
-
-
Save ckung/8833891 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
"use strict"; | |
var positions = []; | |
var currentScrollPosition = 0; | |
var ScrollPositions = { | |
currentScrollPosition: function() { | |
return currentScrollPosition; | |
}, | |
setRoutePosition: function(route, position) { | |
positions[route] = position; | |
}, | |
setCurrentScrollPosition: function(position) { | |
currentScrollPosition = position; | |
}, | |
setCurrentScrollPositionFrom: function(route) { | |
currentScrollPosition = positions[route]; | |
} | |
}; | |
return ScrollPositions; | |
}); | |
define(function(require) { | |
"use strict"; | |
var $ = require("jquery"); | |
var ApplicationConfig = require("modules/application/ApplicationConfig"); | |
require("jquery.bbq"); | |
var root = ApplicationConfig.applicationRoot; | |
var URL = { | |
getHashParameters: function() { | |
var hashParameters = $.bbq.getState(); | |
return $.isEmptyObject(hashParameters) ? null : hashParameters; | |
}, | |
getFragment: function() { | |
return window.location.pathname.substr(root.length); | |
} | |
}; | |
return URL; | |
}); | |
define(function(require) { | |
"use strict"; | |
var routeHistory = []; | |
var EventBus = require("EventBus"); | |
var URL = require("modules/util/URL"); | |
var ScrollPositions = require("modules/application/ScrollPositions"); | |
var currentPointer = -1; | |
function getPreviousRoute() { | |
return routeHistory[currentPointer - 1]; | |
} | |
function getNextRoute() { | |
return routeHistory[currentPointer + 1]; | |
} | |
function setNextRoute(route) { | |
routeHistory[currentPointer + 1] = route; | |
} | |
var RouteHistory = { | |
saveLeaveState: function() { | |
var route = URL.getFragment(); | |
var position = $(window).scrollTop(); | |
ScrollPositions.setRoutePosition(route, position); | |
}, | |
init: function() { | |
EventBus.subscribe(EventBus.LINK_CLICK_EVENT, this.saveLeaveState); | |
EventBus.subscribe(EventBus.FIRE_ROUTE_EVENT, this.saveLeaveState); | |
}, | |
reset: function() { | |
routeHistory = []; | |
}, | |
isSoftRoute: function(route) { | |
return (this.isBackRoute(route) || this.isForwardRoute(route)); | |
}, | |
isBackRoute: function(route) { | |
return routeHistory.length > 1 && route == getPreviousRoute(); | |
}, | |
isForwardRoute: function(route) { | |
return routeHistory.length > 1 && route == getNextRoute(); | |
}, | |
atAPreviousRoute: function() { | |
//pointer not at the end because it moved back from hitting back | |
return currentPointer !== routeHistory.length - 1; | |
}, | |
record: function(route) { | |
if(this.isBackRoute(route)) { | |
currentPointer--; | |
return currentPointer; | |
} | |
if(this.isForwardRoute(route)) { | |
currentPointer++; | |
return currentPointer; | |
} | |
if(this.atAPreviousRoute()) { | |
setNextRoute(route); | |
currentPointer++; | |
routeHistory = routeHistory.slice(0, currentPointer + 1); | |
} else { | |
routeHistory.push(route); | |
currentPointer++; | |
} | |
return currentPointer; | |
} | |
}; | |
return RouteHistory; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment