Changeset 27806

Show
Ignore:
Timestamp:
01/03/09 08:32:48 (4 years ago)
Author:
hoge1e3
Message:
 
Location:
lang/javascript/tagBuilder
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/tagBuilder/TagBuilder.class.js

    r27756 r27806  
    2323           if (this.expr instanceof Array) return this.expr[0].toLowerCase(); 
    2424   }, 
     25   tagNameIs: function (t) { 
     26       return this.getTagName().eqi(t.toLowerCase()); 
     27   }, 
     28   isTr: function (t) { 
     29       if (this.tagNameIs("tr")) { 
     30                  checkTagName(this.parent.tagName,"table"); 
     31                  return true; 
     32           } 
     33           return false; 
     34   }, 
    2535   get: function () { 
    2636       return this.target.value; 
     
    5161   }, 
    5262   remove: function () { 
    53                 if (this.parent && !this.parent.removeChild) { 
     63          if (this.parent && !this.parent.removeChild) { 
    5464              alert(Object.toJSON(this.parent)); 
    5565          } 
    5666          if (!this.parent) alert("Parent is null"); 
     67          if (this.isTr()) { 
     68              var target=this.target; 
     69                  var rmIdx=-1; 
     70                  $A(this.parent.rows).each(function (e,idx) { 
     71                      if (e===target) { 
     72                              rmIdx=idx; 
     73                          } 
     74                  }); 
     75                  if (rmIdx<0) alert("Index not found"); 
     76                  this.parent.deleteRow(rmIdx); 
     77                  return; 
     78          } 
    5779          this.parent.removeChild(this.target); 
    5880   }, 
    59  
     81    insertBefore:function (e,idx) { 
     82           if (idx==null) idx=0; 
     83           checkNull(this.parentBuilder,"parentBuilder").add( 
     84               e, 
     85               this.indexFromParent()-idx 
     86           ); 
     87        }, 
     88        insertAfter:function (e,idx) { 
     89           if (idx==null) idx=0; 
     90           checkNull(this.parentBuilder,"parentBuilder").add( 
     91               e, 
     92               this.indexFromParent()+1+idx 
     93           ); 
     94        }, 
     95    indexFromParent: function () { 
     96           var res=-1; 
     97           this.getSiblings().each(function (e,i) { 
     98               if (e===t.target) res=i; 
     99           }); 
     100           if (res<0) alert("Index not found"); 
     101           return res; 
     102        }, 
     103        getSiblings: function () { 
     104           if (this.target==null) alert("getSiblings: Target is not set"); 
     105           if (this.siblings) return this.siblings; 
     106           if (this.isTr()) { 
     107                   return this.siblings=$A(this.parent.rows); 
     108           }  
     109           return this.siblings=$A(this.childNodes); 
     110        }, 
    60111    add:function (a, idx) { 
    61112          var target=this.target; 
     
    67118          if (a instanceof Array) return this.addArray(a,idx); 
    68119          if (a instanceof Function) return a(this,idx); 
    69           if (a instanceof TagBuilder) {a.render(target,idx);return this;} 
     120          if (a instanceof TagBuilder) { 
     121                a.parentBuilder=this; 
     122                a.render(target,idx); 
     123                return this; 
     124          } 
    70125          if (typeof(a)=="object" && a.render instanceof Function) { 
    71126              a.render(this,idx);return this; 
     
    170225   return typeof(e)=="object" && e.tagName; 
    171226} 
     227function checkNull(v,msg){ 
     228   if (v==null) alert(msg+" is null"); 
     229   return v; 
     230} 
    172231 
    173232function tag(a) { 
  • lang/javascript/tagBuilder/sample3.html

    r27754 r27806  
    44        <script src="prototype.js"></script> 
    55        <script src="TagBuilder.class.js"></script> 
    6         <script src="misc/WindowWidget.class.js"></script> 
    7         <script src="misc/Mouse.js"></script> 
    86        <script> 
    97          var list; 
     8          var data=["foo","bar","baz"]; 
    109          function exec() { 
    1110             $tag("button").target.disabled=true; 
    12              var w=new WindowWidget(["span","test"]); 
    13                  w.show(); 
     11             $tag("body").add(["table",{border:1}, 
     12                     ["tr",["td","No"],["td","Str"],["td","Button"]], 
     13                         function (tagBuilder) { 
     14                            data.each(function (str,no) { 
     15                                   var tr=tag("tr", 
     16                                       ["td",no], 
     17                                           ["td",str], 
     18                                       ["td",["button",{onclick:rm},"Remove"]] 
     19                                   ); 
     20                                   tagBuilder.add(tr); 
     21                                   function rm() { 
     22                                      tr.remove(); 
     23                                   } 
     24                            }); 
     25                         } 
     26             ]); 
    1427          } 
    1528        </script> 
    1629  </head> 
    17         <body  
    18           onmousemove="Mouse.handleEvents(event,'move')" 
    19           onmousedown="Mouse.handleEvents(event,'down')" 
    20           onmouseup="Mouse.handleEvents(event,'up')"> 
     30  <body id="body"> 
    2131     <h1>Tag Builder Test 3</h1> 
    22           <p>* Requires programs on ./misc/ directory </p> 
    2332     <button id="button" onclick="exec()">Start</button> 
    2433  </body>