| 
									
										
										
										
											2015-09-07 18:44:10 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | define(function(require){ var module = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //var DEBUG = DEBUG != null ? DEBUG : true
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | var keyboard = require('../keyboard') | 
					
						
							| 
									
										
										
										
											2015-12-08 07:42:07 +03:00
										 |  |  | var object = require('../object') | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 18:44:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | // helpers...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var proxyToDom = | 
					
						
							|  |  |  | module.proxyToDom =  | 
					
						
							|  |  |  | function(name){ | 
					
						
							|  |  |  | 	return function(){  | 
					
						
							|  |  |  | 		this.dom[name].apply(this.dom, arguments) | 
					
						
							|  |  |  | 		return this  | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX triggering events from here and from jQuery/dom has a 
 | 
					
						
							|  |  |  | // 		different effect...
 | 
					
						
							|  |  |  | var triggerEventWithSource = | 
					
						
							|  |  |  | module.triggerEventWithSource =  | 
					
						
							|  |  |  | function(){ | 
					
						
							|  |  |  | 	var args = args2array(arguments) | 
					
						
							|  |  |  | 	var evt = args.shift() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if(typeof(evt) == typeof('str')){ | 
					
						
							|  |  |  | 		evt = $.Event(evt) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	evt.source = this | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	args.splice(0, 0, evt) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	this.dom.trigger.apply(this.dom, args) | 
					
						
							|  |  |  | 	return this  | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | var WidgetClassPrototype = { | 
					
						
							| 
									
										
										
										
											2015-11-09 22:48:13 +03:00
										 |  |  | 	make: function(obj, client, options){ | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 		console.error('Widget must define a .make method.') | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var WidgetPrototype = { | 
					
						
							|  |  |  | 	dom: null, | 
					
						
							|  |  |  | 	client: null, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	options: { | 
					
						
							|  |  |  | 		nonPropagatedEvents: [ | 
					
						
							|  |  |  | 			'click', | 
					
						
							|  |  |  | 			'keydown', | 
					
						
							|  |  |  | 		], | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	keyboard: null, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX triggering events from here and from jQuery/dom has a 
 | 
					
						
							|  |  |  | 	// 		different effect...
 | 
					
						
							|  |  |  | 	trigger: triggerEventWithSource, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// proxy event api...
 | 
					
						
							|  |  |  | 	on: proxyToDom('on'), | 
					
						
							|  |  |  | 	one: proxyToDom('one'), | 
					
						
							|  |  |  | 	off: proxyToDom('off'), | 
					
						
							|  |  |  | 	bind: proxyToDom('bind'), | 
					
						
							|  |  |  | 	unbind: proxyToDom('unbind'), | 
					
						
							|  |  |  | 	deligate: proxyToDom('deligate'), | 
					
						
							|  |  |  | 	undeligate: proxyToDom('undeligate'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX this will not:
 | 
					
						
							| 
									
										
										
										
											2015-10-02 17:32:13 +03:00
										 |  |  | 	// 		- attach dom to parent... (???)
 | 
					
						
							|  |  |  | 	// 		- handle focus... (???)
 | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 	// XXX same as ContainerPrototype.__init__ but skips client...
 | 
					
						
							|  |  |  | 	__init__: function(parent, options){ | 
					
						
							|  |  |  | 		var that = this | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		parent = this.parent = $(parent || 'body') | 
					
						
							|  |  |  | 		options = options || {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// merge options...
 | 
					
						
							|  |  |  | 		var opts = Object.create(this.options) | 
					
						
							|  |  |  | 		Object.keys(options).forEach(function(n){ opts[n] = options[n] }) | 
					
						
							|  |  |  | 		options = this.options = opts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// build the dom...
 | 
					
						
							|  |  |  | 		if(this.constructor.make){ | 
					
						
							| 
									
										
										
										
											2015-11-09 22:48:13 +03:00
										 |  |  | 			this.dom = this.constructor.make(this, options) | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 17:32:13 +03:00
										 |  |  | 		// XXX do we do this here???
 | 
					
						
							|  |  |  | 		/* | 
					
						
							|  |  |  | 		if(parent && this.dom){ | 
					
						
							|  |  |  | 			parent.append(this.dom) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 		// add keyboard handler...
 | 
					
						
							| 
									
										
										
										
											2015-09-22 23:55:49 +03:00
										 |  |  | 		if(this.keyboard && this.dom){ | 
					
						
							|  |  |  | 			this.dom.keydown( | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 				keyboard.makeKeyboardHandler( | 
					
						
							|  |  |  | 					this.keyboard, | 
					
						
							|  |  |  | 					options.logKeys, | 
					
						
							|  |  |  | 					this)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Widget =  | 
					
						
							|  |  |  | module.Widget =  | 
					
						
							|  |  |  | object.makeConstructor('Widget',  | 
					
						
							|  |  |  | 		WidgetClassPrototype,  | 
					
						
							|  |  |  | 		WidgetPrototype) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ContainerClassPrototype = { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ContainerPrototype = { | 
					
						
							| 
									
										
										
										
											2015-09-22 23:55:49 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	focus: function(handler){ | 
					
						
							|  |  |  | 		if(handler != null){ | 
					
						
							|  |  |  | 			this.on('focus', handler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.dom.focus() | 
					
						
							|  |  |  | 			this.client | 
					
						
							|  |  |  | 				&& this.client.focus  | 
					
						
							|  |  |  | 				&& this.client.focus() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 	// XXX this is the same as WidgetPrototype.__init__ but also handles
 | 
					
						
							|  |  |  | 	// 		the client...
 | 
					
						
							|  |  |  | 	__init__: function(parent, client, options){ | 
					
						
							|  |  |  | 		var that = this | 
					
						
							|  |  |  | 		parent = this.parent = $(parent || 'body') | 
					
						
							|  |  |  | 		options = options || {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.client = client | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// merge options...
 | 
					
						
							|  |  |  | 		var opts = Object.create(this.options) | 
					
						
							|  |  |  | 		Object.keys(options).forEach(function(n){ opts[n] = options[n] }) | 
					
						
							|  |  |  | 		options = this.options = opts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// build the dom...
 | 
					
						
							|  |  |  | 		if(this.constructor.make){ | 
					
						
							| 
									
										
										
										
											2015-09-22 23:55:49 +03:00
										 |  |  | 			this.dom = this.constructor | 
					
						
							| 
									
										
										
										
											2015-11-09 22:48:13 +03:00
										 |  |  | 				.make(this, client.dom || client, options) | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// add keyboard handler...
 | 
					
						
							| 
									
										
										
										
											2015-09-22 23:55:49 +03:00
										 |  |  | 		if(this.keyboard && this.dom){ | 
					
						
							|  |  |  | 			this.dom.keydown( | 
					
						
							| 
									
										
										
										
											2015-09-20 21:54:28 +03:00
										 |  |  | 				keyboard.makeKeyboardHandler( | 
					
						
							|  |  |  | 					this.keyboard, | 
					
						
							|  |  |  | 					options.logKeys, | 
					
						
							|  |  |  | 					this)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return this | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Container =  | 
					
						
							|  |  |  | module.Container =  | 
					
						
							|  |  |  | object.makeConstructor('Container',  | 
					
						
							|  |  |  | 		ContainerClassPrototype,  | 
					
						
							|  |  |  | 		ContainerPrototype) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Container.prototype.__proto__ = Widget.prototype | 
					
						
							| 
									
										
										
										
											2015-09-07 18:44:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                                                */ | 
					
						
							|  |  |  | return module }) |