Skip to content

Instantly share code, notes, and snippets.

@tilgovi
Created September 27, 2014 00:39
Show Gist options
  • Select an option

  • Save tilgovi/a495f60cafa24cdccac2 to your computer and use it in GitHub Desktop.

Select an option

Save tilgovi/a495f60cafa24cdccac2 to your computer and use it in GitHub Desktop.

Revisions

  1. tilgovi created this gist Sep 27, 2014.
    68 changes: 68 additions & 0 deletions history.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    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
    ]