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 = dh.append(document.body, {
133 // '<div id="overlay"></div>' +
134 '<div class="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>' +
166 'lightbox outer-image-container image-container ' +
167 'lightbox-image hover-nav prev-link next-link loading loading-link ' +
168 'image-data-container image-data image-details caption number-display '
169 'bottom-nav bottom-nav-close';
171 Roo.each(ids.split(' '),
173 var vid = id.replace(/\-/g,'');
174 th[vid] = this.el.child('.'+id);
179 th[vid].setVisibilityMode(Roo.Element.DISPLAY);
183 //Roo.get('overlay').hide();
184 //Roo.get('overlay').on('click', this.end, this);
185 this.lightbox.hide();
186 this.lightbox.on('click', function(event) {
187 if (Roo.get(event.getTarget()).hasClass('lightbox')) {
192 this.outerimagecontainer.setStyle({ width: size, height: size });
194 this.prevlink.on('click',
197 this.changeImage(this.activeImage - 1);
200 this.nextlink.on('click',
203 this.changeImage(this.activeImage + 1);
206 this.loadinglink.on('click',
212 this.bottomnavclose.on('click',
221 initializeCSS: function() {
222 if (typeof(Roo.ux.Lightbox.css) != 'undefined') {
225 Roo.ux.Lightbox.css = Roo.util.CSS.createStyleSheet( {
227 position: 'absolute',
230 'text-align': 'center',
233 '.roo-lightbox a img' : { border: 'none' },
234 '.roo-lightbox .outer-image-container' : {
235 position: 'relative';
236 'background-color': '#fff';
242 '.roo-lightbox image-container' :{ padding: '10px' }
244 '.roo-lightbox .loading' {
254 '.roo-lightbox loading a' : {
255 background:'url(../images/loading.gif) 0 0 no-repeat;
261 '.roo-lightbox bottom-nav-close' : {
262 background:'url(../images/close.gif) 0 0 no-repeat',
267 '.roo-lightbox .hoverNav' :{
275 '.roo-lightbox .image-container>.hover-nav' :{ left:'0' },
276 '.roo-lightbox .hover-nav a' :{ outline:'none'},
278 '.roo-lightbox .prev-link, .roo-lightbox .next-link' : {
281 'background-image':'url(data:image/gif;base64,AAAA)',
284 '.roo-lightbox .prev-link' : { left:'0','float':'left'},
285 '.roo-lightbox .next-link' : { right:'0','float':'right'},
286 '.roo-lightbox .prev-link:hover, .roo-lightbox .prev-link:visited:hover ' :{
287 background:'url(../images/prevlabel.gif) left 15% no-repeat'
289 '.roo-lightbox .next-link:hover, .roo-lightbox .next-link:visited:hover ' :{
290 background:'url(../images/nextlabel.gif) right 15% no-repeat'
293 '.roo-lightbox .image-data-container' : {
294 font: '10px Verdana, Helvetica, sans-serif',
295 background-color: #fff;
302 '.roo-lightbox .image-data' : { padding:'0 10px', color: '#666' },
303 '.roo-lightbox .image-data .image-details ' : { width: '70%', 'float': 'left', 'text-align': 'left'} ,
304 '.roo-lightbox .image-data .caption' : { 'font-weight': 'bold' },
305 '.roo-lightbox .image-data .numberDisplay' : { 'display': 'block', clear: 'left', 'padding-bottom': '1.0em' } ,
306 '.roo-lightbox .image-data .bottomNavClose' : { 'float': 'right', 'padding-bottom': '0.7em','outline': 'none'}
314 * Loops through anchor tags looking for 'lightbox' references and applies onclick
315 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
318 updateImageList: function() {
319 this.updateImageList = Roo.emptyFn;
320 if (this.imageArray.length) {
323 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
324 this.imageArray.push(Roo.get(e));
325 Roo.get(e).on('click', (function(event, tg, tga,tgb) {
330 }).createDelegate(this, [e], true));
337 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
339 start: function(imageLink) {
341 // hide all the objects that cause problems..
342 $$('select', 'object', 'embed').each(
344 node.style.visibility = 'hidden' }
347 // stretch overlay to fill page and fade in
348 //var arrayPageSize = this.getPageSize();
349 Roo.get(document.body).mask(false);
350 Roo.get(document.body)._mask.setHeight(Roo.lib.Dom.getDocumentHeight())
351 //new Effect.Appear(this.overlay,
352 // { duration: this.overlayDuration, from: 0.0, to: this.overlayOpacity });
354 //this.imageArray = [];
358 imageLink = Roo.get(imageLink);
359 //console.log(imageLink.id);
360 Roo.each(this.imageArray, function (e, i) {
361 // console.log(e.id);
362 if (e.id == imageLink.id) {
368 // let's assume the constructor sorts out the list of images..
370 if ((imageLink.rel == 'lightbox')){
371 // if image is NOT part of a set, add single image to imageArray
372 this.imageArray.push([imageLink.href, imageLink.title]);
374 // if image is part of a set..
376 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
377 collect(function(anchor){ return [anchor.href, anchor.title]; }).
380 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
383 var s = Roo.get(document).getScroll();
385 // calculate top and left offset for the lightbox
387 var lightboxTop = s.top + (Roo.lib.Dom.getViewHeight() / 10);
388 var lightboxLeft = s.left
389 this.lightbox.setStyle({
390 top: lightboxTop + 'px',
391 left: lightboxLeft + 'px' ,
392 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.1
394 //console.log("show lightbox");
395 this.lightbox.show();
399 this.changeImage(imageNum);
404 // Hide most elements and preload image in preparation for resizing image container.
406 changeImage: function(imageNum) {
408 this.activeImage = imageNum; // update global var
410 // hide elements during transition
412 this.loading.setStyle({
413 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.2
416 this.loadinglink.setX( (Roo.lib.Dom.getViewWidth() / 2) - 16);
417 //this.loadingLink.show();
418 // center the loading?
422 this.hovernav.hide();
423 this.prevlink.hide();
424 this.nextlink.hide();
425 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
426 this.imagedatacontainer.setStyle({opacity: .0001});
427 this.numberdisplay.hide();
429 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
430 // once image is preloaded, resize image container
433 imgPreloader.on('load', function() {
435 this.lightboximage.dom.src = this.imageArray[this.activeImage].dom.href;
437 this.resizeimagecontainer(imgPreloader.getWidth(), imgPreloader.getHeight());
438 imgPreloader.remove();
440 imgPreloader.dom.src = this.imageArray[this.activeImage].dom.href;
444 // resizeImageContainer()
446 resizeImageContainer: function(imgWidth, imgHeight) {
448 // make sure we have some sensible sizes..
449 imgWidth = Math.max(50, imgWidth);
450 imgHeight = Math.max(50, imgHeight);
452 // get current width and height
453 var widthCurrent = this.outerimagecontainer.getWidth();
454 var heightCurrent = this.outerimagecontainer.getHeight();
456 //fixme need better calcs.
458 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
459 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
463 // get new width and height
464 var bs = this.borderSize * 2;
466 var widthNew = imgWidth + bs;
467 var heightNew = imgHeight + bs;
469 if (widthNew > ww || heightNew > wh) {
470 // bigger than window.
471 // scale here... - bit nasty..
472 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
473 //console.log(rescale);
474 imgWidth = Math.floor(imgWidth / rescale);
475 imgHeight = Math.floor(imgHeight / rescale);
476 widthNew = imgWidth + bs;
477 heightNew = imgHeight + bs;
482 this.lightboximage.set( { width : imgWidth, height : imgHeight });
485 // scalars based on change from old to new
486 var xScale = (widthNew / widthCurrent) * 100;
487 var yScale = (heightNew / heightCurrent) * 100;
489 // calculate size difference between new and old image, and resize if necessary
490 var wDiff = widthCurrent - widthNew;
491 var hDiff = heightCurrent - heightNew;
494 this.outerimagecontainer.animate(
495 { width: {to: widthNew}, height: {to: heightNew} },
504 // if new and old image are same size and no scaling transition is necessary,
505 // do a quick pause to prevent image flicker.
507 if ((hDiff == 0) && (wDiff == 0)){
509 if (Roo.isIE) timeout = 250;
513 this.prevlink.setStyle({ height: imgHeight + 'px' });
514 this.nextlink.setStyle({ height: imgHeight + 'px' });
515 this.imagedatacontainer.setStyle({ width: widthNew + 'px'}); // the text area..
517 }).defer(timeout / 1000, this);
522 // Display image and begin preloading neighbors.
524 showImage: function(){
527 //this.loadingLink.hide();
529 this.lightboximage.animate(
531 opacity : { to: 1.0 }
535 _this.updateDetails();
539 this.preloadNeighborImages();
544 // Display caption, image number, and bottom nav.
546 updateDetails: function() {
548 // if caption is not null
549 if (this.imageArray[this.activeImage].dom.title != ""){
550 this.caption.update(this.imageArray[this.activeImage].dom.title);
554 // if image is part of set display 'Image x of x'
555 if (this.imageArray.length > 1){
556 this.numberdisplay.update(
557 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
558 this.numberDisplay.show();
561 this.imagedatacontainer.animate(
563 //slide down ?: , from: 0.0, to: 1.0
564 opacity : { to: 1.0 }
568 // update overlay size and update nav
569 //var arrayPageSize = this.getPageSize();
570 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
579 // Display appropriate previous and next hover navigation.
581 updateNav: function() {
583 this.hovernav.show();
585 // if not first image in set, display prev image button
586 if (this.activeImage > 0) this.prevlink.show();
588 // if not last image in set, display next image button
589 if (this.activeImage < (this.imageArray.length - 1)) this.nextlink.show();
591 this.enableKeyboardNav();
595 // enableKeyboardNav()
597 enableKeyboardNav: function() {
598 Roo.get(document.body).on('keydown', this.keyboardAction, this);
602 // disableKeyboardNav()
604 disableKeyboardNav: function() {
605 Roo.get(document.body).un('keydown', this.keyboardAction, this);
611 keyboardAction: function(event)
613 var keycode = event.keyCode;
616 if (event.DOM_VK_ESCAPE) { // mozilla
617 escapeKey = event.DOM_VK_ESCAPE;
622 var key = String.fromCharCode(keycode).toLowerCase();
624 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
626 } else if ((key == 'p') || (keycode == 37)){ // display previous image
627 if (this.activeImage != 0){
628 this.disableKeyboardNav();
629 this.changeImage(this.activeImage - 1);
631 } else if ((key == 'n') || (keycode == 39)){ // display next image
632 if (this.activeImage != (this.imageArray.length - 1)){
633 this.disableKeyboardNav();
634 this.changeImage(this.activeImage + 1);
640 // preloadNeighborImages()
641 // Preload previous and next images.
643 preloadNeighborImages: function(){
644 var preloadNextImage, preloadPrevImage;
645 if (this.imageArray.length > this.activeImage + 1){
646 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
647 preloadNextImage.on('load', function() { preloadNextImage.remove() });
648 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].dom.href;
650 if (this.activeImage > 0){
651 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
652 preloadPrevImage.on('load', function() { preloadPrevImage.remove() });
653 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].dom.href;
662 this.disableKeyboardNav();
663 //console.log('lightbox hide');
664 this.lightbox.hide();
666 Roo.get(document.body).unmask();
667 // show all the objects that cause problems..
668 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });