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) {
51 Roo.apply(Lightbox.prototype,
55 * @cfg fileLoadingImage {string} loading image
57 fileLoadingImage: '/lightbox/images/loading.gif',
59 * @cfg fileBottomNavCloseImage {string} close image
61 fileBottomNavCloseImage: '/lightbox/images/closelabel.gif',
63 * @cfg overlayOpacity {number} controls transparency of shadow overlay
67 * @cfg fileLoadingImage {boolean} toggles resizing animations
71 * @cfg resizeSpeed {number} controls the speed of the image
72 * resizing animations (1=slowest and 10=fastest)
76 * @cfg borderSize {number} if you adjust the padding in the CSS,
77 * you will need to update this variable
81 * @cfg labelImage {string} When grouping images this is used to write: Image # of #.
82 * Change it for non-english localization
86 * @cfg labelOf {string} When grouping images this is used to write: Image # of #.
103 activeImage: undefined,
107 * Constructor runs on completion of the DOM loading. Calls updateImageList and then
108 * the function inserts html at the bottom of the page which is used to display the shadow
109 * overlay and the image container.
113 initialize: function() {
115 this.updateImageList();
117 // TBD this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
118 LightboxOptions.resizeSpeed = Math.min(LightboxOptions.resizeSpeed, 10);
119 LightboxOptions.resizeSpeed = Math.max(LightboxOptions.resizeSpeed, 1);
121 this.resizeDuration = LightboxOptions.animate ? ((11 - LightboxOptions.resizeSpeed) * 0.15) : 0;
122 this.overlayDuration = LightboxOptions.animate ? 0.2 : 0; // shadow fade in/out duration
124 // When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
125 // If animations are turned off, it will be hidden as to prevent a flicker of a
126 // white 250 by 250 box.
127 var size = (LightboxOptions.animate ? 250 : 1) + 'px';
128 var dh = Roo.DomHelper;
129 this.el = dh.append(document.body, {
131 // '<div id="overlay"></div>' +
132 '<div id="lightbox">' +
133 ' <div id="outerImageContainer">' +
134 ' <div id="imageContainer">' +
135 ' <img id="lightboxImage">' +
136 ' <div style="" id="hoverNav">' +
137 ' <a href="#" id="prevLink"></a>' +
138 ' <a href="#" id="nextLink"></a>' +
140 ' <div id="loading">' +
141 ' <a href="#" id="loadingLink">' +
142 ' <img src="images/loading.gif">' +
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 ' <a href="#" id="bottomNavClose">' +
155 ' <img src="images/close.gif">' +
167 //Roo.get('overlay').hide();
168 //Roo.get('overlay').on('click', this.end, this);
169 Roo.get('lightbox').hide();
170 Roo.get('lightbox').on('click', function(event) {
171 if (event.element().id == 'lightbox') this.end();
174 Roo.get('outerImageContainer').setStyle({ width: size, height: size });
176 Roo.get('prevLink').on('click',
179 this.changeImage(this.activeImage - 1);
182 Roo.get('nextLink').on('click',
185 this.changeImage(this.activeImage + 1);
188 Roo.get('loadingLink').on('click',
194 Roo.get('bottomNavClose').on('click',
203 'lightbox outerImageContainer imageContainer ' +
204 'lightboxImage hoverNav prevLink nextLink loading loadingLink ' +
205 'imageDataContainer imageData imageDetails caption numberDisplay '
206 'bottomNav bottomNavClose';
208 Roo.each(ids.split(' '),
210 th[id] = Roo.get(id);
217 * Loops through anchor tags looking for 'lightbox' references and applies onclick
218 * events to appropriate links. You can rerun after dynamically adding images w/ajax.
221 updateImageList: function() {
222 this.updateImageList = Roo.emptyFn;
224 Roo.each(Roo.DomQuery.select('a[rel^=lightbox]'), function(e) {
225 this.imageList.push(Roo.get(e));
226 Roo.get(e).on('click', function() {
227 //event.stopPropagation();
236 * Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
238 start: function(imageLink) {
240 // hide all the objects that cause problems..
241 $$('select', 'object', 'embed').each(
243 node.style.visibility = 'hidden' }
246 // stretch overlay to fill page and fade in
247 //var arrayPageSize = this.getPageSize();
250 this._mask = Roo.DomHelper.append(document.body, {cls:"roo-el-mask"}, true);
251 this._mask.on('click', this.end, this);
253 this._mask.setDisplayed(true); // no animation..
255 // ie will not expand full height automatically
256 if(Roo.isIE && !(Roo.isIE7 && Roo.isStrict) && this.getStyle('height') == 'auto'){
257 this._mask.setHeight( Ext.lib.Dom.getViewHeight());
260 //new Effect.Appear(this.overlay,
261 // { duration: this.overlayDuration, from: 0.0, to: LightboxOptions.overlayOpacity });
263 this.imageArray = [];
266 // let's assume the constructor sorts out the list of images..
268 if ((imageLink.rel == 'lightbox')){
269 // if image is NOT part of a set, add single image to imageArray
270 this.imageArray.push([imageLink.href, imageLink.title]);
272 // if image is part of a set..
274 $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
275 collect(function(anchor){ return [anchor.href, anchor.title]; }).
278 while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
281 var s = Roo.get(document).getScroll();
283 // calculate top and left offset for the lightbox
285 var lightboxTop = s.top + (Ext.lib.Dom.getViewHeight() / 10);
286 var lightboxLeft = s.left
287 this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' })
288 this.lightbox.show();
290 this.changeImage(imageNum);
295 // Hide most elements and preload image in preparation for resizing image container.
297 changeImage: function(imageNum) {
299 this.activeImage = imageNum; // update global var
301 // hide elements during transition
305 this.lightboxImage.hide();
307 this.hoverNav.hide();
308 this.prevLink.hide();
309 this.nextLink.hide();
310 // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
311 this.imageDataContainer.setStyle({opacity: .0001});
312 this.numberDisplay.hide();
314 var imgPreloader = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
315 // once image is preloaded, resize image container
318 imgPreloader.on('load', function() {
319 this.lightboxImage.src = this.imageArray[this.activeImage][0];
320 this.resizeImageContainer(imgPreloader.getWidth(), imgPreloader.getHeight());
322 imgPreloader.src = this.imageArray[this.activeImage][0];
326 // resizeImageContainer()
328 resizeImageContainer: function(imgWidth, imgHeight) {
330 // make sure we have some sensible sizes..
331 imgWidth = Math.max(50, imgWidth);
332 imgHeight = Math.max(50, imgHeight);
334 // get current width and height
335 var widthCurrent = this.outerImageContainer.getWidth();
336 var heightCurrent = this.outerImageContainer.getHeight();
338 //fixme need better calcs.
340 var ww = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
341 var wh = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
345 // get new width and height
346 var bs = this.borderSize * 2;
348 var widthNew = imgWidth + bs;
349 var heightNew = imgHeight + bs;
351 if (widthNew > ww || heightNew > wh) {
352 // bigger than window.
353 // scale here... - bit nasty..
354 var rescale = 1.1 * Math.max( widthNew / ww , heightNew / wh);
355 //console.log(rescale);
356 imgWidth = Math.floor(imgWidth / rescale);
357 imgHeight = Math.floor(imgHeight / rescale);
358 widthNew = imgWidth + bs;
359 heightNew = imgHeight + bs;
364 this.lightboxImage.set( { width : imgWidth, height : imgHeight });
367 // scalars based on change from old to new
368 var xScale = (widthNew / widthCurrent) * 100;
369 var yScale = (heightNew / heightCurrent) * 100;
371 // calculate size difference between new and old image, and resize if necessary
372 var wDiff = widthCurrent - widthNew;
373 var hDiff = heightCurrent - heightNew;
376 this.outerImageContainer.animate(
377 { width: {to: widthNew}, height: {to: heightNew},
386 // if new and old image are same size and no scaling transition is necessary,
387 // do a quick pause to prevent image flicker.
389 if ((hDiff == 0) && (wDiff == 0)){
391 if (Roo.isIE) timeout = 250;
395 this.prevLink.setStyle({ height: imgHeight + 'px' });
396 this.nextLink.setStyle({ height: imgHeight + 'px' });
397 this.imageDataContainer.setStyle({ width: widthNew + 'px', width: heightNew + 'px', });
399 }).defer(timeout / 1000, this);
404 // Display image and begin preloading neighbors.
406 showImage: function(){
409 this.lightboxImage.animate(
411 opacity : { from : 0.0 , to: 1.0 }
414 _this.updateDetails();
418 this.preloadNeighborImages();
423 // Display caption, image number, and bottom nav.
425 updateDetails: function() {
427 // if caption is not null
428 if (this.imageArray[this.activeImage][1] != ""){
429 this.caption.update(this.imageArray[this.activeImage][1]).show();
432 // if image is part of set display 'Image x of x'
433 if (this.imageArray.length > 1){
434 this.numberDisplay.update(
435 this.labelImage + ' ' + (this.activeImage + 1) + ' ' + this.labelOf + ' ' + this.imageArray.length)
436 this.numberDisplay.show();
439 this.imageDataContainer.animate(
441 //slide down ?: , from: 0.0, to: 1.0
442 opacity : { { from : 0.0 , to: 1.0 }
446 // update overlay size and update nav
447 //var arrayPageSize = this.getPageSize();
448 //this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
457 // Display appropriate previous and next hover navigation.
459 updateNav: function() {
461 this.hoverNav.show();
463 // if not first image in set, display prev image button
464 if (this.activeImage > 0) this.prevLink.show();
466 // if not last image in set, display next image button
467 if (this.activeImage < (this.imageArray.length - 1)) this.nextLink.show();
469 this.enableKeyboardNav();
473 // enableKeyboardNav()
475 enableKeyboardNav: function() {
476 Roo.get(document.body).on('keydown', this.keyboardAction, this);
480 // disableKeyboardNav()
482 disableKeyboardNav: function() {
483 Roo.get(document.body).un('keydown', this.keyboardAction, this);
489 keyboardAction: function(event) {
490 var keycode = event.keyCode;
493 if (event.DOM_VK_ESCAPE) { // mozilla
494 escapeKey = event.DOM_VK_ESCAPE;
499 var key = String.fromCharCode(keycode).toLowerCase();
501 if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
503 } else if ((key == 'p') || (keycode == 37)){ // display previous image
504 if (this.activeImage != 0){
505 this.disableKeyboardNav();
506 this.changeImage(this.activeImage - 1);
508 } else if ((key == 'n') || (keycode == 39)){ // display next image
509 if (this.activeImage != (this.imageArray.length - 1)){
510 this.disableKeyboardNav();
511 this.changeImage(this.activeImage + 1);
517 // preloadNeighborImages()
518 // Preload previous and next images.
520 preloadNeighborImages: function(){
521 var preloadNextImage, preloadPrevImage;
522 if (this.imageArray.length > this.activeImage + 1){
523 preloadNextImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
524 preloadNextImage.src = this.imageArray[this.activeImage + 1][0];
526 if (this.activeImage > 0){
527 preloadPrevImage = Roo.DomHelper.append(document.body, { tag: 'img' } , true);
528 preloadPrevImage.src = this.imageArray[this.activeImage - 1][0];
537 this.disableKeyboardNav();
538 this.lightbox.hide();
540 // show all the objects that cause problems..
541 //$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });