X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6:/src/ext-core/src/core/DomHelper.js..7a654f8d43fdb43d78b63d90528bed6e86b608cc:/src/core/src/dom/DomHelper.js diff --git a/src/ext-core/src/core/DomHelper.js b/src/core/src/dom/DomHelper.js similarity index 70% rename from src/ext-core/src/core/DomHelper.js rename to src/core/src/dom/DomHelper.js index b3037d7f..afc11a40 100644 --- a/src/ext-core/src/core/DomHelper.js +++ b/src/core/src/dom/DomHelper.js @@ -1,11 +1,5 @@ -/*! - * Ext JS Library 3.3.1 - * Copyright(c) 2006-2010 Sencha Inc. - * licensing@sencha.com - * http://www.sencha.com/license - */ /** - * @class Ext.DomHelper + * @class Ext.core.DomHelper *

The DomHelper class provides a layer of abstraction from DOM and transparently supports creating * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates * from your DOM building code.

@@ -39,7 +33,7 @@ *

This is an example, where an unordered list with 3 children items is appended to an existing * element with id 'my-div':


-var dh = Ext.DomHelper; // create shorthand alias
+var dh = Ext.core.DomHelper; // create shorthand alias
 // specification object
 var spec = {
     id: 'my-ul',
@@ -83,19 +77,19 @@ for(var i = 0; i < 5, i++){
  * 

An example using a template:


 var html = '{2}';
 
-var tpl = new Ext.DomHelper.createTemplate(html);
-tpl.append('blog-roll', ['link1', 'http://www.jackslocum.com/', "Jack's Site"]);
+var tpl = new Ext.core.DomHelper.createTemplate(html);
+tpl.append('blog-roll', ['link1', 'http://www.edspencer.net/', "Ed's Site"]);
 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin's Site"]);
  * 

* *

The same example using named parameters:


 var html = '{text}';
 
-var tpl = new Ext.DomHelper.createTemplate(html);
+var tpl = new Ext.core.DomHelper.createTemplate(html);
 tpl.append('blog-roll', {
     id: 'link1',
-    url: 'http://www.jackslocum.com/',
-    text: "Jack's Site"
+    url: 'http://www.edspencer.net/',
+    text: "Ed's Site"
 });
 tpl.append('blog-roll', {
     id: 'link2',
@@ -115,7 +109,7 @@ tpl.append('blog-roll', {
  * 

 var html = '{text}';
 
-var tpl = new Ext.DomHelper.createTemplate(html);
+var tpl = new Ext.core.DomHelper.createTemplate(html);
 tpl.compile();
 
 //... use template like normal
@@ -128,17 +122,17 @@ tpl.compile();
  * then the string is used as innerHTML. If {@link #useDom} is true, a string specification
  * results in the creation of a text node. Usage:

*

-Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance
+Ext.core.DomHelper.useDom = true; // force it to use DOM; reduces performance
  * 
* @singleton */ -Ext.DomHelper = function(){ +Ext.ns('Ext.core'); +Ext.core.DomHelper = function(){ var tempTableEl = null, emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i, tableRe = /^table|tbody|tr|td$/i, confRe = /tag|children|cn|html$/i, tableElRe = /td|tr|tbody/i, - cssRe = /([a-z0-9-]+)\s*:\s*([^;\s]+(?:\s*[^;\s]+)*);?/gi, endRe = /end/i, pub, // kill repeat to save bytes @@ -155,9 +149,66 @@ Ext.DomHelper = function(){ // private function doInsert(el, o, returnElement, pos, sibling, append){ - var newNode = pub.insertHtml(pos, Ext.getDom(el), createHtml(o)); + el = Ext.getDom(el); + var newNode; + if (pub.useDom) { + newNode = createDom(o, null); + if (append) { + el.appendChild(newNode); + } else { + (sibling == 'firstChild' ? el : el.parentNode).insertBefore(newNode, el[sibling] || el); + } + } else { + newNode = Ext.core.DomHelper.insertHtml(pos, el, Ext.core.DomHelper.createHtml(o)); + } return returnElement ? Ext.get(newNode, true) : newNode; } + + function createDom(o, parentNode){ + var el, + doc = document, + useSet, + attr, + val, + cn; + + if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted + el = doc.createDocumentFragment(); // in one shot using a DocumentFragment + for (var i = 0, l = o.length; i < l; i++) { + createDom(o[i], el); + } + } else if (typeof o == 'string') { // Allow a string as a child spec. + el = doc.createTextNode(o); + } else { + el = doc.createElement( o.tag || 'div' ); + useSet = !!el.setAttribute; // In IE some elements don't have setAttribute + for (attr in o) { + if(!confRe.test(attr)){ + val = o[attr]; + if(attr == 'cls'){ + el.className = val; + }else{ + if(useSet){ + el.setAttribute(attr, val); + }else{ + el[attr] = val; + } + } + } + } + Ext.core.DomHelper.applyStyles(el, o.style); + + if ((cn = o.children || o.cn)) { + createDom(cn, el); + } else if (o.html) { + el.innerHTML = o.html; + } + } + if(parentNode){ + parentNode.appendChild(el); + } + return el; + } // build as innerHTML where available function createHtml(o){ @@ -165,16 +216,17 @@ Ext.DomHelper = function(){ attr, val, key, - cn; + cn, + i; if(typeof o == "string"){ b = o; } else if (Ext.isArray(o)) { - for (var i=0; i < o.length; i++) { + for (i=0; i < o.length; i++) { if(o[i]) { b += createHtml(o[i]); } - }; + } } else { b += '<' + (o.tag = o.tag || 'div'); for (attr in o) { @@ -184,13 +236,13 @@ Ext.DomHelper = function(){ b += ' ' + attr + '="'; for (key in val) { b += key + ':' + val[key] + ';'; - }; + } b += '"'; }else{ b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"'; } } - }; + } // Now either just close the tag or try to add children and close the tag. if (emptyTags.test(o.tag)) { b += '/>'; @@ -216,7 +268,8 @@ Ext.DomHelper = function(){ el = el.firstChild; } // If the result is multiple siblings, then encapsulate them into one fragment. - if(ns = el.nextSibling){ + ns = el.nextSibling; + if (ns){ var df = document.createDocumentFragment(); while(el){ ns = el.nextSibling; @@ -240,7 +293,7 @@ Ext.DomHelper = function(){ if(tag == 'td' && (where == afterbegin || where == beforeend) || !tableElRe.test(tag) && (where == beforebegin || where == afterend)) { - return; + return null; } before = where == beforebegin ? el : where == afterend ? el.nextSibling : @@ -261,8 +314,28 @@ Ext.DomHelper = function(){ el.insertBefore(node, before); return node; } + + /** + * @ignore + * Fix for IE9 createContextualFragment missing method + */ + function createContextualFragment(html){ + var div = document.createElement("div"), + fragment = document.createDocumentFragment(), + i = 0, + length, childNodes; + + div.innerHTML = html; + childNodes = div.childNodes; + length = childNodes.length; + for (; i < length; i++) { + fragment.appendChild(childNodes[i].cloneNode(true)); + } + return fragment; + } + pub = { /** * Returns the markup for the passed Element(s) config. @@ -281,24 +354,14 @@ Ext.DomHelper = function(){ */ applyStyles : function(el, styles){ if (styles) { - var matches; - el = Ext.fly(el); if (typeof styles == "function") { styles = styles.call(); } if (typeof styles == "string") { - /** - * Since we're using the g flag on the regex, we need to set the lastIndex. - * This automatically happens on some implementations, but not others, see: - * http://stackoverflow.com/questions/2645273/javascript-regular-expression-literal-persists-between-function-calls - * http://blog.stevenlevithan.com/archives/fixing-javascript-regexp - */ - cssRe.lastIndex = 0; - while ((matches = cssRe.exec(styles))) { - el.setStyle(matches[1], matches[2]); - } - } else if (typeof styles == "object") { + styles = Ext.core.Element.parseStyles(styles); + } + if (typeof styles == "object") { el.setStyle(styles); } } @@ -307,28 +370,30 @@ Ext.DomHelper = function(){ /** * Inserts an HTML fragment into the DOM. * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd. - * @param {HTMLElement} el The context element + * @param {HTMLElement/TextNode} el The context element * @param {String} html The HTML fragment * @return {HTMLElement} The new node */ insertHtml : function(where, el, html){ var hash = {}, hashVal, - setStart, range, - frag, rangeEl, + setStart, + frag, rs; where = where.toLowerCase(); // add these here because they are used in both branches of the condition. hash[beforebegin] = ['BeforeBegin', 'previousSibling']; hash[afterend] = ['AfterEnd', 'nextSibling']; - + + // if IE and context element is an HTMLElement if (el.insertAdjacentHTML) { if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){ return rs; } + // add these two to the hash. hash[afterbegin] = ['AfterBegin', 'firstChild']; hash[beforeend] = ['BeforeEnd', 'lastChild']; @@ -336,19 +401,34 @@ Ext.DomHelper = function(){ el.insertAdjacentHTML(hashVal[0], html); return el[hashVal[1]]; } + // if (not IE and context element is an HTMLElement) or TextNode } else { - range = el.ownerDocument.createRange(); + // we cannot insert anything inside a textnode so... + if (Ext.isTextNode(el)) { + where = where === 'afterbegin' ? 'beforebegin' : where; + where = where === 'beforeend' ? 'afterend' : where; + } + range = Ext.supports.CreateContextualFragment ? el.ownerDocument.createRange() : undefined; setStart = 'setStart' + (endRe.test(where) ? 'After' : 'Before'); if (hash[where]) { - range[setStart](el); - frag = range.createContextualFragment(html); + if (range) { + range[setStart](el); + frag = range.createContextualFragment(html); + } else { + frag = createContextualFragment(html); + } el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling); return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling']; } else { rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child'; if (el.firstChild) { - range[setStart](el[rangeEl]); - frag = range.createContextualFragment(html); + if (range) { + range[setStart](el[rangeEl]); + frag = range.createContextualFragment(html); + } else { + frag = createContextualFragment(html); + } + if(where == afterbegin){ el.insertBefore(frag, el.firstChild); }else{ @@ -360,15 +440,23 @@ Ext.DomHelper = function(){ return el[rangeEl]; } } - throw 'Illegal insertion point -> "' + where + '"'; + // + Ext.Error.raise({ + sourceClass: 'Ext.core.DomHelper', + sourceMethod: 'insertHtml', + htmlToInsert: html, + targetElement: el, + msg: 'Illegal insertion point reached: "' + where + '"' + }); + // }, /** * Creates new DOM element(s) and inserts them before el. * @param {Mixed} el The context element * @param {Object/String} o The DOM object spec (and children) or raw HTML blob - * @param {Boolean} returnElement (optional) true to return a Ext.Element - * @return {HTMLElement/Ext.Element} The new node + * @param {Boolean} returnElement (optional) true to return a Ext.core.Element + * @return {HTMLElement/Ext.core.Element} The new node */ insertBefore : function(el, o, returnElement){ return doInsert(el, o, returnElement, beforebegin); @@ -378,8 +466,8 @@ Ext.DomHelper = function(){ * Creates new DOM element(s) and inserts them after el. * @param {Mixed} el The context element * @param {Object} o The DOM object spec (and children) - * @param {Boolean} returnElement (optional) true to return a Ext.Element - * @return {HTMLElement/Ext.Element} The new node + * @param {Boolean} returnElement (optional) true to return a Ext.core.Element + * @return {HTMLElement/Ext.core.Element} The new node */ insertAfter : function(el, o, returnElement){ return doInsert(el, o, returnElement, afterend, 'nextSibling'); @@ -389,8 +477,8 @@ Ext.DomHelper = function(){ * Creates new DOM element(s) and inserts them as the first child of el. * @param {Mixed} el The context element * @param {Object/String} o The DOM object spec (and children) or raw HTML blob - * @param {Boolean} returnElement (optional) true to return a Ext.Element - * @return {HTMLElement/Ext.Element} The new node + * @param {Boolean} returnElement (optional) true to return a Ext.core.Element + * @return {HTMLElement/Ext.core.Element} The new node */ insertFirst : function(el, o, returnElement){ return doInsert(el, o, returnElement, afterbegin, 'firstChild'); @@ -400,8 +488,8 @@ Ext.DomHelper = function(){ * Creates new DOM element(s) and appends them to el. * @param {Mixed} el The context element * @param {Object/String} o The DOM object spec (and children) or raw HTML blob - * @param {Boolean} returnElement (optional) true to return a Ext.Element - * @return {HTMLElement/Ext.Element} The new node + * @param {Boolean} returnElement (optional) true to return a Ext.core.Element + * @return {HTMLElement/Ext.core.Element} The new node */ append : function(el, o, returnElement){ return doInsert(el, o, returnElement, beforeend, '', true); @@ -411,8 +499,8 @@ Ext.DomHelper = function(){ * Creates new DOM element(s) and overwrites the contents of el with them. * @param {Mixed} el The context element * @param {Object/String} o The DOM object spec (and children) or raw HTML blob - * @param {Boolean} returnElement (optional) true to return a Ext.Element - * @return {HTMLElement/Ext.Element} The new node + * @param {Boolean} returnElement (optional) true to return a Ext.core.Element + * @return {HTMLElement/Ext.core.Element} The new node */ overwrite : function(el, o, returnElement){ el = Ext.getDom(el); @@ -420,7 +508,27 @@ Ext.DomHelper = function(){ return returnElement ? Ext.get(el.firstChild) : el.firstChild; }, - createHtml : createHtml + createHtml : createHtml, + + /** + * Creates new DOM element(s) without inserting them to the document. + * @param {Object/String} o The DOM object spec (and children) or raw HTML blob + * @return {HTMLElement} The new uninserted node + */ + createDom: createDom, + + /** True to force the use of DOM instead of html fragments @type Boolean */ + useDom : false, + + /** + * Creates a new Ext.Template from the DOM object spec. + * @param {Object} o The DOM object spec (and children) + * @return {Ext.Template} The new template + */ + createTemplate : function(o){ + var html = Ext.core.DomHelper.createHtml(o); + return Ext.create('Ext.Template', html); + } }; return pub; }();