1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-core.DomHelper'>/**
2 </span> * @class Ext.core.DomHelper
3 * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
4 * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
5 * from your DOM building code.</p>
7 * <p><b><u>DomHelper element specification object</u></b></p>
8 * <p>A specification object is used when creating elements. Attributes of this object
9 * are assumed to be element attributes, except for 4 special attributes:
10 * <div class="mdetail-params"><ul>
11 * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
12 * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
13 * same kind of element definition objects to be created and appended. These can be nested
14 * as deep as you want.</div></li>
15 * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
16 * This will end up being either the "class" attribute on a HTML fragment or className
17 * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
18 * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
19 * </ul></div></p>
21 * <p><b><u>Insertion methods</u></b></p>
22 * <p>Commonly used insertion methods:
23 * <div class="mdetail-params"><ul>
24 * <li><b><tt>{@link #append}</tt></b> : <div class="sub-desc"></div></li>
25 * <li><b><tt>{@link #insertBefore}</tt></b> : <div class="sub-desc"></div></li>
26 * <li><b><tt>{@link #insertAfter}</tt></b> : <div class="sub-desc"></div></li>
27 * <li><b><tt>{@link #overwrite}</tt></b> : <div class="sub-desc"></div></li>
28 * <li><b><tt>{@link #createTemplate}</tt></b> : <div class="sub-desc"></div></li>
29 * <li><b><tt>{@link #insertHtml}</tt></b> : <div class="sub-desc"></div></li>
30 * </ul></div></p>
32 * <p><b><u>Example</u></b></p>
33 * <p>This is an example, where an unordered list with 3 children items is appended to an existing
34 * element with id <tt>'my-div'</tt>:<br>
35 <pre><code>
36 var dh = Ext.core.DomHelper; // create shorthand alias
37 // specification object
42 // append children after creating
43 children: [ // may also specify 'cn' instead of 'children'
44 {tag: 'li', id: 'item0', html: 'List Item 0'},
45 {tag: 'li', id: 'item1', html: 'List Item 1'},
46 {tag: 'li', id: 'item2', html: 'List Item 2'}
50 'my-div', // the context element 'my-div' can either be the id or the actual node
51 spec // the specification object
53 </code></pre></p>
54 * <p>Element creation specification parameters in this class may also be passed as an Array of
55 * specification objects. This can be used to insert multiple sibling nodes into an existing
56 * container very efficiently. For example, to add more list items to the example above:<pre><code>
58 {tag: 'li', id: 'item3', html: 'List Item 3'},
59 {tag: 'li', id: 'item4', html: 'List Item 4'}
61 * </code></pre></p>
63 * <p><b><u>Templating</u></b></p>
64 * <p>The real power is in the built-in templating. Instead of creating or appending any elements,
65 * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to
66 * insert new elements. Revisiting the example above, we could utilize templating this time:
67 * <pre><code>
69 var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
71 var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});
73 for(var i = 0; i < 5, i++){
74 tpl.append(list, [i]); // use template to append to the actual node
76 * </code></pre></p>
77 * <p>An example using a template:<pre><code>
78 var html = '<a id="{0}" href="{1}" class="nav">{2}</a>';
80 var tpl = new Ext.core.DomHelper.createTemplate(html);
81 tpl.append('blog-roll', ['link1', 'http://www.edspencer.net/', "Ed&#39;s Site"]);
82 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin&#39;s Site"]);
83 * </code></pre></p>
85 * <p>The same example using named parameters:<pre><code>
86 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
88 var tpl = new Ext.core.DomHelper.createTemplate(html);
89 tpl.append('blog-roll', {
91 url: 'http://www.edspencer.net/',
92 text: "Ed&#39;s Site"
94 tpl.append('blog-roll', {
96 url: 'http://www.dustindiaz.com/',
97 text: "Dustin&#39;s Site"
99 * </code></pre></p>
101 * <p><b><u>Compiling Templates</u></b></p>
102 * <p>Templates are applied using regular expressions. The performance is great, but if
103 * you are adding a bunch of DOM elements using the same template, you can increase
104 * performance even further by {@link Ext.Template#compile "compiling"} the template.
105 * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and
106 * broken up at the different variable points and a dynamic function is created and eval'ed.
107 * The generated function performs string concatenation of these parts and the passed
108 * variables instead of using regular expressions.
109 * <pre><code>
110 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
112 var tpl = new Ext.core.DomHelper.createTemplate(html);
115 //... use template like normal
116 * </code></pre></p>
118 * <p><b><u>Performance Boost</u></b></p>
119 * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
120 * of DOM can significantly boost performance.</p>
121 * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>,
122 * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification
123 * results in the creation of a text node. Usage:</p>
124 * <pre><code>
125 Ext.core.DomHelper.useDom = true; // force it to use DOM; reduces performance
126 * </code></pre>
130 Ext.core.DomHelper = function(){
131 var tempTableEl = null,
132 emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i,
133 tableRe = /^table|tbody|tr|td$/i,
134 confRe = /tag|children|cn|html$/i,
135 tableElRe = /td|tr|tbody/i,
138 // kill repeat to save bytes
139 afterbegin = 'afterbegin',
140 afterend = 'afterend',
141 beforebegin = 'beforebegin',
142 beforeend = 'beforeend',
143 ts = '<table>',
144 te = '</table>',
145 tbs = ts+'<tbody>',
146 tbe = '</tbody>'+te,
147 trs = tbs + '<tr>',
148 tre = '</tr>'+tbe;
151 function doInsert(el, o, returnElement, pos, sibling, append){
155 newNode = createDom(o, null);
157 el.appendChild(newNode);
159 (sibling == 'firstChild' ? el : el.parentNode).insertBefore(newNode, el[sibling] || el);
162 newNode = Ext.core.DomHelper.insertHtml(pos, el, Ext.core.DomHelper.createHtml(o));
164 return returnElement ? Ext.get(newNode, true) : newNode;
167 function createDom(o, parentNode){
175 if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted
176 el = doc.createDocumentFragment(); // in one shot using a DocumentFragment
177 for (var i = 0, l = o.length; i < l; i++) {
180 } else if (typeof o == 'string') { // Allow a string as a child spec.
181 el = doc.createTextNode(o);
183 el = doc.createElement( o.tag || 'div' );
184 useSet = !!el.setAttribute; // In IE some elements don't have setAttribute
186 if(!confRe.test(attr)){
192 el.setAttribute(attr, val);
199 Ext.core.DomHelper.applyStyles(el, o.style);
201 if ((cn = o.children || o.cn)) {
204 el.innerHTML = o.html;
208 parentNode.appendChild(el);
213 // build as innerHTML where available
214 function createHtml(o){
222 if(typeof o == "string"){
224 } else if (Ext.isArray(o)) {
225 for (i=0; i < o.length; i++) {
227 b += createHtml(o[i]);
231 b += '<' + (o.tag = o.tag || 'div');
234 if(!confRe.test(attr)){
235 if (typeof val == "object") {
236 b += ' ' + attr + '="';
238 b += key + ':' + val[key] + ';';
242 b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"';
246 // Now either just close the tag or try to add children and close the tag.
247 if (emptyTags.test(o.tag)) {
251 if ((cn = o.children || o.cn)) {
256 b += '</' + o.tag + '>';
262 function ieTable(depth, s, h, e){
263 tempTableEl.innerHTML = [s, h, e].join('');
267 while(++i < depth){
270 // If the result is multiple siblings, then encapsulate them into one fragment.
273 var df = document.createDocumentFragment();
284 <span id='Ext-core.DomHelper-method-insertIntoTable'> /**
286 * Nasty code for IE's broken table implementation
288 function insertIntoTable(tag, where, el, html) {
292 tempTableEl = tempTableEl || document.createElement('div');
294 if(tag == 'td' && (where == afterbegin || where == beforeend) ||
295 !tableElRe.test(tag) && (where == beforebegin || where == afterend)) {
298 before = where == beforebegin ? el :
299 where == afterend ? el.nextSibling :
300 where == afterbegin ? el.firstChild : null;
302 if (where == beforebegin || where == afterend) {
306 if (tag == 'td' || (tag == 'tr' && (where == beforeend || where == afterbegin))) {
307 node = ieTable(4, trs, html, tre);
308 } else if ((tag == 'tbody' && (where == beforeend || where == afterbegin)) ||
309 (tag == 'tr' && (where == beforebegin || where == afterend))) {
310 node = ieTable(3, tbs, html, tbe);
312 node = ieTable(2, ts, html, te);
314 el.insertBefore(node, before);
318 <span id='Ext-core.DomHelper-method-createContextualFragment'> /**
320 * Fix for IE9 createContextualFragment missing method
322 function createContextualFragment(html){
323 var div = document.createElement("div"),
324 fragment = document.createDocumentFragment(),
328 div.innerHTML = html;
329 childNodes = div.childNodes;
330 length = childNodes.length;
332 for (; i < length; i++) {
333 fragment.appendChild(childNodes[i].cloneNode(true));
340 <span id='Ext-core.DomHelper-method-markup'> /**
341 </span> * Returns the markup for the passed Element(s) config.
342 * @param {Object} o The DOM object spec (and children)
345 markup : function(o){
346 return createHtml(o);
349 <span id='Ext-core.DomHelper-method-applyStyles'> /**
350 </span> * Applies a style specification to an element.
351 * @param {String/HTMLElement} el The element to apply styles to
352 * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or
353 * a function which returns such a specification.
355 applyStyles : function(el, styles){
358 if (typeof styles == "function") {
359 styles = styles.call();
361 if (typeof styles == "string") {
362 styles = Ext.core.Element.parseStyles(styles);
364 if (typeof styles == "object") {
370 <span id='Ext-core.DomHelper-method-insertHtml'> /**
371 </span> * Inserts an HTML fragment into the DOM.
372 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
373 * @param {HTMLElement/TextNode} el The context element
374 * @param {String} html The HTML fragment
375 * @return {HTMLElement} The new node
377 insertHtml : function(where, el, html){
386 where = where.toLowerCase();
387 // add these here because they are used in both branches of the condition.
388 hash[beforebegin] = ['BeforeBegin', 'previousSibling'];
389 hash[afterend] = ['AfterEnd', 'nextSibling'];
391 // if IE and context element is an HTMLElement
392 if (el.insertAdjacentHTML) {
393 if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){
397 // add these two to the hash.
398 hash[afterbegin] = ['AfterBegin', 'firstChild'];
399 hash[beforeend] = ['BeforeEnd', 'lastChild'];
400 if ((hashVal = hash[where])) {
401 el.insertAdjacentHTML(hashVal[0], html);
402 return el[hashVal[1]];
404 // if (not IE and context element is an HTMLElement) or TextNode
406 // we cannot insert anything inside a textnode so...
407 if (Ext.isTextNode(el)) {
408 where = where === 'afterbegin' ? 'beforebegin' : where;
409 where = where === 'beforeend' ? 'afterend' : where;
411 range = Ext.supports.CreateContextualFragment ? el.ownerDocument.createRange() : undefined;
412 setStart = 'setStart' + (endRe.test(where) ? 'After' : 'Before');
416 frag = range.createContextualFragment(html);
418 frag = createContextualFragment(html);
420 el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling);
421 return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling'];
423 rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child';
426 range[setStart](el[rangeEl]);
427 frag = range.createContextualFragment(html);
429 frag = createContextualFragment(html);
432 if(where == afterbegin){
433 el.insertBefore(frag, el.firstChild);
435 el.appendChild(frag);
445 sourceClass: 'Ext.core.DomHelper',
446 sourceMethod: 'insertHtml',
449 msg: 'Illegal insertion point reached: "' + where + '"'
454 <span id='Ext-core.DomHelper-method-insertBefore'> /**
455 </span> * Creates new DOM element(s) and inserts them before el.
456 * @param {Mixed} el The context element
457 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
458 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element
459 * @return {HTMLElement/Ext.core.Element} The new node
461 insertBefore : function(el, o, returnElement){
462 return doInsert(el, o, returnElement, beforebegin);
465 <span id='Ext-core.DomHelper-method-insertAfter'> /**
466 </span> * Creates new DOM element(s) and inserts them after el.
467 * @param {Mixed} el The context element
468 * @param {Object} o The DOM object spec (and children)
469 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element
470 * @return {HTMLElement/Ext.core.Element} The new node
472 insertAfter : function(el, o, returnElement){
473 return doInsert(el, o, returnElement, afterend, 'nextSibling');
476 <span id='Ext-core.DomHelper-method-insertFirst'> /**
477 </span> * Creates new DOM element(s) and inserts them as the first child of el.
478 * @param {Mixed} el The context element
479 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
480 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element
481 * @return {HTMLElement/Ext.core.Element} The new node
483 insertFirst : function(el, o, returnElement){
484 return doInsert(el, o, returnElement, afterbegin, 'firstChild');
487 <span id='Ext-core.DomHelper-method-append'> /**
488 </span> * Creates new DOM element(s) and appends them to el.
489 * @param {Mixed} el The context element
490 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
491 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element
492 * @return {HTMLElement/Ext.core.Element} The new node
494 append : function(el, o, returnElement){
495 return doInsert(el, o, returnElement, beforeend, '', true);
498 <span id='Ext-core.DomHelper-method-overwrite'> /**
499 </span> * Creates new DOM element(s) and overwrites the contents of el with them.
500 * @param {Mixed} el The context element
501 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
502 * @param {Boolean} returnElement (optional) true to return a Ext.core.Element
503 * @return {HTMLElement/Ext.core.Element} The new node
505 overwrite : function(el, o, returnElement){
507 el.innerHTML = createHtml(o);
508 return returnElement ? Ext.get(el.firstChild) : el.firstChild;
511 createHtml : createHtml,
513 <span id='Ext-core.DomHelper-property-createDom'> /**
514 </span> * Creates new DOM element(s) without inserting them to the document.
515 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
516 * @return {HTMLElement} The new uninserted node
518 createDom: createDom,
520 <span id='Ext-core.DomHelper-property-useDom'> /** True to force the use of DOM instead of html fragments @type Boolean */
521 </span> useDom : false,
523 <span id='Ext-core.DomHelper-method-createTemplate'> /**
524 </span> * Creates a new Ext.Template from the DOM object spec.
525 * @param {Object} o The DOM object spec (and children)
526 * @return {Ext.Template} The new template
528 createTemplate : function(o){
529 var html = Ext.core.DomHelper.createHtml(o);
530 return Ext.create('Ext.Template', html);
535 </pre></pre></body></html>