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) {
53 Roo.apply(Roo.ux.Lightbox.prototype,
57 * @cfg fileLoadingImage {string} loading image
59 fileLoadingImage: '/lightbox/images/loading.gif',
61 * @cfg fileBottomNavCloseImage {string} close image
63 fileBottomNavCloseImage: '/lightbox/images/closelabel.gif',
65 * @cfg overlayOpacity {number} controls transparency of shadow overlay
69 * @cfg fileLoadingImage {boolean} toggles resizing animations
73 * @cfg resizeSpeed {number} controls the speed of the image
74 * resizing animations (1=slowest and 10=fastest)
78 * @cfg borderSize {number} if you adjust the padding in the CSS,
79 * you will need to update this variable
83 * @cfg labelImage {string} When grouping images this is used to write: Image # of #.
84 * Change it for non-english localization
88 * @cfg labelOf {string} When grouping images this is used to write: Image # of #.
105 activeImage: undefined,
109 * Constructor runs on completion of the DOM loading. Calls updateImageList and then
110 * the function inserts html at the bottom of the page which is used to display the shadow
111 * overlay and the image container.
115 initialize: function() {
117 this.updateImageList();
119 // TBD this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
120 this.resizeSpeed = Math.min(this.resizeSpeed, 10);
121 this.resizeSpeed = Math.max(this.resizeSpeed, 1);
123 this.resizeDuration = this.animate ? ((11 - this.resizeSpeed) * 0.15) : 0;
124 this.overlayDuration = this.animate ? 0.2 : 0; // shadow fade in/out duration
126 // When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
127 // If animations are turned off, it will be hidden as to prevent a flicker of a
128 // white 250 by 250 box.
129 var size = (this.animate ? 250 : 1) + 'px';
130 var dh = Roo.DomHelper;
131 this.el = dh.append(document.body, {
133 // '<div id="overlay"></div>' +
134 '<div id="lightbox">' +
135 ' <div id="outerImageContainer">' +
136 ' <div id="imageContainer">' +
137 ' <img id="lightboxImage">' +
138 ' <div style="" id="hoverNav">' +
139 ' <a href="#" id="prevLink"></a>' +
140 ' <a href="#" id="nextLink"></a>' +
142 ' <div id="loading">' +
143 ' <a href="#" id="loadingLink"> </a>' +
147 ' <div id="imageDataContainer">' +
148 ' <div id="imageData">' +
149 ' <div id="imageDetails">' +
150 ' <span id="caption"></span>' +
151 ' <span id="numberDisplay"></span>' +
153 ' <div id="bottomNav">' +
154 ' <div href="#" id="bottomNavClose"></div>' +
166 'lightbox outerImageContainer imageContainer ' +
167 'lightboxImage hoverNav prevLink nextLink loading loadingLink ' +
168 'imageDataContainer imageData imageDetails caption numberDisplay '
169 'bottomNav bottomNavClose';
171 Roo.each(ids.split(' '),
174 th[id] = Roo.get(id);
178 th[id].setVisibilityMode(Roo.Element.DISPLAY);
182 //Roo.get('overlay').hide();
183 //Roo.get('overlay').on('click', this.end, this);
184 Roo.get('lightbox').hide();
185 Roo.get('lightbox').on('click', function(event) {
186 if (event.getTarget().id == 'lightbox') {
191 Roo.get('outerImageContainer').setStyle({ width: size, height: size });
193 Roo.get('prevLink').on('click',
196 this.changeImage(this.activeImage - 1);
199 Roo.get('nextLink').on('click',
202 this.changeImage(this.activeImage + 1);
205 Roo.get('loadingLink').on('click',
211 Roo.get('bottomNavClose').on('click',
220 initializeCSS: function() {
221 if (typeof(Roo.ux.Lightbox.css) != 'undefined') {
224 Roo.ux.Lightbox.css = Roo.util.CSS.createStyleSheet( {
226 position: 'absolute',
229 'text-align': 'center',
232 '.roo-lightbox a img' : { border: 'none' },
233 '.roo-lightbox .outer-image-container' : {
234 position: 'relative';
235 'background-color': '#fff';
241 '.roo-lightbox image-container' :{ padding: '10px' }
243 '.roo-lightbox .loading' {
253 '.roo-lightbox loading a' : {
254 background:'url(../images/loading.gif) 0 0 no-repeat;
260 '.roo-lightbox bottom-nav-close' : {
261 background:'url(../images/close.gif) 0 0 no-repeat',
266 '.roo-lightbox .hoverNav' :{
274 '.roo-lightbox .image-container>.hover-nav' :{ left:'0' },
275 '.roo-lightbox .hover-nav a' :{ outline:'none'},
277 '.roo-lightbox .prev-link, .roo-lightbox .next-link' : {
280 'background-image':'url(data:image/gif;base64,AAAA)',
283 '.roo-lightbox .prev-link' : { left:'0','float':'left'},
284 '.roo-lightbox .next-link' : { right:'0','float':'right'},
285 '.roo-lightbox .prev-link:hover, .roo-lightbox .prev-link:visited:hover ' :{
286 background:'url(../images/prevlabel.gif) left 15% no-repeat'
288 '.roo-lightbox .next-link:hover, .roo-lightbox .next-link:visited:hover ' :{
289 background:'url(../images/nextlabel.gif) right 15% no-repeat'
292 #imageDataContainer{ font: 10px Verdana, Helvetica, sans-serif; background-color: #fff; margin: 0 auto; line-height: 1.4em; overflow: auto; width: 100% ; }
294 #imageData{ padding:0 10px; color: #666; }
295 #imageData #imageDetails{ width: 70%; float: left; text-align: left; }
296 #imageData #caption{ font-weight: bold; }
297 #imageData #numberDisplay{ display: block; clear: left; padding-bottom: 1.0em; }
298 #imageData #bottomNavClose{ float: right; padding-bottom: 0.7em; outline: none;}
304 * Loops through anchor tags looking for 'lightbox' references and applies onclick
305 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
308 updateImageList: function() {
309 this.updateImageList = Roo.emptyFn;
310 if (this.imageArray.length) {
313 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
314 this.imageArray.push(Roo.get(e));
315 Roo.get(e).on('click', (function(event, tg, tga,tgb) {
320 }).createDelegate(this, [e], true));
327 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
329 start: function(imageLink) {
331 // hide all the objects that cause problems..
332 $$('select', 'object', 'embed').each(
334 node.style.visibility = 'hidden' }
337 // stretch overlay to fill page and fade in
338 //var arrayPageSize = this.getPageSize();
339 Roo.get(document.body).mask(false);
340 Roo.get(document.body)._mask.setHeight(Roo.lib.Dom.getDocumentHeight())
341 //new Effect.Appear(this.overlay,
342 // { duration: this.overlayDuration, from: 0.0, to: this.overlayOpacity });
344 //this.imageArray = [];
348 imageLink = Roo.get(imageLink);
349 //console.log(imageLink.id);
350 Roo.each(this.imageArray, function (e, i) {
351 // console.log(e.id);
352 if (e.id == imageLink.id) {
358 // let's assume the constructor sorts out the list of images..
360 if ((imageLink.rel == 'lightbox')){
361 // if image is NOT part of a set, add single image to imageArray
362 this.imageArray.push([imageLink.href, imageLink.title]);
364 // if image is part of a set..
366 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
367 collect(function(anchor){ return [anchor.href, anchor.title]; }).
370 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
373 var s = Roo.get(document).getScroll();
375 // calculate top and left offset for the lightbox
377 var lightboxTop = s.top + (Roo.lib.Dom.getViewHeight() / 10);
378 var lightboxLeft = s.left
379 this.lightbox.setStyle({
380 top: lightboxTop + 'px',
381 left: lightboxLeft + 'px' ,
382 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.1
384 //console.log("show lightbox");
385 this.lightbox.show();
389 this.changeImage(imageNum);
394 // Hide most elements and preload image in preparation for resizing image container.
396 changeImage: function(imageNum) {
398 this.activeImage = imageNum; // update global var
400 // hide elements during transition
402 this.loading.setStyle({
403 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.2
406 this.loadingLink.setX( (Roo.lib.Dom.getViewWidth() / 2) - 16);
407 //this.loadingLink.show();
408 // center the loading?
412 this.hoverNav.hide();
413 this.prevLink.hide();
414 this.nextLink.hide();
415 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
416 this.imageDataContainer.setStyle({opacity: .0001});
417 this.numberDisplay.hide();
419 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
420 // once image is preloaded, resize image container
423 imgPreloader.on('load', function() {
425 this.lightboxImage.dom.src = this.imageArray[this.activeImage].dom.href;
427 this.resizeImageContainer(imgPreloader.getWidth(), imgPreloader.getHeight());
428 imgPreloader.remove();
430 imgPreloader.dom.src = this.imageArray[this.activeImage].dom.href;
434 // resizeImageContainer()
436 resizeImageContainer: function(imgWidth, imgHeight) {
438 // make sure we have some sensible sizes..
439 imgWidth = Math.max(50, imgWidth);
440 imgHeight = Math.max(50, imgHeight);
442 // get current width and height
443 var widthCurrent = this.outerImageContainer.getWidth();
444 var heightCurrent = this.outerImageContainer.getHeight();
446 //fixme need better calcs.
448 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
449 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
453 // get new width and height
454 var bs = this.borderSize * 2;
456 var widthNew = imgWidth + bs;
457 var heightNew = imgHeight + bs;
459 if (widthNew > ww || heightNew > wh) {
460 // bigger than window.
461 // scale here... - bit nasty..
462 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
463 //console.log(rescale);
464 imgWidth = Math.floor(imgWidth / rescale);
465 imgHeight = Math.floor(imgHeight / rescale);
466 widthNew = imgWidth + bs;
467 heightNew = imgHeight + bs;
472 this.lightboxImage.set( { width : imgWidth, height : imgHeight });
475 // scalars based on change from old to new
476 var xScale = (widthNew / widthCurrent) * 100;
477 var yScale = (heightNew / heightCurrent) * 100;
479 // calculate size difference between new and old image, and resize if necessary
480 var wDiff = widthCurrent - widthNew;
481 var hDiff = heightCurrent - heightNew;
484 this.outerImageContainer.animate(
485 { width: {to: widthNew}, height: {to: heightNew} },
494 // if new and old image are same size and no scaling transition is necessary,
495 // do a quick pause to prevent image flicker.
497 if ((hDiff == 0) && (wDiff == 0)){
499 if (Roo.isIE) timeout = 250;
503 this.prevLink.setStyle({ height: imgHeight + 'px' });
504 this.nextLink.setStyle({ height: imgHeight + 'px' });
505 this.imageDataContainer.setStyle({ width: widthNew + 'px'}); // the text area..
507 }).defer(timeout / 1000, this);
512 // Display image and begin preloading neighbors.
514 showImage: function(){
517 //this.loadingLink.hide();
519 this.lightboxImage.animate(
521 opacity : { to: 1.0 }
525 _this.updateDetails();
529 this.preloadNeighborImages();
534 // Display caption, image number, and bottom nav.
536 updateDetails: function() {
538 // if caption is not null
539 if (this.imageArray[this.activeImage].dom.title != ""){
540 this.caption.update(this.imageArray[this.activeImage].dom.title);
544 // if image is part of set display 'Image x of x'
545 if (this.imageArray.length > 1){
546 this.numberDisplay.update(
547 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
548 this.numberDisplay.show();
551 this.imageDataContainer.animate(
553 //slide down ?: , from: 0.0, to: 1.0
554 opacity : { to: 1.0 }
558 // update overlay size and update nav
559 //var arrayPageSize = this.getPageSize();
560 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
569 // Display appropriate previous and next hover navigation.
571 updateNav: function() {
573 this.hoverNav.show();
575 // if not first image in set, display prev image button
576 if (this.activeImage > 0) this.prevLink.show();
578 // if not last image in set, display next image button
579 if (this.activeImage < (this.imageArray.length - 1)) this.nextLink.show();
581 this.enableKeyboardNav();
585 // enableKeyboardNav()
587 enableKeyboardNav: function() {
588 Roo.get(document.body).on('keydown', this.keyboardAction, this);
592 // disableKeyboardNav()
594 disableKeyboardNav: function() {
595 Roo.get(document.body).un('keydown', this.keyboardAction, this);
601 keyboardAction: function(event) {
602 var keycode = event.keyCode;
605 if (event.DOM_VK_ESCAPE) { // mozilla
606 escapeKey = event.DOM_VK_ESCAPE;
611 var key = String.fromCharCode(keycode).toLowerCase();
613 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
615 } else if ((key == 'p') || (keycode == 37)){ // display previous image
616 if (this.activeImage != 0){
617 this.disableKeyboardNav();
618 this.changeImage(this.activeImage - 1);
620 } else if ((key == 'n') || (keycode == 39)){ // display next image
621 if (this.activeImage != (this.imageArray.length - 1)){
622 this.disableKeyboardNav();
623 this.changeImage(this.activeImage + 1);
629 // preloadNeighborImages()
630 // Preload previous and next images.
632 preloadNeighborImages: function(){
633 var preloadNextImage, preloadPrevImage;
634 if (this.imageArray.length > this.activeImage + 1){
635 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
636 preloadNextImage.on('load', function() { preloadNextImage.remove() });
637 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].dom.href;
639 if (this.activeImage > 0){
640 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
641 preloadPrevImage.on('load', function() { preloadPrevImage.remove() });
642 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].dom.href;
651 this.disableKeyboardNav();
652 //console.log('lightbox hide');
653 this.lightbox.hide();
655 Roo.get(document.body).unmask();
656 // show all the objects that cause problems..
657 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });