3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.DomHelper"></div>/**
\r
15 * @class Ext.DomHelper
\r
16 * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
\r
17 * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
\r
18 * from your DOM building code.</p>
\r
20 * <p><b><u>DomHelper element specification object</u></b></p>
\r
21 * <p>A specification object is used when creating elements. Attributes of this object
\r
22 * are assumed to be element attributes, except for 4 special attributes:
\r
23 * <div class="mdetail-params"><ul>
\r
24 * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
\r
25 * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
\r
26 * same kind of element definition objects to be created and appended. These can be nested
\r
27 * as deep as you want.</div></li>
\r
28 * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
\r
29 * This will end up being either the "class" attribute on a HTML fragment or className
\r
30 * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
\r
31 * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
\r
34 * <p><b><u>Insertion methods</u></b></p>
\r
35 * <p>Commonly used insertion methods:
\r
36 * <div class="mdetail-params"><ul>
\r
37 * <li><b><tt>{@link #append}</tt></b> : <div class="sub-desc"></div></li>
\r
38 * <li><b><tt>{@link #insertBefore}</tt></b> : <div class="sub-desc"></div></li>
\r
39 * <li><b><tt>{@link #insertAfter}</tt></b> : <div class="sub-desc"></div></li>
\r
40 * <li><b><tt>{@link #overwrite}</tt></b> : <div class="sub-desc"></div></li>
\r
41 * <li><b><tt>{@link #createTemplate}</tt></b> : <div class="sub-desc"></div></li>
\r
42 * <li><b><tt>{@link #insertHtml}</tt></b> : <div class="sub-desc"></div></li>
\r
45 * <p><b><u>Example</u></b></p>
\r
46 * <p>This is an example, where an unordered list with 3 children items is appended to an existing
\r
47 * element with id <tt>'my-div'</tt>:<br>
\r
49 var dh = Ext.DomHelper; // create shorthand alias
\r
50 // specification object
\r
55 // append children after creating
\r
56 children: [ // may also specify 'cn' instead of 'children'
\r
57 {tag: 'li', id: 'item0', html: 'List Item 0'},
\r
58 {tag: 'li', id: 'item1', html: 'List Item 1'},
\r
59 {tag: 'li', id: 'item2', html: 'List Item 2'}
\r
62 var list = dh.append(
\r
63 'my-div', // the context element 'my-div' can either be the id or the actual node
\r
64 spec // the specification object
\r
67 * <p>Element creation specification parameters in this class may also be passed as an Array of
\r
68 * specification objects. This can be used to insert multiple sibling nodes into an existing
\r
69 * container very efficiently. For example, to add more list items to the example above:<pre><code>
\r
70 dh.append('my-ul', [
\r
71 {tag: 'li', id: 'item3', html: 'List Item 3'},
\r
72 {tag: 'li', id: 'item4', html: 'List Item 4'}
\r
76 * <p><b><u>Templating</u></b></p>
\r
77 * <p>The real power is in the built-in templating. Instead of creating or appending any elements,
\r
78 * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to
\r
79 * insert new elements. Revisiting the example above, we could utilize templating this time:
\r
82 var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
\r
84 var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});
\r
86 for(var i = 0; i < 5, i++){
\r
87 tpl.append(list, [i]); // use template to append to the actual node
\r
90 * <p>An example using a template:<pre><code>
\r
91 var html = '<a id="{0}" href="{1}" class="nav">{2}</a>';
\r
93 var tpl = new Ext.DomHelper.createTemplate(html);
\r
94 tpl.append('blog-roll', ['link1', 'http://www.jackslocum.com/', "Jack's Site"]);
\r
95 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin's Site"]);
\r
98 * <p>The same example using named parameters:<pre><code>
\r
99 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
\r
101 var tpl = new Ext.DomHelper.createTemplate(html);
\r
102 tpl.append('blog-roll', {
\r
104 url: 'http://www.jackslocum.com/',
\r
105 text: "Jack's Site"
\r
107 tpl.append('blog-roll', {
\r
109 url: 'http://www.dustindiaz.com/',
\r
110 text: "Dustin's Site"
\r
112 * </code></pre></p>
\r
114 * <p><b><u>Compiling Templates</u></b></p>
\r
115 * <p>Templates are applied using regular expressions. The performance is great, but if
\r
116 * you are adding a bunch of DOM elements using the same template, you can increase
\r
117 * performance even further by {@link Ext.Template#compile "compiling"} the template.
\r
118 * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and
\r
119 * broken up at the different variable points and a dynamic function is created and eval'ed.
\r
120 * The generated function performs string concatenation of these parts and the passed
\r
121 * variables instead of using regular expressions.
\r
123 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
\r
125 var tpl = new Ext.DomHelper.createTemplate(html);
\r
128 //... use template like normal
\r
129 * </code></pre></p>
\r
131 * <p><b><u>Performance Boost</u></b></p>
\r
132 * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
\r
133 * of DOM can significantly boost performance.</p>
\r
134 * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>,
\r
135 * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification
\r
136 * results in the creation of a text node. Usage:</p>
\r
138 Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance
\r
142 Ext.DomHelper = function(){
\r
143 var tempTableEl = null,
\r
144 emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i,
\r
145 tableRe = /^table|tbody|tr|td$/i,
\r
147 // kill repeat to save bytes
\r
148 afterbegin = 'afterbegin',
\r
149 afterend = 'afterend',
\r
150 beforebegin = 'beforebegin',
\r
151 beforeend = 'beforeend',
\r
154 tbs = ts+'<tbody>',
\r
155 tbe = '</tbody>'+te,
\r
156 trs = tbs + '<tr>',
\r
160 function doInsert(el, o, returnElement, pos, sibling, append){
\r
161 var newNode = pub.insertHtml(pos, Ext.getDom(el), createHtml(o));
\r
162 return returnElement ? Ext.get(newNode, true) : newNode;
\r
165 // build as innerHTML where available
\r
166 function createHtml(o){
\r
174 if(Ext.isString(o)){
\r
176 } else if (Ext.isArray(o)) {
\r
177 Ext.each(o, function(v) {
\r
178 b += createHtml(v);
\r
181 b += '<' + (o.tag = o.tag || 'div');
\r
182 Ext.iterate(o, function(attr, val){
\r
183 if(!/tag|children|cn|html$/i.test(attr)){
\r
184 if (Ext.isObject(val)) {
\r
185 b += ' ' + attr + '="';
\r
186 Ext.iterate(val, function(key, keyVal){
\r
187 b += key + ':' + keyVal + ';';
\r
191 b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"';
\r
195 // Now either just close the tag or try to add children and close the tag.
\r
196 if (emptyTags.test(o.tag)) {
\r
200 if ((cn = o.children || o.cn)) {
\r
201 b += createHtml(cn);
\r
205 b += '</' + o.tag + '>';
\r
211 function ieTable(depth, s, h, e){
\r
212 tempTableEl.innerHTML = [s, h, e].join('');
\r
216 while(++i < depth){
\r
217 el = el.firstChild;
\r
219 // If the result is multiple siblings, then encapsulate them into one fragment.
\r
220 if(ns = el.nextSibling){
\r
221 var df = document.createDocumentFragment();
\r
223 ns = el.nextSibling;
\r
224 df.appendChild(el);
\r
234 * Nasty code for IE's broken table implementation
\r
236 function insertIntoTable(tag, where, el, html) {
\r
240 tempTableEl = tempTableEl || document.createElement('div');
\r
242 if(tag == 'td' && (where == afterbegin || where == beforeend) ||
\r
243 !/td|tr|tbody/i.test(tag) && (where == beforebegin || where == afterend)) {
\r
246 before = where == beforebegin ? el :
\r
247 where == afterend ? el.nextSibling :
\r
248 where == afterbegin ? el.firstChild : null;
\r
250 if (where == beforebegin || where == afterend) {
\r
251 el = el.parentNode;
\r
254 if (tag == 'td' || (tag == 'tr' && (where == beforeend || where == afterbegin))) {
\r
255 node = ieTable(4, trs, html, tre);
\r
256 } else if ((tag == 'tbody' && (where == beforeend || where == afterbegin)) ||
\r
257 (tag == 'tr' && (where == beforebegin || where == afterend))) {
\r
258 node = ieTable(3, tbs, html, tbe);
\r
260 node = ieTable(2, ts, html, te);
\r
262 el.insertBefore(node, before);
\r
268 <div id="method-Ext.DomHelper-markup"></div>/**
\r
269 * Returns the markup for the passed Element(s) config.
\r
270 * @param {Object} o The DOM object spec (and children)
\r
273 markup : function(o){
\r
274 return createHtml(o);
\r
277 <div id="method-Ext.DomHelper-applyStyles"></div>/**
\r
278 * Applies a style specification to an element.
\r
279 * @param {String/HTMLElement} el The element to apply styles to
\r
280 * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or
\r
281 * a function which returns such a specification.
\r
283 applyStyles : function(el, styles){
\r
290 if(Ext.isFunction(styles)){
\r
291 styles = styles.call();
\r
293 if(Ext.isString(styles)){
\r
294 styles = styles.trim().split(/\s*(?::|;)\s*/);
\r
295 for(len = styles.length; i < len;){
\r
296 el.setStyle(styles[i++], styles[i++]);
\r
298 }else if (Ext.isObject(styles)){
\r
299 el.setStyle(styles);
\r
304 <div id="method-Ext.DomHelper-insertHtml"></div>/**
\r
305 * Inserts an HTML fragment into the DOM.
\r
306 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
\r
307 * @param {HTMLElement} el The context element
\r
308 * @param {String} html The HTML fragment
\r
309 * @return {HTMLElement} The new node
\r
311 insertHtml : function(where, el, html){
\r
320 where = where.toLowerCase();
\r
321 // add these here because they are used in both branches of the condition.
\r
322 hash[beforebegin] = ['BeforeBegin', 'previousSibling'];
\r
323 hash[afterend] = ['AfterEnd', 'nextSibling'];
\r
325 if (el.insertAdjacentHTML) {
\r
326 if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){
\r
329 // add these two to the hash.
\r
330 hash[afterbegin] = ['AfterBegin', 'firstChild'];
\r
331 hash[beforeend] = ['BeforeEnd', 'lastChild'];
\r
332 if ((hashVal = hash[where])) {
\r
333 el.insertAdjacentHTML(hashVal[0], html);
\r
334 return el[hashVal[1]];
\r
337 range = el.ownerDocument.createRange();
\r
338 setStart = 'setStart' + (/end/i.test(where) ? 'After' : 'Before');
\r
340 range[setStart](el);
\r
341 frag = range.createContextualFragment(html);
\r
342 el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling);
\r
343 return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling'];
\r
345 rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child';
\r
346 if (el.firstChild) {
\r
347 range[setStart](el[rangeEl]);
\r
348 frag = range.createContextualFragment(html);
\r
349 if(where == afterbegin){
\r
350 el.insertBefore(frag, el.firstChild);
\r
352 el.appendChild(frag);
\r
355 el.innerHTML = html;
\r
357 return el[rangeEl];
\r
360 throw 'Illegal insertion point -> "' + where + '"';
\r
363 <div id="method-Ext.DomHelper-insertBefore"></div>/**
\r
364 * Creates new DOM element(s) and inserts them before el.
\r
365 * @param {Mixed} el The context element
\r
366 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
367 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
368 * @return {HTMLElement/Ext.Element} The new node
\r
370 insertBefore : function(el, o, returnElement){
\r
371 return doInsert(el, o, returnElement, beforebegin);
\r
374 <div id="method-Ext.DomHelper-insertAfter"></div>/**
\r
375 * Creates new DOM element(s) and inserts them after el.
\r
376 * @param {Mixed} el The context element
\r
377 * @param {Object} o The DOM object spec (and children)
\r
378 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
379 * @return {HTMLElement/Ext.Element} The new node
\r
381 insertAfter : function(el, o, returnElement){
\r
382 return doInsert(el, o, returnElement, afterend, 'nextSibling');
\r
385 <div id="method-Ext.DomHelper-insertFirst"></div>/**
\r
386 * Creates new DOM element(s) and inserts them as the first child of el.
\r
387 * @param {Mixed} el The context element
\r
388 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
389 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
390 * @return {HTMLElement/Ext.Element} The new node
\r
392 insertFirst : function(el, o, returnElement){
\r
393 return doInsert(el, o, returnElement, afterbegin, 'firstChild');
\r
396 <div id="method-Ext.DomHelper-append"></div>/**
\r
397 * Creates new DOM element(s) and appends them to el.
\r
398 * @param {Mixed} el The context element
\r
399 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
400 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
401 * @return {HTMLElement/Ext.Element} The new node
\r
403 append : function(el, o, returnElement){
\r
404 return doInsert(el, o, returnElement, beforeend, '', true);
\r
407 <div id="method-Ext.DomHelper-overwrite"></div>/**
\r
408 * Creates new DOM element(s) and overwrites the contents of el with them.
\r
409 * @param {Mixed} el The context element
\r
410 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
411 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
412 * @return {HTMLElement/Ext.Element} The new node
\r
414 overwrite : function(el, o, returnElement){
\r
415 el = Ext.getDom(el);
\r
416 el.innerHTML = createHtml(o);
\r
417 return returnElement ? Ext.get(el.firstChild) : el.firstChild;
\r
420 createHtml : createHtml
\r