3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.core.Element
18 Ext.core.Element.addMethods({
20 * Appends the passed element(s) to this element
21 * @param {String/HTMLElement/Array/Element/CompositeElement} el
22 * @return {Ext.core.Element} this
24 appendChild : function(el) {
25 return Ext.get(el).appendTo(this);
29 * Appends this element to the passed element
30 * @param {Mixed} el The new parent element
31 * @return {Ext.core.Element} this
33 appendTo : function(el) {
34 Ext.getDom(el).appendChild(this.dom);
39 * Inserts this element before the passed element in the DOM
40 * @param {Mixed} el The element before which this element will be inserted
41 * @return {Ext.core.Element} this
43 insertBefore : function(el) {
45 el.parentNode.insertBefore(this.dom, el);
50 * Inserts this element after the passed element in the DOM
51 * @param {Mixed} el The element to insert after
52 * @return {Ext.core.Element} this
54 insertAfter : function(el) {
56 el.parentNode.insertBefore(this.dom, el.nextSibling);
61 * Inserts (or creates) an element (or DomHelper config) as the first child of this element
62 * @param {Mixed/Object} el The id or element to insert or a DomHelper config to create and insert
63 * @return {Ext.core.Element} The new child
65 insertFirst : function(el, returnDom) {
67 if (el.nodeType || el.dom || typeof el == 'string') { // element
69 this.dom.insertBefore(el, this.dom.firstChild);
70 return !returnDom ? Ext.get(el) : el;
73 return this.createChild(el, this.dom.firstChild, returnDom);
78 * Inserts (or creates) the passed element (or DomHelper config) as a sibling of this element
79 * @param {Mixed/Object/Array} el The id, element to insert or a DomHelper config to create and insert *or* an array of any of those.
80 * @param {String} where (optional) 'before' or 'after' defaults to before
81 * @param {Boolean} returnDom (optional) True to return the .;ll;l,raw DOM element instead of Ext.core.Element
82 * @return {Ext.core.Element} The inserted Element. If an array is passed, the last inserted element is returned.
84 insertSibling: function(el, where, returnDom){
86 isAfter = (where || 'before').toLowerCase() == 'after',
91 Ext.each(el, function(e) {
92 rt = Ext.fly(insertEl, '_internal').insertSibling(e, where, returnDom);
102 if(el.nodeType || el.dom){
103 rt = me.dom.parentNode.insertBefore(Ext.getDom(el), isAfter ? me.dom.nextSibling : me.dom);
108 if (isAfter && !me.dom.nextSibling) {
109 rt = Ext.core.DomHelper.append(me.dom.parentNode, el, !returnDom);
111 rt = Ext.core.DomHelper[isAfter ? 'insertAfter' : 'insertBefore'](me.dom, el, !returnDom);
118 * Replaces the passed element with this element
119 * @param {Mixed} el The element to replace
120 * @return {Ext.core.Element} this
122 replace : function(el) {
124 this.insertBefore(el);
130 * Replaces this element with the passed element
131 * @param {Mixed/Object} el The new element or a DomHelper config of an element to create
132 * @return {Ext.core.Element} this
134 replaceWith: function(el){
137 if(el.nodeType || el.dom || typeof el == 'string'){
139 me.dom.parentNode.insertBefore(el, me.dom);
141 el = Ext.core.DomHelper.insertBefore(me.dom, el);
144 delete Ext.cache[me.id];
145 Ext.removeNode(me.dom);
146 me.id = Ext.id(me.dom = el);
147 Ext.core.Element.addToCache(me.isFlyweight ? new Ext.core.Element(me.dom) : me);
152 * Creates the passed DomHelper config and appends it to this element or optionally inserts it before the passed child element.
153 * @param {Object} config DomHelper element config object. If no tag is specified (e.g., {tag:'input'}) then a div will be
154 * automatically generated with the specified attributes.
155 * @param {HTMLElement} insertBefore (optional) a child element of this element
156 * @param {Boolean} returnDom (optional) true to return the dom node instead of creating an Element
157 * @return {Ext.core.Element} The new child element
159 createChild : function(config, insertBefore, returnDom) {
160 config = config || {tag:'div'};
162 return Ext.core.DomHelper.insertBefore(insertBefore, config, returnDom !== true);
165 return Ext.core.DomHelper[!this.dom.firstChild ? 'insertFirst' : 'append'](this.dom, config, returnDom !== true);
170 * Creates and wraps this element with another element
171 * @param {Object} config (optional) DomHelper element config object for the wrapper element or null for an empty div
172 * @param {Boolean} returnDom (optional) True to return the raw DOM element instead of Ext.core.Element
173 * @return {HTMLElement/Element} The newly created wrapper element
175 wrap : function(config, returnDom) {
176 var newEl = Ext.core.DomHelper.insertBefore(this.dom, config || {tag: "div"}, !returnDom),
177 d = newEl.dom || newEl;
179 d.appendChild(this.dom);
184 * Inserts an html fragment into this element
185 * @param {String} where Where to insert the html in relation to this element - beforeBegin, afterBegin, beforeEnd, afterEnd.
186 * @param {String} html The HTML fragment
187 * @param {Boolean} returnEl (optional) True to return an Ext.core.Element (defaults to false)
188 * @return {HTMLElement/Ext.core.Element} The inserted node (or nearest related if more than 1 inserted)
190 insertHtml : function(where, html, returnEl) {
191 var el = Ext.core.DomHelper.insertHtml(where, this.dom, html);
192 return returnEl ? Ext.get(el) : el;