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