incubator-xap-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mtu...@apache.org
Subject svn commit: r440150 - /incubator/xap/trunk/src/xap/bridges/basic/DomNodeBridge.js
Date Mon, 04 Sep 2006 18:39:02 GMT
Author: mturyn
Date: Mon Sep  4 11:39:02 2006
New Revision: 440150

URL: http://svn.apache.org/viewvc?view=rev&rev=440150
Log:
Much-improved DomNodeBridge, with proper event handling, featuring a list of the standard
W3C style attributes.

Modified:
    incubator/xap/trunk/src/xap/bridges/basic/DomNodeBridge.js

Modified: incubator/xap/trunk/src/xap/bridges/basic/DomNodeBridge.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/bridges/basic/DomNodeBridge.js?view=diff&rev=440150&r1=440149&r2=440150
==============================================================================
--- incubator/xap/trunk/src/xap/bridges/basic/DomNodeBridge.js (original)
+++ incubator/xap/trunk/src/xap/bridges/basic/DomNodeBridge.js Mon Sep  4 11:39:02 2006
@@ -44,39 +44,177 @@
 Xap.setupClassAsSubclassOf(
 	"xap.bridges.basic.DomNodeBridge",
 	"xap.bridges.basic.AbstractBlackBoxWidgetBridge"
-);
+							);
 
 
 /**
- * Creates the peer component that this bridge controls.
- */
-xap.bridges.basic.DomNodeBridge.prototype.obtainPeer = function() {
-	var parent = this.getElement().getParent();
-	var handler = this.getUiContentHandler().getHandlerForElement( parent );
+ * Set this xaplet's location in the HTML DOM---it's already got one, so do nothing.
+**/ 
+xap.bridges.basic.DomNodeBridge.prototype.setHtmlDomLocation = function(parentPeerDomNode){
+	// Do NOTHING, INTENTIONALLY.
+}
+ 
+
+xap.bridges.basic.DomNodeBridge.domEvents = 
+	{click: "onClick" , mousedown: "onMousedown" , mousemove: "onMousemove" , 
+		mouseout: "onMouseout" , mouseover: "onMouseover" , mouseup: "onMouseup",
+		keyup: "onKeyup", keydown: "onKeydown", textinput: "onTextinput"  
+		} ;
+		
+		
+		
+ 
+/**
+ *  Automatically glue all the peer's "onX..." events to
+ * a method on the bridge that runs our fireEvent("onX...");
+ * preference a particular, generally widget-specific method,
+ * to our own "onCommand".
+**/ 
+xap.bridges.basic.DomNodeBridge.prototype.autoAttachBridgeEventsToPeerEvents = function(){
+	// O.K., <code>this</code> is an object we can use as a  bridge---go on:		
+	var triggers = xap.bridges.basic.DomNodeBridge.domEvents ;
+	// We now have a hash table containing a set of strings (triggers) pointing to 
+	// objects, like "[Foobar.]onBaz"--->aFoobarInstance
+	for(var trigger in triggers){
+		// Create the callback for this "on..." method, if we don't explicitly have it:
+		if(!this[trigger]){
+			// This is the only way I could think of (that worked) to
+			// get the name of the event into the callback definition, so
+			// "onBlur"  --> this.fireEvent("onBlur")
+			this[trigger] = this.getOnXFunction(trigger) ;				
+		}		
+		this.getPeer().addEventListener(
+										trigger,   //click
+										this[triggers[trigger]], //onClick handler
+										false  //don't capture events
+										);
+	}
 	
-	// If we don't already have a peer, try to find one using a standard i.d.
-	// parameter, "wrappedNodeId":
-	if ( !this._peer ) {
-    	try {
-			var nodeId = this.getElement().getAttribute("wrappedNodeId")  ;    	
-			var peer = document.getElementById(nodeId) ;
-			// This method does more than just set the _peer member; it
-			// can also set up handlers.....
-    		this.setPeer(peer) ;
-        } catch ( e ) {
-			xap.bridges.basic.DomNodeBridge.s_log.error("Problem finding a peer DOM node:\n"+e );
-    	}
+	// Connect "onCommand" to a special event for the widget:
+	this.getPeer().addEventListener(this.getPeerOnCommandEvent().toLowerCase().substring(2),this._onCommandEvent,false)
;
+}
+
+// To date, for all these handlers, "this" ends up being resolved to the node;
+//---maybe a different version of addEventListener is needed.
+
+/**
+ * @private
+ * A wrapper for firing an <code>onCommand</code> event, here so that we can
hook that
+ * up to the peer event specified by <code>getPeerOnCommandEvent</code> q.v..
+ * @see getPeerOnCommandEvent
+**/ 
+xap.bridges.basic.DomNodeBridge.prototype._onCommandEvent = function( evt ) {
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onCommand", null, null, clientEvent);
+}
+
+xap.bridges.basic.DomNodeBridge.prototype.onClick = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onClick", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onMousedown = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onMousedown", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onMousemove = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onMousemove", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onMouseout = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onMouseout", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onMouseover = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onMouseover", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onMouseup = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onMouseup", null, null, clientEvent) ;
+}
+
+//Keyboard events:
+xap.bridges.basic.DomNodeBridge.prototype.onKeydown = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onKeydown", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onTextinput = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onTextinput", null, null, clientEvent) ;
+}
+xap.bridges.basic.DomNodeBridge.prototype.onKeyup = function( evt ){
+	var clientEvent = new xap.session.ClientEvent(this.handler.getElement(),this.handler.getSession());
+	clientEvent.eventObject = evt;
+	this.handler.fireEvent("onKeyup",null,null,clientEvent) ;
+}
+
+
+
+
+//Unfortunately, this duplicates some work from DojoWidgetBridge---don't want to
+// make the latter a superclass of this one, since this should be independent
+// of toolkit.
+
+/**
+ * These will be recognised by all peers; subclasses will
+ * have to add their own attributes to match their peers:
+**/
+xap.bridges.basic.DomNodeBridge.prototype.getAllowedAttributes = function(){
+	if( !this._allowedAttributes ){
+	// Memoise
+		this._allowedAttributes = 
+			 { x:null, y:null, width:null, height:null, 
+			 		color:null, backgroundColor:null
+			} ;		
 	}
+	return this._allowedAttributes ;
+}	
+
+
+/**
+ * Attribute setter method for 'x' designed to be triggered by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new x-value for the new left edge of the component.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setXAttribute = function(value){
+	// Mediocre behaviour, can improve in subclasses as needed
+	this.getPeer().domNode.style.left = value ;
 }
 
 /**
-  * Attributes not handled here are so far ignored.
+ * Attribute setter method for 'y' designed to be triggered by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new y-value for the new top edge of the component.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setYAttribute = function(value){
+	// Mediocre behaviour, can improve in subclasses as needed
+	this.getPeer().domNode.style.top = value ;
+}
+
+
+
+/**
+  * Attributes not handled here are handled in superclass.
 * */
 xap.bridges.basic.DomNodeBridge.prototype.attributeSet = function( event ) {
 		var name = event.getName();
 		var value = event.getNewValue();
 		var peer = this.getPeer() ;
 
+
 		if(name=="innerHTML"){
 			peer.innerHTML = value ;
 			return ;
@@ -87,25 +225,39 @@
 			if( element ){
 				this.setPeer(element) ;		
 			}
+			return ;
 		}
+
+		var eventName =  name.substring(2).toLowerCase()  ;
+		if( xap.bridges.basic.DomNodeBridge.domEvents[ eventName ] ){
+			return ;
+		}
+		
 		
 		
 	// Prefer setters to direct field settings, node attributes last?:
 		var setterName = "set" + name.substring(0,1).toUpperCase() +name.substring(1) ;
-		if( peer[setterName]){
+		if( this[ setterName+"Attribute"] ){
+			this[ setterName+"Attribute"].call(this,value) ;
+		} else if( peer[setterName] ){
 			peer[setterName].call( peer, value) ;
-		} else if( peer.style[setterName]){
+		} else if(peer.style[setterName]){
 			(peer.style[setterName]).call( peer.style, value) ;
+		} else if (peer[name] || peer[name]==""){
+			peer[name] = value ;
+		} else if (this.constructor.w3cStyleAttributes[name]){
+			peer.style[name] = value ;
 		} else {
-		// Since the peer is a dom node, this default method is open to us:
-			peer.setAttribute(name,value) ;
+		// Default behaviour:
+			this.superclass.attributeSet.call(this,event) ;
 		}
+
 }
-	
+
 /**
  *  Since this just wraps a dom node peer, this function is simple here---return the peer.
  *  ---and because we haven't overwritten AbstractBlackBoxWidgetBridge,
- *  this.getNodeForChildren == this.getRootDomeNode....
+ *  this.getDisplayDomNode == this.getRootDomeNode....
 **/
 xap.bridges.basic.DomNodeBridge.prototype.getRootDomNode = function(){
 	return this.getPeer() ;
@@ -115,17 +267,154 @@
  *  Since this just wraps a dom node peer, this 
  *  function is simple here---return the peer.
 **/
-xap.bridges.basic.DomNodeBridge.prototype.getNodeForChildren = function(){
+xap.bridges.basic.DomNodeBridge.prototype.getDisplayDomNode = function(){
 	return this.getPeer() ;
 }
- 
+
 
 /**
- * Set this xaplet's location in the HTML DOM---it's already got one, so do nothing.
+ * Creates the peer component that this bridge controls.
+ */
+xap.bridges.basic.DomNodeBridge.prototype.obtainPeer = function() {
+	var parent = this.getElement().getParent();
+	var handler = this.getUiContentHandler().getHandlerForElement( parent );
+	
+	// If we don't already have a peer, try to find one using a standard i.d.
+	// parameter, "wrappedNodeId":
+	if ( !this._peer ) {
+    	try {
+			var nodeId = this.getElement().getAttribute("wrappedNodeId")  ;    	
+			var peer = document.getElementById(nodeId) ;
+			// This method does more than just set the _peer member; it
+			// can also set up handlers.....
+    		this.setPeer(peer) ;
+        } catch ( e ) {
+			xap.bridges.basic.DomNodeBridge.s_log.error("Problem finding a peer DOM node:\n"+e );
+    	}
+	}
+	
+	peer.handler = this ;
+}
+
+
+/**
+ * Sets the _peer member and adds listeners in superclasses
+ * then gives the peer a member pointing back here.
 **/ 
-xap.bridges.basic.DomNodeBridge.prototype.setHtmlDomLocation = function(parentPeerDomNode){
-	// Do NOTHING, INTENTIONALLY.
+xap.bridges.basic.DomNodeBridge.prototype.setPeer = function(aPeer){
+    this.superclass.setPeer.call(this,aPeer) ;
+    // We'll want to be able to easily go from
+    // the dom node to the bridge:
+    aPeer.handler = this ;
 }
- 
- 
 
+
+
+
+
+xap.bridges.basic.DomNodeBridge.w3cStyleAttributes =
+{
+azimuth:true, background:true, backgroundAttachment:true, backgroundColor:true,
+backgroundImage:true, backgroundPosition:true, backgroundRepeat:true,
+border:true, borderBottom:true, borderBottomColor:true, borderBottomStyle:true,
+borderBottomWidth:true, borderCollapse:true, borderColor:true, borderLeft:true,
+borderLeftColor:true, borderLeftStyle:true, borderLeftWidth:true,
+borderRight:true, borderRightColor:true, borderRightStyle:true,
+borderRightWidth:true, borderSpacing:true, borderStyle:true, borderTop:true,
+borderTopColor:true, borderTopStyle:true, borderTopWidth:true, borderWidth:true,
+bottom:true, captionSide:true, clear:true, clip:true, color:true, content:true,
+counterIncrement:true, counterReset:true, cssFloat:true, cue:true,
+cueAfter:true, cueBefore:true, cursor:true, direction:true, display:true,
+elevation:true, emptyCells:true, font:true, fontFamily:true, fontSize:true,
+fontSizeAdjust:true, fontStretch:true, fontStyle:true, fontVariant:true,
+fontWeight:true, height:true, left:true, letterSpacing:true, lineHeight:true,
+listStyle:true, listStyleImage:true, listStylePosition:true, listStyleType:true,
+margin:true, marginBottom:true, marginLeft:true, marginRight:true,
+marginTop:true, markerOffset:true, marks:true, maxHeight:true, maxWidth:true,
+minHeight:true, minWidth:true, orphans:true, outline:true, outlineColor:true,
+outlineStyle:true, outlineWidth:true, overflow:true, padding:true,
+paddingBottom:true, paddingLeft:true, paddingRight:true, paddingTop:true,
+page:true, pageBreakAfter:true, pageBreakBefore:true, pageBreakInside:true,
+pause:true, pauseAfter:true, pauseBefore:true, pitch:true, pitchRange:true,
+playDuring:true, position:true, quotes:true, richness:true, right:true,
+size:true, speak:true, speakHeader:true, speakNumeral:true,
+speakPunctuation:true, speechRate:true, stress:true, tableLayout:true,
+textAlign:true, textDecoration:true, textIndent:true, textShadow:true,
+textTransform:true, top:true, unicodeBidi:true, verticalAlign:true,
+visibility:true, voiceFamily:true, volume:true, whiteSpace:true, widows:true,
+width:true, wordSpacing:true, zIndex:true 
+}
+
+
+
+
+
+
+/**
+ * Attribute setter method for component width, designed to be triggered by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new width value for the component.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setWidthAttribute = function(value){
+	this.getPeer().style.width = value ;
+}
+
+
+/**
+ * Attribute setter method for component height, designed to be triggered by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new height value for the component.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setHeightAttribute = function(value){
+	this.getPeer().style.height = value ;
+}
+
+/**
+ * Attribute setter method for the component foreground color, designed to be triggered by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new value for the component's foreground color.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setColorAttribute = function(value){
+	this.getPeer().style.color = value ;
+}
+
+
+/**
+ * Attribute setter method for the component's background's color, designed to be triggered
by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new value for the component background-color.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setBackgroundColorAttribute = function(value){
+	this.getPeer().style.backgroundColor = value ;
+}
+
+/**
+ * Attribute setter method for the node interior's vertical alignment, designed to be triggered
by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new value for the vertical alignment in force within the node.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setVerticalAlignmentAttribute = function(value){
+	this.getPeer().style.verticalAlignment = value ;
+}
+
+/**
+ * Attribute setter method for the node's notional sheet in the page, designed to be triggered
by
+ * <code>xap.bridges.basic.AbstractBlackBoxWidgetBridge.setAttribute</code>
+ * after it's been found via that class' implementation of
+ * <code>getSetterFunctionForAttribute</code>.
+ * @param{string} value The new value for the value influencing when this node will
+ * be displayed.
+**/
+xap.bridges.basic.DomNodeBridge.prototype.setZIndexAttribute = function(value){
+	this.getPeer().style.verticalAlignment = value ;
+}



Mime
View raw message