1(function() { 2 var SOURCES = window.TEXT_VARIABLES.sources; 3 window.Lazyload.js(SOURCES.jquery, function() { 4 var $body = $('body'), $window = $(window); 5 var $pageRoot = $('.js-page-root'), $pageMain = $('.js-page-main'); 6 var activeCount = 0; 7 function modal(options) { 8 var $root = this, visible, onChange, hideWhenWindowScroll = false; 9 var scrollTop; 10 function setOptions(options) { 11 var _options = options || {}; 12 visible = _options.initialVisible === undefined ? false : show; 13 onChange = _options.onChange; 14 hideWhenWindowScroll = _options.hideWhenWindowScroll; 15 } 16 function init() { 17 setState(visible); 18 } 19 function setState(isShow) { 20 if (isShow === visible) { 21 return; 22 } 23 visible = isShow; 24 if (visible) { 25 activeCount++; 26 scrollTop = $(window).scrollTop() || $pageMain.scrollTop(); 27 $root.addClass('modal--show'); 28 $pageMain.scrollTop(scrollTop); 29 activeCount === 1 && ($pageRoot.addClass('show-modal'), $body.addClass('of-hidden')); 30 hideWhenWindowScroll && window.hasEvent('touchstart') && $window.on('scroll', hide); 31 $window.on('keyup', handleKeyup); 32 } else { 33 activeCount > 0 && activeCount--; 34 $root.removeClass('modal--show'); 35 $window.scrollTop(scrollTop); 36 activeCount === 0 && ($pageRoot.removeClass('show-modal'), $body.removeClass('of-hidden')); 37 hideWhenWindowScroll && window.hasEvent('touchstart') && $window.off('scroll', hide); 38 $window.off('keyup', handleKeyup); 39 } 40 onChange && onChange(visible); 41 } 42 function show() { 43 setState(true); 44 } 45 function hide() { 46 setState(false); 47 } 48 function handleKeyup(e) { 49 // Char Code: 27 ESC 50 if (e.which === 27) { 51 hide(); 52 } 53 } 54 setOptions(options); 55 init(); 56 return { 57 show: show, 58 hide: hide, 59 $el: $root 60 }; 61 } 62 $.fn.modal = modal; 63 }); 64})();