3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.DomHelper
11
* Utility class for working with DOM and/or Templates. It transparently supports using HTML fragments or DOM.<br>
12
* This is an example, where an unordered list with 5 children items is appended to an existing element with id 'my-div':<br>
14
var dh = Ext.DomHelper;
15
var list = dh.append('my-div', {
16
id: 'my-ul', tag: 'ul', cls: 'my-list', children: [
17
{tag: 'li', id: 'item0', html: 'List Item 0'},
18
{tag: 'li', id: 'item1', html: 'List Item 1'},
19
{tag: 'li', id: 'item2', html: 'List Item 2'},
20
{tag: 'li', id: 'item3', html: 'List Item 3'},
21
{tag: 'li', id: 'item4', html: 'List Item 4'}
25
* <p>Element creation specification parameters in this class may also be passed as an Array of
26
* specification objects. This can be used to insert multiple sibling nodes into an existing
27
* container very efficiently. For example, to add more list items to the example above:<pre><code>
29
{tag: 'li', id: 'item5', html: 'List Item 5'},
30
{tag: 'li', id: 'item6', html: 'List Item 6'} ]);
32
* <p>Element creation specification parameters may also be strings. If {@link useDom} is false, then the string is used
33
* as innerHTML. If {@link useDom} is true, a string specification results in the creation of a text node.</p>
34
* For more information and examples, see <a href="http://www.jackslocum.com/blog/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">the original blog post</a>.
37
Ext.DomHelper = function(){
38
var tempTableEl = null;
39
var emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i;
40
var tableRe = /^table|tbody|tr|td$/i;
42
// build as innerHTML where available
43
var createHtml = function(o){
44
if(typeof o == 'string'){
49
for (var i = 0, l = o.length; i < l; i++) {
50
b += createHtml(o[i]);
59
if(attr == "tag" || attr == "children" || attr == "cn" || attr == "html" || typeof o[attr] == "function") continue;
62
if(typeof s == "function"){
65
if(typeof s == "string"){
66
b += ' style="' + s + '"';
67
}else if(typeof s == "object"){
70
if(typeof s[key] != "function"){
71
b += key + ":" + s[key] + ";";
78
b += ' class="' + o["cls"] + '"';
79
}else if(attr == "htmlFor"){
80
b += ' for="' + o["htmlFor"] + '"';
82
b += " " + attr + '="' + o[attr] + '"';
86
if(emptyTags.test(o.tag)){
90
var cn = o.children || o.cn;
96
b += "</" + o.tag + ">";
103
var createDom = function(o, parentNode){
105
if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted
106
el = document.createDocumentFragment(); // in one shot using a DocumentFragment
107
for(var i = 0, l = o.length; i < l; i++) {
110
} else if (typeof o == "string") { // Allow a string as a child spec.
111
el = document.createTextNode(o);
113
el = document.createElement(o.tag||'div');
114
var useSet = !!el.setAttribute; // In IE some elements don't have setAttribute
116
if(attr == "tag" || attr == "children" || attr == "cn" || attr == "html" || attr == "style" || typeof o[attr] == "function") continue;
118
el.className = o["cls"];
120
if(useSet) el.setAttribute(attr, o[attr]);
121
else el[attr] = o[attr];
124
Ext.DomHelper.applyStyles(el, o.style);
125
var cn = o.children || o.cn;
129
el.innerHTML = o.html;
133
parentNode.appendChild(el);
138
var ieTable = function(depth, s, h, e){
139
tempTableEl.innerHTML = [s, h, e].join('');
140
var i = -1, el = tempTableEl;
147
// kill repeat to save bytes
157
* Nasty code for IE's broken table implementation
159
var insertIntoTable = function(tag, where, el, html){
161
tempTableEl = document.createElement('div');
166
if(where == 'afterbegin' || where == 'beforeend'){ // INTO a TD
169
if(where == 'beforebegin'){
173
before = el.nextSibling;
176
node = ieTable(4, trs, html, tre);
178
else if(tag == 'tr'){
179
if(where == 'beforebegin'){
182
node = ieTable(3, tbs, html, tbe);
183
} else if(where == 'afterend'){
184
before = el.nextSibling;
186
node = ieTable(3, tbs, html, tbe);
188
if(where == 'afterbegin'){
189
before = el.firstChild;
191
node = ieTable(4, trs, html, tre);
193
} else if(tag == 'tbody'){
194
if(where == 'beforebegin'){
197
node = ieTable(2, ts, html, te);
198
} else if(where == 'afterend'){
199
before = el.nextSibling;
201
node = ieTable(2, ts, html, te);
203
if(where == 'afterbegin'){
204
before = el.firstChild;
206
node = ieTable(3, tbs, html, tbe);
209
if(where == 'beforebegin' || where == 'afterend'){ // OUTSIDE the table
212
if(where == 'afterbegin'){
213
before = el.firstChild;
215
node = ieTable(2, ts, html, te);
217
el.insertBefore(node, before);
223
/** True to force the use of DOM instead of html fragments @type Boolean */
227
* Returns the markup for the passed Element(s) config.
228
* @param {Object} o The DOM object spec (and children)
231
markup : function(o){
232
return createHtml(o);
236
* Applies a style specification to an element.
237
* @param {String/HTMLElement} el The element to apply styles to
238
* @param {String/Object/Function} styles A style specification string eg "width:100px", or object in the form {width:"100px"}, or
239
* a function which returns such a specification.
241
applyStyles : function(el, styles){
244
if(typeof styles == "string"){
245
var re = /\s?([a-z\-]*)\:\s?([^;]*);?/gi;
247
while ((matches = re.exec(styles)) != null){
248
el.setStyle(matches[1], matches[2]);
250
}else if (typeof styles == "object"){
251
for (var style in styles){
252
el.setStyle(style, styles[style]);
254
}else if (typeof styles == "function"){
255
Ext.DomHelper.applyStyles(el, styles.call());
261
* Inserts an HTML fragment into the DOM.
262
* @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
263
* @param {HTMLElement} el The context element
264
* @param {String} html The HTML fragmenet
265
* @return {HTMLElement} The new node
267
insertHtml : function(where, el, html){
268
where = where.toLowerCase();
269
if(el.insertAdjacentHTML){
270
if(tableRe.test(el.tagName)){
272
if(rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html)){
278
el.insertAdjacentHTML('BeforeBegin', html);
279
return el.previousSibling;
281
el.insertAdjacentHTML('AfterBegin', html);
282
return el.firstChild;
284
el.insertAdjacentHTML('BeforeEnd', html);
287
el.insertAdjacentHTML('AfterEnd', html);
288
return el.nextSibling;
290
throw 'Illegal insertion point -> "' + where + '"';
292
var range = el.ownerDocument.createRange();
296
range.setStartBefore(el);
297
frag = range.createContextualFragment(html);
298
el.parentNode.insertBefore(frag, el);
299
return el.previousSibling;
302
range.setStartBefore(el.firstChild);
303
frag = range.createContextualFragment(html);
304
el.insertBefore(frag, el.firstChild);
305
return el.firstChild;
308
return el.firstChild;
312
range.setStartAfter(el.lastChild);
313
frag = range.createContextualFragment(html);
314
el.appendChild(frag);
321
range.setStartAfter(el);
322
frag = range.createContextualFragment(html);
323
el.parentNode.insertBefore(frag, el.nextSibling);
324
return el.nextSibling;
326
throw 'Illegal insertion point -> "' + where + '"';
330
* Creates new DOM element(s) and inserts them before el.
331
* @param {Mixed} el The context element
332
* @param {Object/String} o The DOM object spec (and children) or raw HTML blob
333
* @param {Boolean} returnElement (optional) true to return a Ext.Element
334
* @return {HTMLElement/Ext.Element} The new node
336
insertBefore : function(el, o, returnElement){
337
return this.doInsert(el, o, returnElement, "beforeBegin");
341
* Creates new DOM element(s) and inserts them after el.
342
* @param {Mixed} el The context element
343
* @param {Object} o The DOM object spec (and children)
344
* @param {Boolean} returnElement (optional) true to return a Ext.Element
345
* @return {HTMLElement/Ext.Element} The new node
347
insertAfter : function(el, o, returnElement){
348
return this.doInsert(el, o, returnElement, "afterEnd", "nextSibling");
352
* Creates new DOM element(s) and inserts them as the first child of el.
353
* @param {Mixed} el The context element
354
* @param {Object/String} o The DOM object spec (and children) or raw HTML blob
355
* @param {Boolean} returnElement (optional) true to return a Ext.Element
356
* @return {HTMLElement/Ext.Element} The new node
358
insertFirst : function(el, o, returnElement){
359
return this.doInsert(el, o, returnElement, "afterBegin", "firstChild");
363
doInsert : function(el, o, returnElement, pos, sibling){
367
newNode = createDom(o, null);
368
(sibling === "firstChild" ? el : el.parentNode).insertBefore(newNode, sibling ? el[sibling] : el);
370
var html = createHtml(o);
371
newNode = this.insertHtml(pos, el, html);
373
return returnElement ? Ext.get(newNode, true) : newNode;
377
* Creates new DOM element(s) and appends them to el.
378
* @param {Mixed} el The context element
379
* @param {Object/String} o The DOM object spec (and children) or raw HTML blob
380
* @param {Boolean} returnElement (optional) true to return a Ext.Element
381
* @return {HTMLElement/Ext.Element} The new node
383
append : function(el, o, returnElement){
387
newNode = createDom(o, null);
388
el.appendChild(newNode);
390
var html = createHtml(o);
391
newNode = this.insertHtml("beforeEnd", el, html);
393
return returnElement ? Ext.get(newNode, true) : newNode;
397
* Creates new DOM element(s) and overwrites the contents of el with them.
398
* @param {Mixed} el The context element
399
* @param {Object/String} o The DOM object spec (and children) or raw HTML blob
400
* @param {Boolean} returnElement (optional) true to return a Ext.Element
401
* @return {HTMLElement/Ext.Element} The new node
403
overwrite : function(el, o, returnElement){
405
el.innerHTML = createHtml(o);
406
return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
410
* Creates a new Ext.Template from the DOM object spec.
411
* @param {Object} o The DOM object spec (and children)
412
* @return {Ext.Template} The new template
414
createTemplate : function(o){
415
var html = createHtml(o);
416
return new Ext.Template(html);