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 .outerImageContainer' : {
234 position: 'relative';
235 'background-color': '#fff';
240 #imageContainer{ padding: 10px; }
253 background: url(../images/loading.gif) 0 0 no-repeat;
260 background: url(../images/close.gif) 0 0 no-repeat;
265 #hoverNav{ position: absolute; top: 0; left: 0; height: 100%; width: 100%; z-index: 10; }
266 #imageContainer>#hoverNav{ left: 0;}
267 #hoverNav a{ outline: none;}
269 #prevLink, #nextLink{ width: 49%; height: 100%; background-image: url(data:image/gif;base64,AAAA); /* Trick IE into showing hover */ display: block; }
270 #prevLink { left: 0; float: left;}
271 #nextLink { right: 0; float: right;}
272 #prevLink:hover, #prevLink:visited:hover { background: url(../images/prevlabel.gif) left 15% no-repeat; }
273 #nextLink:hover, #nextLink:visited:hover { background: url(../images/nextlabel.gif) right 15% no-repeat; }
279 * Loops through anchor tags looking for 'lightbox' references and applies onclick
280 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
283 updateImageList: function() {
284 this.updateImageList = Roo.emptyFn;
285 if (this.imageArray.length) {
288 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
289 this.imageArray.push(Roo.get(e));
290 Roo.get(e).on('click', (function(event, tg, tga,tgb) {
295 }).createDelegate(this, [e], true));
302 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
304 start: function(imageLink) {
306 // hide all the objects that cause problems..
307 $$('select', 'object', 'embed').each(
309 node.style.visibility = 'hidden' }
312 // stretch overlay to fill page and fade in
313 //var arrayPageSize = this.getPageSize();
314 Roo.get(document.body).mask(false);
315 Roo.get(document.body)._mask.setHeight(Roo.lib.Dom.getDocumentHeight())
316 //new Effect.Appear(this.overlay,
317 // { duration: this.overlayDuration, from: 0.0, to: this.overlayOpacity });
319 //this.imageArray = [];
323 imageLink = Roo.get(imageLink);
324 //console.log(imageLink.id);
325 Roo.each(this.imageArray, function (e, i) {
326 // console.log(e.id);
327 if (e.id == imageLink.id) {
333 // let's assume the constructor sorts out the list of images..
335 if ((imageLink.rel == 'lightbox')){
336 // if image is NOT part of a set, add single image to imageArray
337 this.imageArray.push([imageLink.href, imageLink.title]);
339 // if image is part of a set..
341 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
342 collect(function(anchor){ return [anchor.href, anchor.title]; }).
345 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
348 var s = Roo.get(document).getScroll();
350 // calculate top and left offset for the lightbox
352 var lightboxTop = s.top + (Roo.lib.Dom.getViewHeight() / 10);
353 var lightboxLeft = s.left
354 this.lightbox.setStyle({
355 top: lightboxTop + 'px',
356 left: lightboxLeft + 'px' ,
357 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.1
359 //console.log("show lightbox");
360 this.lightbox.show();
364 this.changeImage(imageNum);
369 // Hide most elements and preload image in preparation for resizing image container.
371 changeImage: function(imageNum) {
373 this.activeImage = imageNum; // update global var
375 // hide elements during transition
377 this.loading.setStyle({
378 zIndex : Roo.get(document.body)._mask.getStyle('zIndex') * 1.2
381 this.loadingLink.setX( (Roo.lib.Dom.getViewWidth() / 2) - 16);
382 //this.loadingLink.show();
383 // center the loading?
387 this.hoverNav.hide();
388 this.prevLink.hide();
389 this.nextLink.hide();
390 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
391 this.imageDataContainer.setStyle({opacity: .0001});
392 this.numberDisplay.hide();
394 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
395 // once image is preloaded, resize image container
398 imgPreloader.on('load', function() {
400 this.lightboxImage.dom.src = this.imageArray[this.activeImage].dom.href;
402 this.resizeImageContainer(imgPreloader.getWidth(), imgPreloader.getHeight());
403 imgPreloader.remove();
405 imgPreloader.dom.src = this.imageArray[this.activeImage].dom.href;
409 // resizeImageContainer()
411 resizeImageContainer: function(imgWidth, imgHeight) {
413 // make sure we have some sensible sizes..
414 imgWidth = Math.max(50, imgWidth);
415 imgHeight = Math.max(50, imgHeight);
417 // get current width and height
418 var widthCurrent = this.outerImageContainer.getWidth();
419 var heightCurrent = this.outerImageContainer.getHeight();
421 //fixme need better calcs.
423 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
424 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
428 // get new width and height
429 var bs = this.borderSize * 2;
431 var widthNew = imgWidth + bs;
432 var heightNew = imgHeight + bs;
434 if (widthNew > ww || heightNew > wh) {
435 // bigger than window.
436 // scale here... - bit nasty..
437 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
438 //console.log(rescale);
439 imgWidth = Math.floor(imgWidth / rescale);
440 imgHeight = Math.floor(imgHeight / rescale);
441 widthNew = imgWidth + bs;
442 heightNew = imgHeight + bs;
447 this.lightboxImage.set( { width : imgWidth, height : imgHeight });
450 // scalars based on change from old to new
451 var xScale = (widthNew / widthCurrent) * 100;
452 var yScale = (heightNew / heightCurrent) * 100;
454 // calculate size difference between new and old image, and resize if necessary
455 var wDiff = widthCurrent - widthNew;
456 var hDiff = heightCurrent - heightNew;
459 this.outerImageContainer.animate(
460 { width: {to: widthNew}, height: {to: heightNew} },
469 // if new and old image are same size and no scaling transition is necessary,
470 // do a quick pause to prevent image flicker.
472 if ((hDiff == 0) && (wDiff == 0)){
474 if (Roo.isIE) timeout = 250;
478 this.prevLink.setStyle({ height: imgHeight + 'px' });
479 this.nextLink.setStyle({ height: imgHeight + 'px' });
480 this.imageDataContainer.setStyle({ width: widthNew + 'px'}); // the text area..
482 }).defer(timeout / 1000, this);
487 // Display image and begin preloading neighbors.
489 showImage: function(){
492 //this.loadingLink.hide();
494 this.lightboxImage.animate(
496 opacity : { to: 1.0 }
500 _this.updateDetails();
504 this.preloadNeighborImages();
509 // Display caption, image number, and bottom nav.
511 updateDetails: function() {
513 // if caption is not null
514 if (this.imageArray[this.activeImage].dom.title != ""){
515 this.caption.update(this.imageArray[this.activeImage].dom.title);
519 // if image is part of set display 'Image x of x'
520 if (this.imageArray.length > 1){
521 this.numberDisplay.update(
522 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
523 this.numberDisplay.show();
526 this.imageDataContainer.animate(
528 //slide down ?: , from: 0.0, to: 1.0
529 opacity : { to: 1.0 }
533 // update overlay size and update nav
534 //var arrayPageSize = this.getPageSize();
535 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
544 // Display appropriate previous and next hover navigation.
546 updateNav: function() {
548 this.hoverNav.show();
550 // if not first image in set, display prev image button
551 if (this.activeImage > 0) this.prevLink.show();
553 // if not last image in set, display next image button
554 if (this.activeImage < (this.imageArray.length - 1)) this.nextLink.show();
556 this.enableKeyboardNav();
560 // enableKeyboardNav()
562 enableKeyboardNav: function() {
563 Roo.get(document.body).on('keydown', this.keyboardAction, this);
567 // disableKeyboardNav()
569 disableKeyboardNav: function() {
570 Roo.get(document.body).un('keydown', this.keyboardAction, this);
576 keyboardAction: function(event) {
577 var keycode = event.keyCode;
580 if (event.DOM_VK_ESCAPE) { // mozilla
581 escapeKey = event.DOM_VK_ESCAPE;
586 var key = String.fromCharCode(keycode).toLowerCase();
588 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
590 } else if ((key == 'p') || (keycode == 37)){ // display previous image
591 if (this.activeImage != 0){
592 this.disableKeyboardNav();
593 this.changeImage(this.activeImage - 1);
595 } else if ((key == 'n') || (keycode == 39)){ // display next image
596 if (this.activeImage != (this.imageArray.length - 1)){
597 this.disableKeyboardNav();
598 this.changeImage(this.activeImage + 1);
604 // preloadNeighborImages()
605 // Preload previous and next images.
607 preloadNeighborImages: function(){
608 var preloadNextImage, preloadPrevImage;
609 if (this.imageArray.length > this.activeImage + 1){
610 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
611 preloadNextImage.on('load', function() { preloadNextImage.remove() });
612 preloadNextImage.dom.src = this.imageArray[this.activeImage + 1].dom.href;
614 if (this.activeImage > 0){
615 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
616 preloadPrevImage.on('load', function() { preloadPrevImage.remove() });
617 preloadPrevImage.dom.src = this.imageArray[this.activeImage - 1].dom.href;
626 this.disableKeyboardNav();
627 //console.log('lightbox hide');
628 this.lightbox.hide();
630 Roo.get(document.body).unmask();
631 // show all the objects that cause problems..
632 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });