You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

496 lines
18KB

  1. // -----------------------------------------------------------------------------------
  2. //
  3. // Lightbox v2.05
  4. // by Lokesh Dhakar - http://www.lokeshdhakar.com
  5. // Last Modification: 3/18/11
  6. //
  7. // For more information, visit:
  8. // http://lokeshdhakar.com/projects/lightbox2/
  9. //
  10. // Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
  11. // - Free for use in both personal and commercial projects
  12. // - Attribution requires leaving author name, author link, and the license info intact.
  13. //
  14. // Thanks: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.
  15. // Artemy Tregubenko (arty.name) for cleanup and help in updating to latest ver of proto-aculous.
  16. //
  17. // -----------------------------------------------------------------------------------
  18. /*
  19. Table of Contents
  20. -----------------
  21. Configuration
  22. Lightbox Class Declaration
  23. - initialize()
  24. - updateImageList()
  25. - start()
  26. - changeImage()
  27. - resizeImageContainer()
  28. - showImage()
  29. - updateDetails()
  30. - updateNav()
  31. - enableKeyboardNav()
  32. - disableKeyboardNav()
  33. - keyboardAction()
  34. - preloadNeighborImages()
  35. - end()
  36. Function Calls
  37. - document.observe()
  38. */
  39. // -----------------------------------------------------------------------------------
  40. //
  41. // Configurationl
  42. //
  43. LightboxOptions = Object.extend({
  44. fileLoadingImage: 'images/loading.gif',
  45. fileBottomNavCloseImage: 'images/closelabel.gif',
  46. overlayOpacity: 0.8, // controls transparency of shadow overlay
  47. animate: true, // toggles resizing animations
  48. resizeSpeed: 9, // controls the speed of the image resizing animations (1=slowest and 10=fastest)
  49. borderSize: 10, //if you adjust the padding in the CSS, you will need to update this variable
  50. // When grouping images this is used to write: Image # of #.
  51. // Change it for non-english localization
  52. labelImage: "Image",
  53. labelOf: "on"
  54. }, window.LightboxOptions || {});
  55. // -----------------------------------------------------------------------------------
  56. var Lightbox = Class.create();
  57. Lightbox.prototype = {
  58. imageArray: [],
  59. activeImage: undefined,
  60. // initialize()
  61. // Constructor runs on completion of the DOM loading. Calls updateImageList and then
  62. // the function inserts html at the bottom of the page which is used to display the shadow
  63. // overlay and the image container.
  64. //
  65. initialize: function() {
  66. this.updateImageList();
  67. this.keyboardAction = this.keyboardAction.bindAsEventListener(this);
  68. if (LightboxOptions.resizeSpeed > 10) LightboxOptions.resizeSpeed = 10;
  69. if (LightboxOptions.resizeSpeed < 1) LightboxOptions.resizeSpeed = 1;
  70. this.resizeDuration = LightboxOptions.animate ? ((11 - LightboxOptions.resizeSpeed) * 0.15) : 0;
  71. this.overlayDuration = LightboxOptions.animate ? 0.2 : 0; // shadow fade in/out duration
  72. // When Lightbox starts it will resize itself from 250 by 250 to the current image dimension.
  73. // If animations are turned off, it will be hidden as to prevent a flicker of a
  74. // white 250 by 250 box.
  75. var size = (LightboxOptions.animate ? 250 : 1) + 'px';
  76. // Code inserts html at the bottom of the page that looks similar to this:
  77. //
  78. // <div id="overlay"></div>
  79. // <div id="lightbox">
  80. // <div id="outerImageContainer">
  81. // <div id="imageContainer">
  82. // <img id="lightboxImage">
  83. // <div style="" id="hoverNav">
  84. // <a href="#" id="prevLink"></a>
  85. // <a href="#" id="nextLink"></a>
  86. // </div>
  87. // <div id="loading">
  88. // <a href="#" id="loadingLink">
  89. // <img src="images/loading.gif">
  90. // </a>
  91. // </div>
  92. // </div>
  93. // </div>
  94. // <div id="imageDataContainer">
  95. // <div id="imageData">
  96. // <div id="imageDetails">
  97. // <span id="caption"></span>
  98. // <span id="numberDisplay"></span>
  99. // </div>
  100. // <div id="bottomNav">
  101. // <a href="#" id="bottomNavClose">
  102. // <img src="images/close.gif">
  103. // </a>
  104. // </div>
  105. // </div>
  106. // </div>
  107. // </div>
  108. var objBody = $$('body')[0];
  109. objBody.appendChild(Builder.node('div',{id:'overlay'}));
  110. objBody.appendChild(Builder.node('div',{id:'lightbox'}, [
  111. Builder.node('div',{id:'outerImageContainer'},
  112. Builder.node('div',{id:'imageContainer'}, [
  113. Builder.node('img',{id:'lightboxImage'}),
  114. Builder.node('div',{id:'hoverNav'}, [
  115. Builder.node('a',{id:'prevLink', href: '#' }),
  116. Builder.node('a',{id:'nextLink', href: '#' })
  117. ]),
  118. Builder.node('div',{id:'loading'},
  119. Builder.node('a',{id:'loadingLink', href: '#' },
  120. Builder.node('img', {src: LightboxOptions.fileLoadingImage})
  121. )
  122. )
  123. ])
  124. ),
  125. Builder.node('div', {id:'imageDataContainer'},
  126. Builder.node('div',{id:'imageData'}, [
  127. Builder.node('div',{id:'imageDetails'}, [
  128. Builder.node('span',{id:'caption'}),
  129. Builder.node('span',{id:'numberDisplay'})
  130. ]),
  131. Builder.node('div',{id:'bottomNav'},
  132. Builder.node('a',{id:'bottomNavClose', href: '#' },
  133. Builder.node('img', { src: LightboxOptions.fileBottomNavCloseImage })
  134. )
  135. )
  136. ])
  137. )
  138. ]));
  139. $('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
  140. $('lightbox').hide().observe('click', (function(event) { if (event.element().id == 'lightbox') this.end(); }).bind(this));
  141. $('outerImageContainer').setStyle({ width: size, height: size });
  142. $('prevLink').observe('click', (function(event) { event.stop(); this.changeImage(this.activeImage - 1); }).bindAsEventListener(this));
  143. $('nextLink').observe('click', (function(event) { event.stop(); this.changeImage(this.activeImage + 1); }).bindAsEventListener(this));
  144. $('loadingLink').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));
  145. $('bottomNavClose').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));
  146. var th = this;
  147. (function(){
  148. var ids =
  149. 'overlay lightbox outerImageContainer imageContainer lightboxImage hoverNav prevLink nextLink loading loadingLink ' +
  150. 'imageDataContainer imageData imageDetails caption numberDisplay bottomNav bottomNavClose';
  151. $w(ids).each(function(id){ th[id] = $(id); });
  152. }).defer();
  153. },
  154. //
  155. // updateImageList()
  156. // Loops through anchor tags looking for 'lightbox' references and applies onclick
  157. // events to appropriate links. You can rerun after dynamically adding images w/ajax.
  158. //
  159. updateImageList: function() {
  160. this.updateImageList = Prototype.emptyFunction;
  161. document.observe('click', (function(event){
  162. var target = event.findElement('a[rel^=lightbox]') || event.findElement('area[rel^=lightbox]');
  163. if (target) {
  164. event.stop();
  165. this.start(target);
  166. }
  167. }).bind(this));
  168. },
  169. //
  170. // start()
  171. // Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
  172. //
  173. start: function(imageLink) {
  174. $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
  175. // stretch overlay to fill page and fade in
  176. var arrayPageSize = this.getPageSize();
  177. $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
  178. new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: LightboxOptions.overlayOpacity });
  179. this.imageArray = [];
  180. var imageNum = 0;
  181. if ((imageLink.getAttribute("rel") == 'lightbox')){
  182. // if image is NOT part of a set, add single image to imageArray
  183. this.imageArray.push([imageLink.href, imageLink.title]);
  184. } else {
  185. // if image is part of a set..
  186. this.imageArray =
  187. $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
  188. collect(function(anchor){ return [anchor.href, anchor.title]; }).
  189. uniq();
  190. while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
  191. }
  192. // calculate top and left offset for the lightbox
  193. var arrayPageScroll = document.viewport.getScrollOffsets();
  194. var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
  195. var lightboxLeft = arrayPageScroll[0];
  196. this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
  197. this.changeImage(imageNum);
  198. },
  199. //
  200. // changeImage()
  201. // Hide most elements and preload image in preparation for resizing image container.
  202. //
  203. changeImage: function(imageNum) {
  204. this.activeImage = imageNum; // update global var
  205. // hide elements during transition
  206. if (LightboxOptions.animate) this.loading.show();
  207. this.lightboxImage.hide();
  208. this.hoverNav.hide();
  209. this.prevLink.hide();
  210. this.nextLink.hide();
  211. // HACK: Opera9 does not currently support scriptaculous opacity and appear fx
  212. this.imageDataContainer.setStyle({opacity: .0001});
  213. this.numberDisplay.hide();
  214. var imgPreloader = new Image();
  215. // once image is preloaded, resize image container
  216. imgPreloader.onload = (function(){
  217. this.lightboxImage.src = this.imageArray[this.activeImage][0];
  218. /*Bug Fixed by Andy Scott*/
  219. this.lightboxImage.width = imgPreloader.width;
  220. this.lightboxImage.height = imgPreloader.height;
  221. /*End of Bug Fix*/
  222. this.resizeImageContainer(imgPreloader.width, imgPreloader.height);
  223. }).bind(this);
  224. imgPreloader.src = this.imageArray[this.activeImage][0];
  225. },
  226. //
  227. // resizeImageContainer()
  228. //
  229. resizeImageContainer: function(imgWidth, imgHeight) {
  230. // get current width and height
  231. var widthCurrent = this.outerImageContainer.getWidth();
  232. var heightCurrent = this.outerImageContainer.getHeight();
  233. // get new width and height
  234. var widthNew = (imgWidth + LightboxOptions.borderSize * 2);
  235. var heightNew = (imgHeight + LightboxOptions.borderSize * 2);
  236. // scalars based on change from old to new
  237. var xScale = (widthNew / widthCurrent) * 100;
  238. var yScale = (heightNew / heightCurrent) * 100;
  239. // calculate size difference between new and old image, and resize if necessary
  240. var wDiff = widthCurrent - widthNew;
  241. var hDiff = heightCurrent - heightNew;
  242. if (hDiff != 0) new Effect.Scale(this.outerImageContainer, yScale, {scaleX: false, duration: this.resizeDuration, queue: 'front'});
  243. if (wDiff != 0) new Effect.Scale(this.outerImageContainer, xScale, {scaleY: false, duration: this.resizeDuration, delay: this.resizeDuration});
  244. // if new and old image are same size and no scaling transition is necessary,
  245. // do a quick pause to prevent image flicker.
  246. var timeout = 0;
  247. if ((hDiff == 0) && (wDiff == 0)){
  248. timeout = 100;
  249. if (Prototype.Browser.IE) timeout = 250;
  250. }
  251. (function(){
  252. this.prevLink.setStyle({ height: imgHeight + 'px' });
  253. this.nextLink.setStyle({ height: imgHeight + 'px' });
  254. this.imageDataContainer.setStyle({ width: widthNew + 'px' });
  255. this.showImage();
  256. }).bind(this).delay(timeout / 1000);
  257. },
  258. //
  259. // showImage()
  260. // Display image and begin preloading neighbors.
  261. //
  262. showImage: function(){
  263. this.loading.hide();
  264. new Effect.Appear(this.lightboxImage, {
  265. duration: this.resizeDuration,
  266. queue: 'end',
  267. afterFinish: (function(){ this.updateDetails(); }).bind(this)
  268. });
  269. this.preloadNeighborImages();
  270. },
  271. //
  272. // updateDetails()
  273. // Display caption, image number, and bottom nav.
  274. //
  275. updateDetails: function() {
  276. this.caption.update(this.imageArray[this.activeImage][1]).show();
  277. // if image is part of set display 'Image x of x'
  278. if (this.imageArray.length > 1){
  279. this.numberDisplay.update( LightboxOptions.labelImage + ' ' + (this.activeImage + 1) + ' ' + LightboxOptions.labelOf + ' ' + this.imageArray.length).show();
  280. }
  281. new Effect.Parallel(
  282. [
  283. new Effect.SlideDown(this.imageDataContainer, { sync: true, duration: this.resizeDuration, from: 0.0, to: 1.0 }),
  284. new Effect.Appear(this.imageDataContainer, { sync: true, duration: this.resizeDuration })
  285. ],
  286. {
  287. duration: this.resizeDuration,
  288. afterFinish: (function() {
  289. // update overlay size and update nav
  290. var arrayPageSize = this.getPageSize();
  291. this.overlay.setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
  292. this.updateNav();
  293. }).bind(this)
  294. }
  295. );
  296. },
  297. //
  298. // updateNav()
  299. // Display appropriate previous and next hover navigation.
  300. //
  301. updateNav: function() {
  302. this.hoverNav.show();
  303. // if not first image in set, display prev image button
  304. if (this.activeImage > 0) this.prevLink.show();
  305. // if not last image in set, display next image button
  306. if (this.activeImage < (this.imageArray.length - 1)) this.nextLink.show();
  307. this.enableKeyboardNav();
  308. },
  309. //
  310. // enableKeyboardNav()
  311. //
  312. enableKeyboardNav: function() {
  313. document.observe('keydown', this.keyboardAction);
  314. },
  315. //
  316. // disableKeyboardNav()
  317. //
  318. disableKeyboardNav: function() {
  319. document.stopObserving('keydown', this.keyboardAction);
  320. },
  321. //
  322. // keyboardAction()
  323. //
  324. keyboardAction: function(event) {
  325. var keycode = event.keyCode;
  326. var escapeKey;
  327. if (event.DOM_VK_ESCAPE) { // mozilla
  328. escapeKey = event.DOM_VK_ESCAPE;
  329. } else { // ie
  330. escapeKey = 27;
  331. }
  332. var key = String.fromCharCode(keycode).toLowerCase();
  333. if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
  334. this.end();
  335. } else if ((key == 'p') || (keycode == 37)){ // display previous image
  336. if (this.activeImage != 0){
  337. this.disableKeyboardNav();
  338. this.changeImage(this.activeImage - 1);
  339. }
  340. } else if ((key == 'n') || (keycode == 39)){ // display next image
  341. if (this.activeImage != (this.imageArray.length - 1)){
  342. this.disableKeyboardNav();
  343. this.changeImage(this.activeImage + 1);
  344. }
  345. }
  346. },
  347. //
  348. // preloadNeighborImages()
  349. // Preload previous and next images.
  350. //
  351. preloadNeighborImages: function(){
  352. var preloadNextImage, preloadPrevImage;
  353. if (this.imageArray.length > this.activeImage + 1){
  354. preloadNextImage = new Image();
  355. preloadNextImage.src = this.imageArray[this.activeImage + 1][0];
  356. }
  357. if (this.activeImage > 0){
  358. preloadPrevImage = new Image();
  359. preloadPrevImage.src = this.imageArray[this.activeImage - 1][0];
  360. }
  361. },
  362. //
  363. // end()
  364. //
  365. end: function() {
  366. this.disableKeyboardNav();
  367. this.lightbox.hide();
  368. new Effect.Fade(this.overlay, { duration: this.overlayDuration });
  369. $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
  370. },
  371. //
  372. // getPageSize()
  373. //
  374. getPageSize: function() {
  375. var xScroll, yScroll;
  376. if (window.innerHeight && window.scrollMaxY) {
  377. xScroll = window.innerWidth + window.scrollMaxX;
  378. yScroll = window.innerHeight + window.scrollMaxY;
  379. } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  380. xScroll = document.body.scrollWidth;
  381. yScroll = document.body.scrollHeight;
  382. } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  383. xScroll = document.body.offsetWidth;
  384. yScroll = document.body.offsetHeight;
  385. }
  386. var windowWidth, windowHeight;
  387. if (self.innerHeight) { // all except Explorer
  388. if(document.documentElement.clientWidth){
  389. windowWidth = document.documentElement.clientWidth;
  390. } else {
  391. windowWidth = self.innerWidth;
  392. }
  393. windowHeight = self.innerHeight;
  394. } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  395. windowWidth = document.documentElement.clientWidth;
  396. windowHeight = document.documentElement.clientHeight;
  397. } else if (document.body) { // other Explorers
  398. windowWidth = document.body.clientWidth;
  399. windowHeight = document.body.clientHeight;
  400. }
  401. // for small pages with total height less then height of the viewport
  402. if(yScroll < windowHeight){
  403. pageHeight = windowHeight;
  404. } else {
  405. pageHeight = yScroll;
  406. }
  407. // for small pages with total width less then width of the viewport
  408. if(xScroll < windowWidth){
  409. pageWidth = xScroll;
  410. } else {
  411. pageWidth = windowWidth;
  412. }
  413. return [pageWidth,pageHeight];
  414. }
  415. }
  416. document.observe('dom:loaded', function () { new Lightbox(); });