+++ /dev/null
-/*\r
- * Ext JS Library 2.2.1\r
- * Copyright(c) 2006-2009, Ext JS, LLC.\r
- * licensing@extjs.com\r
- * \r
- * http://extjs.com/license\r
- */\r
-\r
-/**\r
- * @class Ext.DomHelper\r
- * Utility class for working with DOM and/or Templates. It transparently supports using HTML fragments or DOM.<br>\r
- * This is an example, where an unordered list with 5 children items is appended to an existing element with id 'my-div':<br>\r
- <pre><code>\r
-var dh = Ext.DomHelper;\r
-var list = dh.append('my-div', {\r
- id: 'my-ul', tag: 'ul', cls: 'my-list', children: [\r
- {tag: 'li', id: 'item0', html: 'List Item 0'},\r
- {tag: 'li', id: 'item1', html: 'List Item 1'},\r
- {tag: 'li', id: 'item2', html: 'List Item 2'},\r
- {tag: 'li', id: 'item3', html: 'List Item 3'},\r
- {tag: 'li', id: 'item4', html: 'List Item 4'}\r
- ]\r
-});\r
- </code></pre>\r
- * <p>Element creation specification parameters in this class may also be passed as an Array of\r
- * specification objects. This can be used to insert multiple sibling nodes into an existing\r
- * container very efficiently. For example, to add more list items to the example above:<pre><code>\r
-dh.append('my-ul', [\r
- {tag: 'li', id: 'item5', html: 'List Item 5'},\r
- {tag: 'li', id: 'item6', html: 'List Item 6'} ]);\r
-</code></pre></p>\r
- * <p>Element creation specification parameters may also be strings. If {@link useDom} is false, then the string is used\r
- * as innerHTML. If {@link useDom} is true, a string specification results in the creation of a text node.</p>\r
- * For more information and examples, see <a href="http://www.jackslocum.com/blog/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">the original blog post</a>.\r
- * @singleton\r
- */\r
-Ext.DomHelper = function(){\r
- var tempTableEl = null;\r
- var emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i;\r
- var tableRe = /^table|tbody|tr|td$/i;\r
-\r
- // build as innerHTML where available\r
- var createHtml = function(o){\r
- if(typeof o == 'string'){\r
- return o;\r
- }\r
- var b = "";\r
- if (Ext.isArray(o)) {\r
- for (var i = 0, l = o.length; i < l; i++) {\r
- b += createHtml(o[i]);\r
- }\r
- return b;\r
- }\r
- if(!o.tag){\r
- o.tag = "div";\r
- }\r
- b += "<" + o.tag;\r
- for(var attr in o){\r
- if(attr == "tag" || attr == "children" || attr == "cn" || attr == "html" || typeof o[attr] == "function") continue;\r
- if(attr == "style"){\r
- var s = o["style"];\r
- if(typeof s == "function"){\r
- s = s.call();\r
- }\r
- if(typeof s == "string"){\r
- b += ' style="' + s + '"';\r
- }else if(typeof s == "object"){\r
- b += ' style="';\r
- for(var key in s){\r
- if(typeof s[key] != "function"){\r
- b += key + ":" + s[key] + ";";\r
- }\r
- }\r
- b += '"';\r
- }\r
- }else{\r
- if(attr == "cls"){\r
- b += ' class="' + o["cls"] + '"';\r
- }else if(attr == "htmlFor"){\r
- b += ' for="' + o["htmlFor"] + '"';\r
- }else{\r
- b += " " + attr + '="' + o[attr] + '"';\r
- }\r
- }\r
- }\r
- if(emptyTags.test(o.tag)){\r
- b += "/>";\r
- }else{\r
- b += ">";\r
- var cn = o.children || o.cn;\r
- if(cn){\r
- b += createHtml(cn);\r
- } else if(o.html){\r
- b += o.html;\r
- }\r
- b += "</" + o.tag + ">";\r
- }\r
- return b;\r
- };\r
-\r
- // build as dom\r
- /** @ignore */\r
- var createDom = function(o, parentNode){\r
- var el;\r
- if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted\r
- el = document.createDocumentFragment(); // in one shot using a DocumentFragment\r
- for(var i = 0, l = o.length; i < l; i++) {\r
- createDom(o[i], el);\r
- }\r
- } else if (typeof o == "string") { // Allow a string as a child spec.\r
- el = document.createTextNode(o);\r
- } else {\r
- el = document.createElement(o.tag||'div');\r
- var useSet = !!el.setAttribute; // In IE some elements don't have setAttribute\r
- for(var attr in o){\r
- if(attr == "tag" || attr == "children" || attr == "cn" || attr == "html" || attr == "style" || typeof o[attr] == "function") continue;\r
- if(attr=="cls"){\r
- el.className = o["cls"];\r
- }else{\r
- if(useSet) el.setAttribute(attr, o[attr]);\r
- else el[attr] = o[attr];\r
- }\r
- }\r
- Ext.DomHelper.applyStyles(el, o.style);\r
- var cn = o.children || o.cn;\r
- if(cn){\r
- createDom(cn, el);\r
- } else if(o.html){\r
- el.innerHTML = o.html;\r
- }\r
- }\r
- if(parentNode){\r
- parentNode.appendChild(el);\r
- }\r
- return el;\r
- };\r
-\r
- var ieTable = function(depth, s, h, e){\r
- tempTableEl.innerHTML = [s, h, e].join('');\r
- var i = -1, el = tempTableEl;\r
- while(++i < depth){\r
- el = el.firstChild;\r
- }\r
- return el;\r
- };\r
-\r
- // kill repeat to save bytes\r
- var ts = '<table>',\r
- te = '</table>',\r
- tbs = ts+'<tbody>',\r
- tbe = '</tbody>'+te,\r
- trs = tbs + '<tr>',\r
- tre = '</tr>'+tbe;\r
-\r
- /**\r
- * @ignore\r
- * Nasty code for IE's broken table implementation\r
- */\r
- var insertIntoTable = function(tag, where, el, html){\r
- if(!tempTableEl){\r
- tempTableEl = document.createElement('div');\r
- }\r
- var node;\r
- var before = null;\r
- if(tag == 'td'){\r
- if(where == 'afterbegin' || where == 'beforeend'){ // INTO a TD\r
- return;\r
- }\r
- if(where == 'beforebegin'){\r
- before = el;\r
- el = el.parentNode;\r
- } else{\r
- before = el.nextSibling;\r
- el = el.parentNode;\r
- }\r
- node = ieTable(4, trs, html, tre);\r
- }\r
- else if(tag == 'tr'){\r
- if(where == 'beforebegin'){\r
- before = el;\r
- el = el.parentNode;\r
- node = ieTable(3, tbs, html, tbe);\r
- } else if(where == 'afterend'){\r
- before = el.nextSibling;\r
- el = el.parentNode;\r
- node = ieTable(3, tbs, html, tbe);\r
- } else{ // INTO a TR\r
- if(where == 'afterbegin'){\r
- before = el.firstChild;\r
- }\r
- node = ieTable(4, trs, html, tre);\r
- }\r
- } else if(tag == 'tbody'){\r
- if(where == 'beforebegin'){\r
- before = el;\r
- el = el.parentNode;\r
- node = ieTable(2, ts, html, te);\r
- } else if(where == 'afterend'){\r
- before = el.nextSibling;\r
- el = el.parentNode;\r
- node = ieTable(2, ts, html, te);\r
- } else{\r
- if(where == 'afterbegin'){\r
- before = el.firstChild;\r
- }\r
- node = ieTable(3, tbs, html, tbe);\r
- }\r
- } else{ // TABLE\r
- if(where == 'beforebegin' || where == 'afterend'){ // OUTSIDE the table\r
- return;\r
- }\r
- if(where == 'afterbegin'){\r
- before = el.firstChild;\r
- }\r
- node = ieTable(2, ts, html, te);\r
- }\r
- el.insertBefore(node, before);\r
- return node;\r
- };\r
-\r
-\r
- return {\r
- /** True to force the use of DOM instead of html fragments @type Boolean */\r
- useDom : false,\r
-\r
- /**\r
- * Returns the markup for the passed Element(s) config.\r
- * @param {Object} o The DOM object spec (and children)\r
- * @return {String}\r
- */\r
- markup : function(o){\r
- return createHtml(o);\r
- },\r
-\r
- /**\r
- * Applies a style specification to an element.\r
- * @param {String/HTMLElement} el The element to apply styles to\r
- * @param {String/Object/Function} styles A style specification string eg "width:100px", or object in the form {width:"100px"}, or\r
- * a function which returns such a specification.\r
- */\r
- applyStyles : function(el, styles){\r
- if(styles){\r
- el = Ext.fly(el);\r
- if(typeof styles == "string"){\r
- var re = /\s?([a-z\-]*)\:\s?([^;]*);?/gi;\r
- var matches;\r
- while ((matches = re.exec(styles)) != null){\r
- el.setStyle(matches[1], matches[2]);\r
- }\r
- }else if (typeof styles == "object"){\r
- for (var style in styles){\r
- el.setStyle(style, styles[style]);\r
- }\r
- }else if (typeof styles == "function"){\r
- Ext.DomHelper.applyStyles(el, styles.call());\r
- }\r
- }\r
- },\r
-\r
- /**\r
- * Inserts an HTML fragment into the DOM.\r
- * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.\r
- * @param {HTMLElement} el The context element\r
- * @param {String} html The HTML fragmenet\r
- * @return {HTMLElement} The new node\r
- */\r
- insertHtml : function(where, el, html){\r
- where = where.toLowerCase();\r
- if(el.insertAdjacentHTML){\r
- if(tableRe.test(el.tagName)){\r
- var rs;\r
- if(rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html)){\r
- return rs;\r
- }\r
- }\r
- switch(where){\r
- case "beforebegin":\r
- el.insertAdjacentHTML('BeforeBegin', html);\r
- return el.previousSibling;\r
- case "afterbegin":\r
- el.insertAdjacentHTML('AfterBegin', html);\r
- return el.firstChild;\r
- case "beforeend":\r
- el.insertAdjacentHTML('BeforeEnd', html);\r
- return el.lastChild;\r
- case "afterend":\r
- el.insertAdjacentHTML('AfterEnd', html);\r
- return el.nextSibling;\r
- }\r
- throw 'Illegal insertion point -> "' + where + '"';\r
- }\r
- var range = el.ownerDocument.createRange();\r
- var frag;\r
- switch(where){\r
- case "beforebegin":\r
- range.setStartBefore(el);\r
- frag = range.createContextualFragment(html);\r
- el.parentNode.insertBefore(frag, el);\r
- return el.previousSibling;\r
- case "afterbegin":\r
- if(el.firstChild){\r
- range.setStartBefore(el.firstChild);\r
- frag = range.createContextualFragment(html);\r
- el.insertBefore(frag, el.firstChild);\r
- return el.firstChild;\r
- }else{\r
- el.innerHTML = html;\r
- return el.firstChild;\r
- }\r
- case "beforeend":\r
- if(el.lastChild){\r
- range.setStartAfter(el.lastChild);\r
- frag = range.createContextualFragment(html);\r
- el.appendChild(frag);\r
- return el.lastChild;\r
- }else{\r
- el.innerHTML = html;\r
- return el.lastChild;\r
- }\r
- case "afterend":\r
- range.setStartAfter(el);\r
- frag = range.createContextualFragment(html);\r
- el.parentNode.insertBefore(frag, el.nextSibling);\r
- return el.nextSibling;\r
- }\r
- throw 'Illegal insertion point -> "' + where + '"';\r
- },\r
-\r
- /**\r
- * Creates new DOM element(s) and inserts them before el.\r
- * @param {Mixed} el The context element\r
- * @param {Object/String} o The DOM object spec (and children) or raw HTML blob\r
- * @param {Boolean} returnElement (optional) true to return a Ext.Element\r
- * @return {HTMLElement/Ext.Element} The new node\r
- */\r
- insertBefore : function(el, o, returnElement){\r
- return this.doInsert(el, o, returnElement, "beforeBegin");\r
- },\r
-\r
- /**\r
- * Creates new DOM element(s) and inserts them after el.\r
- * @param {Mixed} el The context element\r
- * @param {Object} o The DOM object spec (and children)\r
- * @param {Boolean} returnElement (optional) true to return a Ext.Element\r
- * @return {HTMLElement/Ext.Element} The new node\r
- */\r
- insertAfter : function(el, o, returnElement){\r
- return this.doInsert(el, o, returnElement, "afterEnd", "nextSibling");\r
- },\r
-\r
- /**\r
- * Creates new DOM element(s) and inserts them as the first child of el.\r
- * @param {Mixed} el The context element\r
- * @param {Object/String} o The DOM object spec (and children) or raw HTML blob\r
- * @param {Boolean} returnElement (optional) true to return a Ext.Element\r
- * @return {HTMLElement/Ext.Element} The new node\r
- */\r
- insertFirst : function(el, o, returnElement){\r
- return this.doInsert(el, o, returnElement, "afterBegin", "firstChild");\r
- },\r
-\r
- // private\r
- doInsert : function(el, o, returnElement, pos, sibling){\r
- el = Ext.getDom(el);\r
- var newNode;\r
- if(this.useDom){\r
- newNode = createDom(o, null);\r
- (sibling === "firstChild" ? el : el.parentNode).insertBefore(newNode, sibling ? el[sibling] : el);\r
- }else{\r
- var html = createHtml(o);\r
- newNode = this.insertHtml(pos, el, html);\r
- }\r
- return returnElement ? Ext.get(newNode, true) : newNode;\r
- },\r
-\r
- /**\r
- * Creates new DOM element(s) and appends them to el.\r
- * @param {Mixed} el The context element\r
- * @param {Object/String} o The DOM object spec (and children) or raw HTML blob\r
- * @param {Boolean} returnElement (optional) true to return a Ext.Element\r
- * @return {HTMLElement/Ext.Element} The new node\r
- */\r
- append : function(el, o, returnElement){\r
- el = Ext.getDom(el);\r
- var newNode;\r
- if(this.useDom){\r
- newNode = createDom(o, null);\r
- el.appendChild(newNode);\r
- }else{\r
- var html = createHtml(o);\r
- newNode = this.insertHtml("beforeEnd", el, html);\r
- }\r
- return returnElement ? Ext.get(newNode, true) : newNode;\r
- },\r
-\r
- /**\r
- * Creates new DOM element(s) and overwrites the contents of el with them.\r
- * @param {Mixed} el The context element\r
- * @param {Object/String} o The DOM object spec (and children) or raw HTML blob\r
- * @param {Boolean} returnElement (optional) true to return a Ext.Element\r
- * @return {HTMLElement/Ext.Element} The new node\r
- */\r
- overwrite : function(el, o, returnElement){\r
- el = Ext.getDom(el);\r
- el.innerHTML = createHtml(o);\r
- return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;\r
- },\r
-\r
- /**\r
- * Creates a new Ext.Template from the DOM object spec.\r
- * @param {Object} o The DOM object spec (and children)\r
- * @return {Ext.Template} The new template\r
- */\r
- createTemplate : function(o){\r
- var html = createHtml(o);\r
- return new Ext.Template(html);\r
- }\r
- };\r
-}();\r