photoswipe-ui-default.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*! PhotoSwipe Default UI - 4.1.0 - 2015-07-11
  2. * http://photoswipe.com
  3. * Copyright (c) 2015 Dmitry Semenov; */
  4. /**
  5. *
  6. * UI on top of main sliding area (caption, arrows, close button, etc.).
  7. * Built just using public methods/properties of PhotoSwipe.
  8. *
  9. */
  10. (function (root, factory) {
  11. if (typeof define === 'function' && define.amd) {
  12. define(factory);
  13. } else if (typeof exports === 'object') {
  14. module.exports = factory();
  15. } else {
  16. root.PhotoSwipeUI_Default = factory();
  17. }
  18. })(this, function () {
  19. 'use strict';
  20. var PhotoSwipeUI_Default =
  21. function(pswp, framework) {
  22. var ui = this;
  23. var _overlayUIUpdated = false,
  24. _controlsVisible = true,
  25. _fullscrenAPI,
  26. _controls,
  27. _captionContainer,
  28. _fakeCaptionContainer,
  29. _indexIndicator,
  30. _shareButton,
  31. _shareModal,
  32. _shareModalHidden = true,
  33. _initalCloseOnScrollValue,
  34. _isIdle,
  35. _listen,
  36. _loadingIndicator,
  37. _loadingIndicatorHidden,
  38. _loadingIndicatorTimeout,
  39. _galleryHasOneSlide,
  40. _options,
  41. _defaultUIOptions = {
  42. barsSize: {top:44, bottom:'auto'},
  43. closeElClasses: ['item', 'caption', 'zoom-wrap', 'ui', 'top-bar'],
  44. timeToIdle: 4000,
  45. timeToIdleOutside: 1000,
  46. loadingIndicatorDelay: 1000, // 2s
  47. addCaptionHTMLFn: function(item, captionEl /*, isFake */) {
  48. if(!item.title) {
  49. captionEl.children[0].innerHTML = '';
  50. return false;
  51. }
  52. captionEl.children[0].innerHTML = item.title;
  53. return true;
  54. },
  55. closeEl:true,
  56. captionEl: true,
  57. fullscreenEl: true,
  58. zoomEl: true,
  59. shareEl: true,
  60. counterEl: true,
  61. arrowEl: true,
  62. preloaderEl: true,
  63. coverEl: true,
  64. hideEl: true,
  65. tapToClose: false,
  66. tapToToggleControls: true,
  67. clickToCloseNonZoomable: true,
  68. shareButtons: [
  69. {id:'facebook', label:'Share on Facebook', url:'https://www.facebook.com/sharer/sharer.php?u={{url}}'},
  70. {id:'twitter', label:'Tweet', url:'https://twitter.com/intent/tweet?text={{text}}&url={{url}}'},
  71. {id:'pinterest', label:'Pin it', url:'http://www.pinterest.com/pin/create/button/'+
  72. '?url={{url}}&media={{image_url}}&description={{text}}'},
  73. {id:'download', label:'Download image', url:'{{raw_image_url}}', download:true}
  74. ],
  75. getImageURLForShare: function( /* shareButtonData */ ) {
  76. return pswp.currItem.src || '';
  77. },
  78. getPageURLForShare: function( /* shareButtonData */ ) {
  79. return window.location.href;
  80. },
  81. getTextForShare: function( /* shareButtonData */ ) {
  82. return pswp.currItem.title || '';
  83. },
  84. indexIndicatorSep: ' / '
  85. },
  86. _blockControlsTap,
  87. _blockControlsTapTimeout;
  88. var _onControlsTap = function(e) {
  89. if(_blockControlsTap) {
  90. return true;
  91. }
  92. e = e || window.event;
  93. if(_options.timeToIdle && _options.mouseUsed && !_isIdle) {
  94. // reset idle timer
  95. _onIdleMouseMove();
  96. }
  97. var target = e.target || e.srcElement,
  98. uiElement,
  99. clickedClass = target.className,
  100. found;
  101. for(var i = 0; i < _uiElements.length; i++) {
  102. uiElement = _uiElements[i];
  103. if(uiElement.onTap && clickedClass.indexOf('pswp__' + uiElement.name ) > -1 ) {
  104. uiElement.onTap();
  105. found = true;
  106. }
  107. }
  108. if(found) {
  109. if(e.stopPropagation) {
  110. e.stopPropagation();
  111. }
  112. _blockControlsTap = true;
  113. // Some versions of Android don't prevent ghost click event
  114. // when preventDefault() was called on touchstart and/or touchend.
  115. //
  116. // This happens on v4.3, 4.2, 4.1,
  117. // older versions strangely work correctly,
  118. // but just in case we add delay on all of them)
  119. var tapDelay = framework.features.isOldAndroid ? 600 : 30;
  120. _blockControlsTapTimeout = setTimeout(function() {
  121. _blockControlsTap = false;
  122. }, tapDelay);
  123. }
  124. },
  125. _fitControlsInViewport = function() {
  126. return !pswp.likelyTouchDevice || _options.mouseUsed || screen.width > 1200;
  127. },
  128. _togglePswpClass = function(el, cName, add) {
  129. framework[ (add ? 'add' : 'remove') + 'Class' ](el, 'pswp__' + cName);
  130. },
  131. // add class when there is just one item in the gallery
  132. // (by default it hides left/right arrows and 1ofX counter)
  133. _countNumItems = function() {
  134. var hasOneSlide = (_options.getNumItemsFn() === 1);
  135. if(hasOneSlide !== _galleryHasOneSlide) {
  136. _togglePswpClass(_controls, 'ui--one-slide', hasOneSlide);
  137. _galleryHasOneSlide = hasOneSlide;
  138. }
  139. },
  140. _toggleShareModalClass = function() {
  141. _togglePswpClass(_shareModal, 'share-modal--hidden', _shareModalHidden);
  142. },
  143. _toggleShareModal = function() {
  144. _shareModalHidden = !_shareModalHidden;
  145. if(!_shareModalHidden) {
  146. _toggleShareModalClass();
  147. setTimeout(function() {
  148. if(!_shareModalHidden) {
  149. framework.addClass(_shareModal, 'pswp__share-modal--fade-in');
  150. }
  151. }, 30);
  152. } else {
  153. framework.removeClass(_shareModal, 'pswp__share-modal--fade-in');
  154. setTimeout(function() {
  155. if(_shareModalHidden) {
  156. _toggleShareModalClass();
  157. }
  158. }, 300);
  159. }
  160. if(!_shareModalHidden) {
  161. _updateShareURLs();
  162. }
  163. return false;
  164. },
  165. _openWindowPopup = function(e) {
  166. e = e || window.event;
  167. var target = e.target || e.srcElement;
  168. pswp.shout('shareLinkClick', e, target);
  169. if(!target.href) {
  170. return false;
  171. }
  172. if( target.hasAttribute('download') ) {
  173. return true;
  174. }
  175. window.open(target.href, 'pswp_share', 'scrollbars=yes,resizable=yes,toolbar=no,'+
  176. 'location=yes,width=550,height=420,top=100,left=' +
  177. (window.screen ? Math.round(screen.width / 2 - 275) : 100) );
  178. if(!_shareModalHidden) {
  179. _toggleShareModal();
  180. }
  181. return false;
  182. },
  183. _updateShareURLs = function() {
  184. var shareButtonOut = '',
  185. shareButtonData,
  186. shareURL,
  187. image_url,
  188. page_url,
  189. share_text;
  190. for(var i = 0; i < _options.shareButtons.length; i++) {
  191. shareButtonData = _options.shareButtons[i];
  192. image_url = _options.getImageURLForShare(shareButtonData);
  193. page_url = _options.getPageURLForShare(shareButtonData);
  194. share_text = _options.getTextForShare(shareButtonData);
  195. shareURL = shareButtonData.url.replace('{{url}}', encodeURIComponent(page_url) )
  196. .replace('{{image_url}}', encodeURIComponent(image_url) )
  197. .replace('{{raw_image_url}}', image_url )
  198. .replace('{{text}}', encodeURIComponent(share_text) );
  199. shareButtonOut += '<a href="' + shareURL + '" target="_blank" '+
  200. 'class="pswp__share--' + shareButtonData.id + '"' +
  201. (shareButtonData.download ? 'download' : '') + '>' +
  202. shareButtonData.label + '</a>';
  203. if(_options.parseShareButtonOut) {
  204. shareButtonOut = _options.parseShareButtonOut(shareButtonData, shareButtonOut);
  205. }
  206. }
  207. _shareModal.children[0].innerHTML = shareButtonOut;
  208. _shareModal.children[0].onclick = _openWindowPopup;
  209. },
  210. _hasCloseClass = function(target) {
  211. for(var i = 0; i < _options.closeElClasses.length; i++) {
  212. if( framework.hasClass(target, 'pswp__' + _options.closeElClasses[i]) ) {
  213. return true;
  214. }
  215. }
  216. },
  217. _idleInterval,
  218. _idleTimer,
  219. _idleIncrement = 0,
  220. _onIdleMouseMove = function() {
  221. clearTimeout(_idleTimer);
  222. _idleIncrement = 0;
  223. if(_isIdle) {
  224. ui.setIdle(false);
  225. }
  226. },
  227. _onMouseLeaveWindow = function(e) {
  228. e = e ? e : window.event;
  229. var from = e.relatedTarget || e.toElement;
  230. if (!from || from.nodeName === 'HTML') {
  231. clearTimeout(_idleTimer);
  232. _idleTimer = setTimeout(function() {
  233. ui.setIdle(true);
  234. }, _options.timeToIdleOutside);
  235. }
  236. },
  237. _setupFullscreenAPI = function() {
  238. if(_options.fullscreenEl) {
  239. if(!_fullscrenAPI) {
  240. _fullscrenAPI = ui.getFullscreenAPI();
  241. }
  242. if(_fullscrenAPI) {
  243. framework.bind(document, _fullscrenAPI.eventK, ui.updateFullscreen);
  244. ui.updateFullscreen();
  245. framework.addClass(pswp.template, 'pswp--supports-fs');
  246. } else {
  247. framework.removeClass(pswp.template, 'pswp--supports-fs');
  248. }
  249. }
  250. },
  251. _setupLoadingIndicator = function() {
  252. // Setup loading indicator
  253. if(_options.preloaderEl) {
  254. _toggleLoadingIndicator(true);
  255. _listen('beforeChange', function() {
  256. clearTimeout(_loadingIndicatorTimeout);
  257. // display loading indicator with delay
  258. _loadingIndicatorTimeout = setTimeout(function() {
  259. if(pswp.currItem && pswp.currItem.loading) {
  260. if( !pswp.allowProgressiveImg() || (pswp.currItem.img && !pswp.currItem.img.naturalWidth) ) {
  261. // show preloader if progressive loading is not enabled,
  262. // or image width is not defined yet (because of slow connection)
  263. _toggleLoadingIndicator(false);
  264. // items-controller.js function allowProgressiveImg
  265. }
  266. } else {
  267. _toggleLoadingIndicator(true); // hide preloader
  268. }
  269. }, _options.loadingIndicatorDelay);
  270. });
  271. _listen('imageLoadComplete', function(index, item) {
  272. if(pswp.currItem === item) {
  273. _toggleLoadingIndicator(true);
  274. }
  275. });
  276. }
  277. },
  278. _toggleLoadingIndicator = function(hide) {
  279. if( _loadingIndicatorHidden !== hide ) {
  280. _togglePswpClass(_loadingIndicator, 'preloader--active', !hide);
  281. _loadingIndicatorHidden = hide;
  282. }
  283. },
  284. _applyNavBarGaps = function(item) {
  285. var gap = item.vGap;
  286. if( _fitControlsInViewport() ) {
  287. var bars = _options.barsSize;
  288. if(_options.captionEl && bars.bottom === 'auto') {
  289. if(!_fakeCaptionContainer) {
  290. _fakeCaptionContainer = framework.createEl('pswp__caption pswp__caption--fake');
  291. _fakeCaptionContainer.appendChild( framework.createEl('pswp__caption__center') );
  292. _controls.insertBefore(_fakeCaptionContainer, _captionContainer);
  293. framework.addClass(_controls, 'pswp__ui--fit');
  294. }
  295. if( _options.addCaptionHTMLFn(item, _fakeCaptionContainer, true) ) {
  296. var captionSize = _fakeCaptionContainer.clientHeight;
  297. gap.bottom = parseInt(captionSize,10) || 44;
  298. } else {
  299. gap.bottom = bars.top; // if no caption, set size of bottom gap to size of top
  300. }
  301. } else {
  302. gap.bottom = bars.bottom === 'auto' ? 0 : bars.bottom;
  303. }
  304. // height of top bar is static, no need to calculate it
  305. gap.top = bars.top;
  306. } else {
  307. gap.top = gap.bottom = 0;
  308. }
  309. },
  310. _setupIdle = function() {
  311. // Hide controls when mouse is used
  312. if(_options.timeToIdle) {
  313. _listen('mouseUsed', function() {
  314. framework.bind(document, 'mousemove', _onIdleMouseMove);
  315. framework.bind(document, 'mouseout', _onMouseLeaveWindow);
  316. _idleInterval = setInterval(function() {
  317. _idleIncrement++;
  318. if(_idleIncrement === 2) {
  319. ui.setIdle(true);
  320. }
  321. }, _options.timeToIdle / 2);
  322. });
  323. }
  324. },
  325. _setupHidingControlsDuringGestures = function() {
  326. // Hide controls on vertical drag
  327. _listen('onVerticalDrag', function(now) {
  328. if(_controlsVisible && now < 0.95) {
  329. ui.hideControls();
  330. } else if(!_controlsVisible && now >= 0.95) {
  331. ui.showControls();
  332. }
  333. });
  334. // Hide controls when pinching to close
  335. var pinchControlsHidden;
  336. _listen('onPinchClose' , function(now) {
  337. if(_controlsVisible && now < 0.9) {
  338. ui.hideControls();
  339. pinchControlsHidden = true;
  340. } else if(pinchControlsHidden && !_controlsVisible && now > 0.9) {
  341. ui.showControls();
  342. }
  343. });
  344. _listen('zoomGestureEnded', function() {
  345. pinchControlsHidden = false;
  346. if(pinchControlsHidden && !_controlsVisible) {
  347. ui.showControls();
  348. }
  349. });
  350. };
  351. var _uiElements = [
  352. {
  353. name: 'caption',
  354. option: 'captionEl',
  355. onInit: function(el) {
  356. _captionContainer = el;
  357. }
  358. },
  359. {
  360. name: 'share-modal',
  361. option: 'shareEl',
  362. onInit: function(el) {
  363. _shareModal = el;
  364. },
  365. onTap: function() {
  366. _toggleShareModal();
  367. }
  368. },
  369. {
  370. name: 'button--share',
  371. option: 'shareEl',
  372. onInit: function(el) {
  373. _shareButton = el;
  374. },
  375. onTap: function() {
  376. _toggleShareModal();
  377. }
  378. },
  379. {
  380. name: 'button--zoom',
  381. option: 'zoomEl',
  382. onTap: pswp.toggleDesktopZoom
  383. },
  384. {
  385. name: 'counter',
  386. option: 'counterEl',
  387. onInit: function(el) {
  388. _indexIndicator = el;
  389. }
  390. },
  391. {
  392. name: 'button--close',
  393. option: 'closeEl',
  394. onTap: pswp.close
  395. },
  396. {
  397. name: 'button--arrow--left',
  398. option: 'arrowEl',
  399. onTap: pswp.prev
  400. },
  401. {
  402. name: 'button--arrow--right',
  403. option: 'arrowEl',
  404. onTap: pswp.next
  405. },
  406. {
  407. name: 'button--fs',
  408. option: 'fullscreenEl',
  409. onTap: function() {
  410. if(_fullscrenAPI.isFullscreen()) {
  411. _fullscrenAPI.exit();
  412. } else {
  413. _fullscrenAPI.enter();
  414. }
  415. }
  416. },
  417. {
  418. name: 'preloader',
  419. option: 'preloaderEl',
  420. onInit: function(el) {
  421. _loadingIndicator = el;
  422. }
  423. },
  424. {
  425. name: 'button--cover',
  426. option: 'coverEl',
  427. onTap: function(e) {
  428. pswp.shout('setCoverClick', pswp.currItem);
  429. }
  430. },
  431. {
  432. name: 'button--hide',
  433. option: 'hideEl',
  434. onTap: function(e) {
  435. pswp.shout('hideClick', pswp.currItem);
  436. }
  437. }
  438. ];
  439. var _setupUIElements = function() {
  440. var item,
  441. classAttr,
  442. uiElement;
  443. var loopThroughChildElements = function(sChildren) {
  444. if(!sChildren) {
  445. return;
  446. }
  447. var l = sChildren.length;
  448. for(var i = 0; i < l; i++) {
  449. item = sChildren[i];
  450. classAttr = item.className;
  451. for(var a = 0; a < _uiElements.length; a++) {
  452. uiElement = _uiElements[a];
  453. if(classAttr.indexOf('pswp__' + uiElement.name) > -1 ) {
  454. if( _options[uiElement.option] ) { // if element is not disabled from options
  455. framework.removeClass(item, 'pswp__element--disabled');
  456. if(uiElement.onInit) {
  457. uiElement.onInit(item);
  458. }
  459. //item.style.display = 'block';
  460. } else {
  461. framework.addClass(item, 'pswp__element--disabled');
  462. //item.style.display = 'none';
  463. }
  464. }
  465. }
  466. }
  467. };
  468. loopThroughChildElements(_controls.children);
  469. var topBar = framework.getChildByClass(_controls, 'pswp__top-bar');
  470. if(topBar) {
  471. loopThroughChildElements( topBar.children );
  472. }
  473. };
  474. ui.init = function() {
  475. // extend options
  476. framework.extend(pswp.options, _defaultUIOptions, true);
  477. // create local link for fast access
  478. _options = pswp.options;
  479. // find pswp__ui element
  480. _controls = framework.getChildByClass(pswp.scrollWrap, 'pswp__ui');
  481. // create local link
  482. _listen = pswp.listen;
  483. _setupHidingControlsDuringGestures();
  484. // update controls when slides change
  485. _listen('beforeChange', ui.update);
  486. // toggle zoom on double-tap
  487. _listen('doubleTap', function(point) {
  488. var initialZoomLevel = pswp.currItem.initialZoomLevel;
  489. if(pswp.getZoomLevel() !== initialZoomLevel) {
  490. pswp.zoomTo(initialZoomLevel, point, 333);
  491. } else {
  492. pswp.zoomTo(_options.getDoubleTapZoom(false, pswp.currItem), point, 333);
  493. }
  494. });
  495. // Allow text selection in caption
  496. _listen('preventDragEvent', function(e, isDown, preventObj) {
  497. var t = e.target || e.srcElement;
  498. if(
  499. t &&
  500. t.className && e.type.indexOf('mouse') > -1 &&
  501. ( t.className.indexOf('__caption') > 0 || (/(SMALL|STRONG|EM)/i).test(t.tagName) )
  502. ) {
  503. preventObj.prevent = false;
  504. }
  505. });
  506. // bind events for UI
  507. _listen('bindEvents', function() {
  508. framework.bind(_controls, 'pswpTap click', _onControlsTap);
  509. framework.bind(pswp.scrollWrap, 'pswpTap', ui.onGlobalTap);
  510. if(!pswp.likelyTouchDevice) {
  511. framework.bind(pswp.scrollWrap, 'mouseover', ui.onMouseOver);
  512. }
  513. });
  514. // unbind events for UI
  515. _listen('unbindEvents', function() {
  516. if(!_shareModalHidden) {
  517. _toggleShareModal();
  518. }
  519. if(_idleInterval) {
  520. clearInterval(_idleInterval);
  521. }
  522. framework.unbind(document, 'mouseout', _onMouseLeaveWindow);
  523. framework.unbind(document, 'mousemove', _onIdleMouseMove);
  524. framework.unbind(_controls, 'pswpTap click', _onControlsTap);
  525. framework.unbind(pswp.scrollWrap, 'pswpTap', ui.onGlobalTap);
  526. framework.unbind(pswp.scrollWrap, 'mouseover', ui.onMouseOver);
  527. if(_fullscrenAPI) {
  528. framework.unbind(document, _fullscrenAPI.eventK, ui.updateFullscreen);
  529. if(_fullscrenAPI.isFullscreen()) {
  530. _options.hideAnimationDuration = 0;
  531. _fullscrenAPI.exit();
  532. }
  533. _fullscrenAPI = null;
  534. }
  535. });
  536. // clean up things when gallery is destroyed
  537. _listen('destroy', function() {
  538. if(_options.captionEl) {
  539. if(_fakeCaptionContainer) {
  540. _controls.removeChild(_fakeCaptionContainer);
  541. }
  542. framework.removeClass(_captionContainer, 'pswp__caption--empty');
  543. }
  544. if(_shareModal) {
  545. _shareModal.children[0].onclick = null;
  546. }
  547. framework.removeClass(_controls, 'pswp__ui--over-close');
  548. framework.addClass( _controls, 'pswp__ui--hidden');
  549. ui.setIdle(false);
  550. });
  551. if(!_options.showAnimationDuration) {
  552. framework.removeClass( _controls, 'pswp__ui--hidden');
  553. }
  554. _listen('initialZoomIn', function() {
  555. if(_options.showAnimationDuration) {
  556. framework.removeClass( _controls, 'pswp__ui--hidden');
  557. }
  558. });
  559. _listen('initialZoomOut', function() {
  560. framework.addClass( _controls, 'pswp__ui--hidden');
  561. });
  562. _listen('parseVerticalMargin', _applyNavBarGaps);
  563. _setupUIElements();
  564. if(_options.shareEl && _shareButton && _shareModal) {
  565. _shareModalHidden = true;
  566. }
  567. _countNumItems();
  568. _setupIdle();
  569. _setupFullscreenAPI();
  570. _setupLoadingIndicator();
  571. };
  572. ui.setIdle = function(isIdle) {
  573. _isIdle = isIdle;
  574. _togglePswpClass(_controls, 'ui--idle', isIdle);
  575. };
  576. ui.update = function() {
  577. // Don't update UI if it's hidden
  578. if(_controlsVisible && pswp.currItem) {
  579. ui.updateIndexIndicator();
  580. if(_options.captionEl) {
  581. _options.addCaptionHTMLFn(pswp.currItem, _captionContainer);
  582. _togglePswpClass(_captionContainer, 'caption--empty', !pswp.currItem.title);
  583. }
  584. _overlayUIUpdated = true;
  585. } else {
  586. _overlayUIUpdated = false;
  587. }
  588. if(!_shareModalHidden) {
  589. _toggleShareModal();
  590. }
  591. _countNumItems();
  592. };
  593. ui.updateFullscreen = function(e) {
  594. if(e) {
  595. // some browsers change window scroll position during the fullscreen
  596. // so PhotoSwipe updates it just in case
  597. setTimeout(function() {
  598. pswp.setScrollOffset( 0, framework.getScrollY() );
  599. }, 50);
  600. }
  601. // toogle pswp--fs class on root element
  602. framework[ (_fullscrenAPI.isFullscreen() ? 'add' : 'remove') + 'Class' ](pswp.template, 'pswp--fs');
  603. };
  604. ui.updateIndexIndicator = function() {
  605. if(_options.counterEl) {
  606. _indexIndicator.innerHTML = (pswp.getCurrentIndex()+1) +
  607. _options.indexIndicatorSep +
  608. _options.getNumItemsFn();
  609. }
  610. };
  611. ui.onGlobalTap = function(e) {
  612. e = e || window.event;
  613. var target = e.target || e.srcElement;
  614. if(_blockControlsTap) {
  615. return;
  616. }
  617. if(e.detail && e.detail.pointerType === 'mouse') {
  618. // close gallery if clicked outside of the image
  619. if(_hasCloseClass(target)) {
  620. pswp.close();
  621. return;
  622. }
  623. if(framework.hasClass(target, 'pswp__img')) {
  624. if(pswp.getZoomLevel() === 1 && pswp.getZoomLevel() <= pswp.currItem.fitRatio) {
  625. if(_options.clickToCloseNonZoomable) {
  626. pswp.close();
  627. }
  628. } else {
  629. pswp.toggleDesktopZoom(e.detail.releasePoint);
  630. }
  631. }
  632. } else {
  633. // tap anywhere (except buttons) to toggle visibility of controls
  634. if(_options.tapToToggleControls) {
  635. if(_controlsVisible) {
  636. ui.hideControls();
  637. } else {
  638. ui.showControls();
  639. }
  640. }
  641. // tap to close gallery
  642. if(_options.tapToClose && (framework.hasClass(target, 'pswp__img') || _hasCloseClass(target)) ) {
  643. pswp.close();
  644. return;
  645. }
  646. }
  647. };
  648. ui.onMouseOver = function(e) {
  649. e = e || window.event;
  650. var target = e.target || e.srcElement;
  651. // add class when mouse is over an element that should close the gallery
  652. _togglePswpClass(_controls, 'ui--over-close', _hasCloseClass(target));
  653. };
  654. ui.hideControls = function() {
  655. framework.addClass(_controls,'pswp__ui--hidden');
  656. _controlsVisible = false;
  657. };
  658. ui.showControls = function() {
  659. _controlsVisible = true;
  660. if(!_overlayUIUpdated) {
  661. ui.update();
  662. }
  663. framework.removeClass(_controls,'pswp__ui--hidden');
  664. };
  665. ui.supportsFullscreen = function() {
  666. var d = document;
  667. return !!(d.exitFullscreen || d.mozCancelFullScreen || d.webkitExitFullscreen || d.msExitFullscreen);
  668. };
  669. ui.getFullscreenAPI = function() {
  670. var dE = document.documentElement,
  671. api,
  672. tF = 'fullscreenchange';
  673. if (dE.requestFullscreen) {
  674. api = {
  675. enterK: 'requestFullscreen',
  676. exitK: 'exitFullscreen',
  677. elementK: 'fullscreenElement',
  678. eventK: tF
  679. };
  680. } else if(dE.mozRequestFullScreen ) {
  681. api = {
  682. enterK: 'mozRequestFullScreen',
  683. exitK: 'mozCancelFullScreen',
  684. elementK: 'mozFullScreenElement',
  685. eventK: 'moz' + tF
  686. };
  687. } else if(dE.webkitRequestFullscreen) {
  688. api = {
  689. enterK: 'webkitRequestFullscreen',
  690. exitK: 'webkitExitFullscreen',
  691. elementK: 'webkitFullscreenElement',
  692. eventK: 'webkit' + tF
  693. };
  694. } else if(dE.msRequestFullscreen) {
  695. api = {
  696. enterK: 'msRequestFullscreen',
  697. exitK: 'msExitFullscreen',
  698. elementK: 'msFullscreenElement',
  699. eventK: 'MSFullscreenChange'
  700. };
  701. }
  702. if(api) {
  703. api.enter = function() {
  704. // disable close-on-scroll in fullscreen
  705. _initalCloseOnScrollValue = _options.closeOnScroll;
  706. _options.closeOnScroll = false;
  707. if(this.enterK === 'webkitRequestFullscreen') {
  708. pswp.template[this.enterK]( Element.ALLOW_KEYBOARD_INPUT );
  709. } else {
  710. return pswp.template[this.enterK]();
  711. }
  712. };
  713. api.exit = function() {
  714. _options.closeOnScroll = _initalCloseOnScrollValue;
  715. return document[this.exitK]();
  716. };
  717. api.isFullscreen = function() { return document[this.elementK]; };
  718. }
  719. return api;
  720. };
  721. };
  722. return PhotoSwipeUI_Default;
  723. });