| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							| 
									
										
										
										
											2020-11-26 22:56:23 +03:00
										 |  |  | * XXX need ability to extend event to implement proxy events... | 
					
						
							|  |  |  | * 		...i.e. .on(..) / ... get called on one object but the handler | 
					
						
							|  |  |  | * 		bound on a different object via a proxy event method... | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | * XXX is types/events the right place for this??? | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | * XXX should we have .pre/.post events??? | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | * XXX should we propogate event handling to parent/overloaded events??? | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | * | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | **********************************************/  /* c8 ignore next 2 */ | 
					
						
							|  |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var object = require('ig-object') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | // Event method wrappers...
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-25 20:39:55 +03:00
										 |  |  | var EventCommand =  | 
					
						
							|  |  |  | module.EventCommand =  | 
					
						
							|  |  |  | object.Constructor('EventCommand', { | 
					
						
							|  |  |  | 	name: null, | 
					
						
							|  |  |  | 	__init__: function(name, data={}){ | 
					
						
							|  |  |  | 		Object.assign(this, data, {name}) }, | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.TRIGGER = module.EventCommand('TRIGGER') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | // Create an "eventful" method...
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // The resulting method can be either called directly or via .trigger(..).
 | 
					
						
							|  |  |  | // Handlrs can be bound to it via .on(..) and unbound via .off(..) and 
 | 
					
						
							|  |  |  | // calling it will trigger the handlers either after the user func(..)
 | 
					
						
							|  |  |  | // return or when the user calles the passed handler(..) function.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | // 	Eventful(name[, options])
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | // 		-> method
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | // 	Eventful(name, func[, options])
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | // 		-> method
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	Trigger the event...
 | 
					
						
							|  |  |  | //	method(...args)
 | 
					
						
							|  |  |  | //		-> ..
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:43:45 +03:00
										 |  |  | //		NOTE: if func(..) returns undefined this will return undefined 
 | 
					
						
							|  |  |  | //			when called without a context and the call context otherwise
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 	func(handle, ...args)
 | 
					
						
							|  |  |  | // 		-> ..
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	trigger event handlers...
 | 
					
						
							|  |  |  | //	handle()
 | 
					
						
							|  |  |  | //	handle(true)
 | 
					
						
							|  |  |  | //		-> true
 | 
					
						
							|  |  |  | //		-> false
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-05-25 20:54:12 +03:00
										 |  |  | //	trigger event handlers and overload handler arguments...
 | 
					
						
							|  |  |  | //	handle(true, ...)
 | 
					
						
							|  |  |  | //		-> true
 | 
					
						
							|  |  |  | //		-> false
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | //	prevent event handlers from triggering...
 | 
					
						
							|  |  |  | //	handle(false)
 | 
					
						
							|  |  |  | //		-> undefined
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Special case: EventCommand...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	EventCommand instance can be passed as the first argument of method, 
 | 
					
						
							|  |  |  | //	in this case the event function will get it but the event handlers 
 | 
					
						
							|  |  |  | //	will not...
 | 
					
						
							|  |  |  | //	This is done to be able to externally pass commands to event methods
 | 
					
						
							|  |  |  | //	that get handled in a special way by the function but not passed to 
 | 
					
						
							|  |  |  | //	the event handlers...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 		method(<event-command>, ...args)
 | 
					
						
							|  |  |  | // 			-> ..
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 		func(handle, <event-command>, ...args)
 | 
					
						
							|  |  |  | // 			-> ..
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: calling handle(false) will exiplicitly disable calling the 
 | 
					
						
							|  |  |  | // 		handlers for that call...
 | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | var Eventful = | 
					
						
							|  |  |  | module.Eventful = | 
					
						
							|  |  |  | object.Constructor('Eventful', { | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	handlerLocation: 'context', | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:43:45 +03:00
										 |  |  | 	// Sets the default return mode for .__call__(..) if either .func(..)
 | 
					
						
							|  |  |  | 	// is not set or returns undefined...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// Can be:
 | 
					
						
							|  |  |  | 	// 		'context'
 | 
					
						
							|  |  |  | 	// 		undefined
 | 
					
						
							|  |  |  | 	defaultReturn: 'context', | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 	name: null, | 
					
						
							|  |  |  | 	func: null, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toString: function(){ | 
					
						
							|  |  |  | 		return this.func ? | 
					
						
							|  |  |  | 			`${this.constructor.name} ` | 
					
						
							|  |  |  | 				+(this.func.toString() | 
					
						
							|  |  |  | 					.replace(/^(function[^(]*\()[^,)]*, ?/, '$1'))  | 
					
						
							|  |  |  | 			: `${this.constructor.name} function ${this.name}(){}` }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	__event_handlers__: null, | 
					
						
							|  |  |  | 	bind: function(context, handler){ | 
					
						
							|  |  |  | 		var handlers =  | 
					
						
							|  |  |  | 			// local...
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 			this.handlerLocation == 'method' ? | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 				(this.__event_handlers__ = this.__event_handlers__ || []) | 
					
						
							|  |  |  | 			// context (default)...
 | 
					
						
							| 
									
										
										
										
											2022-08-04 16:39:36 +03:00
										 |  |  | 			//: (context.__event_handlers__ == null ?
 | 
					
						
							|  |  |  | 			: !context.hasOwnProperty('__event_handlers__') ? | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 				Object.defineProperty(context, '__event_handlers__', { | 
					
						
							|  |  |  | 						value: {[this.name]: (handlers = [])}, | 
					
						
							|  |  |  | 						enumerable: false, | 
					
						
							| 
									
										
										
										
											2022-08-04 16:31:05 +03:00
										 |  |  | 						configurable: true, | 
					
						
							|  |  |  | 						writable: true, | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 					})  | 
					
						
							|  |  |  | 					&& handlers | 
					
						
							| 
									
										
										
										
											2022-08-04 16:39:36 +03:00
										 |  |  | 			: (context.__event_handlers__[this.name] =  | 
					
						
							|  |  |  | 				context.__event_handlers__[this.name] || []) | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 		// add handler...
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 		handlers.push(handler) | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 		return this }, | 
					
						
							|  |  |  | 	unbind: function(context, handler){ | 
					
						
							|  |  |  | 		var handlers =  | 
					
						
							| 
									
										
										
										
											2022-08-04 16:48:28 +03:00
										 |  |  | 			this.handlerLocation == 'method' ? | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 				method.__event_handlers__ | 
					
						
							| 
									
										
										
										
											2022-08-04 16:48:28 +03:00
										 |  |  | 			//: (context.__event_handlers__ || {})[this.name]) || []
 | 
					
						
							|  |  |  | 			: context.hasOwnProperty('__event_handlers__') ? | 
					
						
							|  |  |  | 				(context.__event_handlers__ || {})[this.name] || [] | 
					
						
							|  |  |  | 			: [] | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 		handlers.splice(0, handlers.length, | 
					
						
							|  |  |  | 			...handlers.filter(function(h){ | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 				return h !== handler | 
					
						
							|  |  |  | 					&& h.__event_original_handler !== handler })) | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 		return this }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	__call__: function(context, ...args){ | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 		var that = this | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 		var handlers =  | 
					
						
							|  |  |  | 			this.handlerLocation == 'method' ? | 
					
						
							|  |  |  | 				(this.__event_handlers__ || []) | 
					
						
							|  |  |  | 			: [] | 
					
						
							|  |  |  | 		// context (default)...
 | 
					
						
							|  |  |  | 		// NOTE: these are allways called...
 | 
					
						
							|  |  |  | 		handlers = handlers | 
					
						
							| 
									
										
										
										
											2022-08-04 16:48:28 +03:00
										 |  |  | 			//.concat((context.__event_handlers__ || {})[this.name] || [])
 | 
					
						
							|  |  |  | 			.concat(context.hasOwnProperty('__event_handlers__') ? | 
					
						
							|  |  |  | 				(context.__event_handlers__ || {})[this.name] || [] | 
					
						
							|  |  |  | 				: []) | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// NOTE: this will stop event handling if one of the handlers 
 | 
					
						
							|  |  |  | 		// 		explicitly returns false...
 | 
					
						
							|  |  |  | 		// NOTE: if the user does not call handle() it will be called 
 | 
					
						
							|  |  |  | 		// 		after the event action is done but before it returns...
 | 
					
						
							|  |  |  | 		// NOTE: to explicitly disable calling the handlers func must 
 | 
					
						
							|  |  |  | 		// 		call handle(false)
 | 
					
						
							|  |  |  | 		var did_handle = false | 
					
						
							| 
									
										
										
										
											2022-05-25 20:54:12 +03:00
										 |  |  | 		var handle = function(run=true, ...alt_args){ | 
					
						
							| 
									
										
										
										
											2020-12-01 20:37:08 +03:00
										 |  |  | 			did_handle = true | 
					
						
							| 
									
										
										
										
											2022-05-25 20:54:12 +03:00
										 |  |  | 			var a = (run === true  | 
					
						
							|  |  |  | 					&& arguments.length > 1) ? | 
					
						
							|  |  |  | 				alt_args | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 				: args | 
					
						
							| 
									
										
										
										
											2022-05-25 20:54:12 +03:00
										 |  |  | 			a = a[0] instanceof EventCommand ? | 
					
						
							|  |  |  | 				a.slice(1) | 
					
						
							|  |  |  | 				: a | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 			return run ? | 
					
						
							|  |  |  | 				handlers | 
					
						
							|  |  |  | 					.reduce(function(res, handler){  | 
					
						
							|  |  |  | 						return res === true  | 
					
						
							| 
									
										
										
										
											2020-12-02 17:43:31 +03:00
										 |  |  | 							&& handler.call(context, that.name, ...a) !== false }, true)  | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 				: undefined }  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// call...
 | 
					
						
							|  |  |  | 		var res = this.func ? | 
					
						
							|  |  |  | 			this.func.call(context, handle, ...args) | 
					
						
							|  |  |  | 			: undefined | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// call the handlers if the user either didn't call handle()
 | 
					
						
							|  |  |  | 		// or explicitly called handle(false)...
 | 
					
						
							|  |  |  | 		!did_handle | 
					
						
							|  |  |  | 			&& handle() | 
					
						
							| 
									
										
										
										
											2022-10-24 17:43:45 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return res  | 
					
						
							|  |  |  | 			?? (this.defaultReturn == 'context' ? | 
					
						
							|  |  |  |    				context | 
					
						
							|  |  |  | 				: res ) }, | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	__init__: function(name, func, options={}){ | 
					
						
							|  |  |  | 		options = func && typeof(func) != 'function' ? | 
					
						
							|  |  |  | 			func | 
					
						
							|  |  |  | 			: options | 
					
						
							|  |  |  | 		Object.assign(this, options) | 
					
						
							|  |  |  | 		Object.defineProperty(this, 'name', { value: name }) | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 		func  | 
					
						
							|  |  |  | 			&& typeof(func) == 'function' | 
					
						
							|  |  |  | 			&& Object.defineProperty(this, 'func', {  | 
					
						
							|  |  |  | 				value: func,  | 
					
						
							|  |  |  | 				enumerable: false, | 
					
						
							|  |  |  | 			}) }, | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | // Extends Eventful(..) adding ability to bind events via the 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | // resulting method directly by passing it a function...
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2020-11-15 03:12:33 +03:00
										 |  |  | //	Event(name[, options])
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | //		-> method
 | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2020-11-15 03:12:33 +03:00
										 |  |  | //	Event(name, func[, options])
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | //		-> method
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	Bind handler...
 | 
					
						
							|  |  |  | //	method(handler)
 | 
					
						
							|  |  |  | //		-> this
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	Unbind handler...
 | 
					
						
							|  |  |  | //	method(handler, false)
 | 
					
						
							|  |  |  | //		-> this
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	Trigger handlers...
 | 
					
						
							|  |  |  | //	method(...args)
 | 
					
						
							|  |  |  | //		-> this
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	func(handle, ...args)
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2020-11-15 05:59:19 +03:00
										 |  |  | // Special case:
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	Force trigger event...
 | 
					
						
							|  |  |  | //	method(TRIGGER, ...args)
 | 
					
						
							|  |  |  | //		-> this
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This will pass args to the event action regardless whether the first 
 | 
					
						
							|  |  |  | // arg is a function or not...
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | var Event = | 
					
						
							| 
									
										
										
										
											2020-11-15 03:12:33 +03:00
										 |  |  | module.Event = | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | object.Constructor('Event', Eventful, { | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 	toString: function(){ | 
					
						
							|  |  |  | 		return this.orig_func ? | 
					
						
							|  |  |  | 			'Event ' | 
					
						
							|  |  |  | 				+this.orig_func.toString() | 
					
						
							|  |  |  | 					.replace(/^(function[^(]*\()[^,)]*, ?/, '$1') | 
					
						
							|  |  |  | 			: `Event function ${this.name}(){}`}, | 
					
						
							|  |  |  | 	__call__: function(context, ...args){ | 
					
						
							| 
									
										
										
										
											2022-10-24 17:43:45 +03:00
										 |  |  | 		// add handler...
 | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 		// NOTE: when the first arg is an event command this will
 | 
					
						
							|  |  |  | 		// 		fall through to calling the action...
 | 
					
						
							| 
									
										
										
										
											2022-10-24 17:43:45 +03:00
										 |  |  | 		if(typeof(args[0]) == 'function'){ | 
					
						
							|  |  |  | 			this.bind(context, args[0])  | 
					
						
							|  |  |  | 			return context | 
					
						
							|  |  |  | 		// call the action...
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return object.parentCall(Event.prototype.__call__, this, context, ...args) } },  | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | // Like Event(..) but produces an event method that can only be triggered 
 | 
					
						
							|  |  |  | // via .trigger(name, ...), calling this is a no-op...
 | 
					
						
							| 
									
										
										
										
											2020-11-25 20:39:55 +03:00
										 |  |  | var PureEvent = | 
					
						
							|  |  |  | module.PureEvent = | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | object.Constructor('PureEvent', Event, { | 
					
						
							|  |  |  | 	toString: function(){ | 
					
						
							|  |  |  | 		return `PureEvent ${this.name}(){}`}, | 
					
						
							| 
									
										
										
										
											2022-10-28 14:08:26 +03:00
										 |  |  | 	__init__: function(name, func, options={}){ | 
					
						
							|  |  |  | 		if(typeof(func) != 'function'){ | 
					
						
							|  |  |  | 			options = func | 
					
						
							|  |  |  | 			func = undefined } | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 		object.parentCall(PureEvent.prototype.__init__, this, | 
					
						
							|  |  |  | 			name,  | 
					
						
							| 
									
										
										
										
											2022-10-28 14:15:28 +03:00
										 |  |  | 			function(handle, trigger, ...args){  | 
					
						
							| 
									
										
										
										
											2022-10-28 14:08:26 +03:00
										 |  |  | 				trigger === module.TRIGGER ? | 
					
						
							|  |  |  | 					func && func.call(this, handle, ...args) | 
					
						
							|  |  |  | 					: handle(false) }, options) }, | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2020-11-26 22:56:23 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | //---------------------------------------------------------------------
 | 
					
						
							|  |  |  | // Mixins...
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | // XXX might be nice to add support to pre/post handlers...
 | 
					
						
							|  |  |  | // XXX still not sure about the builtin-local event control flow...
 | 
					
						
							| 
									
										
										
										
											2020-11-15 02:53:05 +03:00
										 |  |  | // XXX do we need to be able to force global handler???
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | var EventHandlerMixin =  | 
					
						
							| 
									
										
										
										
											2020-11-19 02:24:09 +03:00
										 |  |  | module.EventHandlerMixin = object.Mixin('EventHandlerMixin', { | 
					
						
							| 
									
										
										
										
											2020-11-24 05:48:54 +03:00
										 |  |  | 	//__event_handlers__: null,
 | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 	on: function(evt, func){ | 
					
						
							|  |  |  | 		// event...
 | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | 		if(evt in this  | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 				&& this[evt].bind){ | 
					
						
							|  |  |  | 			this[evt].bind(this, func) | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 		// non-event...
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2022-08-04 16:39:36 +03:00
										 |  |  | 			//this.__event_handlers__ == null
 | 
					
						
							|  |  |  | 			!this.hasOwnProperty('__event_handlers__') | 
					
						
							| 
									
										
										
										
											2020-11-24 05:48:54 +03:00
										 |  |  | 				&& Object.defineProperty(this, '__event_handlers__', { | 
					
						
							|  |  |  | 					value: {}, | 
					
						
							|  |  |  | 					enumerable: false, | 
					
						
							| 
									
										
										
										
											2022-08-04 16:31:05 +03:00
										 |  |  | 					configurable: true, | 
					
						
							|  |  |  | 					writable: true, | 
					
						
							| 
									
										
										
										
											2020-11-24 05:48:54 +03:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 			;(this.__event_handlers__[evt] =  | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 					this.__event_handlers__[evt] || []) | 
					
						
							|  |  |  | 				.push(func) } | 
					
						
							|  |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | 	one: function(evt, func){ | 
					
						
							|  |  |  | 		var handler | 
					
						
							|  |  |  | 		this.on(evt,  | 
					
						
							| 
									
										
										
										
											2020-11-15 05:13:35 +03:00
										 |  |  | 			handler = Object.assign( | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | 				function(handle, ...args){ | 
					
						
							|  |  |  | 					this.off(evt, handler) | 
					
						
							| 
									
										
										
										
											2020-11-15 05:13:35 +03:00
										 |  |  | 					return func.call(this, handle, ...args) }.bind(this), | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 				{__event_original_handler: func})) | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 	// XXX do we need .off(evt, 'all')
 | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | 	off: function(evt, func){ | 
					
						
							|  |  |  | 		// event...
 | 
					
						
							|  |  |  | 		if(evt in this  | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 				&& this[evt].unbind){ | 
					
						
							|  |  |  | 			this[evt].unbind(this, func) | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | 		// non-event...
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 			var handlers = this.__event_handlers__ | 
					
						
							|  |  |  | 				&& (this.__event_handlers__[evt] || []) | 
					
						
							|  |  |  | 			handlers | 
					
						
							| 
									
										
										
										
											2020-11-15 02:50:39 +03:00
										 |  |  | 				&& handlers.splice(0, handlers.length, | 
					
						
							|  |  |  | 					...handlers.filter(function(h){ | 
					
						
							|  |  |  | 						return h !== func  | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 							&& h.__event_original_handler !== func })) } | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 	// XXX revise...
 | 
					
						
							| 
									
										
										
										
											2020-11-13 13:37:10 +03:00
										 |  |  | 	trigger: function(evt, ...args){ | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 		evt in this ? | 
					
						
							|  |  |  | 			// XXX add a better check...
 | 
					
						
							|  |  |  | 			this[evt](module.TRIGGER, ...args) | 
					
						
							| 
									
										
										
										
											2022-08-04 16:48:28 +03:00
										 |  |  | 			//: this.__event_handlers__
 | 
					
						
							|  |  |  | 			: this.hasOwnProperty('__event_handlers__') | 
					
						
							| 
									
										
										
										
											2020-12-01 05:48:50 +03:00
										 |  |  | 				&& (this.__event_handlers__[evt] || []) | 
					
						
							|  |  |  | 					.forEach(function(h){ h(evt, ...args) })  | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 		return this }, | 
					
						
							| 
									
										
										
										
											2020-11-19 02:24:09 +03:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | // NOTE: this can't be added via Object.assign(..), use object.mixinFlat(..) 
 | 
					
						
							|  |  |  | // 		instead...
 | 
					
						
							|  |  |  | var EventDocMixin =  | 
					
						
							| 
									
										
										
										
											2020-11-19 02:24:09 +03:00
										 |  |  | module.EventDocMixin = object.Mixin('EventDocMixin', { | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | 	get eventful(){ | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 		return object.deepKeys(this) | 
					
						
							|  |  |  | 			.filter(function(n){  | 
					
						
							|  |  |  | 				// avoid triggering props...
 | 
					
						
							| 
									
										
										
										
											2022-07-06 00:13:09 +03:00
										 |  |  | 				return !object.values(this, n, true).next().value.get | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 					// XXX this is too strict...
 | 
					
						
							| 
									
										
										
										
											2022-05-25 17:42:40 +03:00
										 |  |  | 					&& (this[n] || {}).constructor === Eventful}.bind(this)) }, | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 	get events(){ | 
					
						
							|  |  |  | 		return object.deepKeys(this) | 
					
						
							|  |  |  | 			.filter(function(n){  | 
					
						
							|  |  |  | 				// avoid triggering props...
 | 
					
						
							| 
									
										
										
										
											2022-07-06 00:13:09 +03:00
										 |  |  | 				return !object.values(this, n, true).next().value.get | 
					
						
							| 
									
										
										
										
											2020-12-01 19:54:47 +03:00
										 |  |  | 					// XXX this is too strict...
 | 
					
						
							|  |  |  | 					&& (this[n] || {}).constructor === Event }.bind(this)) }, | 
					
						
							| 
									
										
										
										
											2020-11-19 02:24:09 +03:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var EventMixin =  | 
					
						
							| 
									
										
										
										
											2020-11-19 02:24:09 +03:00
										 |  |  | module.EventMixin =  | 
					
						
							|  |  |  | object.Mixin('EventMixin',  | 
					
						
							|  |  |  | 	EventHandlerMixin, | 
					
						
							|  |  |  | 	EventDocMixin) | 
					
						
							| 
									
										
										
										
											2020-11-15 00:24:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:35:41 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |