jquery.magnific-popup.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  1. /*! Magnific Popup - v0.9.9 - 2013-12-27
  2. * http://dimsemenov.com/plugins/magnific-popup/
  3. * Copyright (c) 2013 Dmitry Semenov; */
  4. ;(function($) {
  5. /*>>core*/
  6. /**
  7. *
  8. * Magnific Popup Core JS file
  9. *
  10. */
  11. /**
  12. * Private static constants
  13. */
  14. var CLOSE_EVENT = 'Close',
  15. BEFORE_CLOSE_EVENT = 'BeforeClose',
  16. AFTER_CLOSE_EVENT = 'AfterClose',
  17. BEFORE_APPEND_EVENT = 'BeforeAppend',
  18. MARKUP_PARSE_EVENT = 'MarkupParse',
  19. OPEN_EVENT = 'Open',
  20. CHANGE_EVENT = 'Change',
  21. NS = 'mfp',
  22. EVENT_NS = '.' + NS,
  23. READY_CLASS = 'mfp-ready',
  24. REMOVING_CLASS = 'mfp-removing',
  25. PREVENT_CLOSE_CLASS = 'mfp-prevent-close';
  26. /**
  27. * Private vars
  28. */
  29. var mfp, // As we have only one instance of MagnificPopup object, we define it locally to not to use 'this'
  30. MagnificPopup = function(){},
  31. _isJQ = !!(window.jQuery),
  32. _prevStatus,
  33. _window = $(window),
  34. _body,
  35. _document,
  36. _prevContentType,
  37. _wrapClasses,
  38. _currPopupType;
  39. /**
  40. * Private functions
  41. */
  42. var _mfpOn = function(name, f) {
  43. mfp.ev.on(NS + name + EVENT_NS, f);
  44. },
  45. _getEl = function(className, appendTo, html, raw) {
  46. var el = document.createElement('div');
  47. el.className = 'mfp-'+className;
  48. if(html) {
  49. el.innerHTML = html;
  50. }
  51. if(!raw) {
  52. el = $(el);
  53. if(appendTo) {
  54. el.appendTo(appendTo);
  55. }
  56. } else if(appendTo) {
  57. appendTo.appendChild(el);
  58. }
  59. return el;
  60. },
  61. _mfpTrigger = function(e, data) {
  62. mfp.ev.triggerHandler(NS + e, data);
  63. if(mfp.st.callbacks) {
  64. // converts "mfpEventName" to "eventName" callback and triggers it if it's present
  65. e = e.charAt(0).toLowerCase() + e.slice(1);
  66. if(mfp.st.callbacks[e]) {
  67. mfp.st.callbacks[e].apply(mfp, $.isArray(data) ? data : [data]);
  68. }
  69. }
  70. },
  71. _getCloseBtn = function(type) {
  72. if(type !== _currPopupType || !mfp.currTemplate.closeBtn) {
  73. mfp.currTemplate.closeBtn = $( mfp.st.closeMarkup.replace('%title%', mfp.st.tClose ) );
  74. _currPopupType = type;
  75. }
  76. return mfp.currTemplate.closeBtn;
  77. },
  78. // Initialize Magnific Popup only when called at least once
  79. _checkInstance = function() {
  80. if(!$.magnificPopup.instance) {
  81. mfp = new MagnificPopup();
  82. mfp.init();
  83. $.magnificPopup.instance = mfp;
  84. }
  85. },
  86. // CSS transition detection, http://stackoverflow.com/questions/7264899/detect-css-transitions-using-javascript-and-without-modernizr
  87. supportsTransitions = function() {
  88. var s = document.createElement('p').style, // 's' for style. better to create an element if body yet to exist
  89. v = ['ms','O','Moz','Webkit']; // 'v' for vendor
  90. if( s['transition'] !== undefined ) {
  91. return true;
  92. }
  93. while( v.length ) {
  94. if( v.pop() + 'Transition' in s ) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. };
  100. /**
  101. * Public functions
  102. */
  103. MagnificPopup.prototype = {
  104. constructor: MagnificPopup,
  105. /**
  106. * Initializes Magnific Popup plugin.
  107. * This function is triggered only once when $.fn.magnificPopup or $.magnificPopup is executed
  108. */
  109. init: function() {
  110. var appVersion = navigator.appVersion;
  111. mfp.isIE7 = appVersion.indexOf("MSIE 7.") !== -1;
  112. mfp.isIE8 = appVersion.indexOf("MSIE 8.") !== -1;
  113. mfp.isLowIE = mfp.isIE7 || mfp.isIE8;
  114. mfp.isAndroid = (/android/gi).test(appVersion);
  115. mfp.isIOS = (/iphone|ipad|ipod/gi).test(appVersion);
  116. mfp.supportsTransition = supportsTransitions();
  117. // We disable fixed positioned lightbox on devices that don't handle it nicely.
  118. // If you know a better way of detecting this - let me know.
  119. mfp.probablyMobile = (mfp.isAndroid || mfp.isIOS || /(Opera Mini)|Kindle|webOS|BlackBerry|(Opera Mobi)|(Windows Phone)|IEMobile/i.test(navigator.userAgent) );
  120. _document = $(document);
  121. mfp.popupsCache = {};
  122. },
  123. /**
  124. * Opens popup
  125. * @param data [description]
  126. */
  127. open: function(data) {
  128. if(!_body) {
  129. _body = $(document.body);
  130. }
  131. var i;
  132. if(data.isObj === false) {
  133. // convert jQuery collection to array to avoid conflicts later
  134. mfp.items = data.items.toArray();
  135. mfp.index = 0;
  136. var items = data.items,
  137. item;
  138. for(i = 0; i < items.length; i++) {
  139. item = items[i];
  140. if(item.parsed) {
  141. item = item.el[0];
  142. }
  143. if(item === data.el[0]) {
  144. mfp.index = i;
  145. break;
  146. }
  147. }
  148. } else {
  149. mfp.items = $.isArray(data.items) ? data.items : [data.items];
  150. mfp.index = data.index || 0;
  151. }
  152. // if popup is already opened - we just update the content
  153. if(mfp.isOpen) {
  154. mfp.updateItemHTML();
  155. return;
  156. }
  157. mfp.types = [];
  158. _wrapClasses = '';
  159. if(data.mainEl && data.mainEl.length) {
  160. mfp.ev = data.mainEl.eq(0);
  161. } else {
  162. mfp.ev = _document;
  163. }
  164. if(data.key) {
  165. if(!mfp.popupsCache[data.key]) {
  166. mfp.popupsCache[data.key] = {};
  167. }
  168. mfp.currTemplate = mfp.popupsCache[data.key];
  169. } else {
  170. mfp.currTemplate = {};
  171. }
  172. mfp.st = $.extend(true, {}, $.magnificPopup.defaults, data );
  173. mfp.fixedContentPos = mfp.st.fixedContentPos === 'auto' ? !mfp.probablyMobile : mfp.st.fixedContentPos;
  174. if(mfp.st.modal) {
  175. mfp.st.closeOnContentClick = false;
  176. mfp.st.closeOnBgClick = false;
  177. mfp.st.showCloseBtn = false;
  178. mfp.st.enableEscapeKey = false;
  179. }
  180. // Building markup
  181. // main containers are created only once
  182. if(!mfp.bgOverlay) {
  183. // Dark overlay
  184. mfp.bgOverlay = _getEl('bg').on('click'+EVENT_NS, function() {
  185. mfp.close();
  186. });
  187. mfp.wrap = _getEl('wrap').attr('tabindex', -1).on('click'+EVENT_NS, function(e) {
  188. if(mfp._checkIfClose(e.target)) {
  189. mfp.close();
  190. }
  191. });
  192. mfp.container = _getEl('container', mfp.wrap);
  193. }
  194. mfp.contentContainer = _getEl('content');
  195. if(mfp.st.preloader) {
  196. mfp.preloader = _getEl('preloader', mfp.container, mfp.st.tLoading);
  197. }
  198. // Initializing modules
  199. var modules = $.magnificPopup.modules;
  200. for(i = 0; i < modules.length; i++) {
  201. var n = modules[i];
  202. n = n.charAt(0).toUpperCase() + n.slice(1);
  203. mfp['init'+n].call(mfp);
  204. }
  205. _mfpTrigger('BeforeOpen');
  206. if(mfp.st.showCloseBtn) {
  207. // Close button
  208. if(!mfp.st.closeBtnInside) {
  209. mfp.wrap.append( _getCloseBtn() );
  210. } else {
  211. _mfpOn(MARKUP_PARSE_EVENT, function(e, template, values, item) {
  212. values.close_replaceWith = _getCloseBtn(item.type);
  213. });
  214. _wrapClasses += ' mfp-close-btn-in';
  215. }
  216. }
  217. if(mfp.st.alignTop) {
  218. _wrapClasses += ' mfp-align-top';
  219. }
  220. if(mfp.fixedContentPos) {
  221. mfp.wrap.css({
  222. overflow: mfp.st.overflowY,
  223. overflowX: 'hidden',
  224. overflowY: mfp.st.overflowY
  225. });
  226. } else {
  227. mfp.wrap.css({
  228. top: _window.scrollTop(),
  229. position: 'absolute'
  230. });
  231. }
  232. if( mfp.st.fixedBgPos === false || (mfp.st.fixedBgPos === 'auto' && !mfp.fixedContentPos) ) {
  233. mfp.bgOverlay.css({
  234. height: _document.height(),
  235. position: 'absolute'
  236. });
  237. }
  238. if(mfp.st.enableEscapeKey) {
  239. // Close on ESC key
  240. _document.on('keyup' + EVENT_NS, function(e) {
  241. if(e.keyCode === 27) {
  242. mfp.close();
  243. }
  244. });
  245. }
  246. _window.on('resize' + EVENT_NS, function() {
  247. mfp.updateSize();
  248. });
  249. if(!mfp.st.closeOnContentClick) {
  250. _wrapClasses += ' mfp-auto-cursor';
  251. }
  252. if(_wrapClasses)
  253. mfp.wrap.addClass(_wrapClasses);
  254. // this triggers recalculation of layout, so we get it once to not to trigger twice
  255. var windowHeight = mfp.wH = _window.height();
  256. var windowStyles = {};
  257. if( mfp.fixedContentPos ) {
  258. if(mfp._hasScrollBar(windowHeight)){
  259. var s = mfp._getScrollbarSize();
  260. if(s) {
  261. windowStyles.marginRight = s;
  262. }
  263. }
  264. }
  265. if(mfp.fixedContentPos) {
  266. if(!mfp.isIE7) {
  267. windowStyles.overflow = 'hidden';
  268. } else {
  269. // ie7 double-scroll bug
  270. $('body, html').css('overflow', 'hidden');
  271. }
  272. }
  273. var classesToadd = mfp.st.mainClass;
  274. if(mfp.isIE7) {
  275. classesToadd += ' mfp-ie7';
  276. }
  277. if(classesToadd) {
  278. mfp._addClassToMFP( classesToadd );
  279. }
  280. // add content
  281. mfp.updateItemHTML();
  282. _mfpTrigger('BuildControls');
  283. // remove scrollbar, add margin e.t.c
  284. $('html').css(windowStyles);
  285. // add everything to DOM
  286. mfp.bgOverlay.add(mfp.wrap).prependTo( mfp.st.prependTo || _body );
  287. // Save last focused element
  288. mfp._lastFocusedEl = document.activeElement;
  289. // Wait for next cycle to allow CSS transition
  290. setTimeout(function() {
  291. if(mfp.content) {
  292. mfp._addClassToMFP(READY_CLASS);
  293. mfp._setFocus();
  294. } else {
  295. // if content is not defined (not loaded e.t.c) we add class only for BG
  296. mfp.bgOverlay.addClass(READY_CLASS);
  297. }
  298. // Trap the focus in popup
  299. _document.on('focusin' + EVENT_NS, mfp._onFocusIn);
  300. }, 16);
  301. mfp.isOpen = true;
  302. mfp.updateSize(windowHeight);
  303. _mfpTrigger(OPEN_EVENT);
  304. return data;
  305. },
  306. /**
  307. * Closes the popup
  308. */
  309. close: function() {
  310. if(!mfp.isOpen) return;
  311. _mfpTrigger(BEFORE_CLOSE_EVENT);
  312. mfp.isOpen = false;
  313. // for CSS3 animation
  314. if(mfp.st.removalDelay && !mfp.isLowIE && mfp.supportsTransition ) {
  315. mfp._addClassToMFP(REMOVING_CLASS);
  316. setTimeout(function() {
  317. mfp._close();
  318. }, mfp.st.removalDelay);
  319. } else {
  320. mfp._close();
  321. }
  322. },
  323. /**
  324. * Helper for close() function
  325. */
  326. _close: function() {
  327. _mfpTrigger(CLOSE_EVENT);
  328. var classesToRemove = REMOVING_CLASS + ' ' + READY_CLASS + ' ';
  329. mfp.bgOverlay.detach();
  330. mfp.wrap.detach();
  331. mfp.container.empty();
  332. if(mfp.st.mainClass) {
  333. classesToRemove += mfp.st.mainClass + ' ';
  334. }
  335. mfp._removeClassFromMFP(classesToRemove);
  336. if(mfp.fixedContentPos) {
  337. var windowStyles = {marginRight: ''};
  338. if(mfp.isIE7) {
  339. $('body, html').css('overflow', '');
  340. } else {
  341. windowStyles.overflow = '';
  342. }
  343. $('html').css(windowStyles);
  344. }
  345. _document.off('keyup' + EVENT_NS + ' focusin' + EVENT_NS);
  346. mfp.ev.off(EVENT_NS);
  347. // clean up DOM elements that aren't removed
  348. mfp.wrap.attr('class', 'mfp-wrap').removeAttr('style');
  349. mfp.bgOverlay.attr('class', 'mfp-bg');
  350. mfp.container.attr('class', 'mfp-container');
  351. // remove close button from target element
  352. if(mfp.st.showCloseBtn &&
  353. (!mfp.st.closeBtnInside || mfp.currTemplate[mfp.currItem.type] === true)) {
  354. if(mfp.currTemplate.closeBtn)
  355. mfp.currTemplate.closeBtn.detach();
  356. }
  357. if(mfp._lastFocusedEl) {
  358. $(mfp._lastFocusedEl).focus(); // put tab focus back
  359. }
  360. mfp.currItem = null;
  361. mfp.content = null;
  362. mfp.currTemplate = null;
  363. mfp.prevHeight = 0;
  364. _mfpTrigger(AFTER_CLOSE_EVENT);
  365. },
  366. updateSize: function(winHeight) {
  367. if(mfp.isIOS) {
  368. // fixes iOS nav bars https://github.com/dimsemenov/Magnific-Popup/issues/2
  369. var zoomLevel = document.documentElement.clientWidth / window.innerWidth;
  370. var height = window.innerHeight * zoomLevel;
  371. mfp.wrap.css('height', height);
  372. mfp.wH = height;
  373. } else {
  374. mfp.wH = winHeight || _window.height();
  375. }
  376. // Fixes #84: popup incorrectly positioned with position:relative on body
  377. if(!mfp.fixedContentPos) {
  378. mfp.wrap.css('height', mfp.wH);
  379. }
  380. _mfpTrigger('Resize');
  381. },
  382. /**
  383. * Set content of popup based on current index
  384. */
  385. updateItemHTML: function() {
  386. var item = mfp.items[mfp.index];
  387. // Detach and perform modifications
  388. mfp.contentContainer.detach();
  389. if(mfp.content)
  390. mfp.content.detach();
  391. if(!item.parsed) {
  392. item = mfp.parseEl( mfp.index );
  393. }
  394. var type = item.type;
  395. _mfpTrigger('BeforeChange', [mfp.currItem ? mfp.currItem.type : '', type]);
  396. // BeforeChange event works like so:
  397. // _mfpOn('BeforeChange', function(e, prevType, newType) { });
  398. mfp.currItem = item;
  399. if(!mfp.currTemplate[type]) {
  400. var markup = mfp.st[type] ? mfp.st[type].markup : false;
  401. // allows to modify markup
  402. _mfpTrigger('FirstMarkupParse', markup);
  403. if(markup) {
  404. mfp.currTemplate[type] = $(markup);
  405. } else {
  406. // if there is no markup found we just define that template is parsed
  407. mfp.currTemplate[type] = true;
  408. }
  409. }
  410. if(_prevContentType && _prevContentType !== item.type) {
  411. mfp.container.removeClass('mfp-'+_prevContentType+'-holder');
  412. }
  413. var newContent = mfp['get' + type.charAt(0).toUpperCase() + type.slice(1)](item, mfp.currTemplate[type]);
  414. mfp.appendContent(newContent, type);
  415. item.preloaded = true;
  416. _mfpTrigger(CHANGE_EVENT, item);
  417. _prevContentType = item.type;
  418. // Append container back after its content changed
  419. mfp.container.prepend(mfp.contentContainer);
  420. _mfpTrigger('AfterChange');
  421. },
  422. /**
  423. * Set HTML content of popup
  424. */
  425. appendContent: function(newContent, type) {
  426. mfp.content = newContent;
  427. if(newContent) {
  428. if(mfp.st.showCloseBtn && mfp.st.closeBtnInside &&
  429. mfp.currTemplate[type] === true) {
  430. // if there is no markup, we just append close button element inside
  431. if(!mfp.content.find('.mfp-close').length) {
  432. mfp.content.append(_getCloseBtn());
  433. }
  434. } else {
  435. mfp.content = newContent;
  436. }
  437. } else {
  438. mfp.content = '';
  439. }
  440. _mfpTrigger(BEFORE_APPEND_EVENT);
  441. mfp.container.addClass('mfp-'+type+'-holder');
  442. mfp.contentContainer.append(mfp.content);
  443. },
  444. /**
  445. * Creates Magnific Popup data object based on given data
  446. * @param {int} index Index of item to parse
  447. */
  448. parseEl: function(index) {
  449. var item = mfp.items[index],
  450. type;
  451. if(item.tagName) {
  452. item = { el: $(item) };
  453. } else {
  454. type = item.type;
  455. item = { data: item, src: item.src };
  456. }
  457. if(item.el) {
  458. var types = mfp.types;
  459. // check for 'mfp-TYPE' class
  460. for(var i = 0; i < types.length; i++) {
  461. if( item.el.hasClass('mfp-'+types[i]) ) {
  462. type = types[i];
  463. break;
  464. }
  465. }
  466. item.src = item.el.attr('data-mfp-src');
  467. if(!item.src) {
  468. item.src = item.el.attr('href');
  469. }
  470. }
  471. item.type = type || mfp.st.type || 'inline';
  472. item.index = index;
  473. item.parsed = true;
  474. mfp.items[index] = item;
  475. _mfpTrigger('ElementParse', item);
  476. return mfp.items[index];
  477. },
  478. /**
  479. * Initializes single popup or a group of popups
  480. */
  481. addGroup: function(el, options) {
  482. var eHandler = function(e) {
  483. e.mfpEl = this;
  484. mfp._openClick(e, el, options);
  485. };
  486. if(!options) {
  487. options = {};
  488. }
  489. var eName = 'click.magnificPopup';
  490. options.mainEl = el;
  491. if(options.items) {
  492. options.isObj = true;
  493. el.off(eName).on(eName, eHandler);
  494. } else {
  495. options.isObj = false;
  496. if(options.delegate) {
  497. el.off(eName).on(eName, options.delegate , eHandler);
  498. } else {
  499. options.items = el;
  500. el.off(eName).on(eName, eHandler);
  501. }
  502. }
  503. },
  504. _openClick: function(e, el, options) {
  505. var midClick = options.midClick !== undefined ? options.midClick : $.magnificPopup.defaults.midClick;
  506. if(!midClick && ( e.which === 2 || e.ctrlKey || e.metaKey ) ) {
  507. return;
  508. }
  509. var disableOn = options.disableOn !== undefined ? options.disableOn : $.magnificPopup.defaults.disableOn;
  510. if(disableOn) {
  511. if($.isFunction(disableOn)) {
  512. if( !disableOn.call(mfp) ) {
  513. return true;
  514. }
  515. } else { // else it's number
  516. if( _window.width() < disableOn ) {
  517. return true;
  518. }
  519. }
  520. }
  521. if(e.type) {
  522. e.preventDefault();
  523. // This will prevent popup from closing if element is inside and popup is already opened
  524. if(mfp.isOpen) {
  525. e.stopPropagation();
  526. }
  527. }
  528. options.el = $(e.mfpEl);
  529. if(options.delegate) {
  530. options.items = el.find(options.delegate);
  531. }
  532. mfp.open(options);
  533. },
  534. /**
  535. * Updates text on preloader
  536. */
  537. updateStatus: function(status, text) {
  538. if(mfp.preloader) {
  539. if(_prevStatus !== status) {
  540. mfp.container.removeClass('mfp-s-'+_prevStatus);
  541. }
  542. if(!text && status === 'loading') {
  543. text = mfp.st.tLoading;
  544. }
  545. var data = {
  546. status: status,
  547. text: text
  548. };
  549. // allows to modify status
  550. _mfpTrigger('UpdateStatus', data);
  551. status = data.status;
  552. text = data.text;
  553. mfp.preloader.html(text);
  554. mfp.preloader.find('a').on('click', function(e) {
  555. e.stopImmediatePropagation();
  556. });
  557. mfp.container.addClass('mfp-s-'+status);
  558. _prevStatus = status;
  559. }
  560. },
  561. /*
  562. "Private" helpers that aren't private at all
  563. */
  564. // Check to close popup or not
  565. // "target" is an element that was clicked
  566. _checkIfClose: function(target) {
  567. if($(target).hasClass(PREVENT_CLOSE_CLASS)) {
  568. return;
  569. }
  570. var closeOnContent = mfp.st.closeOnContentClick;
  571. var closeOnBg = mfp.st.closeOnBgClick;
  572. if(closeOnContent && closeOnBg) {
  573. return true;
  574. } else {
  575. // We close the popup if click is on close button or on preloader. Or if there is no content.
  576. if(!mfp.content || $(target).hasClass('mfp-close') || (mfp.preloader && target === mfp.preloader[0]) ) {
  577. return true;
  578. }
  579. // if click is outside the content
  580. if( (target !== mfp.content[0] && !$.contains(mfp.content[0], target)) ) {
  581. if(closeOnBg) {
  582. // last check, if the clicked element is in DOM, (in case it's removed onclick)
  583. if( $.contains(document, target) ) {
  584. return true;
  585. }
  586. }
  587. } else if(closeOnContent) {
  588. return true;
  589. }
  590. }
  591. return false;
  592. },
  593. _addClassToMFP: function(cName) {
  594. mfp.bgOverlay.addClass(cName);
  595. mfp.wrap.addClass(cName);
  596. },
  597. _removeClassFromMFP: function(cName) {
  598. this.bgOverlay.removeClass(cName);
  599. mfp.wrap.removeClass(cName);
  600. },
  601. _hasScrollBar: function(winHeight) {
  602. return ( (mfp.isIE7 ? _document.height() : document.body.scrollHeight) > (winHeight || _window.height()) );
  603. },
  604. _setFocus: function() {
  605. (mfp.st.focus ? mfp.content.find(mfp.st.focus).eq(0) : mfp.wrap).focus();
  606. },
  607. _onFocusIn: function(e) {
  608. if( e.target !== mfp.wrap[0] && !$.contains(mfp.wrap[0], e.target) ) {
  609. mfp._setFocus();
  610. return false;
  611. }
  612. },
  613. _parseMarkup: function(template, values, item) {
  614. var arr;
  615. if(item.data) {
  616. values = $.extend(item.data, values);
  617. }
  618. _mfpTrigger(MARKUP_PARSE_EVENT, [template, values, item] );
  619. $.each(values, function(key, value) {
  620. if(value === undefined || value === false) {
  621. return true;
  622. }
  623. arr = key.split('_');
  624. if(arr.length > 1) {
  625. var el = template.find(EVENT_NS + '-'+arr[0]);
  626. if(el.length > 0) {
  627. var attr = arr[1];
  628. if(attr === 'replaceWith') {
  629. if(el[0] !== value[0]) {
  630. el.replaceWith(value);
  631. }
  632. } else if(attr === 'img') {
  633. if(el.is('img')) {
  634. el.attr('src', value);
  635. } else {
  636. el.replaceWith( '<img src="'+value+'" class="' + el.attr('class') + '" />' );
  637. }
  638. } else {
  639. el.attr(arr[1], value);
  640. }
  641. }
  642. } else {
  643. template.find(EVENT_NS + '-'+key).html(value);
  644. }
  645. });
  646. },
  647. _getScrollbarSize: function() {
  648. // thx David
  649. if(mfp.scrollbarSize === undefined) {
  650. var scrollDiv = document.createElement("div");
  651. scrollDiv.id = "mfp-sbm";
  652. scrollDiv.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
  653. document.body.appendChild(scrollDiv);
  654. mfp.scrollbarSize = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  655. document.body.removeChild(scrollDiv);
  656. }
  657. return mfp.scrollbarSize;
  658. }
  659. }; /* MagnificPopup core prototype end */
  660. /**
  661. * Public static functions
  662. */
  663. $.magnificPopup = {
  664. instance: null,
  665. proto: MagnificPopup.prototype,
  666. modules: [],
  667. open: function(options, index) {
  668. _checkInstance();
  669. if(!options) {
  670. options = {};
  671. } else {
  672. options = $.extend(true, {}, options);
  673. }
  674. options.isObj = true;
  675. options.index = index || 0;
  676. return this.instance.open(options);
  677. },
  678. close: function() {
  679. return $.magnificPopup.instance && $.magnificPopup.instance.close();
  680. },
  681. registerModule: function(name, module) {
  682. if(module.options) {
  683. $.magnificPopup.defaults[name] = module.options;
  684. }
  685. $.extend(this.proto, module.proto);
  686. this.modules.push(name);
  687. },
  688. defaults: {
  689. // Info about options is in docs:
  690. // http://dimsemenov.com/plugins/magnific-popup/documentation.html#options
  691. disableOn: 0,
  692. key: null,
  693. midClick: false,
  694. mainClass: '',
  695. preloader: true,
  696. focus: '', // CSS selector of input to focus after popup is opened
  697. closeOnContentClick: false,
  698. closeOnBgClick: true,
  699. closeBtnInside: true,
  700. showCloseBtn: true,
  701. enableEscapeKey: true,
  702. modal: false,
  703. alignTop: false,
  704. removalDelay: 0,
  705. prependTo: null,
  706. fixedContentPos: 'auto',
  707. fixedBgPos: 'auto',
  708. overflowY: 'auto',
  709. closeMarkup: '<button title="%title%" type="button" class="mfp-close">&times;</button>',
  710. tClose: 'Close (Esc)',
  711. tLoading: 'Loading...'
  712. }
  713. };
  714. $.fn.magnificPopup = function(options) {
  715. _checkInstance();
  716. var jqEl = $(this);
  717. // We call some API method of first param is a string
  718. if (typeof options === "string" ) {
  719. if(options === 'open') {
  720. var items,
  721. itemOpts = _isJQ ? jqEl.data('magnificPopup') : jqEl[0].magnificPopup,
  722. index = parseInt(arguments[1], 10) || 0;
  723. if(itemOpts.items) {
  724. items = itemOpts.items[index];
  725. } else {
  726. items = jqEl;
  727. if(itemOpts.delegate) {
  728. items = items.find(itemOpts.delegate);
  729. }
  730. items = items.eq( index );
  731. }
  732. mfp._openClick({mfpEl:items}, jqEl, itemOpts);
  733. } else {
  734. if(mfp.isOpen)
  735. mfp[options].apply(mfp, Array.prototype.slice.call(arguments, 1));
  736. }
  737. } else {
  738. // clone options obj
  739. options = $.extend(true, {}, options);
  740. /*
  741. * As Zepto doesn't support .data() method for objects
  742. * and it works only in normal browsers
  743. * we assign "options" object directly to the DOM element. FTW!
  744. */
  745. if(_isJQ) {
  746. jqEl.data('magnificPopup', options);
  747. } else {
  748. jqEl[0].magnificPopup = options;
  749. }
  750. mfp.addGroup(jqEl, options);
  751. }
  752. return jqEl;
  753. };
  754. //Quick benchmark
  755. /*
  756. var start = performance.now(),
  757. i,
  758. rounds = 1000;
  759. for(i = 0; i < rounds; i++) {
  760. }
  761. console.log('Test #1:', performance.now() - start);
  762. start = performance.now();
  763. for(i = 0; i < rounds; i++) {
  764. }
  765. console.log('Test #2:', performance.now() - start);
  766. */
  767. /*>>core*/
  768. /*>>inline*/
  769. var INLINE_NS = 'inline',
  770. _hiddenClass,
  771. _inlinePlaceholder,
  772. _lastInlineElement,
  773. _putInlineElementsBack = function() {
  774. if(_lastInlineElement) {
  775. _inlinePlaceholder.after( _lastInlineElement.addClass(_hiddenClass) ).detach();
  776. _lastInlineElement = null;
  777. }
  778. };
  779. $.magnificPopup.registerModule(INLINE_NS, {
  780. options: {
  781. hiddenClass: 'hide', // will be appended with `mfp-` prefix
  782. markup: '',
  783. tNotFound: 'Content not found'
  784. },
  785. proto: {
  786. initInline: function() {
  787. mfp.types.push(INLINE_NS);
  788. _mfpOn(CLOSE_EVENT+'.'+INLINE_NS, function() {
  789. _putInlineElementsBack();
  790. });
  791. },
  792. getInline: function(item, template) {
  793. _putInlineElementsBack();
  794. if(item.src) {
  795. var inlineSt = mfp.st.inline,
  796. el = $(item.src);
  797. if(el.length) {
  798. // If target element has parent - we replace it with placeholder and put it back after popup is closed
  799. var parent = el[0].parentNode;
  800. if(parent && parent.tagName) {
  801. if(!_inlinePlaceholder) {
  802. _hiddenClass = inlineSt.hiddenClass;
  803. _inlinePlaceholder = _getEl(_hiddenClass);
  804. _hiddenClass = 'mfp-'+_hiddenClass;
  805. }
  806. // replace target inline element with placeholder
  807. _lastInlineElement = el.after(_inlinePlaceholder).detach().removeClass(_hiddenClass);
  808. }
  809. mfp.updateStatus('ready');
  810. } else {
  811. mfp.updateStatus('error', inlineSt.tNotFound);
  812. el = $('<div>');
  813. }
  814. item.inlineElement = el;
  815. return el;
  816. }
  817. mfp.updateStatus('ready');
  818. mfp._parseMarkup(template, {}, item);
  819. return template;
  820. }
  821. }
  822. });
  823. /*>>inline*/
  824. /*>>ajax*/
  825. var AJAX_NS = 'ajax',
  826. _ajaxCur,
  827. _removeAjaxCursor = function() {
  828. if(_ajaxCur) {
  829. _body.removeClass(_ajaxCur);
  830. }
  831. },
  832. _destroyAjaxRequest = function() {
  833. _removeAjaxCursor();
  834. if(mfp.req) {
  835. mfp.req.abort();
  836. }
  837. };
  838. $.magnificPopup.registerModule(AJAX_NS, {
  839. options: {
  840. settings: null,
  841. cursor: 'mfp-ajax-cur',
  842. tError: '<a href="%url%">The content</a> could not be loaded.'
  843. },
  844. proto: {
  845. initAjax: function() {
  846. mfp.types.push(AJAX_NS);
  847. _ajaxCur = mfp.st.ajax.cursor;
  848. _mfpOn(CLOSE_EVENT+'.'+AJAX_NS, _destroyAjaxRequest);
  849. _mfpOn('BeforeChange.' + AJAX_NS, _destroyAjaxRequest);
  850. },
  851. getAjax: function(item) {
  852. if(_ajaxCur)
  853. _body.addClass(_ajaxCur);
  854. mfp.updateStatus('loading');
  855. var opts = $.extend({
  856. url: item.src,
  857. success: function(data, textStatus, jqXHR) {
  858. var temp = {
  859. data:data,
  860. xhr:jqXHR
  861. };
  862. _mfpTrigger('ParseAjax', temp);
  863. mfp.appendContent( $(temp.data), AJAX_NS );
  864. item.finished = true;
  865. _removeAjaxCursor();
  866. mfp._setFocus();
  867. setTimeout(function() {
  868. mfp.wrap.addClass(READY_CLASS);
  869. }, 16);
  870. mfp.updateStatus('ready');
  871. _mfpTrigger('AjaxContentAdded');
  872. },
  873. error: function() {
  874. _removeAjaxCursor();
  875. item.finished = item.loadError = true;
  876. mfp.updateStatus('error', mfp.st.ajax.tError.replace('%url%', item.src));
  877. }
  878. }, mfp.st.ajax.settings);
  879. mfp.req = $.ajax(opts);
  880. return '';
  881. }
  882. }
  883. });
  884. /*>>ajax*/
  885. /*>>image*/
  886. var _imgInterval,
  887. _getTitle = function(item) {
  888. if(item.data && item.data.title !== undefined)
  889. return item.data.title;
  890. var src = mfp.st.image.titleSrc;
  891. if(src) {
  892. if($.isFunction(src)) {
  893. return src.call(mfp, item);
  894. } else if(item.el) {
  895. return item.el.attr(src) || '';
  896. }
  897. }
  898. return '';
  899. };
  900. $.magnificPopup.registerModule('image', {
  901. options: {
  902. markup: '<div class="mfp-figure">'+
  903. '<div class="mfp-close"></div>'+
  904. '<figure>'+
  905. '<div class="mfp-img"></div>'+
  906. '<figcaption>'+
  907. '<div class="mfp-bottom-bar">'+
  908. '<div class="mfp-title"></div>'+
  909. '<div class="mfp-counter"></div>'+
  910. '</div>'+
  911. '</figcaption>'+
  912. '</figure>'+
  913. '</div>',
  914. cursor: 'mfp-zoom-out-cur',
  915. titleSrc: 'title',
  916. verticalFit: true,
  917. tError: '<a href="%url%">The image</a> could not be loaded.'
  918. },
  919. proto: {
  920. initImage: function() {
  921. var imgSt = mfp.st.image,
  922. ns = '.image';
  923. mfp.types.push('image');
  924. _mfpOn(OPEN_EVENT+ns, function() {
  925. if(mfp.currItem.type === 'image' && imgSt.cursor) {
  926. _body.addClass(imgSt.cursor);
  927. }
  928. });
  929. _mfpOn(CLOSE_EVENT+ns, function() {
  930. if(imgSt.cursor) {
  931. _body.removeClass(imgSt.cursor);
  932. }
  933. _window.off('resize' + EVENT_NS);
  934. });
  935. _mfpOn('Resize'+ns, mfp.resizeImage);
  936. if(mfp.isLowIE) {
  937. _mfpOn('AfterChange', mfp.resizeImage);
  938. }
  939. },
  940. resizeImage: function() {
  941. var item = mfp.currItem;
  942. if(!item || !item.img) return;
  943. if(mfp.st.image.verticalFit) {
  944. var decr = 0;
  945. // fix box-sizing in ie7/8
  946. if(mfp.isLowIE) {
  947. decr = parseInt(item.img.css('padding-top'), 10) + parseInt(item.img.css('padding-bottom'),10);
  948. }
  949. item.img.css('max-height', mfp.wH-decr);
  950. }
  951. },
  952. _onImageHasSize: function(item) {
  953. if(item.img) {
  954. item.hasSize = true;
  955. if(_imgInterval) {
  956. clearInterval(_imgInterval);
  957. }
  958. item.isCheckingImgSize = false;
  959. _mfpTrigger('ImageHasSize', item);
  960. if(item.imgHidden) {
  961. if(mfp.content)
  962. mfp.content.removeClass('mfp-loading');
  963. item.imgHidden = false;
  964. }
  965. }
  966. },
  967. /**
  968. * Function that loops until the image has size to display elements that rely on it asap
  969. */
  970. findImageSize: function(item) {
  971. var counter = 0,
  972. img = item.img[0],
  973. mfpSetInterval = function(delay) {
  974. if(_imgInterval) {
  975. clearInterval(_imgInterval);
  976. }
  977. // decelerating interval that checks for size of an image
  978. _imgInterval = setInterval(function() {
  979. if(img.naturalWidth > 0) {
  980. mfp._onImageHasSize(item);
  981. return;
  982. }
  983. if(counter > 200) {
  984. clearInterval(_imgInterval);
  985. }
  986. counter++;
  987. if(counter === 3) {
  988. mfpSetInterval(10);
  989. } else if(counter === 40) {
  990. mfpSetInterval(50);
  991. } else if(counter === 100) {
  992. mfpSetInterval(500);
  993. }
  994. }, delay);
  995. };
  996. mfpSetInterval(1);
  997. },
  998. getImage: function(item, template) {
  999. var guard = 0,
  1000. // image load complete handler
  1001. onLoadComplete = function() {
  1002. if(item) {
  1003. if (item.img[0].complete) {
  1004. item.img.off('.mfploader');
  1005. if(item === mfp.currItem){
  1006. mfp._onImageHasSize(item);
  1007. mfp.updateStatus('ready');
  1008. }
  1009. item.hasSize = true;
  1010. item.loaded = true;
  1011. _mfpTrigger('ImageLoadComplete');
  1012. }
  1013. else {
  1014. // if image complete check fails 200 times (20 sec), we assume that there was an error.
  1015. guard++;
  1016. if(guard < 200) {
  1017. setTimeout(onLoadComplete,100);
  1018. } else {
  1019. onLoadError();
  1020. }
  1021. }
  1022. }
  1023. },
  1024. // image error handler
  1025. onLoadError = function() {
  1026. if(item) {
  1027. item.img.off('.mfploader');
  1028. if(item === mfp.currItem){
  1029. mfp._onImageHasSize(item);
  1030. mfp.updateStatus('error', imgSt.tError.replace('%url%', item.src) );
  1031. }
  1032. item.hasSize = true;
  1033. item.loaded = true;
  1034. item.loadError = true;
  1035. }
  1036. },
  1037. imgSt = mfp.st.image;
  1038. var el = template.find('.mfp-img');
  1039. if(el.length) {
  1040. var img = document.createElement('img');
  1041. img.className = 'mfp-img';
  1042. item.img = $(img).on('load.mfploader', onLoadComplete).on('error.mfploader', onLoadError);
  1043. img.src = item.src;
  1044. // without clone() "error" event is not firing when IMG is replaced by new IMG
  1045. // TODO: find a way to avoid such cloning
  1046. if(el.is('img')) {
  1047. item.img = item.img.clone();
  1048. }
  1049. img = item.img[0];
  1050. if(img.naturalWidth > 0) {
  1051. item.hasSize = true;
  1052. } else if(!img.width) {
  1053. item.hasSize = false;
  1054. }
  1055. }
  1056. mfp._parseMarkup(template, {
  1057. title: _getTitle(item),
  1058. img_replaceWith: item.img
  1059. }, item);
  1060. mfp.resizeImage();
  1061. if(item.hasSize) {
  1062. if(_imgInterval) clearInterval(_imgInterval);
  1063. if(item.loadError) {
  1064. template.addClass('mfp-loading');
  1065. mfp.updateStatus('error', imgSt.tError.replace('%url%', item.src) );
  1066. } else {
  1067. template.removeClass('mfp-loading');
  1068. mfp.updateStatus('ready');
  1069. }
  1070. return template;
  1071. }
  1072. mfp.updateStatus('loading');
  1073. item.loading = true;
  1074. if(!item.hasSize) {
  1075. item.imgHidden = true;
  1076. template.addClass('mfp-loading');
  1077. mfp.findImageSize(item);
  1078. }
  1079. return template;
  1080. }
  1081. }
  1082. });
  1083. /*>>image*/
  1084. /*>>zoom*/
  1085. var hasMozTransform,
  1086. getHasMozTransform = function() {
  1087. if(hasMozTransform === undefined) {
  1088. hasMozTransform = document.createElement('p').style.MozTransform !== undefined;
  1089. }
  1090. return hasMozTransform;
  1091. };
  1092. $.magnificPopup.registerModule('zoom', {
  1093. options: {
  1094. enabled: false,
  1095. easing: 'ease-in-out',
  1096. duration: 300,
  1097. opener: function(element) {
  1098. return element.is('img') ? element : element.find('img');
  1099. }
  1100. },
  1101. proto: {
  1102. initZoom: function() {
  1103. var zoomSt = mfp.st.zoom,
  1104. ns = '.zoom',
  1105. image;
  1106. if(!zoomSt.enabled || !mfp.supportsTransition) {
  1107. return;
  1108. }
  1109. var duration = zoomSt.duration,
  1110. getElToAnimate = function(image) {
  1111. var newImg = image.clone().removeAttr('style').removeAttr('class').addClass('mfp-animated-image'),
  1112. transition = 'all '+(zoomSt.duration/1000)+'s ' + zoomSt.easing,
  1113. cssObj = {
  1114. position: 'fixed',
  1115. zIndex: 9999,
  1116. left: 0,
  1117. top: 0,
  1118. '-webkit-backface-visibility': 'hidden'
  1119. },
  1120. t = 'transition';
  1121. cssObj['-webkit-'+t] = cssObj['-moz-'+t] = cssObj['-o-'+t] = cssObj[t] = transition;
  1122. newImg.css(cssObj);
  1123. return newImg;
  1124. },
  1125. showMainContent = function() {
  1126. mfp.content.css('visibility', 'visible');
  1127. },
  1128. openTimeout,
  1129. animatedImg;
  1130. _mfpOn('BuildControls'+ns, function() {
  1131. if(mfp._allowZoom()) {
  1132. clearTimeout(openTimeout);
  1133. mfp.content.css('visibility', 'hidden');
  1134. // Basically, all code below does is clones existing image, puts in on top of the current one and animated it
  1135. image = mfp._getItemToZoom();
  1136. if(!image) {
  1137. showMainContent();
  1138. return;
  1139. }
  1140. animatedImg = getElToAnimate(image);
  1141. animatedImg.css( mfp._getOffset() );
  1142. mfp.wrap.append(animatedImg);
  1143. openTimeout = setTimeout(function() {
  1144. animatedImg.css( mfp._getOffset( true ) );
  1145. openTimeout = setTimeout(function() {
  1146. showMainContent();
  1147. setTimeout(function() {
  1148. animatedImg.remove();
  1149. image = animatedImg = null;
  1150. _mfpTrigger('ZoomAnimationEnded');
  1151. }, 16); // avoid blink when switching images
  1152. }, duration); // this timeout equals animation duration
  1153. }, 16); // by adding this timeout we avoid short glitch at the beginning of animation
  1154. // Lots of timeouts...
  1155. }
  1156. });
  1157. _mfpOn(BEFORE_CLOSE_EVENT+ns, function() {
  1158. if(mfp._allowZoom()) {
  1159. clearTimeout(openTimeout);
  1160. mfp.st.removalDelay = duration;
  1161. if(!image) {
  1162. image = mfp._getItemToZoom();
  1163. if(!image) {
  1164. return;
  1165. }
  1166. animatedImg = getElToAnimate(image);
  1167. }
  1168. animatedImg.css( mfp._getOffset(true) );
  1169. mfp.wrap.append(animatedImg);
  1170. mfp.content.css('visibility', 'hidden');
  1171. setTimeout(function() {
  1172. animatedImg.css( mfp._getOffset() );
  1173. }, 16);
  1174. }
  1175. });
  1176. _mfpOn(CLOSE_EVENT+ns, function() {
  1177. if(mfp._allowZoom()) {
  1178. showMainContent();
  1179. if(animatedImg) {
  1180. animatedImg.remove();
  1181. }
  1182. image = null;
  1183. }
  1184. });
  1185. },
  1186. _allowZoom: function() {
  1187. return mfp.currItem.type === 'image';
  1188. },
  1189. _getItemToZoom: function() {
  1190. if(mfp.currItem.hasSize) {
  1191. return mfp.currItem.img;
  1192. } else {
  1193. return false;
  1194. }
  1195. },
  1196. // Get element postion relative to viewport
  1197. _getOffset: function(isLarge) {
  1198. var el;
  1199. if(isLarge) {
  1200. el = mfp.currItem.img;
  1201. } else {
  1202. el = mfp.st.zoom.opener(mfp.currItem.el || mfp.currItem);
  1203. }
  1204. var offset = el.offset();
  1205. var paddingTop = parseInt(el.css('padding-top'),10);
  1206. var paddingBottom = parseInt(el.css('padding-bottom'),10);
  1207. offset.top -= ( $(window).scrollTop() - paddingTop );
  1208. /*
  1209. Animating left + top + width/height looks glitchy in Firefox, but perfect in Chrome. And vice-versa.
  1210. */
  1211. var obj = {
  1212. width: el.width(),
  1213. // fix Zepto height+padding issue
  1214. height: (_isJQ ? el.innerHeight() : el[0].offsetHeight) - paddingBottom - paddingTop
  1215. };
  1216. // I hate to do this, but there is no another option
  1217. if( getHasMozTransform() ) {
  1218. obj['-moz-transform'] = obj['transform'] = 'translate(' + offset.left + 'px,' + offset.top + 'px)';
  1219. } else {
  1220. obj.left = offset.left;
  1221. obj.top = offset.top;
  1222. }
  1223. return obj;
  1224. }
  1225. }
  1226. });
  1227. /*>>zoom*/
  1228. /*>>iframe*/
  1229. var IFRAME_NS = 'iframe',
  1230. _emptyPage = '//about:blank',
  1231. _fixIframeBugs = function(isShowing) {
  1232. if(mfp.currTemplate[IFRAME_NS]) {
  1233. var el = mfp.currTemplate[IFRAME_NS].find('iframe');
  1234. if(el.length) {
  1235. // reset src after the popup is closed to avoid "video keeps playing after popup is closed" bug
  1236. if(!isShowing) {
  1237. el[0].src = _emptyPage;
  1238. }
  1239. // IE8 black screen bug fix
  1240. if(mfp.isIE8) {
  1241. el.css('display', isShowing ? 'block' : 'none');
  1242. }
  1243. }
  1244. }
  1245. };
  1246. $.magnificPopup.registerModule(IFRAME_NS, {
  1247. options: {
  1248. markup: '<div class="mfp-iframe-scaler">'+
  1249. '<div class="mfp-close"></div>'+
  1250. '<iframe class="mfp-iframe" src="//about:blank" frameborder="0" allowfullscreen></iframe>'+
  1251. '</div>',
  1252. srcAction: 'iframe_src',
  1253. // we don't care and support only one default type of URL by default
  1254. patterns: {
  1255. youtube: {
  1256. index: 'youtube.com',
  1257. id: 'v=',
  1258. src: '//www.youtube.com/embed/%id%?autoplay=1'
  1259. },
  1260. vimeo: {
  1261. index: 'vimeo.com/',
  1262. id: '/',
  1263. src: '//player.vimeo.com/video/%id%?autoplay=1'
  1264. },
  1265. gmaps: {
  1266. index: '//maps.google.',
  1267. src: '%id%&output=embed'
  1268. }
  1269. }
  1270. },
  1271. proto: {
  1272. initIframe: function() {
  1273. mfp.types.push(IFRAME_NS);
  1274. _mfpOn('BeforeChange', function(e, prevType, newType) {
  1275. if(prevType !== newType) {
  1276. if(prevType === IFRAME_NS) {
  1277. _fixIframeBugs(); // iframe if removed
  1278. } else if(newType === IFRAME_NS) {
  1279. _fixIframeBugs(true); // iframe is showing
  1280. }
  1281. }// else {
  1282. // iframe source is switched, don't do anything
  1283. //}
  1284. });
  1285. _mfpOn(CLOSE_EVENT + '.' + IFRAME_NS, function() {
  1286. _fixIframeBugs();
  1287. });
  1288. },
  1289. getIframe: function(item, template) {
  1290. var embedSrc = item.src;
  1291. var iframeSt = mfp.st.iframe;
  1292. $.each(iframeSt.patterns, function() {
  1293. if(embedSrc.indexOf( this.index ) > -1) {
  1294. if(this.id) {
  1295. if(typeof this.id === 'string') {
  1296. embedSrc = embedSrc.substr(embedSrc.lastIndexOf(this.id)+this.id.length, embedSrc.length);
  1297. } else {
  1298. embedSrc = this.id.call( this, embedSrc );
  1299. }
  1300. }
  1301. embedSrc = this.src.replace('%id%', embedSrc );
  1302. return false; // break;
  1303. }
  1304. });
  1305. var dataObj = {};
  1306. if(iframeSt.srcAction) {
  1307. dataObj[iframeSt.srcAction] = embedSrc;
  1308. }
  1309. mfp._parseMarkup(template, dataObj, item);
  1310. mfp.updateStatus('ready');
  1311. return template;
  1312. }
  1313. }
  1314. });
  1315. /*>>iframe*/
  1316. /*>>gallery*/
  1317. /**
  1318. * Get looped index depending on number of slides
  1319. */
  1320. var _getLoopedId = function(index) {
  1321. var numSlides = mfp.items.length;
  1322. if(index > numSlides - 1) {
  1323. return index - numSlides;
  1324. } else if(index < 0) {
  1325. return numSlides + index;
  1326. }
  1327. return index;
  1328. },
  1329. _replaceCurrTotal = function(text, curr, total) {
  1330. return text.replace(/%curr%/gi, curr + 1).replace(/%total%/gi, total);
  1331. };
  1332. $.magnificPopup.registerModule('gallery', {
  1333. options: {
  1334. enabled: false,
  1335. arrowMarkup: '<button title="%title%" type="button" class="mfp-arrow mfp-arrow-%dir%"></button>',
  1336. preload: [0,2],
  1337. navigateByImgClick: true,
  1338. arrows: true,
  1339. tPrev: 'Previous (Left arrow key)',
  1340. tNext: 'Next (Right arrow key)',
  1341. tCounter: '%curr% of %total%'
  1342. },
  1343. proto: {
  1344. initGallery: function() {
  1345. var gSt = mfp.st.gallery,
  1346. ns = '.mfp-gallery',
  1347. supportsFastClick = Boolean($.fn.mfpFastClick);
  1348. mfp.direction = true; // true - next, false - prev
  1349. if(!gSt || !gSt.enabled ) return false;
  1350. _wrapClasses += ' mfp-gallery';
  1351. _mfpOn(OPEN_EVENT+ns, function() {
  1352. if(gSt.navigateByImgClick) {
  1353. mfp.wrap.on('click'+ns, '.mfp-img', function() {
  1354. if(mfp.items.length > 1) {
  1355. mfp.next();
  1356. return false;
  1357. }
  1358. });
  1359. }
  1360. _document.on('keydown'+ns, function(e) {
  1361. if (e.keyCode === 37) {
  1362. mfp.prev();
  1363. } else if (e.keyCode === 39) {
  1364. mfp.next();
  1365. }
  1366. });
  1367. });
  1368. _mfpOn('UpdateStatus'+ns, function(e, data) {
  1369. if(data.text) {
  1370. data.text = _replaceCurrTotal(data.text, mfp.currItem.index, mfp.items.length);
  1371. }
  1372. });
  1373. _mfpOn(MARKUP_PARSE_EVENT+ns, function(e, element, values, item) {
  1374. var l = mfp.items.length;
  1375. values.counter = l > 1 ? _replaceCurrTotal(gSt.tCounter, item.index, l) : '';
  1376. });
  1377. _mfpOn('BuildControls' + ns, function() {
  1378. if(mfp.items.length > 1 && gSt.arrows && !mfp.arrowLeft) {
  1379. var markup = gSt.arrowMarkup,
  1380. arrowLeft = mfp.arrowLeft = $( markup.replace(/%title%/gi, gSt.tPrev).replace(/%dir%/gi, 'left') ).addClass(PREVENT_CLOSE_CLASS),
  1381. arrowRight = mfp.arrowRight = $( markup.replace(/%title%/gi, gSt.tNext).replace(/%dir%/gi, 'right') ).addClass(PREVENT_CLOSE_CLASS);
  1382. var eName = supportsFastClick ? 'mfpFastClick' : 'click';
  1383. arrowLeft[eName](function() {
  1384. mfp.prev();
  1385. });
  1386. arrowRight[eName](function() {
  1387. mfp.next();
  1388. });
  1389. // Polyfill for :before and :after (adds elements with classes mfp-a and mfp-b)
  1390. if(mfp.isIE7) {
  1391. _getEl('b', arrowLeft[0], false, true);
  1392. _getEl('a', arrowLeft[0], false, true);
  1393. _getEl('b', arrowRight[0], false, true);
  1394. _getEl('a', arrowRight[0], false, true);
  1395. }
  1396. mfp.container.append(arrowLeft.add(arrowRight));
  1397. }
  1398. });
  1399. _mfpOn(CHANGE_EVENT+ns, function() {
  1400. if(mfp._preloadTimeout) clearTimeout(mfp._preloadTimeout);
  1401. mfp._preloadTimeout = setTimeout(function() {
  1402. mfp.preloadNearbyImages();
  1403. mfp._preloadTimeout = null;
  1404. }, 16);
  1405. });
  1406. _mfpOn(CLOSE_EVENT+ns, function() {
  1407. _document.off(ns);
  1408. mfp.wrap.off('click'+ns);
  1409. if(mfp.arrowLeft && supportsFastClick) {
  1410. mfp.arrowLeft.add(mfp.arrowRight).destroyMfpFastClick();
  1411. }
  1412. mfp.arrowRight = mfp.arrowLeft = null;
  1413. });
  1414. },
  1415. next: function() {
  1416. mfp.direction = true;
  1417. mfp.index = _getLoopedId(mfp.index + 1);
  1418. mfp.updateItemHTML();
  1419. },
  1420. prev: function() {
  1421. mfp.direction = false;
  1422. mfp.index = _getLoopedId(mfp.index - 1);
  1423. mfp.updateItemHTML();
  1424. },
  1425. goTo: function(newIndex) {
  1426. mfp.direction = (newIndex >= mfp.index);
  1427. mfp.index = newIndex;
  1428. mfp.updateItemHTML();
  1429. },
  1430. preloadNearbyImages: function() {
  1431. var p = mfp.st.gallery.preload,
  1432. preloadBefore = Math.min(p[0], mfp.items.length),
  1433. preloadAfter = Math.min(p[1], mfp.items.length),
  1434. i;
  1435. for(i = 1; i <= (mfp.direction ? preloadAfter : preloadBefore); i++) {
  1436. mfp._preloadItem(mfp.index+i);
  1437. }
  1438. for(i = 1; i <= (mfp.direction ? preloadBefore : preloadAfter); i++) {
  1439. mfp._preloadItem(mfp.index-i);
  1440. }
  1441. },
  1442. _preloadItem: function(index) {
  1443. index = _getLoopedId(index);
  1444. if(mfp.items[index].preloaded) {
  1445. return;
  1446. }
  1447. var item = mfp.items[index];
  1448. if(!item.parsed) {
  1449. item = mfp.parseEl( index );
  1450. }
  1451. _mfpTrigger('LazyLoad', item);
  1452. if(item.type === 'image') {
  1453. item.img = $('<img class="mfp-img" />').on('load.mfploader', function() {
  1454. item.hasSize = true;
  1455. }).on('error.mfploader', function() {
  1456. item.hasSize = true;
  1457. item.loadError = true;
  1458. _mfpTrigger('LazyLoadError', item);
  1459. }).attr('src', item.src);
  1460. }
  1461. item.preloaded = true;
  1462. }
  1463. }
  1464. });
  1465. /*
  1466. Touch Support that might be implemented some day
  1467. addSwipeGesture: function() {
  1468. var startX,
  1469. moved,
  1470. multipleTouches;
  1471. return;
  1472. var namespace = '.mfp',
  1473. addEventNames = function(pref, down, move, up, cancel) {
  1474. mfp._tStart = pref + down + namespace;
  1475. mfp._tMove = pref + move + namespace;
  1476. mfp._tEnd = pref + up + namespace;
  1477. mfp._tCancel = pref + cancel + namespace;
  1478. };
  1479. if(window.navigator.msPointerEnabled) {
  1480. addEventNames('MSPointer', 'Down', 'Move', 'Up', 'Cancel');
  1481. } else if('ontouchstart' in window) {
  1482. addEventNames('touch', 'start', 'move', 'end', 'cancel');
  1483. } else {
  1484. return;
  1485. }
  1486. _window.on(mfp._tStart, function(e) {
  1487. var oE = e.originalEvent;
  1488. multipleTouches = moved = false;
  1489. startX = oE.pageX || oE.changedTouches[0].pageX;
  1490. }).on(mfp._tMove, function(e) {
  1491. if(e.originalEvent.touches.length > 1) {
  1492. multipleTouches = e.originalEvent.touches.length;
  1493. } else {
  1494. //e.preventDefault();
  1495. moved = true;
  1496. }
  1497. }).on(mfp._tEnd + ' ' + mfp._tCancel, function(e) {
  1498. if(moved && !multipleTouches) {
  1499. var oE = e.originalEvent,
  1500. diff = startX - (oE.pageX || oE.changedTouches[0].pageX);
  1501. if(diff > 20) {
  1502. mfp.next();
  1503. } else if(diff < -20) {
  1504. mfp.prev();
  1505. }
  1506. }
  1507. });
  1508. },
  1509. */
  1510. /*>>gallery*/
  1511. /*>>retina*/
  1512. var RETINA_NS = 'retina';
  1513. $.magnificPopup.registerModule(RETINA_NS, {
  1514. options: {
  1515. replaceSrc: function(item) {
  1516. return item.src.replace(/\.\w+$/, function(m) { return '@2x' + m; });
  1517. },
  1518. ratio: 1 // Function or number. Set to 1 to disable.
  1519. },
  1520. proto: {
  1521. initRetina: function() {
  1522. if(window.devicePixelRatio > 1) {
  1523. var st = mfp.st.retina,
  1524. ratio = st.ratio;
  1525. ratio = !isNaN(ratio) ? ratio : ratio();
  1526. if(ratio > 1) {
  1527. _mfpOn('ImageHasSize' + '.' + RETINA_NS, function(e, item) {
  1528. item.img.css({
  1529. 'max-width': item.img[0].naturalWidth / ratio,
  1530. 'width': '100%'
  1531. });
  1532. });
  1533. _mfpOn('ElementParse' + '.' + RETINA_NS, function(e, item) {
  1534. item.src = st.replaceSrc(item, ratio);
  1535. });
  1536. }
  1537. }
  1538. }
  1539. }
  1540. });
  1541. /*>>retina*/
  1542. /*>>fastclick*/
  1543. /**
  1544. * FastClick event implementation. (removes 300ms delay on touch devices)
  1545. * Based on https://developers.google.com/mobile/articles/fast_buttons
  1546. *
  1547. * You may use it outside the Magnific Popup by calling just:
  1548. *
  1549. * $('.your-el').mfpFastClick(function() {
  1550. * console.log('Clicked!');
  1551. * });
  1552. *
  1553. * To unbind:
  1554. * $('.your-el').destroyMfpFastClick();
  1555. *
  1556. *
  1557. * Note that it's a very basic and simple implementation, it blocks ghost click on the same element where it was bound.
  1558. * If you need something more advanced, use plugin by FT Labs https://github.com/ftlabs/fastclick
  1559. *
  1560. */
  1561. (function() {
  1562. var ghostClickDelay = 1000,
  1563. supportsTouch = 'ontouchstart' in window,
  1564. unbindTouchMove = function() {
  1565. _window.off('touchmove'+ns+' touchend'+ns);
  1566. },
  1567. eName = 'mfpFastClick',
  1568. ns = '.'+eName;
  1569. // As Zepto.js doesn't have an easy way to add custom events (like jQuery), so we implement it in this way
  1570. $.fn.mfpFastClick = function(callback) {
  1571. return $(this).each(function() {
  1572. var elem = $(this),
  1573. lock;
  1574. if( supportsTouch ) {
  1575. var timeout,
  1576. startX,
  1577. startY,
  1578. pointerMoved,
  1579. point,
  1580. numPointers;
  1581. elem.on('touchstart' + ns, function(e) {
  1582. pointerMoved = false;
  1583. numPointers = 1;
  1584. point = e.originalEvent ? e.originalEvent.touches[0] : e.touches[0];
  1585. startX = point.clientX;
  1586. startY = point.clientY;
  1587. _window.on('touchmove'+ns, function(e) {
  1588. point = e.originalEvent ? e.originalEvent.touches : e.touches;
  1589. numPointers = point.length;
  1590. point = point[0];
  1591. if (Math.abs(point.clientX - startX) > 10 ||
  1592. Math.abs(point.clientY - startY) > 10) {
  1593. pointerMoved = true;
  1594. unbindTouchMove();
  1595. }
  1596. }).on('touchend'+ns, function(e) {
  1597. unbindTouchMove();
  1598. if(pointerMoved || numPointers > 1) {
  1599. return;
  1600. }
  1601. lock = true;
  1602. e.preventDefault();
  1603. clearTimeout(timeout);
  1604. timeout = setTimeout(function() {
  1605. lock = false;
  1606. }, ghostClickDelay);
  1607. callback();
  1608. });
  1609. });
  1610. }
  1611. elem.on('click' + ns, function() {
  1612. if(!lock) {
  1613. callback();
  1614. }
  1615. });
  1616. });
  1617. };
  1618. $.fn.destroyMfpFastClick = function() {
  1619. $(this).off('touchstart' + ns + ' click' + ns);
  1620. if(supportsTouch) _window.off('touchmove'+ns+' touchend'+ns);
  1621. };
  1622. })();
  1623. /*>>fastclick*/
  1624. _checkInstance(); })(window.jQuery || window.Zepto);