1 /* <script type="text/javascript">
2 // -----------------------------------------------------------------------------------
3 // Roo lightbox - based on..
6 // by Lokesh Dhakar - http://www.lokeshdhakar.com
7 // Last Modification: 2/9/08
9 // For more information, visit:
10 // http://lokeshdhakar.com/projects/lightbox2/
12 // Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
13 // - Free for use in both personal and commercial projects
14 // - Attribution requires leaving author name, author link, and the license info intact.
16 // Thanks: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.
17 // Artemy Tregubenko (arty.name) for cleanup and help in updating to latest ver of proto-aculous.
19 // -----------------------------------------------------------------------------------
26 Lightbox Class Declaration
31 - resizeImageContainer()
36 - disableKeyboardNav()
38 - preloadNeighborImages()
45 Roo.namespace('Roo.ux');
47 Roo.ux.Lightbox = function(cfg) {
52 Roo.apply(Roo.ux.Lightbox.prototype,
56 * @cfg fileLoadingImage {string} loading image
58 fileLoadingImage: '/lightbox/images/loading.gif',
60 * @cfg fileBottomNavCloseImage {string} close image
62 fileBottomNavCloseImage: '/lightbox/images/closelabel.gif',
64 * @cfg overlayOpacity {number} controls transparency of shadow overlay
68 * @cfg fileLoadingImage {boolean} toggles resizing animations
72 * @cfg resizeSpeed {number} controls the speed of the image
73 * resizing animations (1=slowest and 10=fastest)
77 * @cfg borderSize {number} if you adjust the padding in the CSS,
78 * you will need to update this variable
82 * @cfg labelImage {string} When grouping images this is used to write: Image # of #.
83 * Change it for non-english localization
87 * @cfg labelOf {string} When grouping images this is used to write: Image # of #.
104 activeImage: undefined,
108 * Constructor runs on completion of the DOM loading. Calls updateImageList and then
109 * the function inserts html at the bottom of the page which is used to display the shadow
110 * overlay and the image container.
114 initialize: function() {
116 this.updateImageList();
118 // TBD this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
119 this.resizeSpeed = Math.min(this.resizeSpeed, 10);
120 this.resizeSpeed = Math.max(this.resizeSpeed, 1);
122 this.resizeDuration = this.animate ? ((11 - this.resizeSpeed) * 0.15) : 0;
123 this.overlayDuration = this.animate ? 0.2 : 0; // shadow fade in/out duration
125 // When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
126 // If animations are turned off, it will be hidden as to prevent a flicker of a
127 // white 250 by 250 box.
128 var size = (this.animate ? 250 : 1) + 'px';
129 var dh = Roo.DomHelper;
130 this.el = dh.append(document.body, {
132 // '<div id="overlay"></div>' +
133 '<div id="lightbox">' +
134 ' <div id="outerImageContainer">' +
135 ' <div id="imageContainer">' +
136 ' <img id="lightboxImage">' +
137 ' <div style="" id="hoverNav">' +
138 ' <a href="#" id="prevLink"></a>' +
139 ' <a href="#" id="nextLink"></a>' +
141 ' <div id="loading">' +
142 ' <a href="#" id="loadingLink"> </a>' +
146 ' <div id="imageDataContainer">' +
147 ' <div id="imageData">' +
148 ' <div id="imageDetails">' +
149 ' <span id="caption"></span>' +
150 ' <span id="numberDisplay"></span>' +
152 ' <div id="bottomNav">' +
153 ' <a href="#" id="bottomNavClose"> </a>' +
164 //Roo.get('overlay').hide();
165 //Roo.get('overlay').on('click', this.end, this);
166 Roo.get('lightbox').hide();
167 Roo.get('lightbox').on('click', function(event) {
168 if (event.getTarget().id == 'lightbox') {
173 Roo.get('outerImageContainer').setStyle({ width: size, height: size });
175 Roo.get('prevLink').on('click',
178 this.changeImage(this.activeImage - 1);
181 Roo.get('nextLink').on('click',
184 this.changeImage(this.activeImage + 1);
187 Roo.get('loadingLink').on('click',
193 Roo.get('bottomNavClose').on('click',
202 'lightbox outerImageContainer imageContainer ' +
203 'lightboxImage hoverNav prevLink nextLink loading loadingLink ' +
204 'imageDataContainer imageData imageDetails caption numberDisplay '
205 'bottomNav bottomNavClose';
207 Roo.each(ids.split(' '),
210 th[id] = Roo.get(id);
214 th[id].setVisibilityMode(Element.DISPLAY);
221 * Loops through anchor tags looking for 'lightbox' references and applies onclick
222 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
225 updateImageList: function() {
226 this.updateImageList = Roo.emptyFn;
228 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
229 this.imageArray.push(Roo.get(e));
230 Roo.get(e).on('click', function(event) {
240 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
242 start: function(imageLink) {
244 // hide all the objects that cause problems..
245 $$('select', 'object', 'embed').each(
247 node.style.visibility = 'hidden' }
250 // stretch overlay to fill page and fade in
251 //var arrayPageSize = this.getPageSize();
254 this._mask = Roo.DomHelper.append(document.body, {cls:"roo-el-mask"}, true);
255 this._mask.on('click', this.end, this);
257 this._mask.setDisplayed(true); // no animation..
259 // ie will not expand full height automatically
260 if(Roo.isIE && !(Roo.isIE7 && Roo.isStrict) && this.getStyle('height') == 'auto'){
261 this._mask.setHeight( Roo.lib.Dom.getViewHeight());
264 //new Effect.Appear(this.overlay,
265 // { duration: this.overlayDuration, from: 0.0, to: this.overlayOpacity });
267 //this.imageArray = [];
270 // let's assume the constructor sorts out the list of images..
272 if ((imageLink.rel == 'lightbox')){
273 // if image is NOT part of a set, add single image to imageArray
274 this.imageArray.push([imageLink.href, imageLink.title]);
276 // if image is part of a set..
278 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
279 collect(function(anchor){ return [anchor.href, anchor.title]; }).
282 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
285 var s = Roo.get(document).getScroll();
287 // calculate top and left offset for the lightbox
289 var lightboxTop = s.top + (Roo.lib.Dom.getViewHeight() / 10);
290 var lightboxLeft = s.left
291 this.lightbox.setStyle({
292 top: lightboxTop + 'px',
293 left: lightboxLeft + 'px' ,
294 zIndex : this._mask.getStyle('zIndex') * 1.1
296 this.lightbox.show();
300 this.changeImage(imageNum);
305 // Hide most elements and preload image in preparation for resizing image container.
307 changeImage: function(imageNum) {
309 this.activeImage = imageNum; // update global var
311 // hide elements during transition
313 this.loading.setStyle({
314 zIndex : this._mask.getStyle('zIndex') * 1.2
317 this.loadingLink.setX( (Roo.lib.Dom.getViewWidth() / 2) - 16);
318 //this.loadingLink.show();
319 // center the loading?
323 this.hoverNav.hide();
324 this.prevLink.hide();
325 this.nextLink.hide();
326 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
327 this.imageDataContainer.setStyle({opacity: .0001});
328 this.numberDisplay.hide();
330 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
331 // once image is preloaded, resize image container
334 imgPreloader.on('load', function() {
335 this.lightboxImage.dom.src = this.imageArray[this.activeImage].dom.href;
336 this.resizeImageContainer(imgPreloader.getWidth(), imgPreloader.getHeight());
338 imgPreloader.dom.src = this.imageArray[this.activeImage].dom.href;
342 // resizeImageContainer()
344 resizeImageContainer: function(imgWidth, imgHeight) {
346 // make sure we have some sensible sizes..
347 imgWidth = Math.max(50, imgWidth);
348 imgHeight = Math.max(50, imgHeight);
350 // get current width and height
351 var widthCurrent = this.outerImageContainer.getWidth();
352 var heightCurrent = this.outerImageContainer.getHeight();
354 //fixme need better calcs.
356 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
357 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
361 // get new width and height
362 var bs = this.borderSize * 2;
364 var widthNew = imgWidth + bs;
365 var heightNew = imgHeight + bs;
367 if (widthNew > ww || heightNew > wh) {
368 // bigger than window.
369 // scale here... - bit nasty..
370 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
371 //console.log(rescale);
372 imgWidth = Math.floor(imgWidth / rescale);
373 imgHeight = Math.floor(imgHeight / rescale);
374 widthNew = imgWidth + bs;
375 heightNew = imgHeight + bs;
380 this.lightboxImage.set( { width : imgWidth, height : imgHeight });
383 // scalars based on change from old to new
384 var xScale = (widthNew / widthCurrent) * 100;
385 var yScale = (heightNew / heightCurrent) * 100;
387 // calculate size difference between new and old image, and resize if necessary
388 var wDiff = widthCurrent - widthNew;
389 var hDiff = heightCurrent - heightNew;
392 this.outerImageContainer.animate(
393 { width: {to: widthNew}, height: {to: heightNew} },
402 // if new and old image are same size and no scaling transition is necessary,
403 // do a quick pause to prevent image flicker.
405 if ((hDiff == 0) && (wDiff == 0)){
407 if (Roo.isIE) timeout = 250;
411 this.prevLink.setStyle({ height: imgHeight + 'px' });
412 this.nextLink.setStyle({ height: imgHeight + 'px' });
413 this.imageDataContainer.setStyle({ width: widthNew + 'px', width: heightNew + 'px', });
415 }).defer(timeout / 1000, this);
420 // Display image and begin preloading neighbors.
422 showImage: function(){
425 //this.loadingLink.hide();
427 this.lightboxImage.animate(
429 opacity : { from : 0.0 , to: 1.0 }
432 _this.updateDetails();
436 this.preloadNeighborImages();
441 // Display caption, image number, and bottom nav.
443 updateDetails: function() {
445 // if caption is not null
446 if (this.imageArray[this.activeImage].dom.title != ""){
447 this.caption.update(this.imageArray[this.activeImage].dom.title);
451 // if image is part of set display 'Image x of x'
452 if (this.imageArray.length > 1){
453 this.numberDisplay.update(
454 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
455 this.numberDisplay.show();
458 this.imageDataContainer.animate(
460 //slide down ?: , from: 0.0, to: 1.0
461 opacity : { to: 1.0 }
465 // update overlay size and update nav
466 //var arrayPageSize = this.getPageSize();
467 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
476 // Display appropriate previous and next hover navigation.
478 updateNav: function() {
480 this.hoverNav.show();
482 // if not first image in set, display prev image button
483 if (this.activeImage > 0) this.prevLink.show();
485 // if not last image in set, display next image button
486 if (this.activeImage < (this.imageArray.length - 1)) this.nextLink.show();
488 this.enableKeyboardNav();
492 // enableKeyboardNav()
494 enableKeyboardNav: function() {
495 Roo.get(document.body).on('keydown', this.keyboardAction, this);
499 // disableKeyboardNav()
501 disableKeyboardNav: function() {
502 Roo.get(document.body).un('keydown', this.keyboardAction, this);
508 keyboardAction: function(event) {
509 var keycode = event.keyCode;
512 if (event.DOM_VK_ESCAPE) { // mozilla
513 escapeKey = event.DOM_VK_ESCAPE;
518 var key = String.fromCharCode(keycode).toLowerCase();
520 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
522 } else if ((key == 'p') || (keycode == 37)){ // display previous image
523 if (this.activeImage != 0){
524 this.disableKeyboardNav();
525 this.changeImage(this.activeImage - 1);
527 } else if ((key == 'n') || (keycode == 39)){ // display next image
528 if (this.activeImage != (this.imageArray.length - 1)){
529 this.disableKeyboardNav();
530 this.changeImage(this.activeImage + 1);
536 // preloadNeighborImages()
537 // Preload previous and next images.
539 preloadNeighborImages: function(){
540 var preloadNextImage, preloadPrevImage;
541 if (this.imageArray.length > this.activeImage + 1){
542 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
543 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].dom.href;
545 if (this.activeImage > 0){
546 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
547 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].dom.href;
556 this.disableKeyboardNav();
557 this.lightbox.hide();
559 this._mask.setDisplayed(false);
560 // show all the objects that cause problems..
561 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });