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,
58 * @cfg overlayOpacity {number} controls transparency of shadow overlay
62 * @cfg animate {boolean} toggles resizing animations
66 * @cfg resizeSpeed {number} controls the speed of the image
67 * resizing animations (1=slowest and 10=fastest)
71 * @cfg borderSize {number} if you adjust the padding in the CSS,
72 * you will need to update this variable
76 * @cfg labelImage {string} When grouping images this is used to write: Image # of #.
77 * Change it for non-english localization
81 * @cfg labelOf {string} When grouping images this is used to write: Image # of #.
98 activeImage: undefined,
102 * Constructor runs on completion of the DOM loading. Calls updateImageList and then
103 * the function inserts html at the bottom of the page which is used to display the shadow
104 * overlay and the image container.
108 initialize: function() {
110 this.updateImageList();
111 this.initializeCSS();
112 // TBD this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
113 this.resizeSpeed = Math.min(this.resizeSpeed, 10);
114 this.resizeSpeed = Math.max(this.resizeSpeed, 1);
116 this.resizeDuration = this.animate ? ((11 - this.resizeSpeed) * 0.15) : 0;
117 this.overlayDuration = this.animate ? 0.2 : 0; // shadow fade in/out duration
119 // When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
120 // If animations are turned off, it will be hidden as to prevent a flicker of a
121 // white 250 by 250 box.
122 var size = (this.animate ? 250 : 1) + 'px';
123 var dh = Roo.DomHelper;
124 this.el = Roo.DomHelper.append(document.body, {
126 '<div class="roo-lightbox-overlay"></div>' +
127 '<div class="roo-lightbox">' +
128 ' <div class="outer-image-container">' +
129 ' <div class="image-container">' +
130 ' <img class="lightbox-image">' +
131 ' <div style="" class="hover-nav">' +
132 ' <a href="#" class="prev-link"></a>' +
133 ' <a href="#" class="next-link"></a>' +
135 ' <div class="loading">' +
136 ' <a href="#" class="loading-link"> </a>' +
140 ' <div class="image-data-container">' +
141 ' <div class="image-data">' +
142 ' <div class="image-details">' +
143 ' <span class="caption"></span>' +
144 ' <span class="number-display"></span>' +
146 ' <div class="bottom-nav">' +
147 ' <div href="#" class="bottom-nav-close"></div>' +
158 var ids = 'roo-lightbox outer-image-container image-container ' +
159 'lightbox-image hover-nav prev-link next-link loading loading-link ' +
160 'image-data-container image-data image-details caption number-display ' +
161 'bottom-nav bottom-nav-close roo-lightbox-overlay';
163 Roo.each(ids.split(' '),
165 var vid = id.replace(/\-/g,'');
166 th[vid] = th.el.child('.'+id);
171 th[vid].setVisibilityMode(Roo.Element.DISPLAY);
173 this.lightbox = this.roolightbox;
175 this.overlay = this.roolightboxoverlay;
178 this.lightbox.hide();
179 this.lightbox.on('click', function(event) {
180 if (Roo.get(event.getTarget()).hasClass('roo-lightbox')) {
185 this.outerimagecontainer.setStyle({ width: size, height: size });
187 this.prevlink.on('click',
190 this.changeImage(this.activeImage - 1);
193 this.nextlink.on('click',
196 this.changeImage(this.activeImage + 1);
199 this.loadinglink.on('click',
205 this.bottomnavclose.on('click',
211 this.overlay.on('click',
219 initializeCSS: function() {
220 if (typeof(Roo.ux.Lightbox.css) != 'undefined') {
223 var rootURL = Roo.BLANK_IMAGE_URL.replace(/\/gray\/s\.gif$/, '');
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' : {
250 'text-align':'center',
254 '.roo-lightbox .loading a' : {
255 background:'url('+ rootURL + '/ux/lightbox/loading.gif) 0 0 no-repeat',
261 '.roo-lightbox .bottom-nav-close' : {
262 background:'url('+ rootURL + '/ux/lightbox/close.gif) 0 0 no-repeat',
267 '.roo-lightbox .hover-nav' :{
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('+ rootURL + '/ux/lightbox/prevlabel.gif) left 15% no-repeat'
289 '.roo-lightbox .next-link:hover, .roo-lightbox .next-link:visited:hover ' :{
290 background:'url('+ rootURL + '/ux/lightbox/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 .number-display' : { 'display': 'block', clear: 'left', 'padding-bottom': '1.0em' } ,
306 '.roo-lightbox .image-data .bottom-nav-close' : { 'float': 'right', 'padding-bottom': '0.7em','outline': 'none'} ,
307 '.roo-lightbox-overlay' : {
308 'background-color': 'black',
311 position: 'absolute',
323 * Loops through anchor tags looking for 'lightbox' references and applies onclick
324 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
327 updateImageList: function() {
328 this.updateImageList = Roo.emptyFn;
329 if (this.imageArray.length) {
332 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
333 this.imageArray.push(Roo.get(e));
334 Roo.get(e).on('click', (function(event, tg, tga,tgb) {
339 }).createDelegate(this, [e], true));
346 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
348 start: function(imageLink) {
350 // hide all the objects that cause problems..
351 $$('select', 'object', 'embed').each(
353 node.style.visibility = 'hidden' }
356 // stretch overlay to fill page and fade in
357 //var arrayPageSize = this.getPageSize();
359 this.overlay.setHeight(Roo.lib.Dom.getDocumentHeight()) ;
360 this.overlay.setWidth(Roo.lib.Dom.getDocumentWidth());
361 this.overlay.fadeIn({
362 endOpacity: this.overlayOpacity,
364 duration: this.overlayDuration
367 //this.imageArray = [];
371 imageLink = Roo.get(imageLink);
372 //console.log(imageLink.id);
373 Roo.each(this.imageArray, function (e, i) {
374 // console.log(e.id);
375 if (e.id == imageLink.id) {
381 // let's assume the constructor sorts out the list of images..
383 if ((imageLink.rel == 'lightbox')){
384 // if image is NOT part of a set, add single image to imageArray
385 this.imageArray.push([imageLink.href, imageLink.title]);
387 // if image is part of a set..
389 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
390 collect(function(anchor){ return [anchor.href, anchor.title]; }).
393 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
396 var s = Roo.get(document).getScroll();
397 // calculate top and left offset for the lightbox
400 //var lightboxTop = s.top + (Roo.lib.Dom.getViewHeight() / 10);
401 var lightboxTop = (Roo.lib.Dom.getViewHeight() / 10);
402 var lightboxLeft = s.left
403 this.lightbox.setStyle({
404 top: lightboxTop + 'px',
405 left: lightboxLeft + 'px' ,
408 //console.log("show lightbox");
409 this.lightbox.show();
412 this.changeImage(imageNum);
417 // Hide most elements and preload image in preparation for resizing image container.
419 changeImage: function(imageNum) {
421 this.activeImage = imageNum; // update global var
423 // hide elements during transition
425 this.loading.setStyle({
429 this.loadinglink.setX( (Roo.lib.Dom.getViewWidth() / 2) - 16);
430 //this.loadingLink.show();
431 // center the loading?
435 this.hovernav.hide();
436 this.prevlink.hide();
437 this.nextlink.hide();
438 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
439 this.imagedatacontainer.setStyle({opacity: .0001});
440 this.numberdisplay.hide();
442 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
443 // once image is preloaded, resize image container
445 imgPreloader.on('load', function() {
447 this.lightboximage.dom.src = this.imageArray[this.activeImage].href ||
448 this.imageArray[this.activeImage].dom.href || this.imageArray[this.activeImage].dom.src;
450 this.resizeImageContainer(this.imageArray[this.activeImage].dom.lwidth || imgPreloader.getWidth(), this.imageArray[this.activeImage].dom.lheight || imgPreloader.getHeight());
451 imgPreloader.remove();
453 imgPreloader.dom.src = this.imageArray[this.activeImage].href ||
454 this.imageArray[this.activeImage].dom.href || this.imageArray[this.activeImage].dom.src;
460 // resizeImageContainer()
462 resizeImageContainer: function(imgWidth, imgHeight) {
464 // make sure we have some sensible sizes..
465 imgWidth = Math.max(50, imgWidth);
466 imgHeight = Math.max(50, imgHeight);
468 // get current width and height
469 var widthCurrent = this.outerimagecontainer.getWidth();
470 var heightCurrent = this.outerimagecontainer.getHeight();
472 //fixme need better calcs.
474 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
475 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
479 // get new width and height
480 var bs = this.borderSize * 2;
482 var widthNew = imgWidth + bs;
483 var heightNew = imgHeight + bs;
485 if (widthNew > ww || heightNew > wh) {
486 // bigger than window.
487 // scale here... - bit nasty..
488 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
489 //console.log(rescale);
490 imgWidth = Math.floor(imgWidth / rescale);
491 imgHeight = Math.floor(imgHeight / rescale);
492 widthNew = imgWidth + bs;
493 heightNew = imgHeight + bs;
498 this.lightboximage.set( { width : imgWidth, height : imgHeight });
501 // scalars based on change from old to new
502 var xScale = (widthNew / widthCurrent) * 100;
503 var yScale = (heightNew / heightCurrent) * 100;
505 // calculate size difference between new and old image, and resize if necessary
506 var wDiff = widthCurrent - widthNew;
507 var hDiff = heightCurrent - heightNew;
510 this.outerimagecontainer.animate(
511 { width: {to: widthNew}, height: {to: heightNew} },
520 // if new and old image are same size and no scaling transition is necessary,
521 // do a quick pause to prevent image flicker.
523 if ((hDiff == 0) && (wDiff == 0)){
525 if (Roo.isIE) timeout = 250;
529 this.prevlink.setStyle({ height: imgHeight + 'px' });
530 this.nextlink.setStyle({ height: imgHeight + 'px' });
531 this.imagedatacontainer.setStyle({ width: widthNew + 'px'}); // the text area..
533 }).defer(timeout / 1000, this);
538 // Display image and begin preloading neighbors.
540 showImage: function(){
543 //this.loadingLink.hide();
545 this.lightboximage.animate(
547 opacity : { to: 1.0 }
551 _this.updateDetails();
555 this.preloadNeighborImages();
560 // Display caption, image number, and bottom nav.
562 updateDetails: function() {
564 // if caption is not null
565 if (this.imageArray[this.activeImage].dom.title != ""){
566 this.caption.update(this.imageArray[this.activeImage].dom.title);
570 // if image is part of set display 'Image x of x'
571 if (this.imageArray.length > 1){
572 this.numberdisplay.update(
573 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
574 this.numberdisplay.show();
577 this.imagedatacontainer.animate(
579 //slide down ?: , from: 0.0, to: 1.0
580 opacity : { to: 1.0 }
584 // update overlay size and update nav
585 //var arrayPageSize = this.getPageSize();
586 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
595 // Display appropriate previous and next hover navigation.
597 updateNav: function() {
599 this.hovernav.show();
601 // if not first image in set, display prev image button
602 if (this.activeImage > 0) this.prevlink.show();
604 // if not last image in set, display next image button
605 if (this.activeImage < (this.imageArray.length - 1)) this.nextlink.show();
607 this.enableKeyboardNav();
611 // enableKeyboardNav()
613 enableKeyboardNav: function() {
614 Roo.get(document.body).on('keydown', this.keyboardAction, this);
618 // disableKeyboardNav()
620 disableKeyboardNav: function() {
621 Roo.get(document.body).un('keydown', this.keyboardAction, this);
627 keyboardAction: function(event)
629 var keycode = event.keyCode;
632 if (event.DOM_VK_ESCAPE) { // mozilla
633 escapeKey = event.DOM_VK_ESCAPE;
638 var key = String.fromCharCode(keycode).toLowerCase();
640 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
642 } else if ((key == 'p') || (keycode == 37)){ // display previous image
643 if (this.activeImage != 0){
644 this.disableKeyboardNav();
645 this.changeImage(this.activeImage - 1);
647 } else if ((key == 'n') || (keycode == 39)){ // display next image
648 if (this.activeImage != (this.imageArray.length - 1)){
649 this.disableKeyboardNav();
650 this.changeImage(this.activeImage + 1);
656 // preloadNeighborImages()
657 // Preload previous and next images.
659 preloadNeighborImages: function(){
660 var preloadNextImage, preloadPrevImage;
661 if (this.imageArray.length > this.activeImage + 1){
662 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
663 preloadNextImage.on('load', function() { preloadNextImage.remove() });
664 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].href ||
665 this.imageArray[this.activeImage + 1].dom.href || this.imageArray[this.activeImage + 1 ].dom.src;;
667 if (this.activeImage > 0){
668 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
669 preloadPrevImage.on('load', function() { preloadPrevImage.remove() });
670 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].href ||
671 this.imageArray[this.activeImage - 1].dom.href || this.imageArray[this.activeImage - 1].dom.src;
680 this.disableKeyboardNav();
681 //console.log('lightbox hide');
682 this.lightbox.hide();
685 Roo.get(document.body).unmask();
686 // show all the objects that cause problems..
687 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });