Created
September 27, 2014 00:39
-
-
Save tilgovi/a495f60cafa24cdccac2 to your computer and use it in GitHub Desktop.
iframe history decorator for history in iframes with angular
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
angular.module('h.history', [ | |
'h.settings' | |
], | |
[ | |
'$provide', 'settings' | |
($provide, settings) -> | |
if settings.framed | |
$provide.decorator '$window', captureHistory | |
$provide.decorator '$sniffer', forceHistory | |
]) | |
captureHistory = [ | |
'$delegate', | |
($delegate) -> | |
history = $delegate.history | |
stack = [$delegate.location.href] | |
top = 1 | |
proxy = | |
back: -> | |
if top > 1 | |
top = top - 1 | |
$delegate.location.replace stack[top-1][2] | |
forward: -> | |
if top < stack.length | |
top = top + 1 | |
$delegate.location.replace stack[top-1][2] | |
go: (n) -> | |
switch | |
when n is 0 | |
return | |
when n + top > stack.length | |
top = stack.length | |
when n + top < 1 | |
top = 1 | |
else | |
top = top + n | |
$delegate.location.replace stack[top-1][2] | |
pushState: (state, title, url) -> | |
stack = stack.slice(0, top++).concat([[state, title, url]]) | |
proxy.state = state | |
proxy.length = top | |
$delegate.location.replace stack[top-1][2] | |
replaceState: (state, title, url) -> | |
stack[top-1] = [state, title, url] | |
proxy.state = state | |
proxy.length = top | |
$delegate.location.replace stack[top-1][2] | |
state: null | |
Object.defineProperty $delegate, 'history', | |
configurable: false | |
enumerable: true | |
value: proxy | |
] | |
forceHistory = [ | |
'$delegate', | |
($delegate) -> | |
$delegate.history = true | |
$delegate | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment