Skip to content

Instantly share code, notes, and snippets.

@benrasmusen
Created September 12, 2019 20:12
Show Gist options
  • Save benrasmusen/516fb94fa5816a2ce4f239948eb425eb to your computer and use it in GitHub Desktop.
Save benrasmusen/516fb94fa5816a2ce4f239948eb425eb to your computer and use it in GitHub Desktop.
/* ========================================================================
* DOM-based Routing
* Based on http://goo.gl/EUTi53 by Paul Irish
*
* Only fires on body classes that match. If a body class contains a dash,
* replace the dash with an underscore when adding it to the object below.
*
* .noConflict()
* The routing is enclosed within an anonymous function so that you can
* always reference jQuery with $, even when in .noConflict() mode.
* ======================================================================== */
(function($) {
// Use this variable to set up the common and page specific functions. If you
// rename this variable, you will also need to rename the namespace below.
var Sage = {
// All pages
'common': {
init: function() {
Sage.header.init();
Sage.sliders.init();
Sage.resource_taxonomy_navigation.init();
if (navigator.appVersion.indexOf("Win")!==-1) {
$('body').addClass('windows');
}
if (navigator.appName === 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie === 1)) {
$('body').addClass('windows-ie');
}
// Trigger wistia videos with all links
// $('.video-panel a[href="#"]').click(function(e){
// e.preventDefault();
// $('.wistia_click_to_play a', $(this).parents('.video-panel'))[0].click();
// });
if (window.location.hash) {
console.log($(window.location.hash).hasClass('modal'));
if ($(window.location.hash).length > 0 && $(window.location.hash).hasClass('modal')) {
$(window.location.hash).modal('show');
}
}
},
finalize: function() {
// JavaScript to be fired on all pages, after page specific JS is fired
}
},
'resource_taxonomy_navigation': {
init: function() {
if ($('.taxonomy-navigation').length > 0) {
$('.taxonomy-label').click(function(e) {
e.preventDefault();
$($(this).attr('href')).slideToggle();
$(this).toggleClass('open');
});
}
}
},
'sliders': {
init: function() {
var $sliderContainers = $('.section-slider .slider-section, .section-team_members_slider .slider-section, .section-featured_slider .slider-section, .featured-resources-wrapper .slider-section');
if ($sliderContainers.length > 0) {
$sliderContainers.each(function(){
var $sliderContainer = $(this),
$sliderRow = $sliderContainer.find('.slider-row'),
$sliderLinksRow = $('.tabs-links-row'),
$sliderLinks = $('.tabs-links');
if ($sliderRow.length > 0) {
var visibleThumbs = 8,
pager = false,
auto = false,
infiniteLoop = false,
thumb = $sliderLinksRow.find($sliderLinks),
sliderPause = 4000;
if ($sliderContainer.data('slider-pager')) {
pager = $sliderContainer.data('slider-pager');
}
if ($sliderContainer.data('auto-advance')) {
auto = $sliderContainer.data('auto-advance');
}
if ($sliderContainer.data('infinite-loop')) {
infiniteLoop = $sliderContainer.data('infinite-loop');
}
if ($sliderContainer.data('slider-pause')) {
sliderPause = $sliderContainer.data('slider-pause');
}
var Slider = $sliderContainer.bxSlider({
controls: true,
pager: pager,
easing: 'easeInOutQuint',
infiniteLoop: infiniteLoop,
speed: 500,
nextText: '>',
prevText: '<',
auto: auto,
pause: sliderPause,
onSlideAfter: function ($slideElement, oldIndex, newIndex) {
thumb.removeClass('selected');
if ($sliderLinksRow.find('[data-slide-number="'+ newIndex +'"]').length > 0) {
$sliderLinksRow.find('[data-slide-number="'+ newIndex +'"]').addClass('selected');
} else if ($sliderLinksRow.find('[data-slide-number]').length === 0) {
thumb.eq(newIndex).addClass('selected');
}
slideTo = Sage.sliders.slideThumbs(newIndex + 1, visibleThumbs);
}
});
thumb.click(function (e) {
var slideNumber = $(this).closest($sliderLinks).index();
if ($(this).data('slide-number')) {
slideNumber = $(this).data('slide-number');
}
Slider.goToSlide(slideNumber);
e.preventDefault();
});
$sliderLinksRow.find($sliderLinks).click(function () {
$sliderLinksRow.find($sliderLinks).removeClass('selected');
$(this).addClass('selected');
});
}
});
}
},
slideThumbs: function(currentSlideNumber, visibleThumbs) {
return Math.floor(currentSlideNumber / visibleThumbs);
}
},
'header': {
init: function() {
var $mobileMenuLink = $('.header-mobile-menu'),
$menuCloseLink = $('.header-mobile-menu-close-link'),
$mobileNav = $('.header-mobile-dropdown'),
$header = $('.header-section');
$mobileMenuLink.on('click', function () {
$mobileNav.addClass('active');
});
$menuCloseLink.on('click', function () {
$mobileNav.removeClass('active');
});
$(window).scroll(function () {
var $scrollTop = $(window).scrollTop();
if($scrollTop > 100) {
$header.addClass('fixed');
}
else {
$header.removeClass('fixed');
}
});
$('#header-search-submit').click(function(e){
var headerSearchForm = $('.header-search-form form');
if (!headerSearchForm.hasClass('form-open')) {
e.preventDefault();
headerSearchForm.addClass('form-open');
}
});
}
},
// Home page
'home': {
init: function() {
if($(window).width() > 767) {
var $button = $('.go-down-link'),
$subHeader = $('.home-subheader-section'),
$subHeaderHeight = $subHeader.height(),
$contentBoxSpacing = 150,
$contentBox = $('.home-subheader-content-box'),
$contentBoxTop = $contentBox.offset().top - $contentBoxSpacing,
$eSourceImageBlock = $('.home-subheader-esource-image-holder'),
$eSourceImageBlockSpacing = 400,
$eSourceImageBlockTop = $eSourceImageBlock.offset().top - $eSourceImageBlockSpacing,
$gdgImageBlock = $('.home-subheader-gdg-image-holder'),
$gdgImageBlockTop = $gdgImageBlock.offset().top,
$lastScrollTop = 0,
$currentOpacity = 1,
$subHeaderContainer = $('.home-subheader-content-container'),
$supportSectionHeight = $('.home-support-section').height(),
$subHeaderFixedCotent = $('.home-subheader-content-container'),
$supportFixedContent = $('.support-section-container'),
$supportSection = $('.home-support-section'),
$supportSectionTop = $supportSection.offset().top,
$supportContainer = $('.support-section-container'),
$supportContainerTop = $supportContainer.offset().top,
$supportContainerWidth = $supportContainer.width(),
$supportContainerPaddingLeft = 15,
$saveSection = $('.home-save-section'),
$saveSectionHeight = $saveSection.height(),
$saveSectionTop = $saveSection.offset().top;
$button.on('click', function () {
$('html, body').animate({
scrollTop: $('[data-scroll = ' + scrollElem + ']').offset().top + 2
});
scrollElem++;
});
$(window).scroll(function () {
var $scrollTop = $(this).scrollTop(),
$docWidth = $(window).width();
if($scrollTop > $lastScrollTop) {
$contentBox.addClass('fixed').css({
top: $contentBoxTop
});
$gdgImageBlock.addClass('fixed').css({
top: $gdgImageBlockTop
});
$eSourceImageBlock.addClass('fixed').css({
top: $eSourceImageBlockTop
});
}
$lastScrollTop = $scrollTop;
if($scrollTop > $supportSectionTop) {
$supportSection.addClass('animate');
scrollElem = $supportSection.data("scroll") + 1;
} else {
scrollElem = 1;
}
if($('body').hasClass('windows-ie')) {
$('.home-support-section.animate .home-support-chat-image').animate({
top: 0,
opacity: 1
}, 500);
$('.home-support-section.animate .home-support-bubble-image').animate({
top: 0,
opacity: 1
}, 500);
}
if($scrollTop > $supportSectionTop) {
$supportContainer.addClass('fixed').css({
top: $supportContainerTop - $supportSectionTop,
left: ($docWidth - $supportContainerWidth) / 2 - $supportContainerPaddingLeft
});
} else {
$supportContainer.removeClass('fixed').removeAttr('style');
}
if($scrollTop > $saveSectionTop) {
$saveSection.addClass('animate');
scrollElem = $saveSection.data("scroll") + 1;
if($('body').hasClass('windows-ie')) {
$('.home-save-section.animate .home-save-video-block').animate({
bottom: 0,
opacity: 1
}, 500);
}
}
if($scrollTop > ($saveSectionHeight + $saveSectionTop)) {
$button.hide();
} else {
$button.show();
}
if($scrollTop > ($subHeaderHeight + $supportSectionHeight)) {
$subHeaderFixedCotent.css('opacity', '0');
$supportFixedContent.css('opacity', '0');
} else {
$subHeaderFixedCotent.css('opacity', '1');
$supportFixedContent.css('opacity', '1');
}
});
}
$('.home-testimonials-slider-section').bxSlider();
if($('body').hasClass('windows-ie')) {
$('.home-subheader-section .home-subheader-esource-image-holder').animate({
bottom: - 150,
opacity: 1
}, 500);
$('.home-subheader-section .home-subheader-gdg-image-holder').delay(500).animate({
bottom: - 20,
opacity: 1
}, 500);
$('.home-subheader-section .home-subheader-content-box').animate({
bottom: 0,
opacity: 1
}, 500);
}
}
},
// About us page, note the change from about-us to about_us.
'about': {
init: function() {
$('.slide-wrapper').each(function(){
var maxHeight = 0;
$('.team-slide .inner', $(this)).each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
$('.team-slide .inner', $(this)).height(maxHeight);
});
$('#team-member-modal').on('show.bs.modal', function(e) {
var parentEl = $(e.relatedTarget).parents('.team-slide');
$('img', this).attr('src', $('img', parentEl).attr('src'));
$('.team-member-name', this).html($('.team-member-name', parentEl).html());
$('.team-member-title', this).html($('.team-member-title', parentEl).html());
$('.team-member-text', this).html($('.team-member-text', parentEl).html());
});
}
},
'page_resource_center': {
init: function() {
$('select').selectric();
}
}
};
// The routing fires all common scripts, followed by the page specific scripts.
// Add additional events for more control over timing e.g. a finalize event
var UTIL = {
fire: function(func, funcname, args) {
var fire;
var namespace = Sage;
funcname = (funcname === undefined) ? 'init' : funcname;
fire = func !== '';
fire = fire && namespace[func];
fire = fire && typeof namespace[func][funcname] === 'function';
if (fire) {
namespace[func][funcname](args);
}
},
loadEvents: function() {
// Fire common init JS
UTIL.fire('common');
// Fire page-specific init JS, and then finalize JS
$.each(document.body.className.replace(/-/g, '_').split(/\s+/), function(i, classnm) {
UTIL.fire(classnm);
UTIL.fire(classnm, 'finalize');
});
// Fire common finalize JS
UTIL.fire('common', 'finalize');
}
};
// Load Events
$(document).ready(UTIL.loadEvents);
})(jQuery); // Fully reference jQuery after this point.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment