| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * Core features that setup the life-cycle and the base interfaces for  | 
					
						
							|  |  |  | * features to use... | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | define(function(require){ var module = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //var DEBUG = DEBUG != null ? DEBUG : true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var actions = require('lib/actions') | 
					
						
							|  |  |  | var features = require('lib/features') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ImageGridFeatures = | 
					
						
							|  |  |  | module.ImageGridFeatures = Object.create(features.FeatureSet) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | // setup exit...
 | 
					
						
							|  |  |  | if(typeof(process) != 'undefined'){ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-31 10:37:21 +03:00
										 |  |  | 	// NOTE: if this passes it is async while when fails it's sync, this
 | 
					
						
							|  |  |  | 	// 		is why we set .runtime to 'nw' optimistically in advance so 
 | 
					
						
							|  |  |  | 	// 		as not to wait if all goes well and set it to 'node' in the 
 | 
					
						
							|  |  |  | 	// 		callback that if fails will fail right away...
 | 
					
						
							|  |  |  | 	ImageGridFeatures.runtime = 'nw' | 
					
						
							|  |  |  | 	requirejs(['nw.gui'],  | 
					
						
							|  |  |  | 		// OK: nw.js
 | 
					
						
							|  |  |  | 		function(){},  | 
					
						
							|  |  |  | 		// ERR: pure node.js...
 | 
					
						
							|  |  |  | 		function(){ ImageGridFeatures.runtime = 'node' }) | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | // browser...
 | 
					
						
							| 
									
										
										
										
											2015-12-31 10:37:21 +03:00
										 |  |  | } else if(typeof(window) != 'undefined'){ | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 	ImageGridFeatures.runtime = 'browser' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // unknown...
 | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  | 	ImageGridFeatures.runtime = 'unknown' | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX should this be a generic library thing???
 | 
					
						
							|  |  |  | // XXX should his have state???
 | 
					
						
							|  |  |  | // 		...if so, should this be a toggler???
 | 
					
						
							|  |  |  | var LifeCycleActions = actions.Actions({ | 
					
						
							|  |  |  | 	start: ['- System/',  | 
					
						
							|  |  |  | 		function(){ | 
					
						
							|  |  |  | 			var that = this | 
					
						
							|  |  |  | 			this.logger && this.logger.emit('start') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// NOTE: jQuery currently provides no way to check if an event
 | 
					
						
							|  |  |  | 			// 		is bound so we'll need to keep track manually...
 | 
					
						
							|  |  |  | 			if(this.__stop_handler == null){ | 
					
						
							|  |  |  | 				var stop = this.__stop_handler = function(){ that.stop() } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 			// set the runtime...
 | 
					
						
							|  |  |  | 			var runtime = this.runtime = ImageGridFeatures.runtime | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 			// nw.js...
 | 
					
						
							|  |  |  | 			if(runtime == 'nw'){ | 
					
						
							|  |  |  | 				var gui = requirejs('nw.gui') | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 				// this handles both reload and close...
 | 
					
						
							|  |  |  | 				$(window).on('beforeunload', stop) | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 				// NOTE: we are using both events as some of them do not
 | 
					
						
							|  |  |  | 				// 		get triggered in specific conditions and some do,
 | 
					
						
							|  |  |  | 				// 		for example, this gets triggered when the window's
 | 
					
						
							|  |  |  | 				// 		'X' is clicked while does not on reload...
 | 
					
						
							|  |  |  | 				this.__nw_stop_handler = function(){ | 
					
						
							|  |  |  | 					var w = this | 
					
						
							|  |  |  | 					try{ | 
					
						
							|  |  |  | 						that | 
					
						
							|  |  |  | 							// wait till ALL the handlers finish before 
 | 
					
						
							|  |  |  | 							// exiting...
 | 
					
						
							|  |  |  | 							.on('stop.post', function(){ | 
					
						
							|  |  |  | 								w.close(true) | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 							.stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// in case something breaks exit...
 | 
					
						
							|  |  |  | 					// XXX not sure if this is correct...
 | 
					
						
							|  |  |  | 					} catch(e){ | 
					
						
							|  |  |  | 						this.close(true) | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 				gui.Window.get().on('close', this.__nw_stop_handler) | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 			// node.js...
 | 
					
						
							|  |  |  | 			} else if(runtime == 'node'){ | 
					
						
							|  |  |  | 				process.on('exit', stop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// browser...
 | 
					
						
							|  |  |  | 			} else if(runtime == 'browser'){ | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 				$(window).on('beforeunload', stop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 			// other...
 | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2015-12-29 07:28:19 +03:00
										 |  |  | 				// XXX
 | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}], | 
					
						
							|  |  |  | 	// unbind events...
 | 
					
						
							|  |  |  | 	stop: ['- System/',  | 
					
						
							|  |  |  | 		function(){ | 
					
						
							|  |  |  | 			// browser & nw...
 | 
					
						
							|  |  |  | 			if(this.__stop_handler  | 
					
						
							|  |  |  | 					&& (this.runtime == 'browser' || this.runtime == 'nw')){ | 
					
						
							|  |  |  | 				$(window).off('beforeunload', this.__stop_handler) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// nw...
 | 
					
						
							|  |  |  | 			if(this.__nw_stop_handler && this.runtime == 'nw'){ | 
					
						
							|  |  |  | 				var gui = requirejs('nw.gui') | 
					
						
							|  |  |  | 				gui.Window.get().off('close', this.__nw_stop_handler) | 
					
						
							|  |  |  | 				delete this.__nw_stop_handler | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// node...
 | 
					
						
							| 
									
										
										
										
											2015-12-31 10:37:21 +03:00
										 |  |  | 			/* XXX there's no process.off(...) | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 			if(this.__stop_handler && this.runtime == 'node'){ | 
					
						
							|  |  |  | 				process.off('exit', this.__stop_handler) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-31 10:37:21 +03:00
										 |  |  | 			*/ | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			delete this.__stop_handler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			this.logger && this.logger.emit('stop') | 
					
						
							|  |  |  | 		}], | 
					
						
							| 
									
										
										
										
											2015-12-26 01:12:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 	// XXX need a clear protocol for this...
 | 
					
						
							|  |  |  | 	// 		something like:
 | 
					
						
							|  |  |  | 	// 			- clear state
 | 
					
						
							|  |  |  | 	// 			- load state
 | 
					
						
							|  |  |  | 	reset: ['System/', | 
					
						
							|  |  |  | 		function(){ | 
					
						
							|  |  |  | 		}], | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var LifeCycle =  | 
					
						
							|  |  |  | module.LifeCycle = ImageGridFeatures.Feature({ | 
					
						
							|  |  |  | 	title: '', | 
					
						
							|  |  |  | 	doc: '', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tag: 'lifecycle', | 
					
						
							| 
									
										
										
										
											2016-01-03 04:49:00 +03:00
										 |  |  | 	priority: 'high', | 
					
						
							| 
									
										
										
										
											2015-12-17 03:34:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	actions: LifeCycleActions, | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                                                */ | 
					
						
							|  |  |  | return module }) |