web/privatedoc/js/builder.js
changeset 7 7a5d38af0e65
equal deleted inserted replaced
6:06c4f682b7b2 7:7a5d38af0e65
       
     1 // script.aculo.us builder.js v1.6.4, Wed Sep 06 11:30:58 CEST 2006
       
     2 
       
     3 // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
       
     4 //
       
     5 // See scriptaculous.js for full license.
       
     6 
       
     7 var Builder = {
       
     8   NODEMAP: {
       
     9     AREA: 'map',
       
    10     CAPTION: 'table',
       
    11     COL: 'table',
       
    12     COLGROUP: 'table',
       
    13     LEGEND: 'fieldset',
       
    14     OPTGROUP: 'select',
       
    15     OPTION: 'select',
       
    16     PARAM: 'object',
       
    17     TBODY: 'table',
       
    18     TD: 'table',
       
    19     TFOOT: 'table',
       
    20     TH: 'table',
       
    21     THEAD: 'table',
       
    22     TR: 'table'
       
    23   },
       
    24   // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
       
    25   //       due to a Firefox bug
       
    26   node: function(elementName) {
       
    27     elementName = elementName.toUpperCase();
       
    28     
       
    29     // try innerHTML approach
       
    30     var parentTag = this.NODEMAP[elementName] || 'div';
       
    31     var parentElement = document.createElement(parentTag);
       
    32     try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
       
    33       parentElement.innerHTML = "<" + elementName + "></" + elementName + ">";
       
    34     } catch(e) {}
       
    35     var element = parentElement.firstChild || null;
       
    36       
       
    37     // see if browser added wrapping tags
       
    38     if(element && (element.tagName != elementName))
       
    39       element = element.getElementsByTagName(elementName)[0];
       
    40     
       
    41     // fallback to createElement approach
       
    42     if(!element) element = document.createElement(elementName);
       
    43     
       
    44     // abort if nothing could be created
       
    45     if(!element) return;
       
    46 
       
    47     // attributes (or text)
       
    48     if(arguments[1])
       
    49       if(this._isStringOrNumber(arguments[1]) ||
       
    50         (arguments[1] instanceof Array)) {
       
    51           this._children(element, arguments[1]);
       
    52         } else {
       
    53           var attrs = this._attributes(arguments[1]);
       
    54           if(attrs.length) {
       
    55             try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
       
    56               parentElement.innerHTML = "<" +elementName + " " +
       
    57                 attrs + "></" + elementName + ">";
       
    58             } catch(e) {}
       
    59             element = parentElement.firstChild || null;
       
    60             // workaround firefox 1.0.X bug
       
    61             if(!element) {
       
    62               element = document.createElement(elementName);
       
    63               for(attr in arguments[1]) 
       
    64                 element[attr == 'class' ? 'className' : attr] = arguments[1][attr];
       
    65             }
       
    66             if(element.tagName != elementName)
       
    67               element = parentElement.getElementsByTagName(elementName)[0];
       
    68             }
       
    69         } 
       
    70 
       
    71     // text, or array of children
       
    72     if(arguments[2])
       
    73       this._children(element, arguments[2]);
       
    74 
       
    75      return element;
       
    76   },
       
    77   _text: function(text) {
       
    78      return document.createTextNode(text);
       
    79   },
       
    80   _attributes: function(attributes) {
       
    81     var attrs = [];
       
    82     for(attribute in attributes)
       
    83       attrs.push((attribute=='className' ? 'class' : attribute) +
       
    84           '="' + attributes[attribute].toString().escapeHTML() + '"');
       
    85     return attrs.join(" ");
       
    86   },
       
    87   _children: function(element, children) {
       
    88     if(typeof children=='object') { // array can hold nodes and text
       
    89       children.flatten().each( function(e) {
       
    90         if(typeof e=='object')
       
    91           element.appendChild(e)
       
    92         else
       
    93           if(Builder._isStringOrNumber(e))
       
    94             element.appendChild(Builder._text(e));
       
    95       });
       
    96     } else
       
    97       if(Builder._isStringOrNumber(children)) 
       
    98          element.appendChild(Builder._text(children));
       
    99   },
       
   100   _isStringOrNumber: function(param) {
       
   101     return(typeof param=='string' || typeof param=='number');
       
   102   },
       
   103   dump: function(scope) { 
       
   104     if(typeof scope != 'object' && typeof scope != 'function') scope = window; //global scope 
       
   105   
       
   106     var tags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " +
       
   107       "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " +
       
   108       "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+
       
   109       "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+
       
   110       "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+
       
   111       "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/);
       
   112   
       
   113     tags.each( function(tag){ 
       
   114       scope[tag] = function() { 
       
   115         return Builder.node.apply(Builder, [tag].concat($A(arguments)));  
       
   116       } 
       
   117     });
       
   118   }
       
   119 }