/* global NexT, CONFIG */ NexT.utils = NexT.$u = { /** * Wrap images with fancybox support. */ wrapImageWithFancyBox: function() { $('.content img') .not(':hidden') .each(function() { var $image = $(this); var imageTitle = $image.attr('title') || $image.attr('alt'); var $imageWrapLink = $image.parent('a'); if ($imageWrapLink.length < 1) { var imageLink = $image.attr('data-original') || $image.attr('src'); $imageWrapLink = $image.wrap('').parent('a'); if ($image.is('.post-gallery img')) { $imageWrapLink.addClass('post-gallery-img'); $imageWrapLink.attr('data-fancybox', 'gallery').attr('rel', 'gallery'); } else if ($image.is('.group-picture img')) { $imageWrapLink.attr('data-fancybox', 'group').attr('rel', 'group'); } else { $imageWrapLink.attr('data-fancybox', 'default').attr('rel', 'default'); } } if (imageTitle) { $imageWrapLink.append('
' + imageTitle + '
'); // Make sure img title tag will show correctly in fancybox $imageWrapLink.attr('title', imageTitle).attr('data-caption', imageTitle); } }); $('.fancybox').fancybox({ loop: true, helpers: { overlay: { locked: false } } }); }, lazyLoadPostsImages: function() { $('#posts').find('img').lazyload({ //placeholder: '/images/loading.gif', effect : 'fadeIn', threshold: 0 }); }, /** * Tabs tag listener (without twitter bootstrap). */ registerTabsTag: function() { var tNav = '.tabs ul.nav-tabs '; // Binding `nav-tabs` & `tab-content` by real time permalink changing. $(function() { $(window).bind('hashchange', function() { var tHash = location.hash; if (tHash !== '' && !tHash.match(/%\S{2}/)) { $(tNav + 'li:has(a[href="' + tHash + '"])').addClass('active').siblings().removeClass('active'); $(tHash).addClass('active').siblings().removeClass('active'); } }).trigger('hashchange'); }); $(tNav + '.tab').on('click', function(href) { href.preventDefault(); // Prevent selected tab to select again. if (!$(this).hasClass('active')) { // Add & Remove active class on `nav-tabs` & `tab-content`. $(this).addClass('active').siblings().removeClass('active'); var tActive = $(this).find('a').attr('href'); $(tActive).addClass('active').siblings().removeClass('active'); // Clear location hash in browser if #permalink exists. if (location.hash !== '') { history.pushState('', document.title, window.location.pathname + window.location.search); } } }); }, registerESCKeyEvent: function() { $(document).on('keyup', function(event) { var shouldDismissSearchPopup = event.which === 27 && $('.search-popup').is(':visible'); if (shouldDismissSearchPopup) { $('.search-popup').hide(); $('.search-popup-overlay').remove(); $('body').css('overflow', ''); } }); }, registerBackToTop: function() { var THRESHOLD = 50; var $top = $('.back-to-top'); function initBackToTop() { $top.toggleClass('back-to-top-on', window.pageYOffset > THRESHOLD); var scrollTop = $(window).scrollTop(); var contentVisibilityHeight = NexT.utils.getContentVisibilityHeight(); var scrollPercent = scrollTop / contentVisibilityHeight; var scrollPercentRounded = Math.round(scrollPercent * 100); var scrollPercentMaxed = scrollPercentRounded > 100 ? 100 : scrollPercentRounded; $('#scrollpercent>span').html(scrollPercentMaxed); } // For init back to top in sidebar if page was scrolled after page refresh. $(window).on('load', function() { initBackToTop(); }); $(window).on('scroll', function() { initBackToTop(); }); $top.on('click', function() { $.isFunction($('html').velocity) ? $('body').velocity('scroll') : $('html, body').animate({ scrollTop: 0 }); }); }, /** * Transform embedded video to support responsive layout. * @see http://toddmotto.com/fluid-and-responsive-youtube-and-vimeo-videos-with-fluidvids-js/ */ embeddedVideoTransformer: function() { var $iframes = $('iframe'); // Supported Players. Extend this if you need more players. var SUPPORTED_PLAYERS = [ 'www.youtube.com', 'player.vimeo.com', 'player.youku.com', 'music.163.com', 'www.tudou.com' ]; var pattern = new RegExp(SUPPORTED_PLAYERS.join('|')); function getDimension($element) { return { width : $element.width(), height: $element.height() }; } function getAspectRadio(width, height) { return height / width * 100; } $iframes.each(function() { var iframe = this; var $iframe = $(this); var oldDimension = getDimension($iframe); var newDimension; if (this.src.search(pattern) > 0) { // Calculate the video ratio based on the iframe's w/h dimensions var videoRatio = getAspectRadio(oldDimension.width, oldDimension.height); // Replace the iframe's dimensions and position the iframe absolute // This is the trick to emulate the video ratio $iframe.width('100%').height('100%') .css({ position: 'absolute', top : '0', left : '0' }); // Wrap the iframe in a new