1 | /*! |
---|
2 | * Ext JS Library 3.0.0 |
---|
3 | * Copyright(c) 2006-2009 Ext JS, LLC |
---|
4 | * licensing@extjs.com |
---|
5 | * http://www.extjs.com/license |
---|
6 | */ |
---|
7 | /** |
---|
8 | * @class Ext.DomHelper |
---|
9 | */ |
---|
10 | Ext.apply(Ext.DomHelper, |
---|
11 | function(){ |
---|
12 | var pub, |
---|
13 | afterbegin = 'afterbegin', |
---|
14 | afterend = 'afterend', |
---|
15 | beforebegin = 'beforebegin', |
---|
16 | beforeend = 'beforeend'; |
---|
17 | |
---|
18 | // private |
---|
19 | function doInsert(el, o, returnElement, pos, sibling, append){ |
---|
20 | el = Ext.getDom(el); |
---|
21 | var newNode; |
---|
22 | if (pub.useDom) { |
---|
23 | newNode = createDom(o, null); |
---|
24 | if (append) { |
---|
25 | el.appendChild(newNode); |
---|
26 | } else { |
---|
27 | (sibling == 'firstChild' ? el : el.parentNode).insertBefore(newNode, el[sibling] || el); |
---|
28 | } |
---|
29 | } else { |
---|
30 | newNode = Ext.DomHelper.insertHtml(pos, el, Ext.DomHelper.createHtml(o)); |
---|
31 | } |
---|
32 | return returnElement ? Ext.get(newNode, true) : newNode; |
---|
33 | } |
---|
34 | |
---|
35 | // build as dom |
---|
36 | /** @ignore */ |
---|
37 | function createDom(o, parentNode){ |
---|
38 | var el, |
---|
39 | doc = document, |
---|
40 | useSet, |
---|
41 | attr, |
---|
42 | val, |
---|
43 | cn; |
---|
44 | |
---|
45 | if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted |
---|
46 | el = doc.createDocumentFragment(); // in one shot using a DocumentFragment |
---|
47 | Ext.each(o, function(v) { |
---|
48 | createDom(v, el); |
---|
49 | }); |
---|
50 | } else if (Ext.isString(o)) { // Allow a string as a child spec. |
---|
51 | el = doc.createTextNode(o); |
---|
52 | } else { |
---|
53 | el = doc.createElement( o.tag || 'div' ); |
---|
54 | useSet = !!el.setAttribute; // In IE some elements don't have setAttribute |
---|
55 | Ext.iterate(o, function(attr, val){ |
---|
56 | if(!/tag|children|cn|html|style/.test(attr)){ |
---|
57 | if(attr == 'cls'){ |
---|
58 | el.className = val; |
---|
59 | }else{ |
---|
60 | if(useSet){ |
---|
61 | el.setAttribute(attr, val); |
---|
62 | }else{ |
---|
63 | el[attr] = val; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | }); |
---|
68 | pub.applyStyles(el, o.style); |
---|
69 | |
---|
70 | if ((cn = o.children || o.cn)) { |
---|
71 | createDom(cn, el); |
---|
72 | } else if (o.html) { |
---|
73 | el.innerHTML = o.html; |
---|
74 | } |
---|
75 | } |
---|
76 | if(parentNode){ |
---|
77 | parentNode.appendChild(el); |
---|
78 | } |
---|
79 | return el; |
---|
80 | } |
---|
81 | |
---|
82 | pub = { |
---|
83 | /** |
---|
84 | * Creates a new Ext.Template from the DOM object spec. |
---|
85 | * @param {Object} o The DOM object spec (and children) |
---|
86 | * @return {Ext.Template} The new template |
---|
87 | */ |
---|
88 | createTemplate : function(o){ |
---|
89 | var html = Ext.DomHelper.createHtml(o); |
---|
90 | return new Ext.Template(html); |
---|
91 | }, |
---|
92 | |
---|
93 | /** True to force the use of DOM instead of html fragments @type Boolean */ |
---|
94 | useDom : false, |
---|
95 | |
---|
96 | /** |
---|
97 | * Applies a style specification to an element. |
---|
98 | * @param {String/HTMLElement} el The element to apply styles to |
---|
99 | * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or |
---|
100 | * a function which returns such a specification. |
---|
101 | */ |
---|
102 | applyStyles : function(el, styles){ |
---|
103 | if(styles){ |
---|
104 | var i = 0, |
---|
105 | len, |
---|
106 | style; |
---|
107 | |
---|
108 | el = Ext.fly(el); |
---|
109 | if(Ext.isFunction(styles)){ |
---|
110 | styles = styles.call(); |
---|
111 | } |
---|
112 | if(Ext.isString(styles)){ |
---|
113 | styles = styles.trim().split(/\s*(?::|;)\s*/); |
---|
114 | for(len = styles.length; i < len;){ |
---|
115 | el.setStyle(styles[i++], styles[i++]); |
---|
116 | } |
---|
117 | }else if (Ext.isObject(styles)){ |
---|
118 | el.setStyle(styles); |
---|
119 | } |
---|
120 | } |
---|
121 | }, |
---|
122 | |
---|
123 | /** |
---|
124 | * Creates new DOM element(s) and inserts them before el. |
---|
125 | * @param {Mixed} el The context element |
---|
126 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
127 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
128 | * @return {HTMLElement/Ext.Element} The new node |
---|
129 | * @hide (repeat) |
---|
130 | */ |
---|
131 | insertBefore : function(el, o, returnElement){ |
---|
132 | return doInsert(el, o, returnElement, beforebegin); |
---|
133 | }, |
---|
134 | |
---|
135 | /** |
---|
136 | * Creates new DOM element(s) and inserts them after el. |
---|
137 | * @param {Mixed} el The context element |
---|
138 | * @param {Object} o The DOM object spec (and children) |
---|
139 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
140 | * @return {HTMLElement/Ext.Element} The new node |
---|
141 | * @hide (repeat) |
---|
142 | */ |
---|
143 | insertAfter : function(el, o, returnElement){ |
---|
144 | return doInsert(el, o, returnElement, afterend, 'nextSibling'); |
---|
145 | }, |
---|
146 | |
---|
147 | /** |
---|
148 | * Creates new DOM element(s) and inserts them as the first child of el. |
---|
149 | * @param {Mixed} el The context element |
---|
150 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
151 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
152 | * @return {HTMLElement/Ext.Element} The new node |
---|
153 | * @hide (repeat) |
---|
154 | */ |
---|
155 | insertFirst : function(el, o, returnElement){ |
---|
156 | return doInsert(el, o, returnElement, afterbegin, 'firstChild'); |
---|
157 | }, |
---|
158 | |
---|
159 | /** |
---|
160 | * Creates new DOM element(s) and appends them to el. |
---|
161 | * @param {Mixed} el The context element |
---|
162 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
163 | * @param {Boolean} returnElement (optional) true to return a Ext.Element |
---|
164 | * @return {HTMLElement/Ext.Element} The new node |
---|
165 | * @hide (repeat) |
---|
166 | */ |
---|
167 | append: function(el, o, returnElement){ |
---|
168 | return doInsert(el, o, returnElement, beforeend, '', true); |
---|
169 | }, |
---|
170 | |
---|
171 | /** |
---|
172 | * Creates new DOM element(s) without inserting them to the document. |
---|
173 | * @param {Object/String} o The DOM object spec (and children) or raw HTML blob |
---|
174 | * @return {HTMLElement} The new uninserted node |
---|
175 | */ |
---|
176 | createDom: createDom |
---|
177 | }; |
---|
178 | return pub; |
---|
179 | }()); |
---|