3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
10 * An extended {@link Ext.Element} object that supports a shadow and shim, constrain to viewport and
11 * automatic maintaining of shadow/shim positions.
12 * @cfg {Boolean} shim False to disable the iframe shim in browsers which need one (defaults to true)
13 * @cfg {String/Boolean} shadow True to automatically create an {@link Ext.Shadow}, or a string indicating the
14 * shadow's display {@link Ext.Shadow#mode}. False to disable the shadow. (defaults to false)
15 * @cfg {Object} dh DomHelper object config to create element with (defaults to {tag: 'div', cls: 'x-layer'}).
16 * @cfg {Boolean} constrain False to disable constrain to viewport (defaults to true)
17 * @cfg {String} cls CSS class to add to the element
18 * @cfg {Number} zindex Starting z-index (defaults to 11000)
19 * @cfg {Number} shadowOffset Number of pixels to offset the shadow (defaults to 4)
20 * @cfg {Boolean} useDisplay
21 * Defaults to use css offsets to hide the Layer. Specify <tt>true</tt>
22 * to use css style <tt>'display:none;'</tt> to hide the Layer.
24 * @param {Object} config An object with config options.
25 * @param {String/HTMLElement} existingEl (optional) Uses an existing DOM element. If the element is not found it creates it.
28 Ext.Layer = function(config, existingEl){
29 config = config || {};
30 var dh = Ext.DomHelper,
31 cp = config.parentEl, pel = cp ? Ext.getDom(cp) : document.body;
34 this.dom = Ext.getDom(existingEl);
37 var o = config.dh || {tag: 'div', cls: 'x-layer'};
38 this.dom = dh.append(pel, o);
41 this.addClass(config.cls);
43 this.constrain = config.constrain !== false;
44 this.setVisibilityMode(Ext.Element.VISIBILITY);
46 this.id = this.dom.id = config.id;
48 this.id = Ext.id(this.dom);
50 this.zindex = config.zindex || this.getZIndex();
51 this.position('absolute', this.zindex);
53 this.shadowOffset = config.shadowOffset || 4;
54 this.shadow = new Ext.Shadow({
55 offset : this.shadowOffset,
59 this.shadowOffset = 0;
61 this.useShim = config.shim !== false && Ext.useShims;
62 this.useDisplay = config.useDisplay;
66 var supr = Ext.Element.prototype;
68 // shims are shared among layer to keep from having 100 iframes
71 Ext.extend(Ext.Layer, Ext.Element, {
73 getZIndex : function(){
74 return this.zindex || parseInt((this.getShim() || this).getStyle('z-index'), 10) || 11000;
84 var shim = shims.shift();
86 shim = this.createShim();
87 shim.enableDisplayMode('block');
88 shim.dom.style.display = 'none';
89 shim.dom.style.visibility = 'visible';
91 var pn = this.dom.parentNode;
92 if(shim.dom.parentNode != pn){
93 pn.insertBefore(shim.dom, this.dom);
95 shim.setStyle('z-index', this.getZIndex()-2);
100 hideShim : function(){
102 this.shim.setDisplayed(false);
103 shims.push(this.shim);
108 disableShadow : function(){
110 this.shadowDisabled = true;
112 this.lastShadowOffset = this.shadowOffset;
113 this.shadowOffset = 0;
117 enableShadow : function(show){
119 this.shadowDisabled = false;
120 this.shadowOffset = this.lastShadowOffset;
121 delete this.lastShadowOffset;
129 // this code can execute repeatedly in milliseconds (i.e. during a drag) so
130 // code size was sacrificed for effeciency (e.g. no getBox/setBox, no XY calls)
131 sync : function(doShow){
132 var shadow = this.shadow;
133 if(!this.updating && this.isVisible() && (shadow || this.useShim)){
134 var shim = this.getShim(),
136 h = this.getHeight(),
137 l = this.getLeft(true),
138 t = this.getTop(true);
140 if(shadow && !this.shadowDisabled){
141 if(doShow && !shadow.isVisible()){
144 shadow.realign(l, t, w, h);
150 // fit the shim behind the shadow, so it is shimmed too
151 var shadowAdj = shadow.el.getXY(), shimStyle = shim.dom.style,
152 shadowSize = shadow.el.getSize();
153 shimStyle.left = (shadowAdj[0])+'px';
154 shimStyle.top = (shadowAdj[1])+'px';
155 shimStyle.width = (shadowSize.width)+'px';
156 shimStyle.height = (shadowSize.height)+'px';
163 shim.setLeftTop(l, t);
169 destroy : function(){
174 this.removeAllListeners();
175 Ext.removeNode(this.dom);
184 beginUpdate : function(){
185 this.updating = true;
189 endUpdate : function(){
190 this.updating = false;
195 hideUnders : function(negOffset){
203 constrainXY : function(){
205 var vw = Ext.lib.Dom.getViewWidth(),
206 vh = Ext.lib.Dom.getViewHeight();
207 var s = Ext.getDoc().getScroll();
209 var xy = this.getXY();
210 var x = xy[0], y = xy[1];
211 var so = this.shadowOffset;
212 var w = this.dom.offsetWidth+so, h = this.dom.offsetHeight+so;
213 // only move it if it needs it
215 // first validate right/bottom
216 if((x + w) > vw+s.left){
220 if((y + h) > vh+s.top){
224 // then make sure top/left isn't negative
235 var ay = this.avoidY;
236 if(y <= ay && (y+h) >= ay){
242 supr.setXY.call(this, xy);
249 getConstrainOffset : function(){
250 return this.shadowOffset;
253 isVisible : function(){
258 showAction : function(){
259 this.visible = true; // track visibility to prevent getStyle calls
260 if(this.useDisplay === true){
261 this.setDisplayed('');
262 }else if(this.lastXY){
263 supr.setXY.call(this, this.lastXY);
264 }else if(this.lastLT){
265 supr.setLeftTop.call(this, this.lastLT[0], this.lastLT[1]);
270 hideAction : function(){
271 this.visible = false;
272 if(this.useDisplay === true){
273 this.setDisplayed(false);
275 this.setLeftTop(-10000,-10000);
279 // overridden Element method
280 setVisible : function(v, a, d, c, e){
290 }.createDelegate(this);
291 supr.setVisible.call(this, true, true, d, cb, e);
294 this.hideUnders(true);
303 }.createDelegate(this);
305 supr.setVisible.call(this, v, a, d, cb, e);
315 storeXY : function(xy){
320 storeLeftTop : function(left, top){
322 this.lastLT = [left, top];
326 beforeFx : function(){
328 return Ext.Layer.superclass.beforeFx.apply(this, arguments);
332 afterFx : function(){
333 Ext.Layer.superclass.afterFx.apply(this, arguments);
334 this.sync(this.isVisible());
338 beforeAction : function(){
339 if(!this.updating && this.shadow){
344 // overridden Element method
345 setLeft : function(left){
346 this.storeLeftTop(left, this.getTop(true));
347 supr.setLeft.apply(this, arguments);
352 setTop : function(top){
353 this.storeLeftTop(this.getLeft(true), top);
354 supr.setTop.apply(this, arguments);
359 setLeftTop : function(left, top){
360 this.storeLeftTop(left, top);
361 supr.setLeftTop.apply(this, arguments);
366 setXY : function(xy, a, d, c, e){
370 var cb = this.createCB(c);
371 supr.setXY.call(this, xy, a, d, cb, e);
379 createCB : function(c){
390 // overridden Element method
391 setX : function(x, a, d, c, e){
392 this.setXY([x, this.getY()], a, d, c, e);
396 // overridden Element method
397 setY : function(y, a, d, c, e){
398 this.setXY([this.getX(), y], a, d, c, e);
402 // overridden Element method
403 setSize : function(w, h, a, d, c, e){
405 var cb = this.createCB(c);
406 supr.setSize.call(this, w, h, a, d, cb, e);
413 // overridden Element method
414 setWidth : function(w, a, d, c, e){
416 var cb = this.createCB(c);
417 supr.setWidth.call(this, w, a, d, cb, e);
424 // overridden Element method
425 setHeight : function(h, a, d, c, e){
427 var cb = this.createCB(c);
428 supr.setHeight.call(this, h, a, d, cb, e);
435 // overridden Element method
436 setBounds : function(x, y, w, h, a, d, c, e){
438 var cb = this.createCB(c);
440 this.storeXY([x, y]);
441 supr.setXY.call(this, [x, y]);
442 supr.setSize.call(this, w, h, a, d, cb, e);
445 supr.setBounds.call(this, x, y, w, h, a, d, cb, e);
451 * Sets the z-index of this layer and adjusts any shadow and shim z-indexes. The layer z-index is automatically
452 * incremented by two more than the value passed in so that it always shows above any shadow or shim (the shadow
453 * element, if any, will be assigned z-index + 1, and the shim element, if any, will be assigned the unmodified z-index).
454 * @param {Number} zindex The new z-index to set
455 * @return {this} The Layer
457 setZIndex : function(zindex){
458 this.zindex = zindex;
459 this.setStyle('z-index', zindex + 2);
461 this.shadow.setZIndex(zindex + 1);
464 this.shim.setStyle('z-index', zindex);