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();
118 this.initializeCSS();
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 = Roo.DomHelper.append(document.body, {
133 // '<div id="overlay"></div>' +
134 '<div class="roo-lightbox">' +
135 ' <div class="outer-image-container">' +
136 ' <div class="image-container">' +
137 ' <img class="lightbox-image">' +
138 ' <div style="" class="hover-nav">' +
139 ' <a href="#" class="prev-link"></a>' +
140 ' <a href="#" class="next-link"></a>' +
142 ' <div class="loading">' +
143 ' <a href="#" class="loading-link"> </a>' +
147 ' <div class="image-data-container">' +
148 ' <div class="image-data">' +
149 ' <div class="image-details">' +
150 ' <span class="caption"></span>' +
151 ' <span class="number-display"></span>' +
153 ' <div class="bottom-nav">' +
154 ' <div href="#" class="bottom-nav-close"></div>' +
165 var ids = 'roo-lightbox outer-image-container image-container ' +
166 'lightbox-image hover-nav prev-link next-link loading loading-link ' +
167 'image-data-container image-data image-details caption number-display ' +
168 'bottom-nav bottom-nav-close';
170 Roo.each(ids.split(' '),
172 var vid = id.replace(/\-/g,'');
174 th[vid] = th.el.child('.'+id);
179 th[vid].setVisibilityMode(Roo.Element.DISPLAY);
181 this.lightbox = this.roolightbox;
183 this.overlay = Roo.DomHelper.append(document.body, { html : '<div class="lightbox-overlay"></div>' }, true);
184 this.overlay.setVisibilityMode(Roo.Element.DISPLAY);
186 this.overlay.on('click', this.end, this);
188 this.lightbox.hide();
189 this.lightbox.on('click', function(event) {
190 if (Roo.get(event.getTarget()).hasClass('lightbox')) {
195 this.outerimagecontainer.setStyle({ width: size, height: size });
197 this.prevlink.on('click',
200 this.changeImage(this.activeImage - 1);
203 this.nextlink.on('click',
206 this.changeImage(this.activeImage + 1);
209 this.loadinglink.on('click',
215 this.bottomnavclose.on('click',
224 initializeCSS: function() {
225 if (typeof(Roo.ux.Lightbox.css) != 'undefined') {
228 Roo.ux.Lightbox.css = Roo.util.CSS.createStyleSheet( {
230 position: 'absolute',
233 'text-align': 'center',
236 '.roo-lightbox a img' : { border: 'none' },
237 '.roo-lightbox .outer-image-container' : {
238 position: 'relative',
239 'background-color': '#fff',
245 '.roo-lightbox image-container' :{ padding: '10px' },
247 '.roo-lightbox .loading' : {
253 'text-align':'center',
257 '.roo-lightbox loading a' : {
258 background:'url(../images/loading.gif) 0 0 no-repeat',
264 '.roo-lightbox bottom-nav-close' : {
265 background:'url(../images/close.gif) 0 0 no-repeat',
270 '.roo-lightbox .hoverNav' :{
278 '.roo-lightbox .image-container>.hover-nav' :{ left:'0' },
279 '.roo-lightbox .hover-nav a' :{ outline:'none'},
281 '.roo-lightbox .prev-link, .roo-lightbox .next-link' : {
284 'background-image':'url(data:image/gif;base64,AAAA)',
287 '.roo-lightbox .prev-link' : { left:'0','float':'left'},
288 '.roo-lightbox .next-link' : { right:'0','float':'right'},
289 '.roo-lightbox .prev-link:hover, .roo-lightbox .prev-link:visited:hover ' :{
290 background:'url(../images/prevlabel.gif) left 15% no-repeat'
292 '.roo-lightbox .next-link:hover, .roo-lightbox .next-link:visited:hover ' :{
293 background:'url(../images/nextlabel.gif) right 15% no-repeat'
296 '.roo-lightbox .image-data-container' : {
297 font: '10px Verdana, Helvetica, sans-serif',
298 'background-color': '#fff',
300 'line-height': '1.4em',
305 '.roo-lightbox .image-data' : { padding:'0 10px', color: '#666' },
306 '.roo-lightbox .image-data .image-details ' : { width: '70%', 'float': 'left', 'text-align': 'left'} ,
307 '.roo-lightbox .image-data .caption' : { 'font-weight': 'bold' },
308 '.roo-lightbox .image-data .numberDisplay' : { 'display': 'block', clear: 'left', 'padding-bottom': '1.0em' } ,
309 '.roo-lightbox .image-data .bottomNavClose' : { 'float': 'right', 'padding-bottom': '0.7em','outline': 'none'}
317 * Loops through anchor tags looking for 'lightbox' references and applies onclick
318 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
321 updateImageList: function() {
322 this.updateImageList = Roo.emptyFn;
323 if (this.imageArray.length) {
326 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
327 this.imageArray.push(Roo.get(e));
328 Roo.get(e).on('click', (function(event, tg, tga,tgb) {
333 }).createDelegate(this, [e], true));
340 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
342 start: function(imageLink) {
344 // hide all the objects that cause problems..
345 $$('select', 'object', 'embed').each(
347 node.style.visibility = 'hidden' }
350 // stretch overlay to fill page and fade in
351 //var arrayPageSize = this.getPageSize();
353 this.overlay.setHeight(Roo.lib.Dom.getDocumentHeight()) ;
354 this.overlay.setWidth(Roo.lib.Dom.getDocumentWidth());
355 this.overlay.fadeIn({
356 endOpacity: this.overlayOpacity,
358 duration: this.overlayDuration
361 //this.imageArray = [];
365 imageLink = Roo.get(imageLink);
366 //console.log(imageLink.id);
367 Roo.each(this.imageArray, function (e, i) {
368 // console.log(e.id);
369 if (e.id == imageLink.id) {
375 // let's assume the constructor sorts out the list of images..
377 if ((imageLink.rel == 'lightbox')){
378 // if image is NOT part of a set, add single image to imageArray
379 this.imageArray.push([imageLink.href, imageLink.title]);
381 // if image is part of a set..
383 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
384 collect(function(anchor){ return [anchor.href, anchor.title]; }).
387 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
390 var s = Roo.get(document).getScroll();
392 // calculate top and left offset for the lightbox
394 var lightboxTop = s.top + (Roo.lib.Dom.getViewHeight() / 10);
395 var lightboxLeft = s.left
396 this.lightbox.setStyle({
397 top: lightboxTop + 'px',
398 left: lightboxLeft + 'px' ,
399 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.1
401 //console.log("show lightbox");
402 this.lightbox.show();
406 this.changeImage(imageNum);
411 // Hide most elements and preload image in preparation for resizing image container.
413 changeImage: function(imageNum) {
415 this.activeImage = imageNum; // update global var
417 // hide elements during transition
419 this.loading.setStyle({
420 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.2
423 this.loadinglink.setX( (Roo.lib.Dom.getViewWidth() / 2) - 16);
424 //this.loadingLink.show();
425 // center the loading?
429 this.hovernav.hide();
430 this.prevlink.hide();
431 this.nextlink.hide();
432 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
433 this.imagedatacontainer.setStyle({opacity: .0001});
434 this.numberdisplay.hide();
436 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
437 // once image is preloaded, resize image container
439 imgPreloader.on('load', function() {
441 this.lightboximage.dom.src = this.imageArray[this.activeImage].dom.href;
443 this.resizeImageContainer(imgPreloader.getWidth(), imgPreloader.getHeight());
444 imgPreloader.remove();
446 imgPreloader.dom.src = this.imageArray[this.activeImage].dom.href;
452 // resizeImageContainer()
454 resizeImageContainer: function(imgWidth, imgHeight) {
456 // make sure we have some sensible sizes..
457 imgWidth = Math.max(50, imgWidth);
458 imgHeight = Math.max(50, imgHeight);
460 // get current width and height
461 var widthCurrent = this.outerimagecontainer.getWidth();
462 var heightCurrent = this.outerimagecontainer.getHeight();
464 //fixme need better calcs.
466 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
467 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
471 // get new width and height
472 var bs = this.borderSize * 2;
474 var widthNew = imgWidth + bs;
475 var heightNew = imgHeight + bs;
477 if (widthNew > ww || heightNew > wh) {
478 // bigger than window.
479 // scale here... - bit nasty..
480 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
481 //console.log(rescale);
482 imgWidth = Math.floor(imgWidth / rescale);
483 imgHeight = Math.floor(imgHeight / rescale);
484 widthNew = imgWidth + bs;
485 heightNew = imgHeight + bs;
490 this.lightboximage.set( { width : imgWidth, height : imgHeight });
493 // scalars based on change from old to new
494 var xScale = (widthNew / widthCurrent) * 100;
495 var yScale = (heightNew / heightCurrent) * 100;
497 // calculate size difference between new and old image, and resize if necessary
498 var wDiff = widthCurrent - widthNew;
499 var hDiff = heightCurrent - heightNew;
502 this.outerimagecontainer.animate(
503 { width: {to: widthNew}, height: {to: heightNew} },
512 // if new and old image are same size and no scaling transition is necessary,
513 // do a quick pause to prevent image flicker.
515 if ((hDiff == 0) && (wDiff == 0)){
517 if (Roo.isIE) timeout = 250;
521 this.prevlink.setStyle({ height: imgHeight + 'px' });
522 this.nextlink.setStyle({ height: imgHeight + 'px' });
523 this.imagedatacontainer.setStyle({ width: widthNew + 'px'}); // the text area..
525 }).defer(timeout / 1000, this);
530 // Display image and begin preloading neighbors.
532 showImage: function(){
535 //this.loadingLink.hide();
537 this.lightboximage.animate(
539 opacity : { to: 1.0 }
543 _this.updateDetails();
547 this.preloadNeighborImages();
552 // Display caption, image number, and bottom nav.
554 updateDetails: function() {
556 // if caption is not null
557 if (this.imageArray[this.activeImage].dom.title != ""){
558 this.caption.update(this.imageArray[this.activeImage].dom.title);
562 // if image is part of set display 'Image x of x'
563 if (this.imageArray.length > 1){
564 this.numberdisplay.update(
565 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
566 this.numberdisplay.show();
569 this.imagedatacontainer.animate(
571 //slide down ?: , from: 0.0, to: 1.0
572 opacity : { to: 1.0 }
576 // update overlay size and update nav
577 //var arrayPageSize = this.getPageSize();
578 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
587 // Display appropriate previous and next hover navigation.
589 updateNav: function() {
591 this.hovernav.show();
593 // if not first image in set, display prev image button
594 if (this.activeImage > 0) this.prevlink.show();
596 // if not last image in set, display next image button
597 if (this.activeImage < (this.imageArray.length - 1)) this.nextlink.show();
599 this.enableKeyboardNav();
603 // enableKeyboardNav()
605 enableKeyboardNav: function() {
606 Roo.get(document.body).on('keydown', this.keyboardAction, this);
610 // disableKeyboardNav()
612 disableKeyboardNav: function() {
613 Roo.get(document.body).un('keydown', this.keyboardAction, this);
619 keyboardAction: function(event)
621 var keycode = event.keyCode;
624 if (event.DOM_VK_ESCAPE) { // mozilla
625 escapeKey = event.DOM_VK_ESCAPE;
630 var key = String.fromCharCode(keycode).toLowerCase();
632 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
634 } else if ((key == 'p') || (keycode == 37)){ // display previous image
635 if (this.activeImage != 0){
636 this.disableKeyboardNav();
637 this.changeImage(this.activeImage - 1);
639 } else if ((key == 'n') || (keycode == 39)){ // display next image
640 if (this.activeImage != (this.imageArray.length - 1)){
641 this.disableKeyboardNav();
642 this.changeImage(this.activeImage + 1);
648 // preloadNeighborImages()
649 // Preload previous and next images.
651 preloadNeighborImages: function(){
652 var preloadNextImage, preloadPrevImage;
653 if (this.imageArray.length > this.activeImage + 1){
654 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
655 preloadNextImage.on('load', function() { preloadNextImage.remove() });
656 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].dom.href;
658 if (this.activeImage > 0){
659 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
660 preloadPrevImage.on('load', function() { preloadPrevImage.remove() });
661 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].dom.href;
670 this.disableKeyboardNav();
671 //console.log('lightbox hide');
672 this.lightbox.hide();
675 Roo.get(document.body).unmask();
676 // show all the objects that cause problems..
677 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });