3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.DomHelper"></div>/**
\r
10 * @class Ext.DomHelper
\r
11 * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
\r
12 * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
\r
13 * from your DOM building code.</p>
\r
15 * <p><b><u>DomHelper element specification object</u></b></p>
\r
16 * <p>A specification object is used when creating elements. Attributes of this object
\r
17 * are assumed to be element attributes, except for 4 special attributes:
\r
18 * <div class="mdetail-params"><ul>
\r
19 * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
\r
20 * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
\r
21 * same kind of element definition objects to be created and appended. These can be nested
\r
22 * as deep as you want.</div></li>
\r
23 * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
\r
24 * This will end up being either the "class" attribute on a HTML fragment or className
\r
25 * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
\r
26 * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
\r
29 * <p><b><u>Insertion methods</u></b></p>
\r
30 * <p>Commonly used insertion methods:
\r
31 * <div class="mdetail-params"><ul>
\r
32 * <li><b><tt>{@link #append}</tt></b> : <div class="sub-desc"></div></li>
\r
33 * <li><b><tt>{@link #insertBefore}</tt></b> : <div class="sub-desc"></div></li>
\r
34 * <li><b><tt>{@link #insertAfter}</tt></b> : <div class="sub-desc"></div></li>
\r
35 * <li><b><tt>{@link #overwrite}</tt></b> : <div class="sub-desc"></div></li>
\r
36 * <li><b><tt>{@link #createTemplate}</tt></b> : <div class="sub-desc"></div></li>
\r
37 * <li><b><tt>{@link #insertHtml}</tt></b> : <div class="sub-desc"></div></li>
\r
40 * <p><b><u>Example</u></b></p>
\r
41 * <p>This is an example, where an unordered list with 3 children items is appended to an existing
\r
42 * element with id <tt>'my-div'</tt>:<br>
\r
44 var dh = Ext.DomHelper; // create shorthand alias
\r
45 // specification object
\r
50 // append children after creating
\r
51 children: [ // may also specify 'cn' instead of 'children'
\r
52 {tag: 'li', id: 'item0', html: 'List Item 0'},
\r
53 {tag: 'li', id: 'item1', html: 'List Item 1'},
\r
54 {tag: 'li', id: 'item2', html: 'List Item 2'}
\r
57 var list = dh.append(
\r
58 'my-div', // the context element 'my-div' can either be the id or the actual node
\r
59 spec // the specification object
\r
62 * <p>Element creation specification parameters in this class may also be passed as an Array of
\r
63 * specification objects. This can be used to insert multiple sibling nodes into an existing
\r
64 * container very efficiently. For example, to add more list items to the example above:<pre><code>
\r
65 dh.append('my-ul', [
\r
66 {tag: 'li', id: 'item3', html: 'List Item 3'},
\r
67 {tag: 'li', id: 'item4', html: 'List Item 4'}
\r
71 * <p><b><u>Templating</u></b></p>
\r
72 * <p>The real power is in the built-in templating. Instead of creating or appending any elements,
\r
73 * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to
\r
74 * insert new elements. Revisiting the example above, we could utilize templating this time:
\r
77 var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
\r
79 var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});
\r
81 for(var i = 0; i < 5, i++){
\r
82 tpl.append(list, [i]); // use template to append to the actual node
\r
85 * <p>An example using a template:<pre><code>
\r
86 var html = '<a id="{0}" href="{1}" class="nav">{2}</a>';
\r
88 var tpl = new Ext.DomHelper.createTemplate(html);
\r
89 tpl.append('blog-roll', ['link1', 'http://www.jackslocum.com/', "Jack's Site"]);
\r
90 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin's Site"]);
\r
93 * <p>The same example using named parameters:<pre><code>
\r
94 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
\r
96 var tpl = new Ext.DomHelper.createTemplate(html);
\r
97 tpl.append('blog-roll', {
\r
99 url: 'http://www.jackslocum.com/',
\r
100 text: "Jack's Site"
\r
102 tpl.append('blog-roll', {
\r
104 url: 'http://www.dustindiaz.com/',
\r
105 text: "Dustin's Site"
\r
107 * </code></pre></p>
\r
109 * <p><b><u>Compiling Templates</u></b></p>
\r
110 * <p>Templates are applied using regular expressions. The performance is great, but if
\r
111 * you are adding a bunch of DOM elements using the same template, you can increase
\r
112 * performance even further by {@link Ext.Template#compile "compiling"} the template.
\r
113 * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and
\r
114 * broken up at the different variable points and a dynamic function is created and eval'ed.
\r
115 * The generated function performs string concatenation of these parts and the passed
\r
116 * variables instead of using regular expressions.
\r
118 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
\r
120 var tpl = new Ext.DomHelper.createTemplate(html);
\r
123 //... use template like normal
\r
124 * </code></pre></p>
\r
126 * <p><b><u>Performance Boost</u></b></p>
\r
127 * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
\r
128 * of DOM can significantly boost performance.</p>
\r
129 * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>,
\r
130 * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification
\r
131 * results in the creation of a text node. Usage:</p>
\r
133 Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance
\r
137 Ext.DomHelper = function(){
\r
138 var tempTableEl = null,
\r
139 emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i,
\r
140 tableRe = /^table|tbody|tr|td$/i,
\r
142 // kill repeat to save bytes
\r
143 afterbegin = 'afterbegin',
\r
144 afterend = 'afterend',
\r
145 beforebegin = 'beforebegin',
\r
146 beforeend = 'beforeend',
\r
149 tbs = ts+'<tbody>',
\r
150 tbe = '</tbody>'+te,
\r
151 trs = tbs + '<tr>',
\r
155 function doInsert(el, o, returnElement, pos, sibling, append){
\r
156 var newNode = pub.insertHtml(pos, Ext.getDom(el), createHtml(o));
\r
157 return returnElement ? Ext.get(newNode, true) : newNode;
\r
160 // build as innerHTML where available
\r
161 function createHtml(o){
\r
169 if(Ext.isString(o)){
\r
171 } else if (Ext.isArray(o)) {
\r
172 for (var i=0; i < o.length; i++) {
\r
174 b += createHtml(o[i]);
\r
178 b += '<' + (o.tag = o.tag || 'div');
\r
179 Ext.iterate(o, function(attr, val){
\r
180 if(!/tag|children|cn|html$/i.test(attr)){
\r
181 if (Ext.isObject(val)) {
\r
182 b += ' ' + attr + '="';
\r
183 Ext.iterate(val, function(key, keyVal){
\r
184 b += key + ':' + keyVal + ';';
\r
188 b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"';
\r
192 // Now either just close the tag or try to add children and close the tag.
\r
193 if (emptyTags.test(o.tag)) {
\r
197 if ((cn = o.children || o.cn)) {
\r
198 b += createHtml(cn);
\r
202 b += '</' + o.tag + '>';
\r
208 function ieTable(depth, s, h, e){
\r
209 tempTableEl.innerHTML = [s, h, e].join('');
\r
213 while(++i < depth){
\r
214 el = el.firstChild;
\r
216 // If the result is multiple siblings, then encapsulate them into one fragment.
\r
217 if(ns = el.nextSibling){
\r
218 var df = document.createDocumentFragment();
\r
220 ns = el.nextSibling;
\r
221 df.appendChild(el);
\r
231 * Nasty code for IE's broken table implementation
\r
233 function insertIntoTable(tag, where, el, html) {
\r
237 tempTableEl = tempTableEl || document.createElement('div');
\r
239 if(tag == 'td' && (where == afterbegin || where == beforeend) ||
\r
240 !/td|tr|tbody/i.test(tag) && (where == beforebegin || where == afterend)) {
\r
243 before = where == beforebegin ? el :
\r
244 where == afterend ? el.nextSibling :
\r
245 where == afterbegin ? el.firstChild : null;
\r
247 if (where == beforebegin || where == afterend) {
\r
248 el = el.parentNode;
\r
251 if (tag == 'td' || (tag == 'tr' && (where == beforeend || where == afterbegin))) {
\r
252 node = ieTable(4, trs, html, tre);
\r
253 } else if ((tag == 'tbody' && (where == beforeend || where == afterbegin)) ||
\r
254 (tag == 'tr' && (where == beforebegin || where == afterend))) {
\r
255 node = ieTable(3, tbs, html, tbe);
\r
257 node = ieTable(2, ts, html, te);
\r
259 el.insertBefore(node, before);
\r
265 <div id="method-Ext.DomHelper-markup"></div>/**
\r
266 * Returns the markup for the passed Element(s) config.
\r
267 * @param {Object} o The DOM object spec (and children)
\r
270 markup : function(o){
\r
271 return createHtml(o);
\r
274 <div id="method-Ext.DomHelper-applyStyles"></div>/**
\r
275 * Applies a style specification to an element.
\r
276 * @param {String/HTMLElement} el The element to apply styles to
\r
277 * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or
\r
278 * a function which returns such a specification.
\r
280 applyStyles : function(el, styles){
\r
287 if(Ext.isFunction(styles)){
\r
288 styles = styles.call();
\r
290 if(Ext.isString(styles)){
\r
291 styles = styles.trim().split(/\s*(?::|;)\s*/);
\r
292 for(len = styles.length; i < len;){
\r
293 el.setStyle(styles[i++], styles[i++]);
\r
295 }else if (Ext.isObject(styles)){
\r
296 el.setStyle(styles);
\r
301 <div id="method-Ext.DomHelper-insertHtml"></div>/**
\r
302 * Inserts an HTML fragment into the DOM.
\r
303 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
\r
304 * @param {HTMLElement} el The context element
\r
305 * @param {String} html The HTML fragment
\r
306 * @return {HTMLElement} The new node
\r
308 insertHtml : function(where, el, html){
\r
317 where = where.toLowerCase();
\r
318 // add these here because they are used in both branches of the condition.
\r
319 hash[beforebegin] = ['BeforeBegin', 'previousSibling'];
\r
320 hash[afterend] = ['AfterEnd', 'nextSibling'];
\r
322 if (el.insertAdjacentHTML) {
\r
323 if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){
\r
326 // add these two to the hash.
\r
327 hash[afterbegin] = ['AfterBegin', 'firstChild'];
\r
328 hash[beforeend] = ['BeforeEnd', 'lastChild'];
\r
329 if ((hashVal = hash[where])) {
\r
330 el.insertAdjacentHTML(hashVal[0], html);
\r
331 return el[hashVal[1]];
\r
334 range = el.ownerDocument.createRange();
\r
335 setStart = 'setStart' + (/end/i.test(where) ? 'After' : 'Before');
\r
337 range[setStart](el);
\r
338 frag = range.createContextualFragment(html);
\r
339 el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling);
\r
340 return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling'];
\r
342 rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child';
\r
343 if (el.firstChild) {
\r
344 range[setStart](el[rangeEl]);
\r
345 frag = range.createContextualFragment(html);
\r
346 if(where == afterbegin){
\r
347 el.insertBefore(frag, el.firstChild);
\r
349 el.appendChild(frag);
\r
352 el.innerHTML = html;
\r
354 return el[rangeEl];
\r
357 throw 'Illegal insertion point -> "' + where + '"';
\r
360 <div id="method-Ext.DomHelper-insertBefore"></div>/**
\r
361 * Creates new DOM element(s) and inserts them before el.
\r
362 * @param {Mixed} el The context element
\r
363 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
364 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
365 * @return {HTMLElement/Ext.Element} The new node
\r
367 insertBefore : function(el, o, returnElement){
\r
368 return doInsert(el, o, returnElement, beforebegin);
\r
371 <div id="method-Ext.DomHelper-insertAfter"></div>/**
\r
372 * Creates new DOM element(s) and inserts them after el.
\r
373 * @param {Mixed} el The context element
\r
374 * @param {Object} o The DOM object spec (and children)
\r
375 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
376 * @return {HTMLElement/Ext.Element} The new node
\r
378 insertAfter : function(el, o, returnElement){
\r
379 return doInsert(el, o, returnElement, afterend, 'nextSibling');
\r
382 <div id="method-Ext.DomHelper-insertFirst"></div>/**
\r
383 * Creates new DOM element(s) and inserts them as the first child of el.
\r
384 * @param {Mixed} el The context element
\r
385 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
386 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
387 * @return {HTMLElement/Ext.Element} The new node
\r
389 insertFirst : function(el, o, returnElement){
\r
390 return doInsert(el, o, returnElement, afterbegin, 'firstChild');
\r
393 <div id="method-Ext.DomHelper-append"></div>/**
\r
394 * Creates new DOM element(s) and appends them to el.
\r
395 * @param {Mixed} el The context element
\r
396 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
397 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
398 * @return {HTMLElement/Ext.Element} The new node
\r
400 append : function(el, o, returnElement){
\r
401 return doInsert(el, o, returnElement, beforeend, '', true);
\r
404 <div id="method-Ext.DomHelper-overwrite"></div>/**
\r
405 * Creates new DOM element(s) and overwrites the contents of el with them.
\r
406 * @param {Mixed} el The context element
\r
407 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
\r
408 * @param {Boolean} returnElement (optional) true to return a Ext.Element
\r
409 * @return {HTMLElement/Ext.Element} The new node
\r
411 overwrite : function(el, o, returnElement){
\r
412 el = Ext.getDom(el);
\r
413 el.innerHTML = createHtml(o);
\r
414 return returnElement ? Ext.get(el.firstChild) : el.firstChild;
\r
417 createHtml : createHtml
\r