jquery.colorbox.js 27 KB

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