jquery.colorbox.js 28 KB

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