3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.DomHelper"></div>/**
16 * @class Ext.DomHelper
17 * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
18 * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
19 * from your DOM building code.</p>
21 * <p><b><u>DomHelper element specification object</u></b></p>
22 * <p>A specification object is used when creating elements. Attributes of this object
23 * are assumed to be element attributes, except for 4 special attributes:
24 * <div class="mdetail-params"><ul>
25 * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
26 * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
27 * same kind of element definition objects to be created and appended. These can be nested
28 * as deep as you want.</div></li>
29 * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
30 * This will end up being either the "class" attribute on a HTML fragment or className
31 * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
32 * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
35 * <p><b><u>Insertion methods</u></b></p>
36 * <p>Commonly used insertion methods:
37 * <div class="mdetail-params"><ul>
38 * <li><b><tt>{@link #append}</tt></b> : <div class="sub-desc"></div></li>
39 * <li><b><tt>{@link #insertBefore}</tt></b> : <div class="sub-desc"></div></li>
40 * <li><b><tt>{@link #insertAfter}</tt></b> : <div class="sub-desc"></div></li>
41 * <li><b><tt>{@link #overwrite}</tt></b> : <div class="sub-desc"></div></li>
42 * <li><b><tt>{@link #createTemplate}</tt></b> : <div class="sub-desc"></div></li>
43 * <li><b><tt>{@link #insertHtml}</tt></b> : <div class="sub-desc"></div></li>
46 * <p><b><u>Example</u></b></p>
47 * <p>This is an example, where an unordered list with 3 children items is appended to an existing
48 * element with id <tt>'my-div'</tt>:<br>
50 var dh = Ext.DomHelper; // create shorthand alias
51 // specification object
56 // append children after creating
57 children: [ // may also specify 'cn' instead of 'children'
58 {tag: 'li', id: 'item0', html: 'List Item 0'},
59 {tag: 'li', id: 'item1', html: 'List Item 1'},
60 {tag: 'li', id: 'item2', html: 'List Item 2'}
64 'my-div', // the context element 'my-div' can either be the id or the actual node
65 spec // the specification object
68 * <p>Element creation specification parameters in this class may also be passed as an Array of
69 * specification objects. This can be used to insert multiple sibling nodes into an existing
70 * container very efficiently. For example, to add more list items to the example above:<pre><code>
72 {tag: 'li', id: 'item3', html: 'List Item 3'},
73 {tag: 'li', id: 'item4', html: 'List Item 4'}
77 * <p><b><u>Templating</u></b></p>
78 * <p>The real power is in the built-in templating. Instead of creating or appending any elements,
79 * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to
80 * insert new elements. Revisiting the example above, we could utilize templating this time:
83 var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
85 var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});
87 for(var i = 0; i < 5, i++){
88 tpl.append(list, [i]); // use template to append to the actual node
91 * <p>An example using a template:<pre><code>
92 var html = '<a id="{0}" href="{1}" class="nav">{2}</a>';
94 var tpl = new Ext.DomHelper.createTemplate(html);
95 tpl.append('blog-roll', ['link1', 'http://www.jackslocum.com/', "Jack's Site"]);
96 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin's Site"]);
99 * <p>The same example using named parameters:<pre><code>
100 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
102 var tpl = new Ext.DomHelper.createTemplate(html);
103 tpl.append('blog-roll', {
105 url: 'http://www.jackslocum.com/',
106 text: "Jack's Site"
108 tpl.append('blog-roll', {
110 url: 'http://www.dustindiaz.com/',
111 text: "Dustin's Site"
115 * <p><b><u>Compiling Templates</u></b></p>
116 * <p>Templates are applied using regular expressions. The performance is great, but if
117 * you are adding a bunch of DOM elements using the same template, you can increase
118 * performance even further by {@link Ext.Template#compile "compiling"} the template.
119 * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and
120 * broken up at the different variable points and a dynamic function is created and eval'ed.
121 * The generated function performs string concatenation of these parts and the passed
122 * variables instead of using regular expressions.
124 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
126 var tpl = new Ext.DomHelper.createTemplate(html);
129 //... use template like normal
132 * <p><b><u>Performance Boost</u></b></p>
133 * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
134 * of DOM can significantly boost performance.</p>
135 * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>,
136 * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification
137 * results in the creation of a text node. Usage:</p>
139 Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance
143 Ext.DomHelper = function(){
144 var tempTableEl = null,
145 emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i,
146 tableRe = /^table|tbody|tr|td$/i,
148 // kill repeat to save bytes
149 afterbegin = 'afterbegin',
150 afterend = 'afterend',
151 beforebegin = 'beforebegin',
152 beforeend = 'beforeend',
161 function doInsert(el, o, returnElement, pos, sibling, append){
162 var newNode = pub.insertHtml(pos, Ext.getDom(el), createHtml(o));
163 return returnElement ? Ext.get(newNode, true) : newNode;
166 // build as innerHTML where available
167 function createHtml(o){
177 } else if (Ext.isArray(o)) {
178 for (var i=0; i < o.length; i++) {
180 b += createHtml(o[i]);
184 b += '<' + (o.tag = o.tag || 'div');
185 Ext.iterate(o, function(attr, val){
186 if(!/tag|children|cn|html$/i.test(attr)){
187 if (Ext.isObject(val)) {
188 b += ' ' + attr + '="';
189 Ext.iterate(val, function(key, keyVal){
190 b += key + ':' + keyVal + ';';
194 b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"';
198 // Now either just close the tag or try to add children and close the tag.
199 if (emptyTags.test(o.tag)) {
203 if ((cn = o.children || o.cn)) {
208 b += '</' + o.tag + '>';
214 function ieTable(depth, s, h, e){
215 tempTableEl.innerHTML = [s, h, e].join('');
222 // If the result is multiple siblings, then encapsulate them into one fragment.
223 if(ns = el.nextSibling){
224 var df = document.createDocumentFragment();
237 * Nasty code for IE's broken table implementation
239 function insertIntoTable(tag, where, el, html) {
243 tempTableEl = tempTableEl || document.createElement('div');
245 if(tag == 'td' && (where == afterbegin || where == beforeend) ||
246 !/td|tr|tbody/i.test(tag) && (where == beforebegin || where == afterend)) {
249 before = where == beforebegin ? el :
250 where == afterend ? el.nextSibling :
251 where == afterbegin ? el.firstChild : null;
253 if (where == beforebegin || where == afterend) {
257 if (tag == 'td' || (tag == 'tr' && (where == beforeend || where == afterbegin))) {
258 node = ieTable(4, trs, html, tre);
259 } else if ((tag == 'tbody' && (where == beforeend || where == afterbegin)) ||
260 (tag == 'tr' && (where == beforebegin || where == afterend))) {
261 node = ieTable(3, tbs, html, tbe);
263 node = ieTable(2, ts, html, te);
265 el.insertBefore(node, before);
271 <div id="method-Ext.DomHelper-markup"></div>/**
272 * Returns the markup for the passed Element(s) config.
273 * @param {Object} o The DOM object spec (and children)
276 markup : function(o){
277 return createHtml(o);
280 <div id="method-Ext.DomHelper-applyStyles"></div>/**
281 * Applies a style specification to an element.
282 * @param {String/HTMLElement} el The element to apply styles to
283 * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or
284 * a function which returns such a specification.
286 applyStyles : function(el, styles){
293 if(Ext.isFunction(styles)){
294 styles = styles.call();
296 if(Ext.isString(styles)){
297 styles = styles.trim().split(/\s*(?::|;)\s*/);
298 for(len = styles.length; i < len;){
299 el.setStyle(styles[i++], styles[i++]);
301 }else if (Ext.isObject(styles)){
307 <div id="method-Ext.DomHelper-insertHtml"></div>/**
308 * Inserts an HTML fragment into the DOM.
309 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
310 * @param {HTMLElement} el The context element
311 * @param {String} html The HTML fragment
312 * @return {HTMLElement} The new node
314 insertHtml : function(where, el, html){
323 where = where.toLowerCase();
324 // add these here because they are used in both branches of the condition.
325 hash[beforebegin] = ['BeforeBegin', 'previousSibling'];
326 hash[afterend] = ['AfterEnd', 'nextSibling'];
328 if (el.insertAdjacentHTML) {
329 if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){
332 // add these two to the hash.
333 hash[afterbegin] = ['AfterBegin', 'firstChild'];
334 hash[beforeend] = ['BeforeEnd', 'lastChild'];
335 if ((hashVal = hash[where])) {
336 el.insertAdjacentHTML(hashVal[0], html);
337 return el[hashVal[1]];
340 range = el.ownerDocument.createRange();
341 setStart = 'setStart' + (/end/i.test(where) ? 'After' : 'Before');
344 frag = range.createContextualFragment(html);
345 el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling);
346 return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling'];
348 rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child';
350 range[setStart](el[rangeEl]);
351 frag = range.createContextualFragment(html);
352 if(where == afterbegin){
353 el.insertBefore(frag, el.firstChild);
355 el.appendChild(frag);
363 throw 'Illegal insertion point -> "' + where + '"';
366 <div id="method-Ext.DomHelper-insertBefore"></div>/**
367 * Creates new DOM element(s) and inserts them before el.
368 * @param {Mixed} el The context element
369 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
370 * @param {Boolean} returnElement (optional) true to return a Ext.Element
371 * @return {HTMLElement/Ext.Element} The new node
373 insertBefore : function(el, o, returnElement){
374 return doInsert(el, o, returnElement, beforebegin);
377 <div id="method-Ext.DomHelper-insertAfter"></div>/**
378 * Creates new DOM element(s) and inserts them after el.
379 * @param {Mixed} el The context element
380 * @param {Object} o The DOM object spec (and children)
381 * @param {Boolean} returnElement (optional) true to return a Ext.Element
382 * @return {HTMLElement/Ext.Element} The new node
384 insertAfter : function(el, o, returnElement){
385 return doInsert(el, o, returnElement, afterend, 'nextSibling');
388 <div id="method-Ext.DomHelper-insertFirst"></div>/**
389 * Creates new DOM element(s) and inserts them as the first child of el.
390 * @param {Mixed} el The context element
391 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
392 * @param {Boolean} returnElement (optional) true to return a Ext.Element
393 * @return {HTMLElement/Ext.Element} The new node
395 insertFirst : function(el, o, returnElement){
396 return doInsert(el, o, returnElement, afterbegin, 'firstChild');
399 <div id="method-Ext.DomHelper-append"></div>/**
400 * Creates new DOM element(s) and appends them to el.
401 * @param {Mixed} el The context element
402 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
403 * @param {Boolean} returnElement (optional) true to return a Ext.Element
404 * @return {HTMLElement/Ext.Element} The new node
406 append : function(el, o, returnElement){
407 return doInsert(el, o, returnElement, beforeend, '', true);
410 <div id="method-Ext.DomHelper-overwrite"></div>/**
411 * Creates new DOM element(s) and overwrites the contents of el with them.
412 * @param {Mixed} el The context element
413 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
414 * @param {Boolean} returnElement (optional) true to return a Ext.Element
415 * @return {HTMLElement/Ext.Element} The new node
417 overwrite : function(el, o, returnElement){
419 el.innerHTML = createHtml(o);
420 return returnElement ? Ext.get(el.firstChild) : el.firstChild;
423 createHtml : createHtml