Allow calling goBack for multiple times

This commit is contained in:
Cheng Zhao 2015-04-26 22:29:51 +08:00
parent 16b2f08cd3
commit dde791d475

View file

@ -10,15 +10,15 @@ class NavigationController
@pendingIndex = -1 @pendingIndex = -1
@webContents.on 'navigation-entry-commited', (event, url) => @webContents.on 'navigation-entry-commited', (event, url) =>
if @pendingIndex >= 0 # Go to index. if @pendingIndex is -1 # Normal navigation.
@currentIndex = @pendingIndex
@pendingIndex = -1
@history[@currentIndex] = url
else # Normal navigation.
@history = @history.slice 0, @currentIndex + 1 # Clear history. @history = @history.slice 0, @currentIndex + 1 # Clear history.
if @history[@currentIndex] isnt url if @history[@currentIndex] isnt url
@currentIndex++ @currentIndex++
@history.push url @history.push url
else # Go to index.
@currentIndex = @pendingIndex
@pendingIndex = -1
@history[@currentIndex] = url
loadUrl: (url, options={}) -> loadUrl: (url, options={}) ->
@pendingIndex = -1 @pendingIndex = -1
@ -43,10 +43,10 @@ class NavigationController
@reload() @reload()
canGoBack: -> canGoBack: ->
@currentIndex > 0 @getActiveIndex() > 0
canGoForward: -> canGoForward: ->
@currentIndex < @history.length @getActiveIndex() < @history.length - 1
canGoToIndex: (index) -> canGoToIndex: (index) ->
index >=0 and index < @history.length index >=0 and index < @history.length
@ -56,12 +56,12 @@ class NavigationController
goBack: -> goBack: ->
return unless @canGoBack() return unless @canGoBack()
@pendingIndex = @currentIndex - 1 @pendingIndex = @getActiveIndex() - 1
@webContents._loadUrl @history[@pendingIndex], {} @webContents._loadUrl @history[@pendingIndex], {}
goForward: -> goForward: ->
return unless @canGoForward() return unless @canGoForward()
@pendingIndex = @currentIndex + 1 @pendingIndex = @getActiveIndex() + 1
@webContents._loadUrl @history[@pendingIndex], {} @webContents._loadUrl @history[@pendingIndex], {}
goToIndex: (index) -> goToIndex: (index) ->
@ -73,4 +73,7 @@ class NavigationController
return unless @canGoToOffset offset return unless @canGoToOffset offset
@goToIndex @currentIndex + offset @goToIndex @currentIndex + offset
getActiveIndex: ->
if @pendingIndex is -1 then @currentIndex else @pendingIndex
module.exports = NavigationController module.exports = NavigationController