diff options
Diffstat (limited to '_includes/scripts/components')
-rw-r--r-- | _includes/scripts/components/lightbox.js | 49 | ||||
-rw-r--r-- | _includes/scripts/components/search.js | 86 | ||||
-rw-r--r-- | _includes/scripts/components/sidebar.js | 30 |
3 files changed, 165 insertions, 0 deletions
diff --git a/_includes/scripts/components/lightbox.js b/_includes/scripts/components/lightbox.js new file mode 100644 index 0000000..51689c2 --- /dev/null +++ b/_includes/scripts/components/lightbox.js @@ -0,0 +1,49 @@ +{%- include scripts/utils/imagesLoad.js -%} +(function () { + var SOURCES = window.TEXT_VARIABLES.sources; + window.Lazyload.js(SOURCES.jquery, function() { + var $pageGalleryModal = $('.js-page-gallery-modal'); + var $images = $('.page__content').find('img:not(.lightbox-ignore)'); + window.imagesLoad($images).then(function() { + /* global Gallery */ + var pageGalleryModal = $pageGalleryModal.modal({ onChange: handleModalChange }); + var gallery = null; + var modalVisible = false; + var i, items = [], image, item; + if($images && $images.length > 0) { + for (i = 0; i < $images.length; i++) { + image = $images.eq(i); + if (image.get(0).naturalWidth > 800) { + items.push({ src: image.attr('src'), w: image.get(0).naturalWidth, h: image.get(0).naturalHeight, $el: image}); + } + } + } + + if(items.length > 0) { + gallery = new Gallery('.gallery', items); + gallery.setOptions({ disabled: !modalVisible }); + gallery.init(); + for (i = 0; i < items.length; i++) { + item = items[i]; + item.$el && (item.$el.addClass('popup-image'), item.$el.on('click', (function() { + var index = i; + return function() { + pageGalleryModal.show(); + gallery.setOptions({ initialSlide: index }); + gallery.refresh(true, { animation: false }); + }; + })())); + } + } + + function handleModalChange(visible) { + modalVisible = visible; + gallery && gallery.setOptions({ disabled: !modalVisible }); + } + + $pageGalleryModal.on('click', function() { + pageGalleryModal.hide(); + }); + }); + }); +})(); diff --git a/_includes/scripts/components/search.js b/_includes/scripts/components/search.js new file mode 100644 index 0000000..e1c8c27 --- /dev/null +++ b/_includes/scripts/components/search.js @@ -0,0 +1,86 @@ + +(function () { + var SOURCES = window.TEXT_VARIABLES.sources; + window.Lazyload.js(SOURCES.jquery, function() { + // search panel + var search = (window.search || (window.search = {})); + var useDefaultSearchBox = window.useDefaultSearchBox === undefined ? + true : window.useDefaultSearchBox ; + + var $searchModal = $('.js-page-search-modal'); + var $searchToggle = $('.js-search-toggle'); + var searchModal = $searchModal.modal({ onChange: handleModalChange, hideWhenWindowScroll: true }); + var modalVisible = false; + search.searchModal = searchModal; + + var $searchBox = null; + var $searchInput = null; + var $searchClear = null; + + function getModalVisible() { + return modalVisible; + } + search.getModalVisible = getModalVisible; + + function handleModalChange(visible) { + modalVisible = visible; + if (visible) { + search.onShow && search.onShow(); + useDefaultSearchBox && $searchInput[0] && $searchInput[0].focus(); + } else { + search.onShow && search.onHide(); + useDefaultSearchBox && $searchInput[0] && $searchInput[0].blur(); + setTimeout(function() { + useDefaultSearchBox && ($searchInput.val(''), $searchBox.removeClass('not-empty')); + search.clear && search.clear(); + window.pageAsideAffix && window.pageAsideAffix.refresh(); + }, 400); + } + } + + $searchToggle.on('click', function() { + modalVisible ? searchModal.hide() : searchModal.show(); + }); + // Char Code: 83 S, 191 / + $(window).on('keyup', function(e) { + if (!modalVisible && !window.isFormElement(e.target || e.srcElement) && (e.which === 83 || e.which === 191)) { + modalVisible || searchModal.show(); + } + }); + + if (useDefaultSearchBox) { + $searchBox = $('.js-search-box'); + $searchInput = $searchBox.children('input'); + $searchClear = $searchBox.children('.js-icon-clear'); + search.getSearchInput = function() { + return $searchInput.get(0); + }; + search.getVal = function() { + return $searchInput.val(); + }; + search.setVal = function(val) { + $searchInput.val(val); + }; + + $searchInput.on('focus', function() { + $(this).addClass('focus'); + }); + $searchInput.on('blur', function() { + $(this).removeClass('focus'); + }); + $searchInput.on('input', window.throttle(function() { + var val = $(this).val(); + if (val === '' || typeof val !== 'string') { + search.clear && search.clear(); + } else { + $searchBox.addClass('not-empty'); + search.onInputNotEmpty && search.onInputNotEmpty(val); + } + }, 400)); + $searchClear.on('click', function() { + $searchInput.val(''); $searchBox.removeClass('not-empty'); + search.clear && search.clear(); + }); + } + }); +})(); diff --git a/_includes/scripts/components/sidebar.js b/_includes/scripts/components/sidebar.js new file mode 100644 index 0000000..92c5591 --- /dev/null +++ b/_includes/scripts/components/sidebar.js @@ -0,0 +1,30 @@ +(function() { + var SOURCES = window.TEXT_VARIABLES.sources; + + window.Lazyload.js(SOURCES.jquery, function() { + var $pageMask = $('.js-page-mask'); + var $pageRoot = $('.js-page-root'); + var $sidebarShow = $('.js-sidebar-show'); + var $sidebarHide = $('.js-sidebar-hide'); + + function freeze(e) { + if (e.target === $pageMask[0]) { + e.preventDefault(); + } + } + function stopBodyScrolling(bool) { + if (bool === true) { + window.addEventListener('touchmove', freeze, { passive: false }); + } else { + window.removeEventListener('touchmove', freeze, { passive: false }); + } + } + + $sidebarShow.on('click', function() { + stopBodyScrolling(true); $pageRoot.addClass('show-sidebar'); + }); + $sidebarHide.on('click', function() { + stopBodyScrolling(false); $pageRoot.removeClass('show-sidebar'); + }); + }); +})(); |