navigation: adding clearHistory method

This commit is contained in:
deepak1556 2015-05-19 22:41:03 +05:30
parent ec5d05e226
commit b0e8cafa00
5 changed files with 29 additions and 4 deletions

View file

@ -4,6 +4,9 @@ ipc = require 'ipc'
ipc.on 'ATOM_SHELL_NAVIGATION_CONTROLLER', (event, method, args...) ->
event.sender[method] args...
ipc.on 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', (event, method, args...) ->
event.returnValue = event.sender[method] args...
# JavaScript implementation of Chromium's NavigationController.
# Instead of relying on Chromium for history control, we compeletely do history
# control on user land, and only rely on WebContents.loadUrl for navigation.
@ -11,10 +14,7 @@ ipc.on 'ATOM_SHELL_NAVIGATION_CONTROLLER', (event, method, args...) ->
# process is restarted everytime.
class NavigationController
constructor: (@webContents) ->
@history = []
@currentIndex = -1
@pendingIndex = -1
@inPageIndex = -1
@clearHistory()
@webContents.on 'navigation-entry-commited', (event, url, inPage, replaceEntry) =>
if @inPageIndex > -1 and not inPage
@ -71,6 +71,12 @@ class NavigationController
canGoToOffset: (offset) ->
@canGoToIndex @currentIndex + offset
clearHistory: ->
@history = []
@currentIndex = -1
@pendingIndex = -1
@inPageIndex = -1
goBack: ->
return unless @canGoBack()
@pendingIndex = @getActiveIndex() - 1
@ -104,4 +110,7 @@ class NavigationController
getActiveIndex: ->
if @pendingIndex is -1 then @currentIndex else @pendingIndex
length: ->
@history.length
module.exports = NavigationController

View file

@ -85,6 +85,13 @@ ipc.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (message, targetOrigin) ->
# Forward history operations to browser.
sendHistoryOperation = (args...) ->
ipc.send 'ATOM_SHELL_NAVIGATION_CONTROLLER', args...
getHistoryOperation = (args...) ->
ipc.sendSync 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', args...
window.history.back = -> sendHistoryOperation 'goBack'
window.history.forward = -> sendHistoryOperation 'goForward'
window.history.go = (offset) -> sendHistoryOperation 'goToOffset', offset
Object.defineProperty window.history, 'length',
get: ->
getHistoryOperation 'length'

View file

@ -244,6 +244,7 @@ registerWebViewElement = ->
"canGoBack"
"canGoForward"
"canGoToOffset"
"clearHistory"
"goBack"
"goForward"
"goToIndex"

View file

@ -778,6 +778,10 @@ Returns whether the web page can go forward.
Returns whether the web page can go to `offset`.
### WebContents.clearHistory()
Clears the navigation history.
### WebContents.goBack()
Makes the web page go back.

View file

@ -165,6 +165,10 @@ Returns whether guest page can go forward.
Returns whether guest page can go to `offset`.
### `<webview>`.clearHistory()
Clears the navigation history.
### `<webview>`.goBack()
Makes guest page go back.