jquery.colorbox.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*!
  2. Colorbox 1.5.14
  3. license: MIT
  4. http://www.jacklmoore.com/colorbox
  5. */
  6. (function ($, document, window) {
  7. var
  8. // Default settings object.
  9. // See http://jacklmoore.com/colorbox for details.
  10. defaults = {
  11. // data sources
  12. html: false,
  13. photo: false,
  14. iframe: false,
  15. inline: false,
  16. // behavior and appearance
  17. transition: "elastic",
  18. speed: 300,
  19. fadeOut: 300,
  20. width: false,
  21. initialWidth: "600",
  22. innerWidth: false,
  23. maxWidth: false,
  24. height: false,
  25. initialHeight: "450",
  26. innerHeight: false,
  27. maxHeight: false,
  28. scalePhotos: true,
  29. scrolling: true,
  30. opacity: 0.9,
  31. preloading: true,
  32. className: false,
  33. overlayClose: true,
  34. escKey: true,
  35. arrowKey: true,
  36. top: false,
  37. bottom: false,
  38. left: false,
  39. right: false,
  40. fixed: false,
  41. data: undefined,
  42. closeButton: true,
  43. fastIframe: true,
  44. open: false,
  45. reposition: true,
  46. loop: true,
  47. slideshow: false,
  48. slideshowAuto: true,
  49. slideshowSpeed: 2500,
  50. slideshowStart: "start slideshow",
  51. slideshowStop: "stop slideshow",
  52. photoRegex: /\.(gif|png|jp(e|g|eg)|bmp|ico|webp|jxr|svg)((#|\?).*)?$/i,
  53. // alternate image paths for high-res displays
  54. retinaImage: false,
  55. retinaUrl: false,
  56. retinaSuffix: '@2x.$1',
  57. // internationalization
  58. current: "image {current} of {total}",
  59. previous: "previous",
  60. next: "next",
  61. close: "×",
  62. xhrError: "This content failed to load.",
  63. imgError: "This image failed to load.",
  64. // accessbility
  65. returnFocus: true,
  66. trapFocus: true,
  67. // callbacks
  68. onOpen: false,
  69. onLoad: false,
  70. onComplete: false,
  71. onCleanup: false,
  72. onClosed: false,
  73. rel: function() {
  74. return this.rel;
  75. },
  76. href: function() {
  77. // using this.href would give the absolute url, when the href may have been inteded as a selector (e.g. '#container')
  78. return $(this).attr('href');
  79. },
  80. title: function() {
  81. return this.title;
  82. }
  83. },
  84. // Abstracting the HTML and event identifiers for easy rebranding
  85. colorbox = 'colorbox',
  86. prefix = 'cbox',
  87. boxElement = prefix + 'Element',
  88. // Events
  89. event_open = prefix + '_open',
  90. event_load = prefix + '_load',
  91. event_complete = prefix + '_complete',
  92. event_cleanup = prefix + '_cleanup',
  93. event_closed = prefix + '_closed',
  94. event_purge = prefix + '_purge',
  95. // Cached jQuery Object Variables
  96. $overlay,
  97. $box,
  98. $wrap,
  99. $content,
  100. $topBorder,
  101. $leftBorder,
  102. $rightBorder,
  103. $bottomBorder,
  104. $related,
  105. $window,
  106. $loaded,
  107. $loadingBay,
  108. $loadingOverlay,
  109. $title,
  110. $current,
  111. $slideshow,
  112. $next,
  113. $prev,
  114. $close,
  115. $groupControls,
  116. $events = $('<a/>'), // $({}) would be prefered, but there is an issue with jQuery 1.4.2
  117. // Variables for cached values or use across multiple functions
  118. settings,
  119. interfaceHeight,
  120. interfaceWidth,
  121. loadedHeight,
  122. loadedWidth,
  123. index,
  124. photo,
  125. open,
  126. active,
  127. closing,
  128. loadingTimer,
  129. publicMethod,
  130. div = "div",
  131. requests = 0,
  132. previousCSS = {},
  133. init;
  134. // ****************
  135. // HELPER FUNCTIONS
  136. // ****************
  137. // Convenience function for creating new jQuery objects
  138. function $tag(tag, id, css) {
  139. var element = document.createElement(tag);
  140. if (id) {
  141. element.id = prefix + id;
  142. }
  143. if (css) {
  144. element.style.cssText = css;
  145. }
  146. return $(element);
  147. }
  148. // Get the window height using innerHeight when available to avoid an issue with iOS
  149. // http://bugs.jquery.com/ticket/6724
  150. function winheight() {
  151. return window.innerHeight ? window.innerHeight : $(window).height();
  152. }
  153. function Settings(element, options) {
  154. if (options !== Object(options)) {
  155. options = {};
  156. }
  157. this.cache = {};
  158. this.el = element;
  159. this.value = function(key) {
  160. var dataAttr;
  161. if (this.cache[key] === undefined) {
  162. dataAttr = $(this.el).attr('data-cbox-'+key);
  163. if (dataAttr !== undefined) {
  164. this.cache[key] = dataAttr;
  165. } else if (options[key] !== undefined) {
  166. this.cache[key] = options[key];
  167. } else if (defaults[key] !== undefined) {
  168. this.cache[key] = defaults[key];
  169. }
  170. }
  171. return this.cache[key];
  172. };
  173. this.get = function(key) {
  174. var value = this.value(key);
  175. return $.isFunction(value) ? value.call(this.el, this) : value;
  176. };
  177. }
  178. // Determine the next and previous members in a group.
  179. function getIndex(increment) {
  180. var
  181. max = $related.length,
  182. newIndex = (index + increment) % max;
  183. return (newIndex < 0) ? max + newIndex : newIndex;
  184. }
  185. // Convert '%' and 'px' values to integers
  186. function setSize(size, dimension) {
  187. return Math.round((/%/.test(size) ? ((dimension === 'x' ? $window.width() : winheight()) / 100) : 1) * parseInt(size, 10));
  188. }
  189. // Checks an href to see if it is a photo.
  190. // There is a force photo option (photo: true) for hrefs that cannot be matched by the regex.
  191. function isImage(settings, url) {
  192. return settings.get('photo') || settings.get('photoRegex').test(url);
  193. }
  194. function retinaUrl(settings, url) {
  195. return settings.get('retinaUrl') && window.devicePixelRatio > 1 ? url.replace(settings.get('photoRegex'), settings.get('retinaSuffix')) : url;
  196. }
  197. function trapFocus(e) {
  198. if ('contains' in $box[0] && !$box[0].contains(e.target) && e.target !== $overlay[0]) {
  199. e.stopPropagation();
  200. $box.focus();
  201. }
  202. }
  203. function setClass(str) {
  204. if (setClass.str !== str) {
  205. $box.add($overlay).removeClass(setClass.str).addClass(str);
  206. setClass.str = str;
  207. }
  208. }
  209. function getRelated(rel) {
  210. index = 0;
  211. if (rel && rel !== false && rel !== 'nofollow') {
  212. $related = $('.' + boxElement).filter(function () {
  213. var options = $.data(this, colorbox);
  214. var settings = new Settings(this, options);
  215. return (settings.get('rel') === rel);
  216. });
  217. index = $related.index(settings.el);
  218. // Check direct calls to Colorbox.
  219. if (index === -1) {
  220. $related = $related.add(settings.el);
  221. index = $related.length - 1;
  222. }
  223. } else {
  224. $related = $(settings.el);
  225. }
  226. }
  227. function trigger(event) {
  228. // for external use
  229. $(document).trigger(event);
  230. // for internal use
  231. $events.triggerHandler(event);
  232. }
  233. var slideshow = (function(){
  234. var active,
  235. className = prefix + "Slideshow_",
  236. click = "click." + prefix,
  237. timeOut;
  238. function clear () {
  239. clearTimeout(timeOut);
  240. }
  241. function set() {
  242. if (settings.get('loop') || $related[index + 1]) {
  243. clear();
  244. timeOut = setTimeout(publicMethod.next, settings.get('slideshowSpeed'));
  245. }
  246. }
  247. function start() {
  248. $slideshow
  249. .html(settings.get('slideshowStop'))
  250. .unbind(click)
  251. .one(click, stop);
  252. $events
  253. .bind(event_complete, set)
  254. .bind(event_load, clear);
  255. $box.removeClass(className + "off").addClass(className + "on");
  256. }
  257. function stop() {
  258. clear();
  259. $events
  260. .unbind(event_complete, set)
  261. .unbind(event_load, clear);
  262. $slideshow
  263. .html(settings.get('slideshowStart'))
  264. .unbind(click)
  265. .one(click, function () {
  266. publicMethod.next();
  267. start();
  268. });
  269. $box.removeClass(className + "on").addClass(className + "off");
  270. }
  271. function reset() {
  272. active = false;
  273. $slideshow.hide();
  274. clear();
  275. $events
  276. .unbind(event_complete, set)
  277. .unbind(event_load, clear);
  278. $box.removeClass(className + "off " + className + "on");
  279. }
  280. return function(){
  281. if (active) {
  282. if (!settings.get('slideshow')) {
  283. $events.unbind(event_cleanup, reset);
  284. reset();
  285. }
  286. } else {
  287. if (settings.get('slideshow') && $related[1]) {
  288. active = true;
  289. $events.one(event_cleanup, reset);
  290. if (settings.get('slideshowAuto')) {
  291. start();
  292. } else {
  293. stop();
  294. }
  295. $slideshow.show();
  296. }
  297. }
  298. };
  299. }());
  300. function launch(element) {
  301. var options;
  302. if (!closing) {
  303. options = $(element).data(colorbox);
  304. settings = new Settings(element, options);
  305. getRelated(settings.get('rel'));
  306. if (!open) {
  307. open = active = true; // Prevents the page-change action from queuing up if the visitor holds down the left or right keys.
  308. setClass(settings.get('className'));
  309. // Show colorbox so the sizes can be calculated in older versions of jQuery
  310. $box.css({visibility:'hidden', display:'block', opacity:''});
  311. $loaded = $tag(div, 'LoadedContent', 'width:0; height:0; overflow:hidden; visibility:hidden');
  312. $content.css({width:'', height:''}).append($loaded);
  313. // Cache values needed for size calculations
  314. interfaceHeight = $topBorder.height() + $bottomBorder.height() + $content.outerHeight(true) - $content.height();
  315. interfaceWidth = $leftBorder.width() + $rightBorder.width() + $content.outerWidth(true) - $content.width();
  316. loadedHeight = $loaded.outerHeight(true);
  317. loadedWidth = $loaded.outerWidth(true);
  318. // Opens inital empty Colorbox prior to content being loaded.
  319. var initialWidth = setSize(settings.get('initialWidth'), 'x');
  320. var initialHeight = setSize(settings.get('initialHeight'), 'y');
  321. var maxWidth = settings.get('maxWidth');
  322. var maxHeight = settings.get('maxHeight');
  323. settings.w = (maxWidth !== false ? Math.min(initialWidth, setSize(maxWidth, 'x')) : initialWidth) - loadedWidth - interfaceWidth;
  324. settings.h = (maxHeight !== false ? Math.min(initialHeight, setSize(maxHeight, 'y')) : initialHeight) - loadedHeight - interfaceHeight;
  325. $loaded.css({width:'', height:settings.h});
  326. publicMethod.position();
  327. trigger(event_open);
  328. settings.get('onOpen');
  329. $groupControls.add($title).hide();
  330. $box.focus();
  331. if (settings.get('trapFocus')) {
  332. // Confine focus to the modal
  333. // Uses event capturing that is not supported in IE8-
  334. if (document.addEventListener) {
  335. document.addEventListener('focus', trapFocus, true);
  336. $events.one(event_closed, function () {
  337. document.removeEventListener('focus', trapFocus, true);
  338. });
  339. }
  340. }
  341. // Return focus on closing
  342. if (settings.get('returnFocus')) {
  343. $events.one(event_closed, function () {
  344. $(settings.el).focus();
  345. });
  346. }
  347. }
  348. var opacity = parseFloat(settings.get('opacity'));
  349. $overlay.css({
  350. opacity: opacity === opacity ? opacity : '',
  351. cursor: settings.get('overlayClose') ? 'pointer' : '',
  352. visibility: 'visible'
  353. }).show();
  354. if (settings.get('closeButton')) {
  355. $close.html(settings.get('close')).appendTo($content);
  356. } else {
  357. $close.appendTo('<div/>'); // replace with .detach() when dropping jQuery < 1.4
  358. }
  359. load();
  360. }
  361. }
  362. // Colorbox's markup needs to be added to the DOM prior to being called
  363. // so that the browser will go ahead and load the CSS background images.
  364. function appendHTML() {
  365. if (!$box) {
  366. init = false;
  367. $window = $(window);
  368. $box = $tag(div).attr({
  369. id: colorbox,
  370. 'class': $.support.opacity === false ? prefix + 'IE' : '', // class for optional IE8 & lower targeted CSS.
  371. role: 'dialog',
  372. tabindex: '-1'
  373. }).hide();
  374. $overlay = $tag(div, "Overlay").hide();
  375. $loadingOverlay = $([$tag(div, "LoadingOverlay")[0],$tag(div, "LoadingGraphic")[0]]);
  376. $wrap = $tag(div, "Wrapper");
  377. $content = $tag(div, "Content").append(
  378. $title = $tag(div, "Title"),
  379. $current = $tag(div, "Current"),
  380. $prev = $('<button type="button"/>').attr({id:prefix+'Previous'}),
  381. $next = $('<button type="button"/>').attr({id:prefix+'Next'}),
  382. $slideshow = $tag('button', "Slideshow"),
  383. $loadingOverlay
  384. );
  385. $close = $('<button type="button"/>').attr({id:prefix+'Close'});
  386. $wrap.append( // The 3x3 Grid that makes up Colorbox
  387. $tag(div).append(
  388. $tag(div, "TopLeft"),
  389. $topBorder = $tag(div, "TopCenter"),
  390. $tag(div, "TopRight")
  391. ),
  392. $tag(div, false, 'clear:left').append(
  393. $leftBorder = $tag(div, "MiddleLeft"),
  394. $content,
  395. $rightBorder = $tag(div, "MiddleRight")
  396. ),
  397. $tag(div, false, 'clear:left').append(
  398. $tag(div, "BottomLeft"),
  399. $bottomBorder = $tag(div, "BottomCenter"),
  400. $tag(div, "BottomRight")
  401. )
  402. ).find('div div').css({'float': 'left'});
  403. $loadingBay = $tag(div, false, 'position:absolute; width:9999px; visibility:hidden; display:none; max-width:none;');
  404. $groupControls = $next.add($prev).add($current).add($slideshow);
  405. }
  406. if (document.body && !$box.parent().length) {
  407. $(document.body).append($overlay, $box.append($wrap, $loadingBay));
  408. }
  409. }
  410. // Add Colorbox's event bindings
  411. function addBindings() {
  412. function clickHandler(e) {
  413. // ignore non-left-mouse-clicks and clicks modified with ctrl / command, shift, or alt.
  414. // See: http://jacklmoore.com/notes/click-events/
  415. if (!(e.which > 1 || e.shiftKey || e.altKey || e.metaKey || e.ctrlKey)) {
  416. e.preventDefault();
  417. launch(this);
  418. }
  419. }
  420. if ($box) {
  421. if (!init) {
  422. init = true;
  423. // Anonymous functions here keep the public method from being cached, thereby allowing them to be redefined on the fly.
  424. $next.click(function () {
  425. publicMethod.next();
  426. });
  427. $prev.click(function () {
  428. publicMethod.prev();
  429. });
  430. $close.click(function () {
  431. publicMethod.close();
  432. });
  433. $overlay.click(function () {
  434. if (settings.get('overlayClose')) {
  435. publicMethod.close();
  436. }
  437. });
  438. // Key Bindings
  439. $(document).bind('keydown.' + prefix, function (e) {
  440. var key = e.keyCode;
  441. if (open && settings.get('escKey') && key === 27) {
  442. e.preventDefault();
  443. publicMethod.close();
  444. }
  445. if (open && settings.get('arrowKey') && $related[1] && !e.altKey) {
  446. if (key === 37) {
  447. e.preventDefault();
  448. $prev.click();
  449. } else if (key === 39) {
  450. e.preventDefault();
  451. $next.click();
  452. }
  453. }
  454. });
  455. if ($.isFunction($.fn.on)) {
  456. // For jQuery 1.7+
  457. $(document).on('click.'+prefix, '.'+boxElement, clickHandler);
  458. } else {
  459. // For jQuery 1.3.x -> 1.6.x
  460. // This code is never reached in jQuery 1.9, so do not contact me about 'live' being removed.
  461. // This is not here for jQuery 1.9, it's here for legacy users.
  462. $('.'+boxElement).live('click.'+prefix, clickHandler);
  463. }
  464. }
  465. return true;
  466. }
  467. return false;
  468. }
  469. // Don't do anything if Colorbox already exists.
  470. if ($[colorbox]) {
  471. return;
  472. }
  473. // Append the HTML when the DOM loads
  474. $(appendHTML);
  475. // ****************
  476. // PUBLIC FUNCTIONS
  477. // Usage format: $.colorbox.close();
  478. // Usage from within an iframe: parent.jQuery.colorbox.close();
  479. // ****************
  480. publicMethod = $.fn[colorbox] = $[colorbox] = function (options, callback) {
  481. var settings;
  482. var $obj = this;
  483. options = options || {};
  484. if ($.isFunction($obj)) { // assume a call to $.colorbox
  485. $obj = $('<a/>');
  486. options.open = true;
  487. } else if (!$obj[0]) { // colorbox being applied to empty collection
  488. return $obj;
  489. }
  490. if (!$obj[0]) { // colorbox being applied to empty collection
  491. return $obj;
  492. }
  493. appendHTML();
  494. if (addBindings()) {
  495. if (callback) {
  496. options.onComplete = callback;
  497. }
  498. $obj.each(function () {
  499. var old = $.data(this, colorbox) || {};
  500. $.data(this, colorbox, $.extend(old, options));
  501. }).addClass(boxElement);
  502. settings = new Settings($obj[0], options);
  503. if (settings.get('open')) {
  504. launch($obj[0]);
  505. }
  506. }
  507. return $obj;
  508. };
  509. publicMethod.position = function (speed, loadedCallback) {
  510. var
  511. css,
  512. top = 0,
  513. left = 0,
  514. offset = $box.offset(),
  515. scrollTop,
  516. scrollLeft;
  517. $window.unbind('resize.' + prefix);
  518. // remove the modal so that it doesn't influence the document width/height
  519. $box.css({top: -9e4, left: -9e4});
  520. scrollTop = $window.scrollTop();
  521. scrollLeft = $window.scrollLeft();
  522. if (settings.get('fixed')) {
  523. offset.top -= scrollTop;
  524. offset.left -= scrollLeft;
  525. $box.css({position: 'fixed'});
  526. } else {
  527. top = scrollTop;
  528. left = scrollLeft;
  529. $box.css({position: 'absolute'});
  530. }
  531. // keeps the top and left positions within the browser's viewport.
  532. if (settings.get('right') !== false) {
  533. left += Math.max($window.width() - settings.w - loadedWidth - interfaceWidth - setSize(settings.get('right'), 'x'), 0);
  534. } else if (settings.get('left') !== false) {
  535. left += setSize(settings.get('left'), 'x');
  536. } else {
  537. left += Math.round(Math.max($window.width() - settings.w - loadedWidth - interfaceWidth, 0) / 2);
  538. }
  539. if (settings.get('bottom') !== false) {
  540. top += Math.max(winheight() - settings.h - loadedHeight - interfaceHeight - setSize(settings.get('bottom'), 'y'), 0);
  541. } else if (settings.get('top') !== false) {
  542. top += setSize(settings.get('top'), 'y');
  543. } else {
  544. top += Math.round(Math.max(winheight() - settings.h - loadedHeight - interfaceHeight, 0) / 2);
  545. }
  546. $box.css({top: offset.top, left: offset.left, visibility:'visible'});
  547. // this gives the wrapper plenty of breathing room so it's floated contents can move around smoothly,
  548. // but it has to be shrank down around the size of div#colorbox when it's done. If not,
  549. // it can invoke an obscure IE bug when using iframes.
  550. $wrap[0].style.width = $wrap[0].style.height = "9999px";
  551. function modalDimensions() {
  552. $topBorder[0].style.width = $bottomBorder[0].style.width = $content[0].style.width = (parseInt($box[0].style.width,10) - interfaceWidth)+'px';
  553. $content[0].style.height = $leftBorder[0].style.height = $rightBorder[0].style.height = (parseInt($box[0].style.height,10) - interfaceHeight)+'px';
  554. }
  555. css = {width: settings.w + loadedWidth + interfaceWidth, height: settings.h + loadedHeight + interfaceHeight, top: top, left: left};
  556. // setting the speed to 0 if the content hasn't changed size or position
  557. if (speed) {
  558. var tempSpeed = 0;
  559. $.each(css, function(i){
  560. if (css[i] !== previousCSS[i]) {
  561. tempSpeed = speed;
  562. return;
  563. }
  564. });
  565. speed = tempSpeed;
  566. }
  567. previousCSS = css;
  568. if (!speed) {
  569. $box.css(css);
  570. }
  571. $box.dequeue().animate(css, {
  572. duration: speed || 0,
  573. complete: function () {
  574. modalDimensions();
  575. active = false;
  576. // shrink the wrapper down to exactly the size of colorbox to avoid a bug in IE's iframe implementation.
  577. $wrap[0].style.width = (settings.w + loadedWidth + interfaceWidth) + "px";
  578. $wrap[0].style.height = (settings.h + loadedHeight + interfaceHeight) + "px";
  579. if (settings.get('reposition')) {
  580. setTimeout(function () { // small delay before binding onresize due to an IE8 bug.
  581. $window.bind('resize.' + prefix, publicMethod.position);
  582. }, 1);
  583. }
  584. if ($.isFunction(loadedCallback)) {
  585. loadedCallback();
  586. }
  587. },
  588. step: modalDimensions
  589. });
  590. };
  591. publicMethod.resize = function (options) {
  592. var scrolltop;
  593. if (open) {
  594. options = options || {};
  595. if (options.width) {
  596. settings.w = setSize(options.width, 'x') - loadedWidth - interfaceWidth;
  597. }
  598. if (options.innerWidth) {
  599. settings.w = setSize(options.innerWidth, 'x');
  600. }
  601. $loaded.css({width: settings.w});
  602. if (options.height) {
  603. settings.h = setSize(options.height, 'y') - loadedHeight - interfaceHeight;
  604. }
  605. if (options.innerHeight) {
  606. settings.h = setSize(options.innerHeight, 'y');
  607. }
  608. if (!options.innerHeight && !options.height) {
  609. scrolltop = $loaded.scrollTop();
  610. $loaded.css({height: "auto"});
  611. settings.h = $loaded.height();
  612. }
  613. $loaded.css({height: settings.h});
  614. if(scrolltop) {
  615. $loaded.scrollTop(scrolltop);
  616. }
  617. publicMethod.position(settings.get('transition') === "none" ? 0 : settings.get('speed'));
  618. }
  619. };
  620. publicMethod.prep = function (object) {
  621. if (!open) {
  622. return;
  623. }
  624. var callback, speed = settings.get('transition') === "none" ? 0 : settings.get('speed');
  625. $loaded.remove();
  626. $loaded = $tag(div, 'LoadedContent').append(object);
  627. function getWidth() {
  628. settings.w = settings.w || $loaded.width();
  629. settings.w = settings.mw && settings.mw < settings.w ? settings.mw : settings.w;
  630. return settings.w;
  631. }
  632. function getHeight() {
  633. settings.h = settings.h || $loaded.height();
  634. settings.h = settings.mh && settings.mh < settings.h ? settings.mh : settings.h;
  635. return settings.h;
  636. }
  637. $loaded.hide()
  638. .appendTo($loadingBay.show())// content has to be appended to the DOM for accurate size calculations.
  639. .css({width: getWidth(), overflow: settings.get('scrolling') ? 'auto' : 'hidden'})
  640. .css({height: getHeight()})// sets the height independently from the width in case the new width influences the value of height.
  641. .prependTo($content);
  642. $loadingBay.hide();
  643. // floating the IMG removes the bottom line-height and fixed a problem where IE miscalculates the width of the parent element as 100% of the document width.
  644. $(photo).css({'float': 'none'});
  645. setClass(settings.get('className'));
  646. callback = function () {
  647. var total = $related.length,
  648. iframe,
  649. complete;
  650. if (!open) {
  651. return;
  652. }
  653. function removeFilter() { // Needed for IE8 in versions of jQuery prior to 1.7.2
  654. if ($.support.opacity === false) {
  655. $box[0].style.removeAttribute('filter');
  656. }
  657. }
  658. complete = function () {
  659. clearTimeout(loadingTimer);
  660. $loadingOverlay.hide();
  661. trigger(event_complete);
  662. settings.get('onComplete');
  663. };
  664. $title.html(settings.get('title')).show();
  665. $loaded.show();
  666. if (total > 1) { // handle grouping
  667. if (typeof settings.get('current') === "string") {
  668. $current.html(settings.get('current').replace('{current}', index + 1).replace('{total}', total)).show();
  669. }
  670. $next[(settings.get('loop') || index < total - 1) ? "show" : "hide"]().html(settings.get('next'));
  671. $prev[(settings.get('loop') || index) ? "show" : "hide"]().html(settings.get('previous'));
  672. slideshow();
  673. // Preloads images within a rel group
  674. if (settings.get('preloading')) {
  675. $.each([getIndex(-1), getIndex(1)], function(){
  676. var img,
  677. i = $related[this],
  678. settings = new Settings(i, $.data(i, colorbox)),
  679. src = settings.get('href');
  680. if (src && isImage(settings, src)) {
  681. src = retinaUrl(settings, src);
  682. img = document.createElement('img');
  683. img.src = src;
  684. }
  685. });
  686. }
  687. } else {
  688. $groupControls.hide();
  689. }
  690. if (settings.get('iframe')) {
  691. iframe = document.createElement('iframe');
  692. if ('frameBorder' in iframe) {
  693. iframe.frameBorder = 0;
  694. }
  695. if ('allowTransparency' in iframe) {
  696. iframe.allowTransparency = "true";
  697. }
  698. if (!settings.get('scrolling')) {
  699. iframe.scrolling = "no";
  700. }
  701. $(iframe)
  702. .attr({
  703. src: settings.get('href'),
  704. name: (new Date()).getTime(), // give the iframe a unique name to prevent caching
  705. 'class': prefix + 'Iframe',
  706. allowFullScreen : true // allow HTML5 video to go fullscreen
  707. })
  708. .one('load', complete)
  709. .appendTo($loaded);
  710. $events.one(event_purge, function () {
  711. iframe.src = "//about:blank";
  712. });
  713. if (settings.get('fastIframe')) {
  714. $(iframe).trigger('load');
  715. }
  716. } else {
  717. complete();
  718. }
  719. if (settings.get('transition') === 'fade') {
  720. $box.fadeTo(speed, 1, removeFilter);
  721. } else {
  722. removeFilter();
  723. }
  724. };
  725. if (settings.get('transition') === 'fade') {
  726. $box.fadeTo(speed, 0, function () {
  727. publicMethod.position(0, callback);
  728. });
  729. } else {
  730. publicMethod.position(speed, callback);
  731. }
  732. };
  733. function load () {
  734. var href, setResize, prep = publicMethod.prep, $inline, request = ++requests;
  735. active = true;
  736. photo = false;
  737. trigger(event_purge);
  738. trigger(event_load);
  739. settings.get('onLoad');
  740. settings.h = settings.get('height') ?
  741. setSize(settings.get('height'), 'y') - loadedHeight - interfaceHeight :
  742. settings.get('innerHeight') && setSize(settings.get('innerHeight'), 'y');
  743. settings.w = settings.get('width') ?
  744. setSize(settings.get('width'), 'x') - loadedWidth - interfaceWidth :
  745. settings.get('innerWidth') && setSize(settings.get('innerWidth'), 'x');
  746. // Sets the minimum dimensions for use in image scaling
  747. settings.mw = settings.w;
  748. settings.mh = settings.h;
  749. // Re-evaluate the minimum width and height based on maxWidth and maxHeight values.
  750. // If the width or height exceed the maxWidth or maxHeight, use the maximum values instead.
  751. if (settings.get('maxWidth')) {
  752. settings.mw = setSize(settings.get('maxWidth'), 'x') - loadedWidth - interfaceWidth;
  753. settings.mw = settings.w && settings.w < settings.mw ? settings.w : settings.mw;
  754. }
  755. if (settings.get('maxHeight')) {
  756. settings.mh = setSize(settings.get('maxHeight'), 'y') - loadedHeight - interfaceHeight;
  757. settings.mh = settings.h && settings.h < settings.mh ? settings.h : settings.mh;
  758. }
  759. href = settings.get('href');
  760. loadingTimer = setTimeout(function () {
  761. $loadingOverlay.show();
  762. }, 100);
  763. if (settings.get('inline')) {
  764. var $target = $(href);
  765. // Inserts an empty placeholder where inline content is being pulled from.
  766. // An event is bound to put inline content back when Colorbox closes or loads new content.
  767. $inline = $('<div>').hide().insertBefore($target);
  768. $events.one(event_purge, function () {
  769. $inline.replaceWith($target);
  770. });
  771. prep($target);
  772. } else if (settings.get('iframe')) {
  773. // IFrame element won't be added to the DOM until it is ready to be displayed,
  774. // to avoid problems with DOM-ready JS that might be trying to run in that iframe.
  775. prep(" ");
  776. } else if (settings.get('html')) {
  777. prep(settings.get('html'));
  778. } else if (isImage(settings, href)) {
  779. href = retinaUrl(settings, href);
  780. photo = new Image();
  781. $(photo)
  782. .addClass(prefix + 'Photo')
  783. .bind('error',function () {
  784. prep($tag(div, 'Error').html(settings.get('imgError')));
  785. })
  786. .one('load', function () {
  787. if (request !== requests) {
  788. return;
  789. }
  790. // A small pause because some browsers will occassionaly report a
  791. // img.width and img.height of zero immediately after the img.onload fires
  792. setTimeout(function(){
  793. var percent;
  794. $.each(['alt', 'longdesc', 'aria-describedby'], function(i,val){
  795. var attr = $(settings.el).attr(val) || $(settings.el).attr('data-'+val);
  796. if (attr) {
  797. photo.setAttribute(val, attr);
  798. }
  799. });
  800. if (settings.get('retinaImage') && window.devicePixelRatio > 1) {
  801. photo.height = photo.height / window.devicePixelRatio;
  802. photo.width = photo.width / window.devicePixelRatio;
  803. }
  804. if (settings.get('scalePhotos')) {
  805. setResize = function () {
  806. photo.height -= photo.height * percent;
  807. photo.width -= photo.width * percent;
  808. };
  809. if (settings.mw && photo.width > settings.mw) {
  810. percent = (photo.width - settings.mw) / photo.width;
  811. setResize();
  812. }
  813. if (settings.mh && photo.height > settings.mh) {
  814. percent = (photo.height - settings.mh) / photo.height;
  815. setResize();
  816. }
  817. }
  818. if (settings.h) {
  819. photo.style.marginTop = Math.max(settings.mh - photo.height, 0) / 2 + 'px';
  820. }
  821. if ($related[1] && (settings.get('loop') || $related[index + 1])) {
  822. photo.style.cursor = 'pointer';
  823. photo.onclick = function () {
  824. publicMethod.next();
  825. };
  826. }
  827. photo.style.width = photo.width + 'px';
  828. photo.style.height = photo.height + 'px';
  829. prep(photo);
  830. }, 1);
  831. });
  832. photo.src = href;
  833. } else if (href) {
  834. $loadingBay.load(href, settings.get('data'), function (data, status) {
  835. if (request === requests) {
  836. prep(status === 'error' ? $tag(div, 'Error').html(settings.get('xhrError')) : $(this).contents());
  837. }
  838. });
  839. }
  840. }
  841. // Navigates to the next page/image in a set.
  842. publicMethod.next = function () {
  843. if (!active && $related[1] && (settings.get('loop') || $related[index + 1])) {
  844. index = getIndex(1);
  845. launch($related[index]);
  846. }
  847. };
  848. publicMethod.prev = function () {
  849. if (!active && $related[1] && (settings.get('loop') || index)) {
  850. index = getIndex(-1);
  851. launch($related[index]);
  852. }
  853. };
  854. // Note: to use this within an iframe use the following format: parent.jQuery.colorbox.close();
  855. publicMethod.close = function () {
  856. if (open && !closing) {
  857. closing = true;
  858. open = false;
  859. trigger(event_cleanup);
  860. settings.get('onCleanup');
  861. $window.unbind('.' + prefix);
  862. $overlay.fadeTo(settings.get('fadeOut') || 0, 0);
  863. $box.stop().fadeTo(settings.get('fadeOut') || 0, 0, function () {
  864. $box.hide();
  865. $overlay.hide();
  866. trigger(event_purge);
  867. $loaded.remove();
  868. setTimeout(function () {
  869. closing = false;
  870. trigger(event_closed);
  871. settings.get('onClosed');
  872. }, 1);
  873. });
  874. }
  875. };
  876. // Removes changes Colorbox made to the document, but does not remove the plugin.
  877. publicMethod.remove = function () {
  878. if (!$box) { return; }
  879. $box.stop();
  880. $[colorbox].close();
  881. $box.stop(false, true).remove();
  882. $overlay.remove();
  883. closing = false;
  884. $box = null;
  885. $('.' + boxElement)
  886. .removeData(colorbox)
  887. .removeClass(boxElement);
  888. $(document).unbind('click.'+prefix).unbind('keydown.'+prefix);
  889. };
  890. // A method for fetching the current element Colorbox is referencing.
  891. // returns a jQuery object.
  892. publicMethod.element = function () {
  893. return $(settings.el);
  894. };
  895. publicMethod.settings = defaults;
  896. }(jQuery, document, window));