Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / ext-core / src / core / Element.insertion.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.Element
9  */
10 Ext.Element.addMethods(
11 function() {
12         var GETDOM = Ext.getDom,
13                 GET = Ext.get,
14                 DH = Ext.DomHelper;
15         
16         return {
17             /**
18              * Appends the passed element(s) to this element
19              * @param {String/HTMLElement/Array/Element/CompositeElement} el
20              * @return {Ext.Element} this
21              */
22             appendChild: function(el){        
23                 return GET(el).appendTo(this);        
24             },
25         
26             /**
27              * Appends this element to the passed element
28              * @param {Mixed} el The new parent element
29              * @return {Ext.Element} this
30              */
31             appendTo: function(el){        
32                 GETDOM(el).appendChild(this.dom);        
33                 return this;
34             },
35         
36             /**
37              * Inserts this element before the passed element in the DOM
38              * @param {Mixed} el The element before which this element will be inserted
39              * @return {Ext.Element} this
40              */
41             insertBefore: function(el){                   
42                 (el = GETDOM(el)).parentNode.insertBefore(this.dom, el);
43                 return this;
44             },
45         
46             /**
47              * Inserts this element after the passed element in the DOM
48              * @param {Mixed} el The element to insert after
49              * @return {Ext.Element} this
50              */
51             insertAfter: function(el){
52                 (el = GETDOM(el)).parentNode.insertBefore(this.dom, el.nextSibling);
53                 return this;
54             },
55         
56             /**
57              * Inserts (or creates) an element (or DomHelper config) as the first child of this element
58              * @param {Mixed/Object} el The id or element to insert or a DomHelper config to create and insert
59              * @return {Ext.Element} The new child
60              */
61             insertFirst: function(el, returnDom){
62             el = el || {};
63             if(el.nodeType || el.dom || typeof el == 'string'){ // element
64                 el = GETDOM(el);
65                 this.dom.insertBefore(el, this.dom.firstChild);
66                 return !returnDom ? GET(el) : el;
67             }else{ // dh config
68                 return this.createChild(el, this.dom.firstChild, returnDom);
69             }
70         },
71         
72             /**
73              * Replaces the passed element with this element
74              * @param {Mixed} el The element to replace
75              * @return {Ext.Element} this
76              */
77             replace: function(el){
78                 el = GET(el);
79                 this.insertBefore(el);
80                 el.remove();
81                 return this;
82             },
83         
84             /**
85              * Replaces this element with the passed element
86              * @param {Mixed/Object} el The new element or a DomHelper config of an element to create
87              * @return {Ext.Element} this
88              */
89             replaceWith: function(el){
90                     var me = this;
91                 
92             if(el.nodeType || el.dom || typeof el == 'string'){
93                 el = GETDOM(el);
94                 me.dom.parentNode.insertBefore(el, me.dom);
95             }else{
96                 el = DH.insertBefore(me.dom, el);
97             }
98                 
99                 delete Ext.elCache[me.id];
100                 Ext.removeNode(me.dom);      
101                 me.id = Ext.id(me.dom = el);
102                 Ext.Element.addToCache(me.isFlyweight ? new Ext.Element(me.dom) : me);     
103             return me;
104             },
105             
106                 /**
107                  * Creates the passed DomHelper config and appends it to this element or optionally inserts it before the passed child element.
108                  * @param {Object} config DomHelper element config object.  If no tag is specified (e.g., {tag:'input'}) then a div will be
109                  * automatically generated with the specified attributes.
110                  * @param {HTMLElement} insertBefore (optional) a child element of this element
111                  * @param {Boolean} returnDom (optional) true to return the dom node instead of creating an Element
112                  * @return {Ext.Element} The new child element
113                  */
114                 createChild: function(config, insertBefore, returnDom){
115                     config = config || {tag:'div'};
116                     return insertBefore ? 
117                            DH.insertBefore(insertBefore, config, returnDom !== true) :  
118                            DH[!this.dom.firstChild ? 'overwrite' : 'append'](this.dom, config,  returnDom !== true);
119                 },
120                 
121                 /**
122                  * Creates and wraps this element with another element
123                  * @param {Object} config (optional) DomHelper element config object for the wrapper element or null for an empty div
124                  * @param {Boolean} returnDom (optional) True to return the raw DOM element instead of Ext.Element
125                  * @return {HTMLElement/Element} The newly created wrapper element
126                  */
127                 wrap: function(config, returnDom){        
128                     var newEl = DH.insertBefore(this.dom, config || {tag: "div"}, !returnDom);
129                     newEl.dom ? newEl.dom.appendChild(this.dom) : newEl.appendChild(this.dom);
130                     return newEl;
131                 },
132                 
133                 /**
134                  * Inserts an html fragment into this element
135                  * @param {String} where Where to insert the html in relation to this element - beforeBegin, afterBegin, beforeEnd, afterEnd.
136                  * @param {String} html The HTML fragment
137                  * @param {Boolean} returnEl (optional) True to return an Ext.Element (defaults to false)
138                  * @return {HTMLElement/Ext.Element} The inserted node (or nearest related if more than 1 inserted)
139                  */
140                 insertHtml : function(where, html, returnEl){
141                     var el = DH.insertHtml(where, this.dom, html);
142                     return returnEl ? Ext.get(el) : el;
143                 }
144         }
145 }());