lightbox_lite.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* $Id: lightbox_lite.js,v 1.1.2.2.2.19 2010/06/07 14:54:30 snpower Exp $ */
  2. /**
  3. * Lightbox JS: Fullsize Image Overlays
  4. * by Lokesh Dhakar - http://www.huddletogether.com
  5. *
  6. * For more information on this script, visit:
  7. * http://huddletogether.com/projects/lightbox/
  8. *
  9. * This script is distributed via Drupal.org with permission from Lokesh Dhakar.
  10. * Under GPL license.
  11. * Mailto: bugzie@gmail.com
  12. */
  13. // start jQuery block
  14. (function ($) {
  15. //
  16. // getPageScroll()
  17. // Returns array with x,y page scroll values.
  18. // Core code from - quirksmode.org
  19. //
  20. function getPageScroll() {
  21. var xScroll, yScroll;
  22. if (self.pageYOffset) {
  23. yScroll = self.pageYOffset;
  24. xScroll = self.pageXOffset;
  25. // Explorer 6 Strict
  26. }
  27. else if (document.documentElement && document.documentElement.scrollTop) {
  28. yScroll = document.documentElement.scrollTop;
  29. xScroll = document.documentElement.scrollLeft;
  30. }
  31. else if (document.body) {// all other Explorers
  32. yScroll = document.body.scrollTop;
  33. xScroll = document.body.scrollLeft;
  34. }
  35. arrayPageScroll = [xScroll, yScroll];
  36. return arrayPageScroll;
  37. }
  38. // getPageSize()
  39. // Returns array with page width, height and window width, height
  40. // Core code from - quirksmode.org
  41. // Edit for Firefox by pHaez
  42. function getPageSize() {
  43. var xScroll, yScroll;
  44. if (window.innerHeight && window.scrollMaxY) {
  45. xScroll = window.innerWidth + window.scrollMaxX;
  46. yScroll = window.innerHeight + window.scrollMaxY;
  47. // all but Explorer Mac
  48. }
  49. else if (document.body.scrollHeight > document.body.offsetHeight) {
  50. xScroll = document.body.scrollWidth;
  51. yScroll = document.body.scrollHeight;
  52. // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  53. }
  54. else {
  55. xScroll = document.body.offsetWidth;
  56. yScroll = document.body.offsetHeight;
  57. }
  58. var windowWidth, windowHeight;
  59. if (self.innerHeight) { // all except Explorer
  60. if (document.documentElement.clientHeight) {
  61. windowWidth = document.documentElement.clientWidth;
  62. }
  63. else {
  64. windowWidth = self.innerWidth;
  65. }
  66. windowHeight = self.innerHeight;
  67. }
  68. else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  69. windowWidth = document.documentElement.clientWidth;
  70. windowHeight = document.documentElement.clientHeight;
  71. }
  72. else if (document.body) { // other Explorers
  73. windowWidth = document.body.clientWidth;
  74. windowHeight = document.body.clientHeight;
  75. }
  76. // for small pages with total height less then height of the viewport
  77. if (yScroll < windowHeight) {
  78. pageHeight = windowHeight;
  79. }
  80. else {
  81. pageHeight = yScroll;
  82. }
  83. // for small pages with total width less then width of the viewport
  84. if (xScroll < windowWidth) {
  85. pageWidth = xScroll;
  86. }
  87. else {
  88. pageWidth = windowWidth;
  89. }
  90. arrayPageSize = [pageWidth, pageHeight, windowWidth, windowHeight];
  91. return arrayPageSize;
  92. }
  93. // pause(numberMillis)
  94. function pause(ms) {
  95. var date = new Date();
  96. var curDate = null;
  97. do { curDate = new Date(); }
  98. while (curDate - date < ms);
  99. }
  100. // hideLightbox()
  101. function hideLightbox() {
  102. // get objects
  103. objOverlay = document.getElementById('lightbox2-overlay');
  104. objLightbox = document.getElementById('lightbox');
  105. // hide lightbox and overlay
  106. objOverlay.style.display = 'none';
  107. objLightbox.style.display = 'none';
  108. // make select boxes visible
  109. selects = document.getElementsByTagName("select");
  110. for (i = 0; i != selects.length; i++) {
  111. if (selects[i].style.display != "none") {
  112. selects[i].style.visibility = "visible";
  113. }
  114. }
  115. // make flash objects visible
  116. embed = document.getElementsByTagName("embed");
  117. for (i = 0; i != embed.length; i++) {
  118. if (embed[i].style.display != "none") {
  119. embed[i].style.visibility = "visible";
  120. }
  121. }
  122. objects = document.getElementsByTagName("object");
  123. for (i = 0; i != objects.length; i++) {
  124. if (objects[i].style.display != "none") {
  125. objects[i].style.visibility = "visible";
  126. }
  127. }
  128. // disable keydown listener
  129. document.onkeydown = '';
  130. }
  131. // getKey(key)
  132. // Gets keycode. If 'x' is pressed then it hides the lightbox.
  133. function getKey(e) {
  134. if (e === null) { // ie
  135. keycode = event.keyCode;
  136. escapeKey = 27;
  137. }
  138. else { // mozilla
  139. keycode = e.keyCode;
  140. escapeKey = e.DOM_VK_ESCAPE;
  141. }
  142. key = String.fromCharCode(keycode).toLowerCase();
  143. if (key == 'x' || key == 'c' || keycode == escapeKey) { hideLightbox(); }
  144. }
  145. // listenKey()
  146. function listenKey () { document.onkeydown = getKey; }
  147. function imgLoadingError(image, objImage, objLink) {
  148. var settings = Drupal.settings.lightbox2;
  149. image.src = settings.default_image;
  150. objImage.src = settings.default_image;
  151. objLink.href = settings.default_image;
  152. }
  153. // showLightbox()
  154. // Preloads images. Pleaces new image in lightbox then centers and displays.
  155. function showLightbox(objLink) {
  156. var settings = Drupal.settings.lightbox2;
  157. // prep objects
  158. var objOverlay = document.getElementById('lightbox2-overlay');
  159. var objLightbox = document.getElementById('lightbox');
  160. var objCaption = document.getElementById('lightboxCaption');
  161. var objImage = document.getElementById('lightboxImage');
  162. var objLoadingImage = document.getElementById('loadingImage');
  163. var objLightboxDetails = document.getElementById('lightboxDetails');
  164. var arrayPageSize = getPageSize();
  165. var arrayPageScroll = getPageScroll();
  166. // set height of Overlay to take up whole page and show
  167. objOverlay.style.height = (arrayPageSize[1] + 'px');
  168. objOverlay.style.display = 'block';
  169. objOverlay.style.opacity = settings.overlay_opacity;
  170. objOverlay.style.backgroundColor = '#' + settings.overlay_color;
  171. // preload image
  172. imgPreload = new Image();
  173. imgPreload.onerror = function() { imgLoadingError(this, objImage, objLink); };
  174. imgPreload.onload = function() {
  175. objImage.src = objLink.href;
  176. // center lightbox and make sure that the top and left values are not
  177. // negative and the image placed outside the viewport
  178. var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
  179. var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
  180. objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
  181. objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
  182. //objLightboxDetails.style.width = imgPreload.width + 'px';
  183. objLightbox.style.width = imgPreload.width + 'px';
  184. if (objLink.getAttribute('title')) {
  185. objCaption.style.display = 'block';
  186. //objCaption.style.width = imgPreload.width + 'px';
  187. objCaption.innerHTML = objLink.getAttribute('title');
  188. }
  189. else {
  190. objCaption.style.display = 'none';
  191. }
  192. // A small pause between the image loading and displaying is required with
  193. // IE, this prevents the previous image displaying for a short burst
  194. // causing flicker.
  195. if (navigator.appVersion.indexOf("MSIE") != -1) {
  196. pause(250);
  197. }
  198. if (objLoadingImage) { objLoadingImage.style.display = 'none'; }
  199. // Hide select boxes as they will 'peek' through the image in IE
  200. selects = document.getElementsByTagName("select");
  201. for (i = 0; i != selects.length; i++) {
  202. if (selects[i].style.display != "none") {
  203. selects[i].style.visibility = "hidden";
  204. }
  205. }
  206. // Hide flash objects as they will 'peek' through the image in IE
  207. embed = document.getElementsByTagName("embed");
  208. for (i = 0; i != embed.length; i++) {
  209. if (embed[i].style.display != "none") {
  210. embed[i].style.visibility = "hidden";
  211. }
  212. }
  213. objects = document.getElementsByTagName("object");
  214. for (i = 0; i != objects.length; i++) {
  215. if (objects[i].style.display != "none") {
  216. objects[i].style.visibility = "hidden";
  217. }
  218. }
  219. objLightbox.style.display = 'block';
  220. // After image is loaded, update the overlay height as the new image might
  221. // have increased the overall page height.
  222. arrayPageSize = getPageSize();
  223. objOverlay.style.height = (arrayPageSize[1] + 'px');
  224. // Check for 'x' keydown
  225. listenKey();
  226. return false;
  227. };
  228. imgPreload.src = objLink.href;
  229. }
  230. // initLightbox()
  231. // Function runs on window load, going through link tags looking for
  232. // rel="lightbox". These links receive onclick events that enable the lightbox
  233. // display for their targets. The function also inserts html markup at the top
  234. // of the page which will be used as a container for the overlay pattern and
  235. // the inline image.
  236. function initLightbox() {
  237. if (!document.getElementsByTagName) { return; }
  238. var anchors = document.getElementsByTagName("a");
  239. // loop through all anchor tags
  240. for (var i = 0; i < anchors.length; i++) {
  241. var anchor = anchors[i];
  242. var relAttribute = String(anchor.getAttribute("rel"));
  243. if (anchor.getAttribute("href") && relAttribute.toLowerCase().match("lightbox")) {
  244. $(anchor).click(function(e) { showLightbox(this); if (e.preventDefault) { e.preventDefault(); } return false; });
  245. }
  246. }
  247. // the rest of this code inserts html at the top of the page that looks like
  248. // this:
  249. // <div id="lightbox2-overlay">
  250. // <a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
  251. // </div>
  252. // <div id="lightbox">
  253. // <a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
  254. // <img id="closeButton" />
  255. // <img id="lightboxImage" />
  256. // </a>
  257. // <div id="lightboxDetails">
  258. // <div id="lightboxCaption"></div>
  259. // <div id="keyboardMsg"></div>
  260. // </div>
  261. // </div>
  262. var objBody = document.getElementsByTagName("body").item(0);
  263. // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
  264. var objOverlay = document.createElement("div");
  265. objOverlay.setAttribute('id', 'lightbox2-overlay');
  266. objOverlay.onclick = function () { hideLightbox(); return false; };
  267. objOverlay.style.display = 'none';
  268. objOverlay.style.position = 'absolute';
  269. objOverlay.style.top = '0';
  270. objOverlay.style.left = '0';
  271. objOverlay.style.zIndex = '90';
  272. objOverlay.style.width = '100%';
  273. objBody.insertBefore(objOverlay, objBody.firstChild);
  274. var arrayPageSize = getPageSize();
  275. var arrayPageScroll = getPageScroll();
  276. // create loader image
  277. var objLoadingImage = document.createElement("span");
  278. objLoadingImage.setAttribute('id', 'loadingImage');
  279. objOverlay.appendChild(objLoadingImage);
  280. // create lightbox div, same note about styles as above
  281. var objLightbox = document.createElement("div");
  282. objLightbox.setAttribute('id', 'lightbox');
  283. objLightbox.style.display = 'none';
  284. objLightbox.style.position = 'absolute';
  285. objLightbox.style.zIndex = '100';
  286. objBody.insertBefore(objLightbox, objOverlay.nextSibling);
  287. // create link
  288. var objLink = document.createElement("a");
  289. objLink.setAttribute('href', '#');
  290. objLink.setAttribute('title', 'Click to close');
  291. objLink.onclick = function () { hideLightbox(); return false; };
  292. objLightbox.appendChild(objLink);
  293. // create close button image
  294. var objCloseButton = document.createElement("span");
  295. objCloseButton.setAttribute('id', 'closeButton');
  296. objLink.appendChild(objCloseButton);
  297. // create image
  298. var objImage = document.createElement("img");
  299. objImage.setAttribute('id', 'lightboxImage');
  300. objLink.appendChild(objImage);
  301. // create details div, a container for the caption and keyboard message
  302. var objLightboxDetails = document.createElement("div");
  303. objLightboxDetails.setAttribute('id', 'lightboxDetails');
  304. objLightbox.appendChild(objLightboxDetails);
  305. // create caption
  306. var objCaption = document.createElement("div");
  307. objCaption.setAttribute('id', 'lightboxCaption');
  308. objCaption.style.display = 'none';
  309. objLightboxDetails.appendChild(objCaption);
  310. // create keyboard message
  311. var settings = Drupal.settings.lightbox2;
  312. var objKeyboardMsg = document.createElement("div");
  313. objKeyboardMsg.setAttribute('id', 'keyboardMsg');
  314. objKeyboardMsg.innerHTML = settings.lite_press_x_close;
  315. objLightboxDetails.appendChild(objKeyboardMsg);
  316. }
  317. Drupal.behaviors.initLightbox2 = {
  318. attach: function(context) {
  319. initLightbox();
  320. }
  321. };
  322. //End jQuery block
  323. }(jQuery));