]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/dom/Underlay.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / dom / Underlay.js
CommitLineData
6527f429
DM
1/**\r
2 * A class that provides an underlay element which displays behind an absolutely positioned\r
3 * target element and tracks its size and position. Abstract base class for\r
4 * {@link Ext.dom.Shadow} and {@link Ext.dom.Shim}\r
5 * \r
6 * \r
7 * @private\r
8 * @abstract\r
9 */\r
10Ext.define('Ext.dom.Underlay', {\r
11 requires: [ 'Ext.dom.UnderlayPool' ],\r
12\r
13 /**\r
14 * @cfg {Ext.dom.Element} target\r
15 * The target element\r
16 */\r
17\r
18 /**\r
19 * @cfg {Number} zIndex\r
20 * The CSS z-index to use for this underlay. Defaults to the z-index of {@link #target}.\r
21 */\r
22\r
23 constructor: function(config) {\r
24 Ext.apply(this, config);\r
25 },\r
26\r
27 /**\r
28 * @protected\r
29 * Called before the underlay is shown, immediately after its element is retrieved\r
30 * from the pool\r
31 */\r
32 beforeShow: Ext.emptyFn,\r
33\r
34 /**\r
35 * @protected\r
36 * Returns the dom element that this underlay should be inserted before.\r
37 * Defaults to the target element\r
38 * @return {Ext.dom.Element}\r
39 */\r
40 getInsertionTarget: function() {\r
41 return this.target;\r
42 },\r
43\r
44 /**\r
45 * @protected\r
46 * @return {Ext.dom.UnderlayPool}\r
47 */\r
48 getPool: function() {\r
49 return this.pool ||\r
50 (this.self.prototype.pool = new Ext.dom.UnderlayPool(this.elementConfig));\r
51 },\r
52\r
53 /**\r
54 * Hides the underlay\r
55 */\r
56 hide: function() {\r
57 var me = this,\r
58 el = me.el;\r
59 \r
60 if (el) {\r
61 el.hide();\r
62 me.getPool().checkIn(el);\r
63 me.el = null;\r
64 me.hidden = true;\r
65 }\r
66 },\r
67\r
68 /**\r
69 * Aligns the underlay to its target element\r
70 * @param {Number} [x] The x position of the target element. If not provided, the \r
71 * x position will be read from the DOM.\r
72 * @param {Number} [y] The y position of the target element. If not provided, the\r
73 * y position will be read from the DOM.\r
74 * @param {Number} [width] The width of the target element. If not provided, the\r
75 * width will be read from the DOM.\r
76 * @param {Number} [height] The height of the target element. If not provided, the\r
77 * height will be read from the DOM.\r
78 */\r
79 realign: function(x, y, width, height) {\r
80 var me = this,\r
81 el = me.el,\r
82 target = me.target,\r
83 offsets = me.offsets,\r
84 max = Math.max;\r
85\r
86 if (el) {\r
87 if (x == null) {\r
88 x = target.getX();\r
89 }\r
90\r
91 if (y == null) {\r
92 y = target.getY();\r
93 }\r
94\r
95 if (width == null) {\r
96 width = target.getWidth();\r
97 }\r
98\r
99 if (height == null) {\r
100 height = target.getHeight();\r
101 }\r
102\r
103 if (offsets) {\r
104 x = x + offsets.x;\r
105 y = y + offsets.y;\r
106 width = max(width + offsets.w, 0);\r
107 height = max(height + offsets.h, 0);\r
108 }\r
109\r
110 el.setXY([x, y]);\r
111 el.setSize(width, height);\r
112 }\r
113 },\r
114\r
115 /**\r
116 * Adjust the z-index of this underlay\r
117 * @param {Number} zIndex The new z-index\r
118 */\r
119 setZIndex: function(zIndex) {\r
120 this.zIndex = zIndex;\r
121\r
122 if (this.el) {\r
123 this.el.setStyle("z-index", zIndex);\r
124 }\r
125 },\r
126\r
127 /**\r
128 * Shows the underlay\r
129 */\r
130 show: function() {\r
131 var me = this,\r
132 target = me.target,\r
133 zIndex = me.zIndex,\r
134 el = me.el,\r
135 insertionTarget = me.getInsertionTarget().dom,\r
136 dom;\r
137\r
138 if (!el) {\r
139 el = me.el = me.getPool().checkOut();\r
140 }\r
141\r
142 me.beforeShow();\r
143\r
144 if (zIndex == null) {\r
145 // For best results, we need the underlay to be as close as possible to its\r
146 // target element in the z-index stacking order without overlaying the target\r
147 // element. Since the UnderlayPool inserted the underlay as high as possible\r
148 // in the dom tree when we checked the underlay out of the pool, we can assume\r
149 // that it comes before the target element in the dom tree, and therefore can\r
150 // give it the exact same index as the target element.\r
151 zIndex = (parseInt(target.getStyle("z-index"), 10));\r
152 }\r
153\r
154 if (zIndex) {\r
155 el.setStyle("z-index", zIndex);\r
156 }\r
157\r
158 // Overlay elements are shared, so fix position to match current owner\r
159 el.setStyle('position', me.fixed ? 'fixed' : '');\r
160\r
161 dom = el.dom;\r
162 if (dom.nextSibling !== insertionTarget) {\r
163 // inserting the underlay as the previous sibling of the target ensures that\r
164 // it will show behind the target, as long as its z-index is less than or equal\r
165 // to the z-index of the target element.\r
166 target.dom.parentNode.insertBefore(dom, insertionTarget);\r
167 }\r
168\r
169 el.show();\r
170 me.realign();\r
171 me.hidden = false;\r
172 }\r
173 \r
174});\r