mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 15:42:59 +00:00 
			
		
		
		
	Refactor browser unload task handling
Make it possible to register multiple task functions that will be called when the window is unloaded
This commit is contained in:
		
							
								
								
									
										26
									
								
								boot/boot.js
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								boot/boot.js
									
									
									
									
									
								
							| @@ -1738,7 +1738,8 @@ $tw.boot.startup = function(options) { | |||||||
| 			languagesEnvVar: "TIDDLYWIKI_LANGUAGE_PATH", | 			languagesEnvVar: "TIDDLYWIKI_LANGUAGE_PATH", | ||||||
| 			editionsEnvVar: "TIDDLYWIKI_EDITION_PATH" | 			editionsEnvVar: "TIDDLYWIKI_EDITION_PATH" | ||||||
| 		}, | 		}, | ||||||
| 		log: {} // Log flags | 		log: {}, // Log flags | ||||||
|  | 		unloadTasks: [] | ||||||
| 	}); | 	}); | ||||||
| 	if(!$tw.boot.tasks.readBrowserTiddlers) { | 	if(!$tw.boot.tasks.readBrowserTiddlers) { | ||||||
| 		// For writable tiddler files, a hashmap of title to {filepath:,type:,hasMetaFile:} | 		// For writable tiddler files, a hashmap of title to {filepath:,type:,hasMetaFile:} | ||||||
| @@ -1797,6 +1798,20 @@ $tw.boot.startup = function(options) { | |||||||
| 	// Install the tiddler deserializer modules | 	// Install the tiddler deserializer modules | ||||||
| 	$tw.Wiki.tiddlerDeserializerModules = Object.create(null); | 	$tw.Wiki.tiddlerDeserializerModules = Object.create(null); | ||||||
| 	$tw.modules.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerModules); | 	$tw.modules.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerModules); | ||||||
|  | 	// Call unload handlers in the browser | ||||||
|  | 	if($tw.browser) { | ||||||
|  | 		window.onbeforeunload = function(event) { | ||||||
|  | 			event = event || {}; | ||||||
|  | 			var result; | ||||||
|  | 			$tw.utils.each($tw.unloadTasks,function(task) { | ||||||
|  | 				var r = task(event); | ||||||
|  | 				if(r) { | ||||||
|  | 					result = r; | ||||||
|  | 				} | ||||||
|  | 			}); | ||||||
|  | 			return result; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| 	// Load tiddlers | 	// Load tiddlers | ||||||
| 	if($tw.boot.tasks.readBrowserTiddlers) { | 	if($tw.boot.tasks.readBrowserTiddlers) { | ||||||
| 		$tw.loadTiddlersBrowser(); | 		$tw.loadTiddlersBrowser(); | ||||||
| @@ -1833,6 +1848,15 @@ $tw.boot.startup = function(options) { | |||||||
| 	$tw.boot.executeNextStartupTask(); | 	$tw.boot.executeNextStartupTask(); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | Add another unload task | ||||||
|  | */ | ||||||
|  | $tw.addUnloadTask = function(task) { | ||||||
|  | 	if($tw.unloadTasks.indexOf(task) === -1) { | ||||||
|  | 		$tw.unloadTasks.push(task); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| /* | /* | ||||||
| Execute the remaining eligible startup tasks | Execute the remaining eligible startup tasks | ||||||
| */ | */ | ||||||
|   | |||||||
| @@ -75,14 +75,14 @@ function SaverHandler(options) { | |||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
| 		// Set up our beforeunload handler | 		// Set up our beforeunload handler | ||||||
| 		window.onbeforeunload = function(event) { | 		$tw.addUnloadTask(function(event) { | ||||||
| 			var confirmationMessage; | 			var confirmationMessage; | ||||||
| 			if(self.isDirty()) { | 			if(self.isDirty()) { | ||||||
| 				confirmationMessage = $tw.language.getString("UnsavedChangesWarning"); | 				confirmationMessage = $tw.language.getString("UnsavedChangesWarning"); | ||||||
| 				event.returnValue = confirmationMessage; // Gecko | 				event.returnValue = confirmationMessage; // Gecko | ||||||
| 			} | 			} | ||||||
| 			return confirmationMessage; | 			return confirmationMessage; | ||||||
| 		}; | 		}); | ||||||
| 	} | 	} | ||||||
| 	// Install the save action handlers | 	// Install the save action handlers | ||||||
| 	if($tw.browser) { | 	if($tw.browser) { | ||||||
|   | |||||||
| @@ -39,14 +39,14 @@ function Syncer(options) { | |||||||
| 	// Browser event handlers | 	// Browser event handlers | ||||||
| 	if($tw.browser) { | 	if($tw.browser) { | ||||||
| 		// Set up our beforeunload handler | 		// Set up our beforeunload handler | ||||||
| 		window.onbeforeunload = function(event) { | 		$tw.addUnloadTask(function(event) { | ||||||
| 			var confirmationMessage; | 			var confirmationMessage; | ||||||
| 			if(self.isDirty()) { | 			if(self.isDirty()) { | ||||||
| 				confirmationMessage = $tw.language.getString("UnsavedChangesWarning"); | 				confirmationMessage = $tw.language.getString("UnsavedChangesWarning"); | ||||||
| 				event.returnValue = confirmationMessage; // Gecko | 				event.returnValue = confirmationMessage; // Gecko | ||||||
| 			} | 			} | ||||||
| 			return confirmationMessage; | 			return confirmationMessage; | ||||||
| 		}; | 		}); | ||||||
| 		// Listen out for login/logout/refresh events in the browser | 		// Listen out for login/logout/refresh events in the browser | ||||||
| 		$tw.rootWidget.addEventListener("tm-login",function() { | 		$tw.rootWidget.addEventListener("tm-login",function() { | ||||||
| 			self.handleLoginEvent(); | 			self.handleLoginEvent(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jermolene
					Jermolene