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