Subversion Repositories wimsdev

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.         DynAPI Distribution
  3.         DynEvent, EventObject, DynElement Classes
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6. */
  7.  
  8. function DynEvent(type,src) {
  9.         this.type = type;
  10.         this.src = src;
  11.         this.origin = src;
  12.         this.propagate = true;
  13.         this.bubble = false;
  14.         this.bubbleChild = null;
  15.         this.defaultValue = true;
  16. };
  17. var p = DynEvent.prototype;
  18. p.getType = function() {return this.type};
  19. p.getSource = function() {return this.src};
  20. p.getOrigin=function() {return this.origin};
  21. p.stopPropagation = function() {this.propagate = false};
  22. p.preventBubble = function() {this.bubble = false};
  23. p.preventDefault = function() {this.defaultValue = false};
  24. p.getBubbleChild = function() {return this.bubbleChild};
  25.  
  26. function EventObject() {
  27.         this.DynObject = DynObject;
  28.         this.DynObject();
  29.         this._listeners = [];
  30. };
  31. EventObject._SubClass={};
  32.  
  33. p = dynapi.setPrototype('EventObject','DynObject');
  34. p.addEventListener = function(el) {
  35.         if (el) {
  36.                 for (var i=0;i<this._listeners.length;i++) if (this._listeners[i]==el) return;
  37.                 this._listeners[this._listeners.length] = el;          
  38.                 // Use onCreate() and onPrecreate() function for create events
  39.                 this._hasContentEvents=(el['oncontentchange'])? true:this._hasContentEvents;
  40.                 this._hasLocationEvents=(el['onlocationchange'])? true:this._hasLocationEvents;
  41.                 this._hasResizeEvents=(el['onresize'])? true:this._hasResizeEvents;
  42.                 this._hasDragEvents=(el['ondragstart']||el['ondragmove']||
  43.                                                         el['ondragend']||el['ondragdrop']||
  44.                                                         el['ondragover']||el['ondragout'])? true:this._hasDragEvents;
  45.  
  46.                 if (this.captureMouseEvents) {
  47.                         this._hasMouseEvents = this._hasMouseEvents||(el.onmousedown || el.onmouseup || el.onmouseover || el.onmouseout || el.onclick || el.ondblclick);
  48.                         if (this._created && !this._hasMouseEvents) this.captureMouseEvents();
  49.                 }
  50.                 if (this.captureKeyEvents)
  51.                         if (this._created && !this._hasKeyEvents && (el.onkeydown || el.onkeyup || el.onkeypress)) this.captureKeyEvents();
  52.         }
  53. };
  54. p.removeEventListener = function(el) {
  55.         if (el) {
  56.                 DynAPI.functions.removeFromArray(this._listeners, el, false);
  57.                 if (!this._listeners.length && this.releaseMouseEvents && this.getClassName()!='DynDocument') this.releaseMouseEvents();
  58.                 if (!this._listeners.length && this.releaseKeyEvents && this.getClassName()!='DynDocument') this.releaseKeyEvents();
  59.         }
  60. };
  61. p.removeAllEventListeners = function() {
  62.         this._listeners = [];
  63. };
  64. p.invokeEvent = function(type,e,args) {
  65.         if (!e) e = new DynEvent(type,this);
  66.         e.src = this;
  67.         e.type = type;
  68.        
  69.         // Check for subclassing
  70.         var clsFn=EventObject._SubClass[this+'_'+type];
  71.         if(clsFn) {
  72.                 if (clsFn(e,args)==false) return;
  73.         };
  74.        
  75.         if (this._listeners.length) for (var i=0;i<this._listeners.length;i++) {
  76.                 if (this._listeners[i]["on"+type]) this._listeners[i]["on"+type](e,args);
  77.                 if (!e.propagate) break;
  78.         }
  79.         if (this["on"+type]) this["on"+type](e,args);
  80.         if (e.bubble && this.parent) {
  81.                 //if ((type=="mouseover" || type=="mouseout") && e._relative==this.parent) return;
  82.                 e.x += this.x;
  83.                 e.y += this.y;
  84.                 e.bubbleChild = this;
  85.                 this.parent.invokeEvent(type,e,args);
  86.         }
  87. };
  88.  
  89. // Add subClassEvent() function to dynapi.functions
  90. dynapi.functions.subClassEvent = function(type,eobj,fn){
  91.         var ek=eobj+'_'+type;
  92.         var cls=EventObject._SubClass;
  93.         if(typeof(fn)=='function') cls[ek]=fn;
  94.         else if(!fn && cls[ek]) delete cls[ek];
  95. };
  96.  
  97. function DynElement() {
  98.         this.EventObject = EventObject;
  99.         this.EventObject();
  100.         this.isChild = false;
  101.         this._created = false;
  102.         this.parent = null;
  103.         this._dyndoc = null;
  104.         this.children = [];
  105.         this._childAnchors = [];
  106. };
  107. DynElement._flagCreate = function(c){ // much faster than using DynElemnt._flagEvent
  108.         var ch=c.children;
  109.         c._created = true;
  110.         if (c._hasCreateFn) c._flagCreateEvent('create');              
  111.         for (var i=0; i<ch.length; i++) this._flagCreate(ch[i]);
  112. };
  113. DynElement._flagPreCreate = function(c){
  114.         var ch=c.children;
  115.         if (c._hasPCreateFn) c._flagCreateEvent('precreate');          
  116.         for (var i=0; i<ch.length; i++) this._flagPreCreate(ch[i]);
  117. };
  118. DynElement._flagEvent = function(c,type) {
  119.         var ch=c.children;
  120.         c.invokeEvent(type);
  121.         for (var i=0; i<ch.length; i++) this._flagEvent(ch[i],type);
  122. };
  123. p = dynapi.setPrototype('DynElement','EventObject');
  124. p.addChild = function(c,alias,inlineID) {
  125.         if (!c) return dynapi.debug.print("Error: no object sent to [DynLayer].addChild()");
  126.         if (c.isChild) c.removeFromParent();
  127.         c.isChild = true;
  128.         c.parent = this;
  129.         if (c._saveAnchor) {
  130.                 c.setAnchor(c._saveAnchor);
  131.                 c._saveAnchor = null;
  132.                 delete c._saveAnchor;
  133.         }
  134.         if(alias) this[alias]=c;
  135.         if(inlineID) c.setID(inlineID,true);
  136.         if (this._created)      {
  137.                 if (c.isInline) c._createInline();
  138.                 else c._create();
  139.         }
  140.         this.children[this.children.length] = c;
  141.         return c;
  142. };
  143. p.removeChild = function(c) {
  144.         var l = this.children.length;
  145.         for (var i=0;i<l && this.children[i]!=c;i++);
  146.         if (i!=l) {
  147.                 c._remove();
  148.                 c._created = false;
  149.                 c.isChild = false;
  150.                 c.parent = null;
  151.                 c.dyndoc = null;
  152.                 this.children[i] = this.children[l-1];
  153.                 this.children[l-1] = null;
  154.                 this.children.length--;
  155.         }
  156. };
  157. p.deleteChild = function(c) {
  158.         c.removeFromParent();
  159.         c._delete();
  160. };
  161. p.deleteAllChildren = function() {
  162.         var l = this.children.length;
  163.         for(var i=0;i<l;i++) {
  164.                 this.children[i].deleteFromParent();
  165.                 delete this.children[i];
  166.         }
  167.         this.children = [];
  168. };
  169. p.deleteFromParent = function () {
  170.         if (this.parent) this.parent.deleteChild(this);
  171. };
  172. p.removeFromParent = function () {
  173.         if (this.parent) this.parent.removeChild(this);
  174. };
  175. p._create = p._createInLine = p._createInserted = p._remove = p._delete = p._destroy = dynapi.functions.Null;
  176.  
  177. p.getChildren = function() {return this.children};
  178. p.getAllChildren = function() {
  179.         var ret = [];
  180.         var temp;
  181.         var l = this.children.length;
  182.         for(var i=0;i<l;i++) {
  183.                 ret[this.children[i].id] = this.children[i];
  184.                 temp = this.children[i].getAll();
  185.                 for(var j in temp) ret[j] = temp[j];
  186.         }
  187.         return ret;
  188. };
  189. p.getParents = function(l) {
  190.         if (l==null) l = [];
  191.         if (this.parent) {
  192.                 l[l.length] = this.parent;
  193.                 l = this.parent.getParents(l);
  194.         }
  195.         return l;
  196. };
  197. p.isParentOf = function(c) {
  198.         if (c) {
  199.                 var p = c.getParents();
  200.                 for (var i=0;i<p.length;i++) {
  201.                         if (p[i]==this) return true;
  202.                 }
  203.         }
  204.         return false;
  205. };
  206. p.isChildOf = function(p) {
  207.         if (!p) return false;
  208.         return p.isParentOf(this);
  209. };
  210. // New onPreCreate() and onCreate() callback functions
  211. p.onCreate = function(fn){
  212.         if(!fn) return;
  213.         if(!this._cfn){this._fn=0;this._cfn=[];}
  214.         var s='create'+this._fn++;
  215.         this._cfn[s]='create';
  216.         this._hasCreateFn=true;
  217.         this[s]=fn;
  218. };
  219. p.onPreCreate = function(fn){
  220.         if(!fn) return;
  221.         if(!this._cfn){this._fn=0;this._cfn=[];}
  222.         var s='precreate'+this._fn++;
  223.         this._cfn[s]='precreate';
  224.         this._hasPCreateFn=true;
  225.         this[s]=fn;
  226. };
  227. p._flagCreateEvent = function(t){
  228.         for(var i in this._cfn){
  229.                 if(this._cfn[i]==t) this[i]();
  230.         };
  231. };
  232.  
  233. p.updateAnchor = function() {
  234.         this.parent._updateAnchor(this.id);
  235. };
  236. p._updateAnchor = function(id) {
  237.         if (!id) return;
  238.         var dlyr = DynObject.all[id];
  239.         var a = this._childAnchors[id];
  240.         var tw = this.w;
  241.         var th = this.h;
  242.         if (a==null || (tw==null && th==null)) return;
  243.        
  244.         // anchoring/docking
  245.         var fn=dynapi.functions;
  246.         var padX=0,padY=0;
  247.         if(a.topA) {
  248.                 anc=fn.getAnchorLocation(a.topA,this);
  249.                 if(anc){padY=anc.y; th=th-padY;}
  250.         }
  251.         if(a.leftA) {
  252.                 anc=(a.leftA==a.topA && anc)? anc:fn.getAnchorLocation(a.leftA,this);
  253.                 if(anc) {padX=anc.x; tw=tw-padX;}
  254.         }
  255.         if(a.bottomA) {
  256.                 anc=fn.getAnchorLocation(a.bottomA,this);
  257.                 th=th-(this.h-anc.y);
  258.         }
  259.         if(a.rightA) {
  260.                 anc=(a.bottomA==a.rightA && anc)? anc:fn.getAnchorLocation(a.rightA,this);
  261.                 if(anc) tw=tw-(this.w-anc.x);                          
  262.         }
  263.        
  264.         var aleft=(tw>0 && a.left && typeof(a.left)=='string')? tw*(parseInt(a.left)/100):a.left;
  265.         var aright=(tw>0 && a.right && typeof(a.right)=='string')? tw*(parseInt(a.right)/100):a.right;
  266.         var atop=(th>0 && a.top && typeof(a.top)=='string')? th*(parseInt(a.top)/100):a.top;
  267.         var abottom=(th>0 && a.bottom && typeof(a.bottom)=='string')? th*(parseInt(a.bottom)/100):a.bottom;
  268.         var x = aleft;
  269.         var y = atop;
  270.         var w = null;
  271.         var h = null;
  272.         var dlyrWidth=dlyr.getWidth();
  273.         var dlyrHeight=dlyr.getHeight();
  274.         if (a.stretchH!=null) {
  275.                 if(typeof(a.stretchH)!='string') w=a.stretchH;
  276.                 else {
  277.                         if(a.stretchH=='*') w = tw - ((aleft!=null)? aleft:0);
  278.                         else w = tw*(parseInt(a.stretchH)/100);
  279.                 }
  280.                 dlyrWidth=w;
  281.         }
  282.         if (a.centerH!=null) {
  283.                 x = Math.ceil(tw/2 - dlyrWidth/2 + a.centerH);
  284.         }else if (aright!=null) {
  285.                 if (aleft!=null) w = (tw - aright) - aleft;
  286.                 else x = (tw - dlyrWidth) - aright;
  287.                 if(tw<=0 && x<0) x=null; // ns4 needs x>=0
  288.         }      
  289.         if (a.stretchV!=null) {
  290.                 if(typeof(a.stretchV)!='string') h=a.stretchV;
  291.                 else {
  292.                         if(a.stretchV=='*') h = th - ((atop!=null)? atop:0);
  293.                         else h = th*(parseInt(a.stretchV)/100);
  294.                 }
  295.                 dlyrHeight=h;
  296.         }      
  297.         if (a.centerV!=null) {
  298.                 y = Math.ceil(th/2 - dlyrHeight/2 + a.centerV);
  299.         }else if (abottom!=null) {
  300.                 if (atop!=null) h = (th - abottom) - atop;
  301.                 else y = (th - dlyrHeight) - abottom;
  302.                 if(th<=0 && y<0) y=null; // ns4 needs y>=0
  303.         }
  304.         if(padX) {x=(x)? x:0;x+=padX}
  305.         if(padY) {y=(y)? y:0;y+=padY}
  306.  
  307.         var tmp=dlyr._hasAnchor;       
  308.         dlyr._hasAnchor=false; // ignore anchor updates of this layer
  309.         if(x!=null||y!=null) dlyr.setLocation(x,y);
  310.         if(w!=null||h!=null) dlyr.setSize(w,h);
  311.         dlyr._hasAnchor = tmp; // useful for preventing stack overflow
  312. };
  313. p._updateAnchors = function() {
  314.         var tw = this.w;
  315.         var th = this.h;
  316.         if (tw==null && th==null) return;
  317.         for (id in this._childAnchors) this._updateAnchor(id);
  318. };
  319.  
  320.  
  321. // Bandwidth timer stop
  322. var ua=dynapi.ua; ua._bwe=new Date;
  323. ua.broadband=((ua._bwe-ua._bws)<=1500)? true:false;
  324.