2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
11 * @extends Ext.Element
\r
12 * An extended {@link Ext.Element} object that supports a shadow and shim, constrain to viewport and
\r
13 * automatic maintaining of shadow/shim positions.
\r
14 * @cfg {Boolean} shim False to disable the iframe shim in browsers which need one (defaults to true)
\r
15 * @cfg {String/Boolean} shadow True to automatically create an {@link Ext.Shadow}, or a string indicating the
\r
16 * shadow's display {@link Ext.Shadow#mode}. False to disable the shadow. (defaults to false)
\r
17 * @cfg {Object} dh DomHelper object config to create element with (defaults to {tag: "div", cls: "x-layer"}).
\r
18 * @cfg {Boolean} constrain False to disable constrain to viewport (defaults to true)
\r
19 * @cfg {String} cls CSS class to add to the element
\r
20 * @cfg {Number} zindex Starting z-index (defaults to 11000)
\r
21 * @cfg {Number} shadowOffset Number of pixels to offset the shadow (defaults to 3)
\r
23 * @param {Object} config An object with config options.
\r
24 * @param {String/HTMLElement} existingEl (optional) Uses an existing DOM element. If the element is not found it creates it.
\r
27 Ext.Layer = function(config, existingEl){
\r
28 config = config || {};
\r
29 var dh = Ext.DomHelper;
\r
30 var cp = config.parentEl, pel = cp ? Ext.getDom(cp) : document.body;
\r
32 this.dom = Ext.getDom(existingEl);
\r
35 var o = config.dh || {tag: "div", cls: "x-layer"};
\r
36 this.dom = dh.append(pel, o);
\r
39 this.addClass(config.cls);
\r
41 this.constrain = config.constrain !== false;
\r
42 this.visibilityMode = Ext.Element.VISIBILITY;
\r
44 this.id = this.dom.id = config.id;
\r
46 this.id = Ext.id(this.dom);
\r
48 this.zindex = config.zindex || this.getZIndex();
\r
49 this.position("absolute", this.zindex);
\r
51 this.shadowOffset = config.shadowOffset || 4;
\r
52 this.shadow = new Ext.Shadow({
\r
53 offset : this.shadowOffset,
\r
54 mode : config.shadow
\r
57 this.shadowOffset = 0;
\r
59 this.useShim = config.shim !== false && Ext.useShims;
\r
60 this.useDisplay = config.useDisplay;
\r
64 var supr = Ext.Element.prototype;
\r
66 // shims are shared among layer to keep from having 100 iframes
\r
69 Ext.extend(Ext.Layer, Ext.Element, {
\r
71 getZIndex : function(){
\r
72 return this.zindex || parseInt(this.getStyle("z-index"), 10) || 11000;
\r
75 getShim : function(){
\r
82 var shim = shims.shift();
\r
84 shim = this.createShim();
\r
85 shim.enableDisplayMode('block');
\r
86 shim.dom.style.display = 'none';
\r
87 shim.dom.style.visibility = 'visible';
\r
89 var pn = this.dom.parentNode;
\r
90 if(shim.dom.parentNode != pn){
\r
91 pn.insertBefore(shim.dom, this.dom);
\r
93 shim.setStyle('z-index', this.getZIndex()-2);
\r
98 hideShim : function(){
\r
100 this.shim.setDisplayed(false);
\r
101 shims.push(this.shim);
\r
106 disableShadow : function(){
\r
108 this.shadowDisabled = true;
\r
109 this.shadow.hide();
\r
110 this.lastShadowOffset = this.shadowOffset;
\r
111 this.shadowOffset = 0;
\r
115 enableShadow : function(show){
\r
117 this.shadowDisabled = false;
\r
118 this.shadowOffset = this.lastShadowOffset;
\r
119 delete this.lastShadowOffset;
\r
127 // this code can execute repeatedly in milliseconds (i.e. during a drag) so
\r
128 // code size was sacrificed for effeciency (e.g. no getBox/setBox, no XY calls)
\r
129 sync : function(doShow){
\r
130 var sw = this.shadow;
\r
131 if(!this.updating && this.isVisible() && (sw || this.useShim)){
\r
132 var sh = this.getShim();
\r
134 var w = this.getWidth(),
\r
135 h = this.getHeight();
\r
137 var l = this.getLeft(true),
\r
138 t = this.getTop(true);
\r
140 if(sw && !this.shadowDisabled){
\r
141 if(doShow && !sw.isVisible()){
\r
144 sw.realign(l, t, w, h);
\r
150 // fit the shim behind the shadow, so it is shimmed too
\r
151 var a = sw.adjusts, s = sh.dom.style;
\r
152 s.left = (Math.min(l, l+a.l))+"px";
\r
153 s.top = (Math.min(t, t+a.t))+"px";
\r
154 s.width = (w+a.w)+"px";
\r
155 s.height = (h+a.h)+"px";
\r
162 sh.setLeftTop(l, t);
\r
169 destroy : function(){
\r
172 this.shadow.hide();
\r
174 this.removeAllListeners();
\r
175 Ext.removeNode(this.dom);
\r
176 Ext.Element.uncache(this.id);
\r
179 remove : function(){
\r
184 beginUpdate : function(){
\r
185 this.updating = true;
\r
189 endUpdate : function(){
\r
190 this.updating = false;
\r
195 hideUnders : function(negOffset){
\r
197 this.shadow.hide();
\r
203 constrainXY : function(){
\r
204 if(this.constrain){
\r
205 var vw = Ext.lib.Dom.getViewWidth(),
\r
206 vh = Ext.lib.Dom.getViewHeight();
\r
207 var s = Ext.getDoc().getScroll();
\r
209 var xy = this.getXY();
\r
210 var x = xy[0], y = xy[1];
\r
211 var w = this.dom.offsetWidth+this.shadowOffset, h = this.dom.offsetHeight+this.shadowOffset;
\r
212 // only move it if it needs it
\r
214 // first validate right/bottom
\r
215 if((x + w) > vw+s.left){
\r
216 x = vw - w - this.shadowOffset;
\r
219 if((y + h) > vh+s.top){
\r
220 y = vh - h - this.shadowOffset;
\r
223 // then make sure top/left isn't negative
\r
234 var ay = this.avoidY;
\r
235 if(y <= ay && (y+h) >= ay){
\r
241 supr.setXY.call(this, xy);
\r
247 isVisible : function(){
\r
248 return this.visible;
\r
252 showAction : function(){
\r
253 this.visible = true; // track visibility to prevent getStyle calls
\r
254 if(this.useDisplay === true){
\r
255 this.setDisplayed("");
\r
256 }else if(this.lastXY){
\r
257 supr.setXY.call(this, this.lastXY);
\r
258 }else if(this.lastLT){
\r
259 supr.setLeftTop.call(this, this.lastLT[0], this.lastLT[1]);
\r
264 hideAction : function(){
\r
265 this.visible = false;
\r
266 if(this.useDisplay === true){
\r
267 this.setDisplayed(false);
\r
269 this.setLeftTop(-10000,-10000);
\r
273 // overridden Element method
\r
274 setVisible : function(v, a, d, c, e){
\r
279 var cb = function(){
\r
284 }.createDelegate(this);
\r
285 supr.setVisible.call(this, true, true, d, cb, e);
\r
288 this.hideUnders(true);
\r
297 }.createDelegate(this);
\r
299 supr.setVisible.call(this, v, a, d, cb, e);
\r
308 storeXY : function(xy){
\r
309 delete this.lastLT;
\r
313 storeLeftTop : function(left, top){
\r
314 delete this.lastXY;
\r
315 this.lastLT = [left, top];
\r
319 beforeFx : function(){
\r
320 this.beforeAction();
\r
321 return Ext.Layer.superclass.beforeFx.apply(this, arguments);
\r
325 afterFx : function(){
\r
326 Ext.Layer.superclass.afterFx.apply(this, arguments);
\r
327 this.sync(this.isVisible());
\r
331 beforeAction : function(){
\r
332 if(!this.updating && this.shadow){
\r
333 this.shadow.hide();
\r
337 // overridden Element method
\r
338 setLeft : function(left){
\r
339 this.storeLeftTop(left, this.getTop(true));
\r
340 supr.setLeft.apply(this, arguments);
\r
344 setTop : function(top){
\r
345 this.storeLeftTop(this.getLeft(true), top);
\r
346 supr.setTop.apply(this, arguments);
\r
350 setLeftTop : function(left, top){
\r
351 this.storeLeftTop(left, top);
\r
352 supr.setLeftTop.apply(this, arguments);
\r
356 setXY : function(xy, a, d, c, e){
\r
358 this.beforeAction();
\r
360 var cb = this.createCB(c);
\r
361 supr.setXY.call(this, xy, a, d, cb, e);
\r
368 createCB : function(c){
\r
379 // overridden Element method
\r
380 setX : function(x, a, d, c, e){
\r
381 this.setXY([x, this.getY()], a, d, c, e);
\r
384 // overridden Element method
\r
385 setY : function(y, a, d, c, e){
\r
386 this.setXY([this.getX(), y], a, d, c, e);
\r
389 // overridden Element method
\r
390 setSize : function(w, h, a, d, c, e){
\r
391 this.beforeAction();
\r
392 var cb = this.createCB(c);
\r
393 supr.setSize.call(this, w, h, a, d, cb, e);
\r
399 // overridden Element method
\r
400 setWidth : function(w, a, d, c, e){
\r
401 this.beforeAction();
\r
402 var cb = this.createCB(c);
\r
403 supr.setWidth.call(this, w, a, d, cb, e);
\r
409 // overridden Element method
\r
410 setHeight : function(h, a, d, c, e){
\r
411 this.beforeAction();
\r
412 var cb = this.createCB(c);
\r
413 supr.setHeight.call(this, h, a, d, cb, e);
\r
419 // overridden Element method
\r
420 setBounds : function(x, y, w, h, a, d, c, e){
\r
421 this.beforeAction();
\r
422 var cb = this.createCB(c);
\r
424 this.storeXY([x, y]);
\r
425 supr.setXY.call(this, [x, y]);
\r
426 supr.setSize.call(this, w, h, a, d, cb, e);
\r
429 supr.setBounds.call(this, x, y, w, h, a, d, cb, e);
\r
435 * Sets the z-index of this layer and adjusts any shadow and shim z-indexes. The layer z-index is automatically
\r
436 * incremented by two more than the value passed in so that it always shows above any shadow or shim (the shadow
\r
437 * element, if any, will be assigned z-index + 1, and the shim element, if any, will be assigned the unmodified z-index).
\r
438 * @param {Number} zindex The new z-index to set
\r
439 * @return {this} The Layer
\r
441 setZIndex : function(zindex){
\r
442 this.zindex = zindex;
\r
443 this.setStyle("z-index", zindex + 2);
\r
445 this.shadow.setZIndex(zindex + 1);
\r
448 this.shim.setStyle("z-index", zindex);
\r