| 
									
										
										
										
											2015-11-12 18:28:04 +08:00
										 |  |  | {ipcMain} = require 'electron' | 
					
						
							| 
									
										
										
										
											2015-05-11 16:03:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # The history operation in renderer is redirected to browser.
 | 
					
						
							| 
									
										
										
										
											2015-11-12 18:28:04 +08:00
										 |  |  | ipcMain.on 'ATOM_SHELL_NAVIGATION_CONTROLLER', (event, method, args...) -> | 
					
						
							| 
									
										
										
										
											2015-05-11 16:03:25 +08:00
										 |  |  |   event.sender[method] args... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-12 18:28:04 +08:00
										 |  |  | ipcMain.on 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', (event, method, args...) -> | 
					
						
							| 
									
										
										
										
											2015-05-19 22:41:03 +05:30
										 |  |  |   event.returnValue = event.sender[method] args... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | # 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.
 | 
					
						
							|  |  |  | # This helps us avoid Chromium's various optimizations so we can ensure renderer
 | 
					
						
							|  |  |  | # process is restarted everytime.
 | 
					
						
							|  |  |  | class NavigationController | 
					
						
							|  |  |  |   constructor: (@webContents) -> | 
					
						
							| 
									
										
										
										
											2015-05-19 22:41:03 +05:30
										 |  |  |     @clearHistory() | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-15 09:20:56 +08:00
										 |  |  |     # webContents may have already navigated to a page.
 | 
					
						
							|  |  |  |     if @webContents._getUrl() | 
					
						
							|  |  |  |       @currentIndex++ | 
					
						
							|  |  |  |       @history.push @webContents._getUrl() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 13:51:52 +08:00
										 |  |  |     @webContents.on 'navigation-entry-commited', (event, url, inPage, replaceEntry) => | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |       if @inPageIndex > -1 and not inPage | 
					
						
							|  |  |  |         # Navigated to a new page, clear in-page mark.
 | 
					
						
							|  |  |  |         @inPageIndex = -1 | 
					
						
							|  |  |  |       else if @inPageIndex is -1 and inPage | 
					
						
							|  |  |  |         # Started in-page navigations.
 | 
					
						
							|  |  |  |         @inPageIndex = @currentIndex | 
					
						
							| 
									
										
										
										
											2015-05-11 13:51:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 14:30:26 +08:00
										 |  |  |       if @pendingIndex >= 0 # Go to index.
 | 
					
						
							|  |  |  |         @currentIndex = @pendingIndex | 
					
						
							|  |  |  |         @pendingIndex = -1 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |         @history[@currentIndex] = url | 
					
						
							| 
									
										
										
										
											2015-05-11 14:30:26 +08:00
										 |  |  |       else if replaceEntry # Non-user initialized navigation.
 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |         @history[@currentIndex] = url | 
					
						
							| 
									
										
										
										
											2015-05-11 14:30:26 +08:00
										 |  |  |       else  # Normal navigation.
 | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  |         @history = @history.slice 0, @currentIndex + 1  # Clear history.
 | 
					
						
							| 
									
										
										
										
											2015-05-11 13:51:52 +08:00
										 |  |  |         currentEntry = @history[@currentIndex] | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |         if currentEntry?.url isnt url | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  |           @currentIndex++ | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |           @history.push url | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   loadUrl: (url, options={}) -> | 
					
						
							|  |  |  |     @pendingIndex = -1 | 
					
						
							|  |  |  |     @webContents._loadUrl url, options | 
					
						
							| 
									
										
										
										
											2015-07-24 13:00:03 +08:00
										 |  |  |     @webContents.emit 'load-url', url, options | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getUrl: -> | 
					
						
							|  |  |  |     if @currentIndex is -1 | 
					
						
							|  |  |  |       '' | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |       @history[@currentIndex] | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   stop: -> | 
					
						
							|  |  |  |     @pendingIndex = -1 | 
					
						
							|  |  |  |     @webContents._stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reload: -> | 
					
						
							|  |  |  |     @pendingIndex = @currentIndex | 
					
						
							|  |  |  |     @webContents._loadUrl @getUrl(), {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reloadIgnoringCache: -> | 
					
						
							|  |  |  |     @webContents._reloadIgnoringCache()  # Rely on WebContents to clear cache.
 | 
					
						
							|  |  |  |     @reload() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canGoBack: -> | 
					
						
							| 
									
										
										
										
											2015-04-26 22:29:51 +08:00
										 |  |  |     @getActiveIndex() > 0 | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   canGoForward: -> | 
					
						
							| 
									
										
										
										
											2015-04-26 22:29:51 +08:00
										 |  |  |     @getActiveIndex() < @history.length - 1 | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   canGoToIndex: (index) -> | 
					
						
							|  |  |  |     index >=0 and index < @history.length | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canGoToOffset: (offset) -> | 
					
						
							|  |  |  |     @canGoToIndex @currentIndex + offset | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 22:41:03 +05:30
										 |  |  |   clearHistory: -> | 
					
						
							|  |  |  |     @history = [] | 
					
						
							|  |  |  |     @currentIndex = -1 | 
					
						
							|  |  |  |     @pendingIndex = -1 | 
					
						
							|  |  |  |     @inPageIndex = -1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  |   goBack: -> | 
					
						
							|  |  |  |     return unless @canGoBack() | 
					
						
							| 
									
										
										
										
											2015-04-26 22:29:51 +08:00
										 |  |  |     @pendingIndex = @getActiveIndex() - 1 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |     if @inPageIndex > -1 and @pendingIndex >= @inPageIndex | 
					
						
							| 
									
										
										
										
											2015-05-11 14:30:26 +08:00
										 |  |  |       @webContents._goBack() | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |       @webContents._loadUrl @history[@pendingIndex], {} | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   goForward: -> | 
					
						
							|  |  |  |     return unless @canGoForward() | 
					
						
							| 
									
										
										
										
											2015-04-26 22:29:51 +08:00
										 |  |  |     @pendingIndex = @getActiveIndex() + 1 | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |     if @inPageIndex > -1 and @pendingIndex >= @inPageIndex | 
					
						
							| 
									
										
										
										
											2015-05-11 14:30:26 +08:00
										 |  |  |       @webContents._goForward() | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-05-11 16:37:53 +08:00
										 |  |  |       @webContents._loadUrl @history[@pendingIndex], {} | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   goToIndex: (index) -> | 
					
						
							|  |  |  |     return unless @canGoToIndex index | 
					
						
							|  |  |  |     @pendingIndex = index | 
					
						
							| 
									
										
										
										
											2015-05-17 03:31:24 +05:30
										 |  |  |     @webContents._loadUrl @history[@pendingIndex], {} | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   goToOffset: (offset) -> | 
					
						
							|  |  |  |     return unless @canGoToOffset offset | 
					
						
							| 
									
										
										
										
											2015-05-11 16:44:01 +08:00
										 |  |  |     pendingIndex = @currentIndex + offset | 
					
						
							|  |  |  |     if @inPageIndex > -1 and pendingIndex >= @inPageIndex | 
					
						
							|  |  |  |       @pendingIndex = pendingIndex | 
					
						
							|  |  |  |       @webContents._goToOffset offset | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @goToIndex pendingIndex | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-26 22:29:51 +08:00
										 |  |  |   getActiveIndex: -> | 
					
						
							|  |  |  |     if @pendingIndex is -1 then @currentIndex else @pendingIndex | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 22:41:03 +05:30
										 |  |  |   length: -> | 
					
						
							|  |  |  |     @history.length | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-26 21:28:30 +08:00
										 |  |  | module.exports = NavigationController |