1 | /*! |
---|
2 | * Ext JS Library 3.4.0 |
---|
3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
4 | * licensing@sencha.com |
---|
5 | * http://www.sencha.com/license |
---|
6 | */ |
---|
7 | /** |
---|
8 | * @class Ext.DomHelper |
---|
9 | * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating |
---|
10 | * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates |
---|
11 | * from your DOM building code.</p> |
---|
12 | * |
---|
13 | * <p><b><u>DomHelper element specification object</u></b></p> |
---|
14 | * <p>A specification object is used when creating elements. Attributes of this object |
---|
15 | * are assumed to be element attributes, except for 4 special attributes: |
---|
16 | * <div class="mdetail-params"><ul> |
---|
17 | * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li> |
---|
18 | * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the |
---|
19 | * same kind of element definition objects to be created and appended. These can be nested |
---|
20 | * as deep as you want.</div></li> |
---|
21 | * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element. |
---|
22 | * This will end up being either the "class" attribute on a HTML fragment or className |
---|
23 | * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li> |
---|
24 | * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li> |
---|
25 | * </ul></div></p> |
---|
26 | * |
---|
27 | * <p><b><u>Insertion methods</u></b></p> |
---|
28 | * <p>Commonly used insertion methods: |
---|
29 | * <div class="mdetail-params"><ul> |
---|
30 | * <li><b><tt>{@link #append}</tt></b> : <div class="sub-desc"></div></li> |
---|
31 | * <li><b><tt>{@link #insertBefore}</tt></b> : <div class="sub-desc"></div></li> |
---|
32 | * <li><b><tt>{@link #insertAfter}</tt></b> : <div class="sub-desc"></div></li> |
---|
33 | * <li><b><tt>{@link #overwrite}</tt></b> : <div class="sub-desc"></div></li> |
---|
34 | * <li><b><tt>{@link #createTemplate}</tt></b> : <div class="sub-desc"></div></li> |
---|
35 | * <li><b><tt>{@link #insertHtml}</tt></b> : <div class="sub-desc"></div></li> |
---|
36 | * </ul></div></p> |
---|
37 | * |
---|
38 | * <p><b><u>Example</u></b></p> |
---|
39 | * <p>This is an example, where an unordered list with 3 children items is appended to an existing |
---|
40 | * element with id <tt>'my-div'</tt>:<br> |
---|
41 | <pre><code> |
---|
42 | var dh = Ext.DomHelper; // create shorthand alias |
---|
43 | // specification object |
---|
44 | var spec = { |
---|
45 | id: 'my-ul', |
---|
46 | tag: 'ul', |
---|
47 | cls: 'my-list', |
---|
48 | // append children after creating |
---|
49 | children: [ // may also specify 'cn' instead of 'children' |
---|
50 | {tag: 'li', id: 'item0', html: 'List Item 0'}, |
---|
51 | {tag: 'li', id: 'item1', html: 'List Item 1'}, |
---|
52 | {tag: 'li', id: 'item2', html: 'List Item 2'} |
---|
53 | ] |
---|
54 | }; |
---|
55 | var list = dh.append( |
---|
56 | 'my-div', // the context element 'my-div' can either be the id or the actual node |
---|
57 | spec // the specification object |
---|
58 | ); |
---|
59 | </code></pre></p> |
---|
60 | * <p>Element creation specification parameters in this class may also be passed as an Array of |
---|
61 | * specification objects. This can be used to insert multiple sibling nodes into an existing |
---|
62 | * container very efficiently. For example, to add more list items to the example above:<pre><code> |
---|
63 | dh.append('my-ul', [ |
---|
64 | {tag: 'li', id: 'item3', html: 'List Item 3'}, |
---|
65 | {tag: 'li', id: 'item4', html: 'List Item 4'} |
---|
66 | ]); |
---|
67 | * </code></pre></p> |
---|
68 | * |
---|
69 | * <p><b><u>Templating</u></b></p> |
---|
70 | * <p>The real power is in the built-in templating. Instead of creating or appending any elements, |
---|
71 | * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to |
---|
72 | * insert new elements. Revisiting the example above, we could utilize templating this time: |
---|
73 | * <pre><code> |
---|
74 | // create the node |
---|
75 | var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'}); |
---|
76 | // get template |
---|
77 | var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'}); |
---|
78 | |
---|
79 | for(var i = 0; i < 5, i++){ |
---|
80 | tpl.append(list, [i]); // use template to append to the actual node |
---|
81 | } |
---|
82 | * </code></pre></p> |
---|
83 | * <p>An example using a template:<pre><code> |
---|
84 | var html = '<a id="{0}" href="{1}" class="nav">{2}</a>'; |
---|
85 | |
---|
86 | var tpl = new Ext.DomHelper.createTemplate(html); |
---|
87 | tpl.append('blog-roll', ['link1', 'http://www.jackslocum.com/', "Jack's Site"]); |
---|
88 | tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin's Site"]); |
---|
89 | * </code></pre></p> |
---|
90 | * |
---|
91 | * <p>The same example using named parameters:<pre><code> |
---|
92 | var html = '<a id="{id}" href="{url}" class="nav">{text}</a>'; |
---|
93 | |
---|
94 | var tpl = new Ext.DomHelper.createTemplate(html); |
---|
95 | tpl.append('blog-roll', { |
---|
96 | id: 'link1', |
---|
97 | url: 'http://www.jackslocum.com/', |
---|
98 | text: "Jack's Site" |
---|
99 | }); |
---|
100 | tpl.append('blog-roll', { |
---|
101 | id: 'link2', |
---|
102 | url: 'http://www.dustindiaz.com/', |
---|
103 | text: "Dustin's Site" |
---|
104 | }); |
---|
105 | * </code></pre></p> |
---|
106 | * |
---|
107 | * <p><b><u>Compiling Templates</u></b></p> |
---|
108 | * <p>Templates are applied using regular expressions. The performance is great, but if |
---|
109 | * you are adding a bunch of DOM elements using the same template, you can increase |
---|
110 | * performance even further by {@link Ext.Template#compile "compiling"} the template. |
---|
111 | * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and |
---|
112 | * broken up at the different variable points and a dynamic function is created and eval'ed. |
---|
113 | * The generated function performs string concatenation of these parts and the passed |
---|
114 | * variables instead of using regular expressions. |
---|
115 | * <pre><code> |
---|
116 | var html = '<a id="{id}" href="{url}" class="nav">{text}</a>'; |
---|
117 | |
---|
118 | var tpl = new Ext.DomHelper.createTemplate(html); |
---|
119 | tpl.compile(); |
---|
120 | |
---|
121 | //... use template like normal |
---|
122 | * </code></pre></p> |
---|
123 | * |
---|
124 | * <p><b><u>Performance Boost</u></b></p> |
---|
125 | * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead |
---|
126 | * of DOM can significantly boost performance.</p> |
---|
127 | * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>, |
---|
128 | * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification |
---|
129 | * results in the creation of a text node. Usage:</p> |
---|
130 | * <pre><code> |
---|
131 | Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance |
---|
132 | * </code></pre> |
---|
133 | * @singleton |
---|
134 | */ |
---|
135 | Ext.DomHelper = function(){ |
---|
136 | var tempTableEl = null, |
---|
137 | emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i, |
---|
138 | tableRe = /^table|tbody|tr|td$/i, |
---|
139 | confRe = /tag|children|cn|html$/i, |
---|
140 | tableElRe = /td|tr|tbody/i, |
---|
141 | cssRe = /([a-z0-9-]+)\s*:\s*([^;\s]+(?:\s*[^;\s]+)*);?/gi, |
---|
142 | endRe = /end/i, |
---|
143 | pub, |
---|
144 | // kill repeat to save bytes |
---|
145 | afterbegin = 'afterbegin', |
---|
146 | afterend = 'afterend', |
---|
147 | beforebegin = 'beforebegin', |
---|
148 | beforeend = 'beforeend', |
---|
149 | ts = '<table>', |
---|
150 | te = '</table>', |
---|
151 | tbs = ts+'<tbody>', |
---|
152 | tbe = '</tbody>'+te, |
---|
153 | trs = tbs + '<tr>', |
---|
154 | tre = '</tr>'+tbe; |
---|
155 | |
---|
156 | // private |
---|
157 | function doInsert(el, o, returnElement, pos, sibling, append){ |
---|
158 | var newNode = pub.insertHtml(pos, Ext.getDom(el), createHtml(o)); |
---|
159 | return returnElement ? Ext.get(newNode, true) : newNode; |
---|
160 | } |
---|
161 | |
---|
162 | // build as innerHTML where available |
---|
163 | function createHtml(o){ |
---|
164 | var b = '', |
---|
165 | attr, |
---|
166 | val, |
---|
167 | key, |
---|
168 | cn; |
---|
169 | |
---|
170 | if(typeof o == "string"){ |
---|
171 | b = o; |
---|
172 | } else if (Ext.isArray(o)) { |
---|
173 | for (var i=0; i < o.length; i++) { |
---|
174 | if(o[i]) { |
---|
175 | b += createHtml(o[i]); |
---|
176 | } |
---|
177 | }; |
---|
178 | } else { |
---|
179 | b += '<' + (o.tag = o.tag || 'div'); |
---|
180 | for (attr in o) { |
---|
181 | val = o[attr]; |
---|
182 | if(!confRe.test(attr)){ |
---|
183 | if (typeof val == "object") { |
---|
184 | b += ' ' + attr + '="'; |
---|
185 | for (key in val) { |
---|
186 | b += key + ':' + val[key] + ';'; |
---|
187 | }; |
---|
188 | b += '"'; |
---|
189 | }else{ |
---|
190 | b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"'; |
---|
191 | } |
---|
192 | } |
---|
193 | }; |
---|
194 | // Now either just close the tag or try to add children and close the tag. |
---|
195 | if (emptyTags.test(o.tag)) { |
---|
196 | b += '/>'; |
---|
197 | } else { |
---|
198 | b += '>'; |
---|
199 | if ((cn = o.children || o.cn)) { |
---|
200 | b += createHtml(cn); |
---|
201 | } else if(o.html){ |
---|
202 | b += o.html; |
---|
203 | } |
---|
204 | b += '</' + o.tag + '>'; |
---|
205 | } |
---|
206 | } |
---|
207 | return b; |
---|
208 | } |
---|
209 | |
---|
210 | function ieTable(depth, s, h, e){ |
---|
211 | tempTableEl.innerHTML = [s, h, e].join(''); |
---|
212 | var i = -1, |
---|
213 | el = tempTableEl, |
---|
214 | ns; |
---|
215 | while(++i < depth){ |
---|
216 | el = el.firstChild; |
---|
217 | } |
---|
218 | // If the result is multiple siblings, then encapsulate them into one fragment. |
---|
219 | if(ns = el.nextSibling){ |
---|
220 | var df = document.createDocumentFragment(); |
---|
221 | while(el){ |
---|
222 | ns = el.nextSibling; |
---|
223 | df.appendChild(el); |
---|
224 | el = ns; |
---|
225 | } |
---|
226 | el = df; |
---|
227 | } |
---|
228 | return el; |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * @ignore |
---|
233 | * Nasty code for IE's broken table implementation |
---|
234 | */ |
---|
235 | function insertIntoTable(tag, where, el, html) { |
---|
236 | var node, |
---|
237 | before; |
---|
238 | |
---|
239 | tempTableEl = tempTableEl || document.createElement('div'); |
---|
240 | |
---|
241 | if(tag == 'td' && (where == afterbegin || where == beforeend) || |
---|
242 | !tableElRe.test(tag) && (where == beforebegin || where == afterend)) { |
---|
243 | return; |
---|
244 | } |
---|
245 | before = where == beforebegin ? el : |
---|
246 | where == afterend ? el.nextSibling : |
---|
247 | where == afterbegin ? el.firstChild : null; |
---|
248 | |
---|
249 | if (where == beforebegin || where == afterend) { |
---|
250 | el = el.parentNode; |
---|
251 | } |
---|
252 | |
---|
253 | if (tag == 'td' || (tag == 'tr' && (where == beforeend || where == afterbegin))) { |
---|
254 | node = ieTable(4, trs, html, tre); |
---|
255 | } else if ((tag == 'tbody' && (where == beforeend || where == afterbegin)) || |
---|
256 | (tag == 'tr' && (where == beforebegin || where == afterend))) { |
---|
257 | node = ieTable(3, tbs, html, tbe); |
---|
258 | } else { |
---|
259 | node = ieTable(2, ts, html, te); |
---|
260 | } |
---|
261 | el.insertBefore(node, before); |
---|
262 | return node; |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * @ignore |
---|
267 | * Fix for IE9 createContextualFragment missing method |
---|
268 | */ |
---|
269 | function createContextualFragment(html){ |
---|
270 | var div = document.createElement("div"), |
---|
271 | fragment = document.createDocumentFragment(), |
---|
272 | i = 0, |
---|
273 | length, childNodes; |
---|
274 | |
---|
275 | div.innerHTML = html; |
---|
276 | childNodes = div.childNodes; |
---|
277 | length = childNodes.length; |
---|
278 | |
---|
279 | for (; i < length; i++) { |
---|
280 | fragment.appendChild(childNodes[i].cloneNode(true)); |
---|
281 | } |
---|
282 | |
---|
283 | return fragment; |
---|
284 | } |
---|
285 | |
---|
286 | pub = { |
---|
287 | /** |
---|
288 | * Returns the markup for the passed Element(s) config. |
---|
289 | * @param {Object} o The DOM object spec (and children) |
---|
290 | * @return {String} |
---|
291 | */ |
---|
292 | markup : function(o){ |
---|
293 | return createHtml(o); |
---|
294 | }, |
---|
295 | |
---|
296 | /** |
---|
297 | * Applies a style specification to an element. |
---|
298 | * @param {String/HTMLElement} el The element to apply styles to |
---|
299 | * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or |
---|
300 | * a function which returns such a specification. |
---|
301 | */ |
---|
302 | applyStyles : function(el, styles){ |
---|
303 | if (styles) { |
---|
304 | var matches; |
---|
305 | |
---|
306 | el = Ext.fly(el); |
---|
307 | if (typeof styles == "function") { |
---|
308 | styles = styles.call(); |
---|
309 | } |
---|
310 | if (typeof styles == "string") { |
---|
311 | /** |
---|
312 | * Since we're using the g flag on the regex, we need to set the lastIndex. |
---|
313 | * This automatically happens on some implementations, but not others, see: |
---|
314 | * http://stackoverflow.com/questions/2645273/javascript-regular-expression-literal-persists-between-function-calls |
---|
315 | * http://blog.stevenlevithan.com/archives/fixing-javascript-regexp |
---|
316 | */ |
---|
317 | cssRe.lastIndex = 0; |
---|
318 | while ((matches = cssRe.exec(styles))) { |
---|
319 | el.setStyle(matches[1], matches[2]); |
---|
320 | } |
---|
321 | } else if (typeof styles == "object") { |
---|
322 | el.setStyle(styles); |
---|
323 | } |
---|
324 | } |
---|
325 | }, |
---|
326 | /** |
---|
327 | * Inserts an HTML fragment into the DOM. |
---|
328 | * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd. |
---|
329 | * @param {HTMLElement} el The context element |
---|
330 | * @param {String} html The HTML fragment |
---|
331 | * @return {HTMLElement} The new node |
---|
332 | */ |
---|
333 | insertHtml : function(where, el, html){ |
---|
334 | var hash = {}, |
---|
335 | hashVal, |
---|
336 | range, |
---|
337 | rangeEl, |
---|
338 | setStart, |
---|
339 | frag, |
---|
340 | rs; |
---|
341 | |
---|
342 | where = where.toLowerCase(); |
---|
343 | // add these here because they are used in both branches of the condition. |
---|
344 | hash[beforebegin] = ['BeforeBegin', 'previousSibling']; |
---|
345 | hash[afterend] = ['AfterEnd', 'nextSibling']; |
---|
346 | |
---|
347 | if (el.insertAdjacentHTML) { |
---|
348 | if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){ |
---|
349 | return rs; |
---|
350 | } |
---|
351 | // add these two to the hash. |
---|
352 | hash[afterbegin] = ['AfterBegin', 'firstChild']; |
---|
353 | hash[beforeend] = ['BeforeEnd', 'lastChild']; |
---|
354 | if ((hashVal = hash[where])) { |
---|
355 | el.insertAdjacentHTML(hashVal[0], html); |
---|
356 | return el[hashVal[1]]; |
---|
357 | } |
---|
358 | } else { |
---|
359 | range = el.ownerDocument.createRange(); |
---|
360 | setStart = 'setStart' + (endRe.test(where) ? 'After' : 'Before'); |
---|
361 | if (hash[where]) { |
---|
362 | range[setStart](el); |
---|
363 | if (!range.createContextualFragment) { |
---|
364 | frag = createContextualFragment(html); |
---|
365 | } |
---|
366 | else { |
---|
367 | frag = range.createContextualFragment(html); |
---|
368 | } |
---|
369 | el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling); |
---|
370 | return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling']; |
---|
371 | } else { |
---|
372 | rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child'; |
---|
373 | if (el.firstChild) { |
---|
374 | range[setStart](el[rangeEl]); |
---|
375 | if (!range.createContextualFragment) { |
---|
376 | frag = createContextualFragment(html); |
---|
377 | } |
---|
378 | else { |
---|
379 | frag = range.createContextualFragment(html); |
---|
380 | } |
---|
381 | if(where == afterbegin){ |
---|
382 | el.insertBefore(frag, el.firstChild); |
---|
383 | }else{ |
---|
384 | el.appendChild(frag); |
---|
385 | } |
---|
386 | } else { |
---|
387 | el.innerHTML = html; |
---|
388 | } |
---|
389 | return el[rangeEl]; |
---|
390 | } |
---|
391 | } |
---|
392 | throw 'Illegal insertion point -> "' + where + '"'; |
---|
393 | }, |
---|
394 | |
---|
395 | /** |
---|
396 | * Creates new DOM element(s) and inserts them before el. |
---|
397 | * @param {Mixed} el The context element |
---|
398 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
399 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
400 | * @return {HTMLElement/Ext.Element} The new node |
---|
401 | */ |
---|
402 | insertBefore : function(el, o, returnElement){ |
---|
403 | return doInsert(el, o, returnElement, beforebegin); |
---|
404 | }, |
---|
405 | |
---|
406 | /** |
---|
407 | * Creates new DOM element(s) and inserts them after el. |
---|
408 | * @param {Mixed} el The context element |
---|
409 | * @param {Object} o The DOM object spec (and children) |
---|
410 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
411 | * @return {HTMLElement/Ext.Element} The new node |
---|
412 | */ |
---|
413 | insertAfter : function(el, o, returnElement){ |
---|
414 | return doInsert(el, o, returnElement, afterend, 'nextSibling'); |
---|
415 | }, |
---|
416 | |
---|
417 | /** |
---|
418 | * Creates new DOM element(s) and inserts them as the first child of el. |
---|
419 | * @param {Mixed} el The context element |
---|
420 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
421 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
422 | * @return {HTMLElement/Ext.Element} The new node |
---|
423 | */ |
---|
424 | insertFirst : function(el, o, returnElement){ |
---|
425 | return doInsert(el, o, returnElement, afterbegin, 'firstChild'); |
---|
426 | }, |
---|
427 | |
---|
428 | /** |
---|
429 | * Creates new DOM element(s) and appends them to el. |
---|
430 | * @param {Mixed} el The context element |
---|
431 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
432 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
433 | * @return {HTMLElement/Ext.Element} The new node |
---|
434 | */ |
---|
435 | append : function(el, o, returnElement){ |
---|
436 | return doInsert(el, o, returnElement, beforeend, '', true); |
---|
437 | }, |
---|
438 | |
---|
439 | /** |
---|
440 | * Creates new DOM element(s) and overwrites the contents of el with them. |
---|
441 | * @param {Mixed} el The context element |
---|
442 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
443 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
444 | * @return {HTMLElement/Ext.Element} The new node |
---|
445 | */ |
---|
446 | overwrite : function(el, o, returnElement){ |
---|
447 | el = Ext.getDom(el); |
---|
448 | el.innerHTML = createHtml(o); |
---|
449 | return returnElement ? Ext.get(el.firstChild) : el.firstChild; |
---|
450 | }, |
---|
451 | |
---|
452 | createHtml : createHtml |
---|
453 | }; |
---|
454 | return pub; |
---|
455 | }(); |
---|