Subversion Repositories wimsdev

Rev

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

  1. /*
  2.         DynAPI Distribution
  3.         DynLayer Base/Common Class
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6.  
  7.         requires: dynapi.api.DynDocument
  8. */
  9.  
  10. var DynLayerBase = {};  // used by library
  11. function DynLayer(html,x,y,w,h,color,image) {
  12.         this.DynElement = DynElement;
  13.         this.DynElement();
  14.  
  15.         if (html && html.constructor==Object){
  16.                 var args=html; // dictionary input
  17.                 html=args.html;
  18.                 x = args.x;
  19.                 y = args.y;
  20.                 w = args.w;
  21.                 h = args.h;
  22.                 color = args.color;
  23.                 image = args.image;
  24.                 this.z = (args.zIndex||1);
  25.                 this._saveAnchor = args.anchor;
  26.                 this.visible = (args.visible==false)?false:true;
  27.                 this._textSelectable = (args.textSelectable==false)?false:true;
  28.                 if (args.id) this.setID(args.id,true);
  29.         }
  30.         else {
  31.                 this.visible = true;
  32.                 this.z = 1;
  33.                 this._saveAnchor = false;
  34.                 this._textSelectable = true;
  35.         }
  36.  
  37.         this.x = x;
  38.         this.y = y;
  39.         this.w = w;
  40.         this.h = h;
  41.         this.bgColor = color;
  42.         this.bgImage = image;
  43.         this.html = (html!=null)? html+'':null; // convert html to string
  44.         this.elm = null;
  45.         this.doc = null;
  46.         this.css = null;
  47. };
  48. var protoElement = dynapi.setPrototype('DynLayer','DynElement');
  49. protoElement._destroy = function() {
  50.         this._destroyAllChildren();
  51.         this.removeAllEventListeners();
  52.         if (this.elm) this._remove();
  53.         DynObject.all[this.id] = null;
  54.         this.children = null;
  55.         this.frame = null;
  56.  
  57.         this.bgImage = null;
  58.         this.bgColor = null;
  59.         this.html = null;
  60.         this.x = null;
  61.         this.y = null;
  62.         this.w = null;
  63.         this.h = null;
  64.         this.z = null;
  65.         this.doc = null;
  66.         this.css = null;
  67.         this._dyndoc = null;
  68.         this.parent = null;
  69. };
  70. protoElement._destroyAllChildren = function() {
  71.         for (var i=0;i<this.children.length;i++) {
  72.                 this.children[i]._destroy();
  73.                 delete this.children[i];
  74.         }
  75.         this.children = [];
  76. };
  77. protoElement._remove = function() {     //! Overwritten by NS4
  78.         var p=this.parent;
  79.         if (this.elm) {
  80.                 //this.elm.style.visibility = "hidden";
  81.                 //this.elm.innerHTML = "";
  82.                 //this.elm.outerHTML = "";
  83.                 var pref=p.elm;
  84.                 if(document.getElementById && document.childNodes){
  85.                         if(this.elm.parentNode) pref = this.elm.parentNode;
  86.                         pref.removeChild(this.elm);
  87.                 } else if (pref && pref.children){
  88.                         this.elm.outerHTML='';
  89.                 }
  90.                 this.elm = null;
  91.  
  92.                 if (this.releaseMouseEvents) this.releaseMouseEvents();
  93.                 if (this.releaseKeyEvents) this.releaseKeyEvents();
  94.         }
  95.         /*this.frame = null;
  96.         this.bgImage = null;
  97.         this.bgColor = null;
  98.         this.html = null;
  99.         this.z = null;
  100.         this.w = null;
  101.         this.h = null;
  102.         this.elm = null;
  103.         this.doc = null;
  104.         this.css = null;*/
  105. };
  106. protoElement._createInserted = function(divs){
  107.         DynLayer._assignElement(this,null,divs); //! NS4 will ignore divs
  108.         DynElement._flagCreate(this);
  109. };
  110. protoElement.getOuterHTML=function() {  //! Overwritten by NS4
  111.         if(this._noStyle) return '<div '+this._cssClass+' id="'+this.id+'">'+this.getInnerHTML()+'</div>';
  112.         else {
  113.                 var s,clip='',bgimage=' background-image:none;';
  114.                 if(this.bgImage!=null) bgimage=' background-image:url('+this.bgImage+');';
  115.                 //else if (this.bgImage==null && this.html==null) bgimage=' background-image:none;';
  116.                 if (this.clip) clip=' clip:rect('+this.clip[0]+'px '+this.clip[1]+'px '+this.clip[2]+'px '+this.clip[3]+'px);';
  117.                 else if (this.w!=null && this.h!=null) clip=' clip:rect(0px '+this.w+'px '+this.h+'px 0px);';
  118.                 return [
  119.                         '\n<div '+this._cssClass+' id="'+this.id+'" style="',
  120.                         ' left:',(this.x!=null? this.x : 0),'px;',
  121.                         ' top:',(this.y!=null? this.y : 0),'px;',
  122.                         ((this.w!=null)? ' width:'+this.w+'px;':''),
  123.                         ((this.h!=null)? ' height:'+this.h+'px;':''),
  124.                         ((this.z)? ' z-index:'+this.z+';':''),
  125.                         ((this._cursor!=null)? ' cursor:'+this._cursor+';':''),
  126.                         ((this.bgColor!=null)? ' background-color:'+this.bgColor+';':''),
  127.                         ((this.visible==false)? ' visibility:hidden;':' visibility:inherit;'),
  128.                         bgimage,
  129.                         clip,
  130.                         this._cssOverflow,
  131.                         this._cssPosition,
  132.                         ';">',
  133.                         this.getInnerHTML(),
  134.                         '</div>'
  135.                 ].join('');
  136.         }
  137. };
  138. protoElement.getInnerHTML=function() {  //! Overwritten by NS4
  139.         var s = '';
  140.         var i,ch=this.children;
  141.         if (this.html!=null) s+=this.html;
  142.         if (this._blkBoardElm) s=('<div id="'+this.id+'_blkboard">'+s+'</div>');
  143.         if(ch.length<50) for (i=0;i<ch.length;i++) s+=ch[i].getOuterHTML();
  144.         else if(ch.length){
  145.                 var ar=['']; // speed improvement for layers with nested children
  146.                 for (i=0;i<ch.length;i++) ar[i]=ch[i].getOuterHTML();
  147.                 s=s+ar.join('');
  148.         }
  149.         return s;
  150. };
  151.  
  152. protoElement.getPageX = function() {return (this.isChild)? this.parent.getPageX()+(this.x||0) : this.x||0}; //! Overwritten by NS4
  153. protoElement.getPageY = function() {return (this.isChild)? this.parent.getPageY()+(this.y||0) : this.y||0}; //! Overwritten by NS4
  154.  
  155. protoElement._cssClass = '';
  156. protoElement.setClass = function(c,noInlineStyle){
  157.         this._className=c;
  158.         if(this.css) this.css.className=c;
  159.         else {
  160.                 this._cssClass=(c)? 'class="'+c+'"':'';
  161.                 this._noStyle=noInlineStyle;
  162.         }
  163. };
  164.  
  165. protoElement.setVisible = function(b) { //! Overwritten by NS4
  166.         //if (b!=this.visible) {
  167.                 this.visible = b;
  168.                 if (this.css) this.css.visibility = b? "inherit" : "hidden";
  169.         //}
  170. };
  171. protoElement.setSize = function(w,h) { //! Overwritten by NS4
  172.         if (this._useMinSize||this._useMaxSize){
  173.                 if (this._minW && w<this._minW) w=this._minW;
  174.                 if (this._minH && h<this._minH) h=this._minH;
  175.                 if (this._maxW && w>this._maxW) w=this._maxW;
  176.                 if (this._maxH && h>this._maxH) h=this._maxH;
  177.         }
  178.         var cw = (w!=null && w!=this.w);
  179.         var ch = (h!=null && h!=this.h);
  180.         if (cw) this.w = w<0? 0 : w;
  181.         if (ch) this.h = h<0? 0 : h;
  182.         if (cw||ch) {
  183.                 if (this._hasAnchor) this.updateAnchor(); // update this anchor
  184.                 if (this._hasChildAnchors) this._updateAnchors(); // update child anchors
  185.                 if (this.css) {
  186.                         if (cw) this.css.width = this.w||0;
  187.                         if (ch) this.css.height = this.h||0;
  188.                         if (cw || ch) this.css.clip = 'rect(0px '+(this.w||0)+'px '+(this.h||0)+'px 0px)';
  189.                         if (this.updateLayout) this.updateLayout();
  190.                 }
  191.         }
  192.         if(this._hasResizeEvents) this.invokeEvent('resize');
  193.         return (cw||ch);
  194. };
  195. protoElement.setMaximumSize = function(w,h){
  196.         this._maxW=w; this._maxH=h;
  197.         this._useMaxSize=(w!=h!=null);
  198.         w=(this.w>w)?w:this.w;
  199.         h=(this.h>h)? h:this.h;
  200.         this.setSize(this.w,this.h);
  201. };
  202. protoElement.setMinimumSize = function(w,h){
  203.         this._minW=w; this._minH=h;
  204.         this._useMinSize=(w!=h!=null);
  205.         this.setSize(this.w,this.h);
  206. };
  207.  
  208. protoElement._position  = 'absolute';
  209. protoElement._cssPosition = ' position:absolute';
  210. protoElement.setPosition = function(p){
  211.         if(p!='relative' && p!='fixed' && p!='absolute') p='absolute';
  212.         this._position=p;
  213.         if (this.css) this.css.position=p;
  214.         else this._cssPosition = ' position:'+p;
  215. };
  216.  
  217. protoElement._overflow='hidden';
  218. protoElement._cssOverflow =' overflow:hidden;';
  219. protoElement.getOverflow = function(){return this._overflow};
  220. protoElement.setOverflow = function(s){
  221.         if(!s) s='default';
  222.         this._overflow=s;
  223.         if(this.css) this.css.overflow=s;
  224.         else this._cssOverflow=' overflow:'+s+';';
  225. };
  226.  
  227. protoElement.getAnchor = function(){
  228.         if(!this.parent) return this._saveAnchors;
  229.         else if (this.parent._childAnchors) {
  230.                 return this.parent._childAnchors[this.id];
  231.         }
  232. };
  233. protoElement.setAnchor = function(anchor) {
  234.         //console.log("[dynapi3x] dynlayer_base.js // setAnchor(" + anchor + ")");
  235.  
  236.         if (anchor == null) {
  237.                 delete this._saveAnchor;
  238.                 if (this.parent && this.parent._childAnchors && this.parent._childAnchors[this.id]){
  239.                         //console.log("[dynapi3x] dynlayer_base.js // setAnchor -- deleting _childAnchors["+this.id+"]");
  240.                         delete this.parent._childAnchors[this.id];
  241.                 }
  242.                 this._hasAnchor = false;
  243.         }
  244.         else if (this.parent) {
  245.                 if (!this.parent._childAnchors) this.parent._childAnchors = {};
  246.                 var parent_anchors = this.parent._childAnchors;
  247.                 parent_anchors[this.id] = anchor;
  248.                 this.parent._updateAnchor(this.id);
  249.                 this._hasAnchor = this.parent._hasChildAnchors = true;
  250.         }
  251.         else this._saveAnchor = anchor;
  252. };
  253. protoElement.setX=function(x) {this.setLocation(x,null)};
  254. protoElement.setY=function(y) {this.setLocation(null,y)};
  255. protoElement.getX=function() {return this.x||0};
  256. protoElement.getY=function() {return this.y||0};
  257. protoElement.setPageX = function(x) {this.setPageLocation(x,null)};
  258. protoElement.setPageY = function(y) {this.setPageLocation(null,y)};
  259. protoElement.getVisible=function() {return this.visible};
  260. protoElement.getZIndex=function() {return this.z};
  261. protoElement.setZIndex=function(z) {
  262.         if (typeof(z)=="object") {
  263.                 if (z.above) this.z = z.above.z + 1;
  264.                 else if (z.below) this.z = z.below.z - 1;
  265.                 else if (z.topmost && this.parent) {
  266.                         var topZ=10000,ch=this.parent.children;
  267.                         for(var i=0;i<ch.length;i++) if (ch[i].z>topZ) topZ=ch[i].z;
  268.                         this.parent._topZ = topZ+2;
  269.                         this.z = this.parent._topZ;
  270.                 }
  271.         }
  272.         else this.z = z;
  273.         if (this.css) this.css.zIndex = this.z;
  274. };
  275. protoElement.getHTML = function() {return this.html};
  276. protoElement.setWidth=function(w) {this.setSize(w,null)};
  277. protoElement.setHeight=function(h) {this.setSize(null,h)};
  278. protoElement.getWidth=function() {return this.w||0};
  279. protoElement.getHeight=function() {return this.h||0};
  280. protoElement.getBgImage=function() {return this.bgImage};
  281. protoElement.getBgColor=function() {return this.bgColor};
  282. protoElement.setBgColor=function(c) {   //! Overwritten by NS4
  283.         if (c==null) c = 'transparent';
  284.         this.bgColor = c;
  285.         if (this.css) this.css.backgroundColor = c;
  286. };
  287. protoElement.setBgImage=function(path) {        //! Overwritten by NS4
  288.         this.bgImage=path;
  289.         if (this.css) this.css.backgroundImage='url('+path+')';
  290. };
  291. protoElement.setClip=function(clip) {   //! Overwritten by NS4
  292.         var cc=this.getClip();
  293.         for (var i=0;i<clip.length;i++) if (clip[i]==null) clip[i]=cc[i];
  294.         this.clip=clip;
  295.         if (this.css==null) return;
  296.         var c=this.css.clip;
  297.         this.css.clip="rect("+clip[0]+"px "+clip[1]+"px "+clip[2]+"px "+clip[3]+"px)";
  298. };
  299. protoElement.getClip=function() {       //! Overwritten by NS4
  300.         if (this.css==null || !this.css.clip) return [0,0,0,0];
  301.         var c = this.css.clip;
  302.         if (c) {
  303.                 if (c.indexOf("rect(")>-1) {
  304.                         c=c.split("rect(")[1].split(")")[0].split("px");
  305.                         for (var i=0;i<c.length;i++) c[i]=parseInt(c[i]);
  306.                         return [c[0],c[1],c[2],c[3]];
  307.                 }
  308.                 else return [0,this.w,this.h,0];
  309.         }
  310. };
  311. /*
  312. protoElement.getElmWidth = function(){
  313.         var w = parseInt(this.css.width);
  314.         if(isNaN(w)) w=this.getContentWidth();
  315.         return w;
  316. };
  317. protoElement.getElmHeight = function(){
  318.         var h = parseInt(this.css.height);
  319.         alert(this.css.height)
  320.         if(isNaN(h)) h=this.getContentWidth();
  321.         return h;
  322. };
  323. */
  324. protoElement.slideTo = function(endx,endy,inc,speed) {
  325.         if (!this._slideActive) {
  326.                 var x = this.x||0;
  327.                 var y = this.y||0;
  328.                 if (endx==null) endx = x;
  329.                 if (endy==null) endy = y;
  330.                 var distx = endx-x;
  331.                 var disty = endy-y;
  332.                 if (x==endx && y==endy) return;
  333.                 var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/(inc||10)-1;
  334.                 var dx = distx/num;
  335.                 var dy = disty/num;
  336.                 this._slideActive = true;
  337.                 this._slide(dx,dy,endx,endy,num,this.x,this.y,1,(speed||20));
  338.         }
  339. };
  340. protoElement.slideStop = function() {
  341.         this._slideActive = false;
  342.         //this.invokeEvent('pathcancel');
  343. };
  344. protoElement._slide = function(dx,dy,endx,endy,num,x,y,i,speed) {
  345.         if (!this._slideActive) this.slideStop();
  346.         else if (i++ < num) {
  347.                 this.invokeEvent('pathrun');
  348.                 if (this._slideActive) {
  349.                         x += dx;
  350.                         y += dy;
  351.                         this.setLocation(Math.round(x),Math.round(y));
  352.                         setTimeout(this+'._slide('+dx+','+dy+','+endx+','+endy+','+num+','+x+','+y+','+i+','+speed+')',speed);
  353.                 }
  354.                 //else this.slideStop();
  355.         }
  356.         else {
  357.                 this._slideActive = false;
  358.                 this.invokeEvent('pathrun');
  359.                 this.setLocation(endx,endy);
  360.                 this.invokeEvent('pathfinish');
  361.         }
  362. };
  363.