PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/lightbox2/js/lightbox_lite.js

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