Subversion Repositories wimsdev

Rev

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

  1. /*
  2.         DynAPI Distribution
  3.         DynDocument Class
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6.        
  7.         requires: dynapi.api.DynElement
  8. */
  9.  
  10. function DynDocument(frame) {
  11.         this.DynElement = DynElement;
  12.         this.DynElement();
  13.         this.frame = frame;
  14.         this.doc = this.frame.document;
  15.         this._dyndoc = this;
  16.         this.x = 0;
  17.         this.y = 0;
  18.         this.w = 0;
  19.         this.h = 0;
  20.         this._topZIndex = 10000;
  21.         var o = this;
  22.         this.frame.onresize = function() {o._handleResize()};
  23.         this.onResizeNS4 = "reload"; // or "redraw"
  24.         this._created = false;
  25. };
  26. var p = dynapi.setPrototype('DynDocument','DynElement');
  27. p._remove = function() {
  28.         this.elm=null;
  29.         this.doc=null;
  30.         this.frame=null;
  31. };
  32. p.getBgColor = function() {
  33.         return this.bgColor;
  34. };
  35. p.getX = p.getY = p.getPageX = p.getPageY = dynapi.functions.Zero;
  36. p.getWidth = function() {
  37.         if (!this.w) this.findDimensions();
  38.         return this.w;
  39. };
  40. p.getHeight = function() {
  41.         if (!this.h) this.findDimensions();
  42.         return this.h;
  43. };
  44. p.findDimensions = function() {
  45.         this.w=(dynapi.ua.ns||dynapi.ua.opera)? this.frame.innerWidth : this.elm.clientWidth;
  46.         this.h=(dynapi.ua.ns||dynapi.ua.opera)? this.frame.innerHeight : this.elm.clientHeight;
  47. };
  48. p.setBgColor = function(color) {
  49.         if (color == null) color='';
  50.         if (dynapi.ua.ns4 && color == '') color = '#ffffff';
  51.         this.bgColor = color;
  52.         this.doc.bgColor = color;
  53. };
  54. p.setFgColor = function(color) {
  55.         if (color == null) color='';
  56.         if (dynapi.ua.ns4 && color == '') color='#ffffff';
  57.         this.fgColor = color;
  58.         this.doc.fgColor = color;
  59. };
  60. p.insertChild = function(c,pos,usebp) { // Blueprint Enabled
  61.         if (c && !c.isInline && c.parent == this) {
  62.                 if(pos) c.setPosition(pos);
  63.                 DynElement._flagPreCreate(c);
  64.                 if(usebp) c.isInline=c._noInlineValues=true;
  65.                 else {
  66.                         this.doc.write(c.getOuterHTML());
  67.                         c._inserted = true;
  68.                 }
  69.         }
  70. };
  71. p.insertAllChildren = function(usebp,bpSrc) { // Blueprint Enabled
  72.         var i,c,str =[''];
  73.         var ch=this.children;
  74.         for(i=0;i<ch.length;i++) {
  75.                 c = ch[i];
  76.                 if(!c.isInline && !c._inserted){
  77.                         DynElement._flagPreCreate(c);
  78.                         if(usebp) c.isInline=c._noInlineValues=true;
  79.                         else {
  80.                                 str[i]=c.getOuterHTML();
  81.                                 c._inserted = true;
  82.                         }
  83.                 }
  84.         }
  85.         if(this._hBuffer.length) this.doc.write(this._hBuffer.join('')); // used by addHTML()
  86.         if(usebp){
  87.                 if(bpSrc) dynapi.frame.document.write('<script type="text/javascript" language="JavaScript" src="'+bpSrc+'"><\/script>');      
  88.         }
  89.         else {
  90.                 this.doc.write(str.join('\n'));
  91.                 this.doc.close();
  92.         }
  93. };
  94.  
  95. p._create = function() {
  96.         var ua=dynapi.ua;
  97.         this._created = true;
  98.         if (ua.ns4) {
  99.                 this.css = this.doc;
  100.                 this.elm = this.doc;
  101.         }
  102.         else {
  103.                 this.elm = this.frame.document.body;
  104.                 this.css = this.frame.document.body.style;
  105.                 if (ua.ie) {
  106.                         this._overflow = this.css.overflow || '';
  107.                 }
  108.                 if (this._cursor) this.css.cursor = this._cursor;
  109.         }
  110.         this.elm._dynobj = this;
  111.         this.doc._dynobj = this; // DynKeyEvent needs this!
  112.         this.findDimensions();
  113.  
  114.         this.fgColor = this.doc.fgColor||'';
  115.         this.bgColor = this.doc.bgColor||'';
  116.  
  117.         var divs;
  118.         // create divs object - speeds up DOM browsers on Win32. Linux & Mac?
  119.         if (ua.ie||ua.dom) {
  120.                 divs={};
  121.                 var dv,all=(ua.ie||ua.opera)? document.all.tags('div') : document.getElementsByTagName('div');
  122.                 var i=0,l=all.length; // very important!
  123.                 while (i<l){
  124.                         dv=all[i];
  125.                         divs[dv.id]=dv;
  126.                         i++;
  127.                 }
  128.         }
  129.        
  130.         var c,ch=this.children;
  131.         for(i=0;i<ch.length;i++){
  132.                 c=ch[i];
  133.                 if (c._inserted) c._createInserted(divs);
  134.                 else if(c.isInline) c._createInline(divs);
  135.                 else c._create();
  136.         };
  137.         this._updateAnchors(); 
  138.  
  139.         if(ua.ie && this._textSelectable==false) this.doc.onselectstart = dynapi.functions.Deny;
  140.        
  141.         if (this.captureMouseEvents) this.captureMouseEvents();
  142.         if (this.captureKeyEvents) this.captureKeyEvents();
  143.         this.invokeEvent('load');
  144. };
  145. p.destroyAllChildren = function() {
  146.         for (var i=0;i<this.children.length;i++) {
  147.                 this.children[i]._destroy();
  148.                 delete this.children[i];
  149.         }
  150.         this.children = [];
  151. };
  152. p._destroy = function() {
  153.         this.destroyAllChildren();
  154.         delete DynObject.all;
  155.         this.elm = null;
  156.         this.css = null;
  157.         this.frame = null;
  158. };
  159.  
  160. p._handleResize = function() {
  161.         var w = this.w;
  162.         var h = this.h;
  163.         this.findDimensions();
  164.         if (this.w!=w || this.h!=h) {
  165.                 if (dynapi.ua.ns4) {
  166.                         if (this.onResizeNS4=="redraw") {
  167.                                 for (var i=0;i<this.children.length;i++) {
  168.                                         this.children[i].elm = null;
  169.                                         if (this.children[i]._created) {
  170.                                                 this.children[i]._created = false;
  171.                                                 this.children[i]._create();
  172.                                         }
  173.                                 }
  174.                                 this.invokeEvent('resize');
  175.                         }
  176.                         else if (this.onResizeNS4=="reload") {
  177.                                 this.doc.location.href = this.doc.location.href;
  178.                         }
  179.                 }
  180.                 else {
  181.                         this.invokeEvent('resize');
  182.                         this._updateAnchors();
  183.                 }
  184.         }
  185. };
  186. p.getCursor = function() {return (this._cursor=='pointer')? 'hand':this._cursor};
  187. p.setCursor = function(c) {
  188.         if (!c) c = 'default';
  189.         else c=(c+'').toLowerCase();
  190.         if (!dynapi.ua.ie && c=='hand') c='pointer';
  191.         if (this._cursor!=c) {
  192.                 this._cursor = c;
  193.                 if (this.css) this.css.cursor = c;
  194.         }
  195. };
  196. p.setTextSelectable = function(b){
  197.         this._textSelectable = b;      
  198.         if(!dynapi.ua.ie) this.captureMouseEvents();
  199.         else{
  200.                 if (this.doc) this.doc.onselectstart = b? dynapi.functions.Allow : dynapi.functions.Deny;
  201.         }
  202.         if (!b) this.setCursor('default');
  203. };
  204. p.showScrollBars = function(b){
  205.         if(b==this._showScroll) return;
  206.         else this._showScroll=b;
  207.         if(dynapi.ua.ie){
  208.                 window.setTimeout('document.body.scroll="'+((b)? 'yes':'no')+'"',100);
  209.         }else if(dynapi.ua.ns||dynapi.ua.opera){
  210.                 if(b){
  211.                         this._docSize=[document.width,document.height];
  212.                         document.width = this.frame.innerWidth;
  213.                         document.height = this.frame.innerHeight;
  214.                 }else if(this._docSize){
  215.                         document.width = this._docSize[0];
  216.                         document.height = this._docSize[1];
  217.                 }
  218.         }
  219. };
  220.  
  221. p._hBuffer = [];
  222. p.addHTML = function(html){
  223.         var elm,ua = dynapi.ua;
  224.         var hbuf=this._hBuffer;
  225.         var cnt=(this._hblc)? this._hblc++:(this._hblc=1);
  226.         if (ua.ns4) {
  227.                 html='<nobr>'+html+'<nobr>';
  228.                 if(!this._created) hbuf[cnt]=html;
  229.                 else {
  230.                         elm=new Layer(0,this.frame);
  231.                         elm.left=elm.top=0;
  232.                         var doc=elm.document;
  233.                         elm.clip.width=dynapi.document.w;
  234.                         elm.clip.height=dynapi.document.h;
  235.                         doc.open();doc.write(html);doc.close();
  236.                         elm.visibility = 'inherit';
  237.                 }
  238.         }
  239.         else {
  240.                 var pelm=this.elm;
  241.                 if(!this._created) hbuf[cnt]=html;
  242.                 else {
  243.                         if(ua.ie){
  244.                                 pelm.insertAdjacentHTML("beforeEnd",html);
  245.                                 elm = pelm.children[pelm.children.length-1];                           
  246.                         }
  247.                         else{
  248.                                 var r = pelm.ownerDocument.createRange();
  249.                                 r.setStartBefore(pelm);
  250.                                 var ptxt = r.createContextualFragment(html);
  251.                                 pelm.appendChild(ptxt);
  252.                                 elm = pelm.lastChild;                          
  253.                         }
  254.                 }
  255.         }
  256. };
  257.  
  258. function main() {
  259.         if (dynapi.document==null) {
  260.                 dynapi.document = new DynDocument(dynapi.frame);
  261.                 if (dynapi.loaded) dynapi.document._create();
  262.                 else dynapi.onLoad(function() {
  263.                         dynapi.document._create();
  264.                 });
  265.         }
  266. };
  267. if (!dynapi.loaded) main();
  268.  
  269.