3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.DomHelper"></div>/**
10 * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
11 * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
12 * from your DOM building code.</p>
14 * <p><b><u>DomHelper element specification object</u></b></p>
15 * <p>A specification object is used when creating elements. Attributes of this object
16 * are assumed to be element attributes, except for 4 special attributes:
17 * <div class="mdetail-params"><ul>
18 * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
19 * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
20 * same kind of element definition objects to be created and appended. These can be nested
21 * as deep as you want.</div></li>
22 * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
23 * This will end up being either the "class" attribute on a HTML fragment or className
24 * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
25 * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
28 * <p><b><u>Insertion methods</u></b></p>
29 * <p>Commonly used insertion methods:
30 * <div class="mdetail-params"><ul>
31 * <li><b><tt>{@link #append}</tt></b> : <div class="sub-desc"></div></li>
32 * <li><b><tt>{@link #insertBefore}</tt></b> : <div class="sub-desc"></div></li>
33 * <li><b><tt>{@link #insertAfter}</tt></b> : <div class="sub-desc"></div></li>
34 * <li><b><tt>{@link #overwrite}</tt></b> : <div class="sub-desc"></div></li>
35 * <li><b><tt>{@link #createTemplate}</tt></b> : <div class="sub-desc"></div></li>
36 * <li><b><tt>{@link #insertHtml}</tt></b> : <div class="sub-desc"></div></li>
39 * <p><b><u>Example</u></b></p>
40 * <p>This is an example, where an unordered list with 3 children items is appended to an existing
41 * element with id <tt>'my-div'</tt>:<br>
43 var dh = Ext.DomHelper; // create shorthand alias
44 // specification object
49 // append children after creating
50 children: [ // may also specify 'cn' instead of 'children'
51 {tag: 'li', id: 'item0', html: 'List Item 0'},
52 {tag: 'li', id: 'item1', html: 'List Item 1'},
53 {tag: 'li', id: 'item2', html: 'List Item 2'}
57 'my-div', // the context element 'my-div' can either be the id or the actual node
58 spec // the specification object
61 * <p>Element creation specification parameters in this class may also be passed as an Array of
62 * specification objects. This can be used to insert multiple sibling nodes into an existing
63 * container very efficiently. For example, to add more list items to the example above:<pre><code>
65 {tag: 'li', id: 'item3', html: 'List Item 3'},
66 {tag: 'li', id: 'item4', html: 'List Item 4'}
70 * <p><b><u>Templating</u></b></p>
71 * <p>The real power is in the built-in templating. Instead of creating or appending any elements,
72 * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to
73 * insert new elements. Revisiting the example above, we could utilize templating this time:
76 var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
78 var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});
80 for(var i = 0; i < 5, i++){
81 tpl.append(list, [i]); // use template to append to the actual node
84 * <p>An example using a template:<pre><code>
85 var html = '<a id="{0}" href="{1}" class="nav">{2}</a>';
87 var tpl = new Ext.DomHelper.createTemplate(html);
88 tpl.append('blog-roll', ['link1', 'http://www.jackslocum.com/', "Jack's Site"]);
89 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin's Site"]);
92 * <p>The same example using named parameters:<pre><code>
93 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
95 var tpl = new Ext.DomHelper.createTemplate(html);
96 tpl.append('blog-roll', {
98 url: 'http://www.jackslocum.com/',
99 text: "Jack's Site"
101 tpl.append('blog-roll', {
103 url: 'http://www.dustindiaz.com/',
104 text: "Dustin's Site"
108 * <p><b><u>Compiling Templates</u></b></p>
109 * <p>Templates are applied using regular expressions. The performance is great, but if
110 * you are adding a bunch of DOM elements using the same template, you can increase
111 * performance even further by {@link Ext.Template#compile "compiling"} the template.
112 * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and
113 * broken up at the different variable points and a dynamic function is created and eval'ed.
114 * The generated function performs string concatenation of these parts and the passed
115 * variables instead of using regular expressions.
117 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
119 var tpl = new Ext.DomHelper.createTemplate(html);
122 //... use template like normal
125 * <p><b><u>Performance Boost</u></b></p>
126 * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
127 * of DOM can significantly boost performance.</p>
128 * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>,
129 * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification
130 * results in the creation of a text node. Usage:</p>
132 Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance
136 Ext.DomHelper = function(){
137 var tempTableEl = null,
138 emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i,
139 tableRe = /^table|tbody|tr|td$/i,
141 // kill repeat to save bytes
142 afterbegin = "afterbegin",
143 afterend = "afterend",
144 beforebegin = "beforebegin",
145 beforeend = "beforeend",
154 function doInsert(el, o, returnElement, pos, sibling, append){
155 var newNode = pub.insertHtml(pos, Ext.getDom(el), createHtml(o));
156 return returnElement ? Ext.get(newNode, true) : newNode;
159 // build as innerHTML where available
160 function createHtml(o){
168 if(typeof o == 'string'){
170 } else if (Ext.isArray(o)) {
171 Ext.each(o, function(v) {
175 b += "<" + (o.tag = o.tag || "div");
176 Ext.iterate(o, function(attr, val){
177 if(!/tag|children|cn|html$/i.test(attr)){
178 if (Ext.isObject(val)) {
179 b += " " + attr + "='";
180 Ext.iterate(val, function(key, keyVal){
181 b += key + ":" + keyVal + ";";
185 b += " " + ({cls : "class", htmlFor : "for"}[attr] || attr) + "='" + val + "'";
189 // Now either just close the tag or try to add children and close the tag.
190 if (emptyTags.test(o.tag)) {
194 if ((cn = o.children || o.cn)) {
199 b += "</" + o.tag + ">";
205 function ieTable(depth, s, h, e){
206 tempTableEl.innerHTML = [s, h, e].join('');
217 * Nasty code for IE's broken table implementation
219 function insertIntoTable(tag, where, el, html) {
223 tempTableEl = tempTableEl || document.createElement('div');
225 if(tag == 'td' && (where == afterbegin || where == beforeend) ||
226 !/td|tr|tbody/i.test(tag) && (where == beforebegin || where == afterend)) {
229 before = where == beforebegin ? el :
230 where == afterend ? el.nextSibling :
231 where == afterbegin ? el.firstChild : null;
233 if (where == beforebegin || where == afterend) {
237 if (tag == 'td' || (tag == "tr" && (where == beforeend || where == afterbegin))) {
238 node = ieTable(4, trs, html, tre);
239 } else if ((tag == "tbody" && (where == beforeend || where == afterbegin)) ||
240 (tag == "tr" && (where == beforebegin || where == afterend))) {
241 node = ieTable(3, tbs, html, tbe);
243 node = ieTable(2, ts, html, te);
245 el.insertBefore(node, before);
251 <div id="method-Ext.DomHelper-markup"></div>/**
252 * Returns the markup for the passed Element(s) config.
253 * @param {Object} o The DOM object spec (and children)
256 markup : function(o){
257 return createHtml(o);
260 <div id="method-Ext.DomHelper-insertHtml"></div>/**
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){
276 where = where.toLowerCase();
277 // add these here because they are used in both branches of the condition.
278 hash[beforebegin] = ['BeforeBegin', 'previousSibling'];
279 hash[afterend] = ['AfterEnd', 'nextSibling'];
281 if (el.insertAdjacentHTML) {
282 if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){
285 // add these two to the hash.
286 hash[afterbegin] = ['AfterBegin', 'firstChild'];
287 hash[beforeend] = ['BeforeEnd', 'lastChild'];
288 if ((hashVal = hash[where])) {
289 el.insertAdjacentHTML(hashVal[0], html);
290 return el[hashVal[1]];
293 range = el.ownerDocument.createRange();
294 setStart = "setStart" + (/end/i.test(where) ? "After" : "Before");
297 frag = range.createContextualFragment(html);
298 el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling);
299 return el[(where == beforebegin ? "previous" : "next") + "Sibling"];
301 rangeEl = (where == afterbegin ? "first" : "last") + "Child";
303 range[setStart](el[rangeEl]);
304 frag = range.createContextualFragment(html);
305 if(where == afterbegin){
306 el.insertBefore(frag, el.firstChild);
308 el.appendChild(frag);
316 throw 'Illegal insertion point -> "' + where + '"';
319 <div id="method-Ext.DomHelper-insertBefore"></div>/**
320 * Creates new DOM element(s) and inserts them before el.
321 * @param {Mixed} el The context element
322 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
323 * @param {Boolean} returnElement (optional) true to return a Ext.Element
324 * @return {HTMLElement/Ext.Element} The new node
326 insertBefore : function(el, o, returnElement){
327 return doInsert(el, o, returnElement, beforebegin);
330 <div id="method-Ext.DomHelper-insertAfter"></div>/**
331 * Creates new DOM element(s) and inserts them after el.
332 * @param {Mixed} el The context element
333 * @param {Object} o The DOM object spec (and children)
334 * @param {Boolean} returnElement (optional) true to return a Ext.Element
335 * @return {HTMLElement/Ext.Element} The new node
337 insertAfter : function(el, o, returnElement){
338 return doInsert(el, o, returnElement, afterend, "nextSibling");
341 <div id="method-Ext.DomHelper-insertFirst"></div>/**
342 * Creates new DOM element(s) and inserts them as the first child of el.
343 * @param {Mixed} el The context element
344 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
345 * @param {Boolean} returnElement (optional) true to return a Ext.Element
346 * @return {HTMLElement/Ext.Element} The new node
348 insertFirst : function(el, o, returnElement){
349 return doInsert(el, o, returnElement, afterbegin, "firstChild");
352 <div id="method-Ext.DomHelper-append"></div>/**
353 * Creates new DOM element(s) and appends them to el.
354 * @param {Mixed} el The context element
355 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
356 * @param {Boolean} returnElement (optional) true to return a Ext.Element
357 * @return {HTMLElement/Ext.Element} The new node
359 append : function(el, o, returnElement){
360 return doInsert(el, o, returnElement, beforeend, "", true);
363 <div id="method-Ext.DomHelper-overwrite"></div>/**
364 * Creates new DOM element(s) and overwrites the contents of el with them.
365 * @param {Mixed} el The context element
366 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
367 * @param {Boolean} returnElement (optional) true to return a Ext.Element
368 * @return {HTMLElement/Ext.Element} The new node
370 overwrite : function(el, o, returnElement){
372 el.innerHTML = createHtml(o);
373 return returnElement ? Ext.get(el.firstChild) : el.firstChild;
376 createHtml : createHtml