]> khamidou — GitHub mirrors - electron.git/commitdiff
Allow calling goBack for multiple times
authorCheng Zhao <zcbenz@gmail.com>
Sun, 26 Apr 2015 14:29:51 +0000 (22:29 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 27 Apr 2015 07:11:59 +0000 (15:11 +0800)
atom/browser/api/lib/navigation-controller.coffee

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