jquery.magnific-popup.js 45 KB

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