Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.         DynAPI Distribution
  3.         TemplateManager Class
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6.        
  7.         Requires: DynLayer
  8. */
  9.  
  10.  
  11. function Template(html,x,y,w,h,color,image){
  12.         this.DynLayer = DynLayer;
  13.         this.DynLayer(html||'',x,y,w,h,color,image);
  14.         this._fields=[];
  15. };
  16. var p = dynapi.setPrototype('Template','DynLayer');
  17. p._TemplateSetHTML = DynLayer.prototype.setHTML;
  18. p._insertField = function(fld,value){
  19.         var re = new RegExp('\\[@'+fld+'\\]');
  20.         this.html = this.html.replace(re,value);
  21. };
  22. p._insertChild = function(c){
  23.         if(!c._created) DynElement._flagPreCreate(c);
  24.         var html=c.getOuterHTML();
  25.         var re = new RegExp('\\[@'+c._tmplFld+'\\]');
  26.         if(dynapi.ua.ns4||dynapi.ua.gecko) {
  27.                 // NS4 inline layers does not honor <td align="right">, they're always left align. To solve this a <table> must be used to wrap the ilayers. This will also ensure that <ilayers> behave like block elements in DOM
  28.                 // Mozilla will collapse table cells whenever the content of the layer changes. To solve this, wrap <div> within <table> and set <table> witdh to <div> width
  29.                 html='<table width="'+c.w+'" cellpadding="0" cellspacing="0" border="0"><tr><td>'+html+'</td></tr></table>'
  30.         }
  31.         this.html = this.html.replace(re,html);
  32. };
  33. p.addChild = function(c,field){
  34.         if (!c || !field) return dynapi.debug.print("Error: no object or field sent to [Template].addChild()");
  35.         if (c.isChild) c.removeFromParent();
  36.         c.isChild = true;
  37.         c.parent = this;
  38.         if (c._saveAnchor) {
  39.                 c.setAnchor(c._saveAnchor);
  40.                 c._saveAnchor = null;
  41.                 delete c._saveAnchor;
  42.         }
  43.         this[field]=c;
  44.         c.isInline=true;
  45.         c._tmplFld=field;
  46.         c._noInlineValues=true;
  47.         if(c.isClass('DynLayer')) {
  48.                 c.setPosition('relative');
  49.                 // NS4 seems to create line breaks with inline layers that contains html
  50.                 if(dynapi.ua.ns4) c.enableBlackboard(); // this will force a <layer> arround the inline html
  51.         }
  52.         this.children[this.children.length] = c;
  53.         return c;      
  54. };
  55. p.clearTemplate = function(){
  56.         this.deleteAllChildren();
  57.         this._TemplateSetHTML('&nbsp;');
  58. };
  59. // Template Object does not support enableBlackboard
  60. p.enableBlackboard = dynapi.functions.Null;    
  61. p.getInnerHTML=function() {    
  62.         var s = '', fld = this._fields;
  63.         var i,c,ch=this.children;
  64.         // insert fields
  65.         for(i in fld) this._insertField(i,fld[i]);
  66.         // insert child layers/objects
  67.         for (i=0;i<ch.length;i++) this._insertChild(ch[i]);    
  68.         if (this.html!=null) {
  69.                 if(!dynapi.ua.ns4) s+=this.html;
  70.                 else {
  71.                         if (this.w==null) s += '<nobr>'+this.html+'</nobr>';
  72.                         else s+=this.html;
  73.                 }
  74.         }
  75.         return s;
  76. };
  77. // generate and display the changes made to the template
  78. p.generate = function(){
  79.         if(!this._created) return;
  80.         else {
  81.                 var h=this.getInnerHTML();
  82.                 var i,c,ch=this.children;
  83.                 this.html = null;
  84.                 this._TemplateSetHTML(h);              
  85.                 for (i=0;i<ch.length;i++) {
  86.                         c=ch[i];                       
  87.                         if(c.isClass('DynLayer')) DynLayer._assignElement(c);
  88.                         if(!c._created) DynElement._flagCreate(c);
  89.                 }
  90.         }
  91. };
  92. p.setHTML = function(html){
  93.         this.html=html;
  94.         if(this._created) this.generate();
  95. };
  96. p.getField = function(fld) {return this._fields[fld]};
  97. p.setField = function(fld,value){
  98.         this._fields[fld]=value;
  99. };
  100.