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