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',
297 'line-height': '1.4em',
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
432 imgPreloader.on('load', function() {
434 this.lightboximage.dom.src = this.imageArray[this.activeImage].dom.href;
436 this.resizeimagecontainer(imgPreloader.getWidth(), imgPreloader.getHeight());
437 imgPreloader.remove();
439 imgPreloader.dom.src = this.imageArray[this.activeImage].dom.href;
445 // resizeImageContainer()
447 resizeImageContainer: function(imgWidth, imgHeight) {
449 // make sure we have some sensible sizes..
450 imgWidth = Math.max(50, imgWidth);
451 imgHeight = Math.max(50, imgHeight);
453 // get current width and height
454 var widthCurrent = this.outerimagecontainer.getWidth();
455 var heightCurrent = this.outerimagecontainer.getHeight();
457 //fixme need better calcs.
459 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
460 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
464 // get new width and height
465 var bs = this.borderSize * 2;
467 var widthNew = imgWidth + bs;
468 var heightNew = imgHeight + bs;
470 if (widthNew > ww || heightNew > wh) {
471 // bigger than window.
472 // scale here... - bit nasty..
473 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
474 //console.log(rescale);
475 imgWidth = Math.floor(imgWidth / rescale);
476 imgHeight = Math.floor(imgHeight / rescale);
477 widthNew = imgWidth + bs;
478 heightNew = imgHeight + bs;
483 this.lightboximage.set( { width : imgWidth, height : imgHeight });
486 // scalars based on change from old to new
487 var xScale = (widthNew / widthCurrent) * 100;
488 var yScale = (heightNew / heightCurrent) * 100;
490 // calculate size difference between new and old image, and resize if necessary
491 var wDiff = widthCurrent - widthNew;
492 var hDiff = heightCurrent - heightNew;
495 this.outerimagecontainer.animate(
496 { width: {to: widthNew}, height: {to: heightNew} },
505 // if new and old image are same size and no scaling transition is necessary,
506 // do a quick pause to prevent image flicker.
508 if ((hDiff == 0) && (wDiff == 0)){
510 if (Roo.isIE) timeout = 250;
514 this.prevlink.setStyle({ height: imgHeight + 'px' });
515 this.nextlink.setStyle({ height: imgHeight + 'px' });
516 this.imagedatacontainer.setStyle({ width: widthNew + 'px'}); // the text area..
518 }).defer(timeout / 1000, this);
523 // Display image and begin preloading neighbors.
525 showImage: function(){
528 //this.loadingLink.hide();
530 this.lightboximage.animate(
532 opacity : { to: 1.0 }
536 _this.updateDetails();
540 this.preloadNeighborImages();
545 // Display caption, image number, and bottom nav.
547 updateDetails: function() {
549 // if caption is not null
550 if (this.imageArray[this.activeImage].dom.title != ""){
551 this.caption.update(this.imageArray[this.activeImage].dom.title);
555 // if image is part of set display 'Image x of x'
556 if (this.imageArray.length > 1){
557 this.numberdisplay.update(
558 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
559 this.numberDisplay.show();
562 this.imagedatacontainer.animate(
564 //slide down ?: , from: 0.0, to: 1.0
565 opacity : { to: 1.0 }
569 // update overlay size and update nav
570 //var arrayPageSize = this.getPageSize();
571 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
580 // Display appropriate previous and next hover navigation.
582 updateNav: function() {
584 this.hovernav.show();
586 // if not first image in set, display prev image button
587 if (this.activeImage > 0) this.prevlink.show();
589 // if not last image in set, display next image button
590 if (this.activeImage < (this.imageArray.length - 1)) this.nextlink.show();
592 this.enableKeyboardNav();
596 // enableKeyboardNav()
598 enableKeyboardNav: function() {
599 Roo.get(document.body).on('keydown', this.keyboardAction, this);
603 // disableKeyboardNav()
605 disableKeyboardNav: function() {
606 Roo.get(document.body).un('keydown', this.keyboardAction, this);
612 keyboardAction: function(event)
614 var keycode = event.keyCode;
617 if (event.DOM_VK_ESCAPE) { // mozilla
618 escapeKey = event.DOM_VK_ESCAPE;
623 var key = String.fromCharCode(keycode).toLowerCase();
625 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
627 } else if ((key == 'p') || (keycode == 37)){ // display previous image
628 if (this.activeImage != 0){
629 this.disableKeyboardNav();
630 this.changeImage(this.activeImage - 1);
632 } else if ((key == 'n') || (keycode == 39)){ // display next image
633 if (this.activeImage != (this.imageArray.length - 1)){
634 this.disableKeyboardNav();
635 this.changeImage(this.activeImage + 1);
641 // preloadNeighborImages()
642 // Preload previous and next images.
644 preloadNeighborImages: function(){
645 var preloadNextImage, preloadPrevImage;
646 if (this.imageArray.length > this.activeImage + 1){
647 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
648 preloadNextImage.on('load', function() { preloadNextImage.remove() });
649 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].dom.href;
651 if (this.activeImage > 0){
652 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
653 preloadPrevImage.on('load', function() { preloadPrevImage.remove() });
654 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].dom.href;
663 this.disableKeyboardNav();
664 //console.log('lightbox hide');
665 this.lightbox.hide();
667 Roo.get(document.body).unmask();
668 // show all the objects that cause problems..
669 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });