3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.Layer"></div>/**
10 * @extends Ext.Element
11 * An extended {@link Ext.Element} object that supports a shadow and shim, constrain to viewport and
12 * automatic maintaining of shadow/shim positions.
13 * @cfg {Boolean} shim False to disable the iframe shim in browsers which need one (defaults to true)
14 * @cfg {String/Boolean} shadow True to automatically create an {@link Ext.Shadow}, or a string indicating the
15 * shadow's display {@link Ext.Shadow#mode}. False to disable the shadow. (defaults to false)
16 * @cfg {Object} dh DomHelper object config to create element with (defaults to {tag: 'div', cls: 'x-layer'}).
17 * @cfg {Boolean} constrain False to disable constrain to viewport (defaults to true)
18 * @cfg {String} cls CSS class to add to the element
19 * @cfg {Number} zindex Starting z-index (defaults to 11000)
20 * @cfg {Number} shadowOffset Number of pixels to offset the shadow (defaults to 4)
21 * @cfg {Boolean} useDisplay
22 * Defaults to use css offsets to hide the Layer. Specify <tt>true</tt>
23 * to use css style <tt>'display:none;'</tt> to hide the Layer.
25 * @param {Object} config An object with config options.
26 * @param {String/HTMLElement} existingEl (optional) Uses an existing DOM element. If the element is not found it creates it.
29 Ext.Layer = function(config, existingEl){
30 config = config || {};
31 var dh = Ext.DomHelper;
32 var 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 sw = this.shadow;
133 if(!this.updating && this.isVisible() && (sw || this.useShim)){
134 var sh = this.getShim();
136 var w = this.getWidth(),
137 h = this.getHeight();
139 var l = this.getLeft(true),
140 t = this.getTop(true);
142 if(sw && !this.shadowDisabled){
143 if(doShow && !sw.isVisible()){
146 sw.realign(l, t, w, h);
152 // fit the shim behind the shadow, so it is shimmed too
153 var a = sw.adjusts, s = sh.dom.style;
154 s.left = (Math.min(l, l+a.l))+'px';
155 s.top = (Math.min(t, t+a.t))+'px';
156 s.width = (w+a.w)+'px';
157 s.height = (h+a.h)+'px';
171 destroy : function(){
176 this.removeAllListeners();
177 Ext.removeNode(this.dom);
178 Ext.Element.uncache(this.id);
186 beginUpdate : function(){
187 this.updating = true;
191 endUpdate : function(){
192 this.updating = false;
197 hideUnders : function(negOffset){
205 constrainXY : function(){
207 var vw = Ext.lib.Dom.getViewWidth(),
208 vh = Ext.lib.Dom.getViewHeight();
209 var s = Ext.getDoc().getScroll();
211 var xy = this.getXY();
212 var x = xy[0], y = xy[1];
213 var so = this.shadowOffset;
214 var w = this.dom.offsetWidth+so, h = this.dom.offsetHeight+so;
215 // only move it if it needs it
217 // first validate right/bottom
218 if((x + w) > vw+s.left){
222 if((y + h) > vh+s.top){
226 // then make sure top/left isn't negative
237 var ay = this.avoidY;
238 if(y <= ay && (y+h) >= ay){
244 supr.setXY.call(this, xy);
251 isVisible : function(){
256 showAction : function(){
257 this.visible = true; // track visibility to prevent getStyle calls
258 if(this.useDisplay === true){
259 this.setDisplayed('');
260 }else if(this.lastXY){
261 supr.setXY.call(this, this.lastXY);
262 }else if(this.lastLT){
263 supr.setLeftTop.call(this, this.lastLT[0], this.lastLT[1]);
268 hideAction : function(){
269 this.visible = false;
270 if(this.useDisplay === true){
271 this.setDisplayed(false);
273 this.setLeftTop(-10000,-10000);
277 // overridden Element method
278 setVisible : function(v, a, d, c, e){
288 }.createDelegate(this);
289 supr.setVisible.call(this, true, true, d, cb, e);
292 this.hideUnders(true);
301 }.createDelegate(this);
303 supr.setVisible.call(this, v, a, d, cb, e);
313 storeXY : function(xy){
318 storeLeftTop : function(left, top){
320 this.lastLT = [left, top];
324 beforeFx : function(){
326 return Ext.Layer.superclass.beforeFx.apply(this, arguments);
330 afterFx : function(){
331 Ext.Layer.superclass.afterFx.apply(this, arguments);
332 this.sync(this.isVisible());
336 beforeAction : function(){
337 if(!this.updating && this.shadow){
342 // overridden Element method
343 setLeft : function(left){
344 this.storeLeftTop(left, this.getTop(true));
345 supr.setLeft.apply(this, arguments);
350 setTop : function(top){
351 this.storeLeftTop(this.getLeft(true), top);
352 supr.setTop.apply(this, arguments);
357 setLeftTop : function(left, top){
358 this.storeLeftTop(left, top);
359 supr.setLeftTop.apply(this, arguments);
364 setXY : function(xy, a, d, c, e){
368 var cb = this.createCB(c);
369 supr.setXY.call(this, xy, a, d, cb, e);
377 createCB : function(c){
388 // overridden Element method
389 setX : function(x, a, d, c, e){
390 this.setXY([x, this.getY()], a, d, c, e);
394 // overridden Element method
395 setY : function(y, a, d, c, e){
396 this.setXY([this.getX(), y], a, d, c, e);
400 // overridden Element method
401 setSize : function(w, h, a, d, c, e){
403 var cb = this.createCB(c);
404 supr.setSize.call(this, w, h, a, d, cb, e);
411 // overridden Element method
412 setWidth : function(w, a, d, c, e){
414 var cb = this.createCB(c);
415 supr.setWidth.call(this, w, a, d, cb, e);
422 // overridden Element method
423 setHeight : function(h, a, d, c, e){
425 var cb = this.createCB(c);
426 supr.setHeight.call(this, h, a, d, cb, e);
433 // overridden Element method
434 setBounds : function(x, y, w, h, a, d, c, e){
436 var cb = this.createCB(c);
438 this.storeXY([x, y]);
439 supr.setXY.call(this, [x, y]);
440 supr.setSize.call(this, w, h, a, d, cb, e);
443 supr.setBounds.call(this, x, y, w, h, a, d, cb, e);
449 * Sets the z-index of this layer and adjusts any shadow and shim z-indexes. The layer z-index is automatically
450 * incremented by two more than the value passed in so that it always shows above any shadow or shim (the shadow
451 * element, if any, will be assigned z-index + 1, and the shim element, if any, will be assigned the unmodified z-index).
452 * @param {Number} zindex The new z-index to set
453 * @return {this} The Layer
455 setZIndex : function(zindex){
456 this.zindex = zindex;
457 this.setStyle('z-index', zindex + 2);
459 this.shadow.setZIndex(zindex + 1);
462 this.shim.setStyle('z-index', zindex);