[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.Layer |
---|
| 9 | * @extends Ext.Element |
---|
| 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. |
---|
| 23 | * @constructor |
---|
| 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. |
---|
| 26 | */ |
---|
| 27 | (function(){ |
---|
| 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; |
---|
| 32 | |
---|
| 33 | if (existingEl) { |
---|
| 34 | this.dom = Ext.getDom(existingEl); |
---|
| 35 | } |
---|
| 36 | if(!this.dom){ |
---|
| 37 | var o = config.dh || {tag: 'div', cls: 'x-layer'}; |
---|
| 38 | this.dom = dh.append(pel, o); |
---|
| 39 | } |
---|
| 40 | if(config.cls){ |
---|
| 41 | this.addClass(config.cls); |
---|
| 42 | } |
---|
| 43 | this.constrain = config.constrain !== false; |
---|
| 44 | this.setVisibilityMode(Ext.Element.VISIBILITY); |
---|
| 45 | if(config.id){ |
---|
| 46 | this.id = this.dom.id = config.id; |
---|
| 47 | }else{ |
---|
| 48 | this.id = Ext.id(this.dom); |
---|
| 49 | } |
---|
| 50 | this.zindex = config.zindex || this.getZIndex(); |
---|
| 51 | this.position('absolute', this.zindex); |
---|
| 52 | if(config.shadow){ |
---|
| 53 | this.shadowOffset = config.shadowOffset || 4; |
---|
| 54 | this.shadow = new Ext.Shadow({ |
---|
| 55 | offset : this.shadowOffset, |
---|
| 56 | mode : config.shadow |
---|
| 57 | }); |
---|
| 58 | }else{ |
---|
| 59 | this.shadowOffset = 0; |
---|
| 60 | } |
---|
| 61 | this.useShim = config.shim !== false && Ext.useShims; |
---|
| 62 | this.useDisplay = config.useDisplay; |
---|
| 63 | this.hide(); |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | var supr = Ext.Element.prototype; |
---|
| 67 | |
---|
| 68 | // shims are shared among layer to keep from having 100 iframes |
---|
| 69 | var shims = []; |
---|
| 70 | |
---|
| 71 | Ext.extend(Ext.Layer, Ext.Element, { |
---|
| 72 | |
---|
| 73 | getZIndex : function(){ |
---|
| 74 | return this.zindex || parseInt((this.getShim() || this).getStyle('z-index'), 10) || 11000; |
---|
| 75 | }, |
---|
| 76 | |
---|
| 77 | getShim : function(){ |
---|
| 78 | if(!this.useShim){ |
---|
| 79 | return null; |
---|
| 80 | } |
---|
| 81 | if(this.shim){ |
---|
| 82 | return this.shim; |
---|
| 83 | } |
---|
| 84 | var shim = shims.shift(); |
---|
| 85 | if(!shim){ |
---|
| 86 | shim = this.createShim(); |
---|
| 87 | shim.enableDisplayMode('block'); |
---|
| 88 | shim.dom.style.display = 'none'; |
---|
| 89 | shim.dom.style.visibility = 'visible'; |
---|
| 90 | } |
---|
| 91 | var pn = this.dom.parentNode; |
---|
| 92 | if(shim.dom.parentNode != pn){ |
---|
| 93 | pn.insertBefore(shim.dom, this.dom); |
---|
| 94 | } |
---|
| 95 | shim.setStyle('z-index', this.getZIndex()-2); |
---|
| 96 | this.shim = shim; |
---|
| 97 | return shim; |
---|
| 98 | }, |
---|
| 99 | |
---|
| 100 | hideShim : function(){ |
---|
| 101 | if(this.shim){ |
---|
| 102 | this.shim.setDisplayed(false); |
---|
| 103 | shims.push(this.shim); |
---|
| 104 | delete this.shim; |
---|
| 105 | } |
---|
| 106 | }, |
---|
| 107 | |
---|
| 108 | disableShadow : function(){ |
---|
| 109 | if(this.shadow){ |
---|
| 110 | this.shadowDisabled = true; |
---|
| 111 | this.shadow.hide(); |
---|
| 112 | this.lastShadowOffset = this.shadowOffset; |
---|
| 113 | this.shadowOffset = 0; |
---|
| 114 | } |
---|
| 115 | }, |
---|
| 116 | |
---|
| 117 | enableShadow : function(show){ |
---|
| 118 | if(this.shadow){ |
---|
| 119 | this.shadowDisabled = false; |
---|
| 120 | if(Ext.isDefined(this.lastShadowOffset)) { |
---|
| 121 | this.shadowOffset = this.lastShadowOffset; |
---|
| 122 | delete this.lastShadowOffset; |
---|
| 123 | } |
---|
| 124 | if(show){ |
---|
| 125 | this.sync(true); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | }, |
---|
| 129 | |
---|
| 130 | // private |
---|
| 131 | // this code can execute repeatedly in milliseconds (i.e. during a drag) so |
---|
| 132 | // code size was sacrificed for effeciency (e.g. no getBox/setBox, no XY calls) |
---|
| 133 | sync : function(doShow){ |
---|
| 134 | var shadow = this.shadow; |
---|
| 135 | if(!this.updating && this.isVisible() && (shadow || this.useShim)){ |
---|
| 136 | var shim = this.getShim(), |
---|
| 137 | w = this.getWidth(), |
---|
| 138 | h = this.getHeight(), |
---|
| 139 | l = this.getLeft(true), |
---|
| 140 | t = this.getTop(true); |
---|
| 141 | |
---|
| 142 | if(shadow && !this.shadowDisabled){ |
---|
| 143 | if(doShow && !shadow.isVisible()){ |
---|
| 144 | shadow.show(this); |
---|
| 145 | }else{ |
---|
| 146 | shadow.realign(l, t, w, h); |
---|
| 147 | } |
---|
| 148 | if(shim){ |
---|
| 149 | if(doShow){ |
---|
| 150 | shim.show(); |
---|
| 151 | } |
---|
| 152 | // fit the shim behind the shadow, so it is shimmed too |
---|
| 153 | var shadowAdj = shadow.el.getXY(), shimStyle = shim.dom.style, |
---|
| 154 | shadowSize = shadow.el.getSize(); |
---|
| 155 | shimStyle.left = (shadowAdj[0])+'px'; |
---|
| 156 | shimStyle.top = (shadowAdj[1])+'px'; |
---|
| 157 | shimStyle.width = (shadowSize.width)+'px'; |
---|
| 158 | shimStyle.height = (shadowSize.height)+'px'; |
---|
| 159 | } |
---|
| 160 | }else if(shim){ |
---|
| 161 | if(doShow){ |
---|
| 162 | shim.show(); |
---|
| 163 | } |
---|
| 164 | shim.setSize(w, h); |
---|
| 165 | shim.setLeftTop(l, t); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | }, |
---|
| 169 | |
---|
| 170 | // private |
---|
| 171 | destroy : function(){ |
---|
| 172 | this.hideShim(); |
---|
| 173 | if(this.shadow){ |
---|
| 174 | this.shadow.hide(); |
---|
| 175 | } |
---|
| 176 | this.removeAllListeners(); |
---|
| 177 | Ext.removeNode(this.dom); |
---|
| 178 | delete this.dom; |
---|
| 179 | }, |
---|
| 180 | |
---|
| 181 | remove : function(){ |
---|
| 182 | this.destroy(); |
---|
| 183 | }, |
---|
| 184 | |
---|
| 185 | // private |
---|
| 186 | beginUpdate : function(){ |
---|
| 187 | this.updating = true; |
---|
| 188 | }, |
---|
| 189 | |
---|
| 190 | // private |
---|
| 191 | endUpdate : function(){ |
---|
| 192 | this.updating = false; |
---|
| 193 | this.sync(true); |
---|
| 194 | }, |
---|
| 195 | |
---|
| 196 | // private |
---|
| 197 | hideUnders : function(negOffset){ |
---|
| 198 | if(this.shadow){ |
---|
| 199 | this.shadow.hide(); |
---|
| 200 | } |
---|
| 201 | this.hideShim(); |
---|
| 202 | }, |
---|
| 203 | |
---|
| 204 | // private |
---|
| 205 | constrainXY : function(){ |
---|
| 206 | if(this.constrain){ |
---|
| 207 | var vw = Ext.lib.Dom.getViewWidth(), |
---|
| 208 | vh = Ext.lib.Dom.getViewHeight(); |
---|
| 209 | var s = Ext.getDoc().getScroll(); |
---|
| 210 | |
---|
| 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 |
---|
| 216 | var moved = false; |
---|
| 217 | // first validate right/bottom |
---|
| 218 | if((x + w) > vw+s.left){ |
---|
| 219 | x = vw - w - so; |
---|
| 220 | moved = true; |
---|
| 221 | } |
---|
| 222 | if((y + h) > vh+s.top){ |
---|
| 223 | y = vh - h - so; |
---|
| 224 | moved = true; |
---|
| 225 | } |
---|
| 226 | // then make sure top/left isn't negative |
---|
| 227 | if(x < s.left){ |
---|
| 228 | x = s.left; |
---|
| 229 | moved = true; |
---|
| 230 | } |
---|
| 231 | if(y < s.top){ |
---|
| 232 | y = s.top; |
---|
| 233 | moved = true; |
---|
| 234 | } |
---|
| 235 | if(moved){ |
---|
| 236 | if(this.avoidY){ |
---|
| 237 | var ay = this.avoidY; |
---|
| 238 | if(y <= ay && (y+h) >= ay){ |
---|
| 239 | y = ay-h-5; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | xy = [x, y]; |
---|
| 243 | this.storeXY(xy); |
---|
| 244 | supr.setXY.call(this, xy); |
---|
| 245 | this.sync(); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | return this; |
---|
| 249 | }, |
---|
| 250 | |
---|
| 251 | getConstrainOffset : function(){ |
---|
| 252 | return this.shadowOffset; |
---|
| 253 | }, |
---|
| 254 | |
---|
| 255 | isVisible : function(){ |
---|
| 256 | return this.visible; |
---|
| 257 | }, |
---|
| 258 | |
---|
| 259 | // private |
---|
| 260 | showAction : function(){ |
---|
| 261 | this.visible = true; // track visibility to prevent getStyle calls |
---|
| 262 | if(this.useDisplay === true){ |
---|
| 263 | this.setDisplayed(''); |
---|
| 264 | }else if(this.lastXY){ |
---|
| 265 | supr.setXY.call(this, this.lastXY); |
---|
| 266 | }else if(this.lastLT){ |
---|
| 267 | supr.setLeftTop.call(this, this.lastLT[0], this.lastLT[1]); |
---|
| 268 | } |
---|
| 269 | }, |
---|
| 270 | |
---|
| 271 | // private |
---|
| 272 | hideAction : function(){ |
---|
| 273 | this.visible = false; |
---|
| 274 | if(this.useDisplay === true){ |
---|
| 275 | this.setDisplayed(false); |
---|
| 276 | }else{ |
---|
| 277 | this.setLeftTop(-10000,-10000); |
---|
| 278 | } |
---|
| 279 | }, |
---|
| 280 | |
---|
| 281 | // overridden Element method |
---|
| 282 | setVisible : function(v, a, d, c, e){ |
---|
| 283 | if(v){ |
---|
| 284 | this.showAction(); |
---|
| 285 | } |
---|
| 286 | if(a && v){ |
---|
| 287 | var cb = function(){ |
---|
| 288 | this.sync(true); |
---|
| 289 | if(c){ |
---|
| 290 | c(); |
---|
| 291 | } |
---|
| 292 | }.createDelegate(this); |
---|
| 293 | supr.setVisible.call(this, true, true, d, cb, e); |
---|
| 294 | }else{ |
---|
| 295 | if(!v){ |
---|
| 296 | this.hideUnders(true); |
---|
| 297 | } |
---|
| 298 | var cb = c; |
---|
| 299 | if(a){ |
---|
| 300 | cb = function(){ |
---|
| 301 | this.hideAction(); |
---|
| 302 | if(c){ |
---|
| 303 | c(); |
---|
| 304 | } |
---|
| 305 | }.createDelegate(this); |
---|
| 306 | } |
---|
| 307 | supr.setVisible.call(this, v, a, d, cb, e); |
---|
| 308 | if(v){ |
---|
| 309 | this.sync(true); |
---|
| 310 | }else if(!a){ |
---|
| 311 | this.hideAction(); |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | return this; |
---|
| 315 | }, |
---|
| 316 | |
---|
| 317 | storeXY : function(xy){ |
---|
| 318 | delete this.lastLT; |
---|
| 319 | this.lastXY = xy; |
---|
| 320 | }, |
---|
| 321 | |
---|
| 322 | storeLeftTop : function(left, top){ |
---|
| 323 | delete this.lastXY; |
---|
| 324 | this.lastLT = [left, top]; |
---|
| 325 | }, |
---|
| 326 | |
---|
| 327 | // private |
---|
| 328 | beforeFx : function(){ |
---|
| 329 | this.beforeAction(); |
---|
| 330 | return Ext.Layer.superclass.beforeFx.apply(this, arguments); |
---|
| 331 | }, |
---|
| 332 | |
---|
| 333 | // private |
---|
| 334 | afterFx : function(){ |
---|
| 335 | Ext.Layer.superclass.afterFx.apply(this, arguments); |
---|
| 336 | this.sync(this.isVisible()); |
---|
| 337 | }, |
---|
| 338 | |
---|
| 339 | // private |
---|
| 340 | beforeAction : function(){ |
---|
| 341 | if(!this.updating && this.shadow){ |
---|
| 342 | this.shadow.hide(); |
---|
| 343 | } |
---|
| 344 | }, |
---|
| 345 | |
---|
| 346 | // overridden Element method |
---|
| 347 | setLeft : function(left){ |
---|
| 348 | this.storeLeftTop(left, this.getTop(true)); |
---|
| 349 | supr.setLeft.apply(this, arguments); |
---|
| 350 | this.sync(); |
---|
| 351 | return this; |
---|
| 352 | }, |
---|
| 353 | |
---|
| 354 | setTop : function(top){ |
---|
| 355 | this.storeLeftTop(this.getLeft(true), top); |
---|
| 356 | supr.setTop.apply(this, arguments); |
---|
| 357 | this.sync(); |
---|
| 358 | return this; |
---|
| 359 | }, |
---|
| 360 | |
---|
| 361 | setLeftTop : function(left, top){ |
---|
| 362 | this.storeLeftTop(left, top); |
---|
| 363 | supr.setLeftTop.apply(this, arguments); |
---|
| 364 | this.sync(); |
---|
| 365 | return this; |
---|
| 366 | }, |
---|
| 367 | |
---|
| 368 | setXY : function(xy, a, d, c, e){ |
---|
| 369 | this.fixDisplay(); |
---|
| 370 | this.beforeAction(); |
---|
| 371 | this.storeXY(xy); |
---|
| 372 | var cb = this.createCB(c); |
---|
| 373 | supr.setXY.call(this, xy, a, d, cb, e); |
---|
| 374 | if(!a){ |
---|
| 375 | cb(); |
---|
| 376 | } |
---|
| 377 | return this; |
---|
| 378 | }, |
---|
| 379 | |
---|
| 380 | // private |
---|
| 381 | createCB : function(c){ |
---|
| 382 | var el = this; |
---|
| 383 | return function(){ |
---|
| 384 | el.constrainXY(); |
---|
| 385 | el.sync(true); |
---|
| 386 | if(c){ |
---|
| 387 | c(); |
---|
| 388 | } |
---|
| 389 | }; |
---|
| 390 | }, |
---|
| 391 | |
---|
| 392 | // overridden Element method |
---|
| 393 | setX : function(x, a, d, c, e){ |
---|
| 394 | this.setXY([x, this.getY()], a, d, c, e); |
---|
| 395 | return this; |
---|
| 396 | }, |
---|
| 397 | |
---|
| 398 | // overridden Element method |
---|
| 399 | setY : function(y, a, d, c, e){ |
---|
| 400 | this.setXY([this.getX(), y], a, d, c, e); |
---|
| 401 | return this; |
---|
| 402 | }, |
---|
| 403 | |
---|
| 404 | // overridden Element method |
---|
| 405 | setSize : function(w, h, a, d, c, e){ |
---|
| 406 | this.beforeAction(); |
---|
| 407 | var cb = this.createCB(c); |
---|
| 408 | supr.setSize.call(this, w, h, a, d, cb, e); |
---|
| 409 | if(!a){ |
---|
| 410 | cb(); |
---|
| 411 | } |
---|
| 412 | return this; |
---|
| 413 | }, |
---|
| 414 | |
---|
| 415 | // overridden Element method |
---|
| 416 | setWidth : function(w, a, d, c, e){ |
---|
| 417 | this.beforeAction(); |
---|
| 418 | var cb = this.createCB(c); |
---|
| 419 | supr.setWidth.call(this, w, a, d, cb, e); |
---|
| 420 | if(!a){ |
---|
| 421 | cb(); |
---|
| 422 | } |
---|
| 423 | return this; |
---|
| 424 | }, |
---|
| 425 | |
---|
| 426 | // overridden Element method |
---|
| 427 | setHeight : function(h, a, d, c, e){ |
---|
| 428 | this.beforeAction(); |
---|
| 429 | var cb = this.createCB(c); |
---|
| 430 | supr.setHeight.call(this, h, a, d, cb, e); |
---|
| 431 | if(!a){ |
---|
| 432 | cb(); |
---|
| 433 | } |
---|
| 434 | return this; |
---|
| 435 | }, |
---|
| 436 | |
---|
| 437 | // overridden Element method |
---|
| 438 | setBounds : function(x, y, w, h, a, d, c, e){ |
---|
| 439 | this.beforeAction(); |
---|
| 440 | var cb = this.createCB(c); |
---|
| 441 | if(!a){ |
---|
| 442 | this.storeXY([x, y]); |
---|
| 443 | supr.setXY.call(this, [x, y]); |
---|
| 444 | supr.setSize.call(this, w, h, a, d, cb, e); |
---|
| 445 | cb(); |
---|
| 446 | }else{ |
---|
| 447 | supr.setBounds.call(this, x, y, w, h, a, d, cb, e); |
---|
| 448 | } |
---|
| 449 | return this; |
---|
| 450 | }, |
---|
| 451 | |
---|
| 452 | /** |
---|
| 453 | * Sets the z-index of this layer and adjusts any shadow and shim z-indexes. The layer z-index is automatically |
---|
| 454 | * incremented by two more than the value passed in so that it always shows above any shadow or shim (the shadow |
---|
| 455 | * element, if any, will be assigned z-index + 1, and the shim element, if any, will be assigned the unmodified z-index). |
---|
| 456 | * @param {Number} zindex The new z-index to set |
---|
| 457 | * @return {this} The Layer |
---|
| 458 | */ |
---|
| 459 | setZIndex : function(zindex){ |
---|
| 460 | this.zindex = zindex; |
---|
| 461 | this.setStyle('z-index', zindex + 2); |
---|
| 462 | if(this.shadow){ |
---|
| 463 | this.shadow.setZIndex(zindex + 1); |
---|
| 464 | } |
---|
| 465 | if(this.shim){ |
---|
| 466 | this.shim.setStyle('z-index', zindex); |
---|
| 467 | } |
---|
| 468 | return this; |
---|
| 469 | } |
---|
| 470 | }); |
---|
| 471 | })(); |
---|