You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xap-commits@incubator.apache.org by jm...@apache.org on 2006/08/25 22:24:42 UTC
svn commit: r436985 -
/incubator/xap/trunk/src/xap/bridges/basic/AbstractBlackBoxWidgetBridge.js
Author: jmargaris
Date: Fri Aug 25 15:24:42 2006
New Revision: 436985
URL: http://svn.apache.org/viewvc?rev=436985&view=rev
Log:
refactoring
Modified:
incubator/xap/trunk/src/xap/bridges/basic/AbstractBlackBoxWidgetBridge.js
Modified: incubator/xap/trunk/src/xap/bridges/basic/AbstractBlackBoxWidgetBridge.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/bridges/basic/AbstractBlackBoxWidgetBridge.js?rev=436985&r1=436984&r2=436985&view=diff
==============================================================================
--- incubator/xap/trunk/src/xap/bridges/basic/AbstractBlackBoxWidgetBridge.js (original)
+++ incubator/xap/trunk/src/xap/bridges/basic/AbstractBlackBoxWidgetBridge.js Fri Aug 25 15:24:42 2006
@@ -33,7 +33,7 @@
/*
* TO_DO: 1.) Find better names for the methods that return DOM nodes (to be used as parents and children), will
- * need to create one (am currently using getDisplayDomNode() for parent and container, a bad idea.)
+ * need to create one (am currently using getNodeForChildren() for parent and container, a bad idea.)
*.2.) Rename private methods with leading underscores "_".
* 3.) Maybe find a better name for this class; the "BlackBox" is there to make a point about avoiding
* making direct calls to peer methods from the bridge, but having been made it might be a bit much.
@@ -88,8 +88,6 @@
*
**/
xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.init = function() {
-
-
this.createPeer();
this.parseInitialAttributes( this.getElement() );
this.parseInitialChildren( this.getElement() );
@@ -121,14 +119,6 @@
var eqq = (typ== xap.bridges.basic.AbstractBlackBoxWidgetBridge.FUNCTION) ;
return eqq ;
}
-
-/**
- * @private
- * @static
- * List of the names of methods any bridge in this class' subtree should have.
-**/
-xap.bridges.basic.AbstractBlackBoxWidgetBridge.interfaceMethods =
- {obtainPeer:null,attributeSet:null,findPeerEvents:null,getDisplayDomNode:null,getRootDomNode:null} ;
/**
* Actually now more like <code>processPeer</code>, since it now
@@ -212,8 +202,8 @@
var parentPeerDomNode = null ;
- if ( parentHandler.getDisplayDomNode ){
- if (!parentHandler.getDisplayDomNode()){
+ if ( parentHandler.getNodeForChildren ){
+ if (!parentHandler.getNodeForChildren()){
throw new xap.util.Exception(
"Problem inserting component managed by "
+ this.toString()
@@ -223,43 +213,33 @@
) ;
return ;
}
- // The parent is another bridge
- // Get the display node associated with the bridge's parent, since this represents the
- // place in the HTML DOM
- parentPeerDomNode = parentHandler.getDisplayDomNode() ;
+ // The parent is another bridge
+ // Get the display node associated with the bridge's parent, since this represents the
+ // place in the HTML DOM
+ parentPeerDomNode = parentHandler.getNodeForChildren() ;
} else if (parentPeer.innerHTML){
// This is already an HTML element or equivalent:
// (Is there a better way of telling an HTML element?)
// TO_DO: make sure that we can remove this option---bridges
// should see only other bridges' components only through methods.
// As it stands, if the parent's handler is an AbstractBlackBoxWidgetBridge
- // or subclass, it will have a getDisplayDomNode method, and we
+ // or subclass, it will have a getNodeForChildren method, and we
// won't get here....
parentPeerDomNode = parentPeer ;
} else {
throw new xap.util.Exception("Parent must either be a XapElement or a bridge.") ;
}
- // TO_DO: is there a better, generic, way
+ // TODO: is there a better, generic, way
// of describing an element that contains
// a widget---maybe anything that can call appendChild?
- if ( ! parentPeerDomNode.tagName.match(/(DIV|BODY|TD)/)
- ){
+ if ( ! parentPeerDomNode.tagName.match(/(DIV|BODY|TD)/)){
xap.bridges.basic.AbstractBlackBoxWidgetBridge.s_log.error("Bogus parent peer:" + parentPeer );
} else {
-
// A bit of contractual programming---make sure this is a bridge of the sort we can use:
if(!this._peer){
throw new xap.util.Exception("Haven't obtained a source object to manage.") ;
}
- for( var meth in xap.bridges.basic.AbstractBlackBoxWidgetBridge.interfaceMethods){
- if(!this[meth] ){
- throw new xap.util.Exception("Supposed bridge object does not have member \""+ meth+"\".") ;
- }
- if( !xap.bridges.basic.AbstractBlackBoxWidgetBridge.s_IsFunction(this[meth]) ){
- throw new xap.util.Exception("Bridge object member \""+ meth+"\" is not a function.") ;
- }
- }
// Connects (and if necessary, creates) the bridge events mirroring the peer's.
this.autoAttachBridgeEventsToPeerEvents() ;
@@ -273,7 +253,16 @@
* Default behaviour: most widgets are not containers, so they have
* none such. Lowest common denominator.
**/
-xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getDisplayDomNode = function(){
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getNodeForChildren = function(){
+ return null;
+}
+
+/**
+ * Returns the node that represents this widget at the top level,
+ * for example the wrapping div. We will set basic attributes
+ * like color on this div in the absence of better setter methods.
+ */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getRootDomNode = function(){
return null;
}
@@ -348,40 +337,6 @@
return document.getElementById(xapDisplayId);
}
-
-/**
- * @return true, as this class and those prototyping with it are bridges:
- *
-**/
-xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.isBridge = function(){
- return true ;
-}
-
-
-/**
- * This is a default crude behaviour---look for a setter on the peer and use it, otherwise look for
- * a field of the given name and return it. Subclasses should short-circuit this being called if they want to
- * do anything interesting (e.g. aButton.getText--> peer.getCaption) .
- * @param name{String}
- * @return object or primitive from the getter
-**/
-xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getAttribute = function(name){
-
- // (Make sure that map's there, first:)
- if ( !this._getNameToSetterMap() || !this._getNameToGetterMap() ){
- this.initialiseAttributeMaps() ;
- } //If we needed to initialise the maps.
-
- var getter = this._getNameToSetterMap()[name] ;
- if ( getter ){
- result = getter.call(this) ;
- } else {
- throw new xap.util.Exception("No getter for property: "+name+".") ;
- }
-
- return result ;
-}
-
/**
* When a XAL attribute is processed, it triggers a call to <code>attributeSet()</code>;
@@ -393,37 +348,15 @@
xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.attributeSet = function( event ) {
var name = event.getName();
var value = event.getNewValue();
- var peer = this.getPeer() ;
-
- if( name=="xmlns"){
- // Not used except at start-up, where it's otherwise found
- return ;
- }
- if( name=="displayerId"){
- // Not used except at start-up, where it's otherwise found
- return ;
- }
-
- if( name=="onCommand"){
- // This is an event we've already mapped-to; if the attribute
- // value has changed, that will only matter the next time it's
- // triggered, at which point the new value will be found (since
- // fireEvent() doesn't cache it in any event) and used:
- return ;
- }
-
- if( this.eventTriggers && this.eventTriggers[name]){
- // This is one of our widget-specific mapped events;
- // see "onCommand" super.
- return ;
- }
// O.K., this is an attribute we're supposed to handle;
// find the function we're supposed to use by looking at
// the list the class/constructor keeps:
// (Make sure that map's there, first:)
- if ( !this._getNameToSetterMap() || !this._getNameToGetterMap() ){
+
+ //TODO move this to a place where it will get called less often like init?
+ if ( !this._getNameToSetterMap() ){
this.initialiseAttributeMaps() ;
} //If we needed to initialise the maps.
@@ -433,19 +366,16 @@
// when extending the class from its superclass:)
var setterFunction = this.getSetterFunctionForAttribute(name) ;
- if(!setterFunction){
- throw new xap.util.Exception(
- "No attribute setter found for property \""
- +name
- +"\" on bridge "
- +this.toString()
- +"."
- );
- }
-
// This is our convention: the setter on the bridge will take this object, the name, and the value:
- setterFunction.call(this,value) ;
-
+ if (setterFunction){
+ setterFunction.call(this,value) ;
+ }
+ else{
+
+ //TODO have a strict mode?
+ xap.util.LogFactory.getLog("AbstractWidget").
+ debug("Unrecognized attribute " + name + " on element " + this.getElement().nodeName);
+ }
}
@@ -486,6 +416,31 @@
}
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getNewAllowedAttributes = function(){
+ return ["x","y","width","height","backgroundColor",
+ "borderWidth","borderColor","borderStyle","margin",
+ "padding","color","textDecoration","fontFamily",
+ "fontStyle","fontWeight", "fontSize", "textAlign"];
+}
+
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getAllowedAttributes = function(){
+ if (this.constructor._allowedAttributes){
+ return this.constructor._allowedAttributes;
+ }
+ else{
+ var allowedAttributes = [];
+ allowedAttributes = allowedAttributes.concat(this.getNewAllowedAttributes());
+ if (this.superclass && this.superclass.getAllowedAttributes){
+ allowedAttributes = allowedAttributes.concat(this.superclass.getAllowedAttributes());
+ }
+ this.constructor._allowedAttributes = allowedAttributes;
+ xap.util.LogFactory.getLog("AbstractWidget").debug("Widget " + this +
+ " supports the following attributes:" + allowedAttributes);
+ return allowedAttributes;
+ }
+
+}
+
/**
* Set this xaplet's location in the HTML DOM:
* @param parentPeerDomNode---default location....
@@ -522,29 +477,20 @@
return this._getNameToSetterMap() [name] ;
}
-
-/**
- * We need this here in order to have this class' constructor
- * hold the code that will do something more intersting
- * when the class instance actually have attributes:
-**/
-xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.getAllowedAttributes = function(){
- return {} ;
-}
-
-
xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.initialiseAttributeMaps = function(){
// Initialise the default name-to[gs]etter map used under attributeSet:
+
+ //TODO is this once per object? or once per class?
this.constructor._nameToSetterMap = new Object() ;
- this.constructor._nameToGetterMap = new Object() ;
var allowed = this.getAllowedAttributes() ;
- for( var propName in allowed ){
+
+ for( var i = 0; i<allowed.length; i++ ){
+ var propName = allowed[i];
var capitalisedName = propName.substring(0,1).toUpperCase() ;
if( propName.length > 1 ){
capitalisedName += propName.substring(1) ;
}
this._getNameToSetterMap() [propName] = this["set"+capitalisedName+"Attribute"] ;
- this._getNameToGetterMap() [propName] = this["get"+capitalisedName+"Attribute"] ;
}
}
@@ -558,12 +504,89 @@
return this.constructor._nameToSetterMap ;
}
+/** XML attribute set method for "x" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setXAttribute = function(value){
+ this.getRootDomNode().style.left = value ;
+}
+
+/** XML attribute set method for "y" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setYAttribute = function(value){
+ this.getRootDomNode().style.top = value ;
+}
+
+/** XML attribute set method for "width" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setWidthAttribute = function(value){
+ this.getRootDomNode().style.width = value ;
+}
+
+/** XML attribute set method for "height" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setHeightAttribute = function(value){
+ this.getRootDomNode().style.height = value ;
+}
+
+
+/** XML attribute set method for "backgroundColor" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setBackgroundColorAttribute = function(value){
+ this.getRootDomNode().style.backgroundColor = value ;
+}
+
+/** XML attribute set method for "borderWidth" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setBorderWidthAttribute = function(value){
+ this.getRootDomNode().style.borderWidth = value ;
+}
+
+/** XML attribute set method for "borderStyle" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setBorderStyleAttribute = function(value){
+ this.getRootDomNode().style.borderStyle = value ;
+}
+
+/** XML attribute set method for "borderColor" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setBorderColorAttribute = function(value){
+ this.getRootDomNode().style.borderColor = value ;
+}
+
+/** XML attribute set method for "color" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setColorAttribute = function(value){
+ this.getRootDomNode().style.color = value ;
+}
+
+/** XML attribute set method for "fontFamily" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setFontFamilyAttribute = function(value){
+ this.getRootDomNode().style.fontFamily = value ;
+}
+
+/** XML attribute set method for "fontSize" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setFontSizeAttribute = function(value){
+ this.getRootDomNode().style.fontSize = value ;
+}
+
+/** XML attribute set method for "fontStyle" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setFontStyleAttribute = function(value){
+ this.getRootDomNode().style.fontStyle = value ;
+}
+
+/** XML attribute set method for "fontWeight" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setFontWeightAttribute = function(value){
+ this.getRootDomNode().style.fontWeight = value ;
+}
+
+/** XML attribute set method for "margin" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setMarginAttribute = function(value){
+ this.getRootDomNode().style.margin = value ;
+}
+
+/** XML attribute set method for "textDecoration" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setTextDecorationAttribute = function(value){
+ this.getRootDomNode().style.textDecoration = value ;
+}
+
+/** XML attribute set method for "padding" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setPaddingAttribute = function(value){
+ this.getRootDomNode().style.padding = value ;
+}
+
+/** XML attribute set method for "textAlign" */
+xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype.setTextAlignAttribute = function(value){
+ this.getRootDomNode().style.textAlign = value ;
+}
-/**
- * Returns a mapping of a property name to a getter instance method; the
- * dictionary itself is held on the class (constructor), since it should be
- * the same for all instances.
-**/
-xap.bridges.basic.AbstractBlackBoxWidgetBridge.prototype._getNameToGetterMap = function(){
- return this.constructor._nameToGetterMap ;
-}
\ No newline at end of file