3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.Layer"></div>/**
16 * @extends Ext.Element
17 * An extended {@link Ext.Element} object that supports a shadow and shim, constrain to viewport and
18 * automatic maintaining of shadow/shim positions.
19 * @cfg {Boolean} shim False to disable the iframe shim in browsers which need one (defaults to true)
20 * @cfg {String/Boolean} shadow True to automatically create an {@link Ext.Shadow}, or a string indicating the
21 * shadow's display {@link Ext.Shadow#mode}. False to disable the shadow. (defaults to false)
22 * @cfg {Object} dh DomHelper object config to create element with (defaults to {tag: 'div', cls: 'x-layer'}).
23 * @cfg {Boolean} constrain False to disable constrain to viewport (defaults to true)
24 * @cfg {String} cls CSS class to add to the element
25 * @cfg {Number} zindex Starting z-index (defaults to 11000)
26 * @cfg {Number} shadowOffset Number of pixels to offset the shadow (defaults to 4)
27 * @cfg {Boolean} useDisplay
28 * Defaults to use css offsets to hide the Layer. Specify <tt>true</tt>
29 * to use css style <tt>'display:none;'</tt> to hide the Layer.
31 * @param {Object} config An object with config options.
32 * @param {String/HTMLElement} existingEl (optional) Uses an existing DOM element. If the element is not found it creates it.
35 Ext.Layer = function(config, existingEl){
36 config = config || {};
37 var dh = Ext.DomHelper;
38 var cp = config.parentEl, pel = cp ? Ext.getDom(cp) : document.body;
40 this.dom = Ext.getDom(existingEl);
43 var o = config.dh || {tag: 'div', cls: 'x-layer'};
44 this.dom = dh.append(pel, o);
47 this.addClass(config.cls);
49 this.constrain = config.constrain !== false;
50 this.setVisibilityMode(Ext.Element.VISIBILITY);
52 this.id = this.dom.id = config.id;
54 this.id = Ext.id(this.dom);
56 this.zindex = config.zindex || this.getZIndex();
57 this.position('absolute', this.zindex);
59 this.shadowOffset = config.shadowOffset || 4;
60 this.shadow = new Ext.Shadow({
61 offset : this.shadowOffset,
65 this.shadowOffset = 0;
67 this.useShim = config.shim !== false && Ext.useShims;
68 this.useDisplay = config.useDisplay;
72 var supr = Ext.Element.prototype;
74 // shims are shared among layer to keep from having 100 iframes
77 Ext.extend(Ext.Layer, Ext.Element, {
79 getZIndex : function(){
80 return this.zindex || parseInt((this.getShim() || this).getStyle('z-index'), 10) || 11000;
90 var shim = shims.shift();
92 shim = this.createShim();
93 shim.enableDisplayMode('block');
94 shim.dom.style.display = 'none';
95 shim.dom.style.visibility = 'visible';
97 var pn = this.dom.parentNode;
98 if(shim.dom.parentNode != pn){
99 pn.insertBefore(shim.dom, this.dom);
101 shim.setStyle('z-index', this.getZIndex()-2);
106 hideShim : function(){
108 this.shim.setDisplayed(false);
109 shims.push(this.shim);
114 disableShadow : function(){
116 this.shadowDisabled = true;
118 this.lastShadowOffset = this.shadowOffset;
119 this.shadowOffset = 0;
123 enableShadow : function(show){
125 this.shadowDisabled = false;
126 this.shadowOffset = this.lastShadowOffset;
127 delete this.lastShadowOffset;
135 // this code can execute repeatedly in milliseconds (i.e. during a drag) so
136 // code size was sacrificed for effeciency (e.g. no getBox/setBox, no XY calls)
137 sync : function(doShow){
138 var sw = this.shadow;
139 if(!this.updating && this.isVisible() && (sw || this.useShim)){
140 var sh = this.getShim();
142 var w = this.getWidth(),
143 h = this.getHeight();
145 var l = this.getLeft(true),
146 t = this.getTop(true);
148 if(sw && !this.shadowDisabled){
149 if(doShow && !sw.isVisible()){
152 sw.realign(l, t, w, h);
158 // fit the shim behind the shadow, so it is shimmed too
159 var a = sw.adjusts, s = sh.dom.style;
160 s.left = (Math.min(l, l+a.l))+'px';
161 s.top = (Math.min(t, t+a.t))+'px';
162 s.width = (w+a.w)+'px';
163 s.height = (h+a.h)+'px';
177 destroy : function(){
182 this.removeAllListeners();
183 Ext.removeNode(this.dom);
184 Ext.Element.uncache(this.id);
192 beginUpdate : function(){
193 this.updating = true;
197 endUpdate : function(){
198 this.updating = false;
203 hideUnders : function(negOffset){
211 constrainXY : function(){
213 var vw = Ext.lib.Dom.getViewWidth(),
214 vh = Ext.lib.Dom.getViewHeight();
215 var s = Ext.getDoc().getScroll();
217 var xy = this.getXY();
218 var x = xy[0], y = xy[1];
219 var so = this.shadowOffset;
220 var w = this.dom.offsetWidth+so, h = this.dom.offsetHeight+so;
221 // only move it if it needs it
223 // first validate right/bottom
224 if((x + w) > vw+s.left){
228 if((y + h) > vh+s.top){
232 // then make sure top/left isn't negative
243 var ay = this.avoidY;
244 if(y <= ay && (y+h) >= ay){
250 supr.setXY.call(this, xy);
257 isVisible : function(){
262 showAction : function(){
263 this.visible = true; // track visibility to prevent getStyle calls
264 if(this.useDisplay === true){
265 this.setDisplayed('');
266 }else if(this.lastXY){
267 supr.setXY.call(this, this.lastXY);
268 }else if(this.lastLT){
269 supr.setLeftTop.call(this, this.lastLT[0], this.lastLT[1]);
274 hideAction : function(){
275 this.visible = false;
276 if(this.useDisplay === true){
277 this.setDisplayed(false);
279 this.setLeftTop(-10000,-10000);
283 // overridden Element method
284 setVisible : function(v, a, d, c, e){
294 }.createDelegate(this);
295 supr.setVisible.call(this, true, true, d, cb, e);
298 this.hideUnders(true);
307 }.createDelegate(this);
309 supr.setVisible.call(this, v, a, d, cb, e);
319 storeXY : function(xy){
324 storeLeftTop : function(left, top){
326 this.lastLT = [left, top];
330 beforeFx : function(){
332 return Ext.Layer.superclass.beforeFx.apply(this, arguments);
336 afterFx : function(){
337 Ext.Layer.superclass.afterFx.apply(this, arguments);
338 this.sync(this.isVisible());
342 beforeAction : function(){
343 if(!this.updating && this.shadow){
348 // overridden Element method
349 setLeft : function(left){
350 this.storeLeftTop(left, this.getTop(true));
351 supr.setLeft.apply(this, arguments);
356 setTop : function(top){
357 this.storeLeftTop(this.getLeft(true), top);
358 supr.setTop.apply(this, arguments);
363 setLeftTop : function(left, top){
364 this.storeLeftTop(left, top);
365 supr.setLeftTop.apply(this, arguments);
370 setXY : function(xy, a, d, c, e){
374 var cb = this.createCB(c);
375 supr.setXY.call(this, xy, a, d, cb, e);
383 createCB : function(c){
394 // overridden Element method
395 setX : function(x, a, d, c, e){
396 this.setXY([x, this.getY()], a, d, c, e);
400 // overridden Element method
401 setY : function(y, a, d, c, e){
402 this.setXY([this.getX(), y], a, d, c, e);
406 // overridden Element method
407 setSize : function(w, h, a, d, c, e){
409 var cb = this.createCB(c);
410 supr.setSize.call(this, w, h, a, d, cb, e);
417 // overridden Element method
418 setWidth : function(w, a, d, c, e){
420 var cb = this.createCB(c);
421 supr.setWidth.call(this, w, a, d, cb, e);
428 // overridden Element method
429 setHeight : function(h, a, d, c, e){
431 var cb = this.createCB(c);
432 supr.setHeight.call(this, h, a, d, cb, e);
439 // overridden Element method
440 setBounds : function(x, y, w, h, a, d, c, e){
442 var cb = this.createCB(c);
444 this.storeXY([x, y]);
445 supr.setXY.call(this, [x, y]);
446 supr.setSize.call(this, w, h, a, d, cb, e);
449 supr.setBounds.call(this, x, y, w, h, a, d, cb, e);
455 * Sets the z-index of this layer and adjusts any shadow and shim z-indexes. The layer z-index is automatically
456 * incremented by two more than the value passed in so that it always shows above any shadow or shim (the shadow
457 * element, if any, will be assigned z-index + 1, and the shim element, if any, will be assigned the unmodified z-index).
458 * @param {Number} zindex The new z-index to set
459 * @return {this} The Layer
461 setZIndex : function(zindex){
462 this.zindex = zindex;
463 this.setStyle('z-index', zindex + 2);
465 this.shadow.setZIndex(zindex + 1);
468 this.shim.setStyle('z-index', zindex);