dimension/static/js/main.js

415 lines
10 KiB
JavaScript
Raw Normal View History

2017-01-17 13:31:18 -05:00
/*
Dimension by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
2017-01-17 13:31:18 -05:00
*/
(function($) {
skel.breakpoints({
xlarge: '(max-width: 1680px)',
large: '(max-width: 1280px)',
medium: '(max-width: 980px)',
small: '(max-width: 736px)',
xsmall: '(max-width: 480px)',
xxsmall: '(max-width: 360px)'
});
2017-01-17 13:31:18 -05:00
$(function() {
2017-01-17 13:31:18 -05:00
var $window = $(window),
$body = $('body'),
$wrapper = $('#wrapper'),
$header = $('#header'),
$footer = $('#footer'),
$languages = $('#languages'),
$main = $('#main'),
$main_articles = $main.children('article');
2017-01-17 13:31:18 -05:00
// Disable animations/transitions until the page has loaded.
$body.addClass('is-loading');
2017-01-17 13:31:18 -05:00
$window.on('load', function() {
window.setTimeout(function() {
$body.removeClass('is-loading');
}, 100);
});
2017-01-17 13:31:18 -05:00
// Fix: Placeholder polyfill.
$('form').placeholder();
2017-01-17 13:31:18 -05:00
// Fix: Flexbox min-height bug on IE.
if (skel.vars.IEVersion < 12) {
2017-01-17 13:31:18 -05:00
var flexboxFixTimeoutId;
2017-01-17 13:31:18 -05:00
$window.on('resize.flexbox-fix', function() {
2017-01-17 13:31:18 -05:00
clearTimeout(flexboxFixTimeoutId);
2017-01-17 13:31:18 -05:00
flexboxFixTimeoutId = setTimeout(function() {
2017-01-17 13:31:18 -05:00
if ($wrapper.prop('scrollHeight') > $window.height())
$wrapper.css('height', 'auto');
else
$wrapper.css('height', '100vh');
2017-01-17 13:31:18 -05:00
}, 250);
2017-01-17 13:31:18 -05:00
}).triggerHandler('resize.flexbox-fix');
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Nav.
var $nav = $header.children('nav'),
$nav_li = $nav.find('li');
2017-01-17 13:31:18 -05:00
// Add "middle" alignment classes if we're dealing with an even number of items.
if ($nav_li.length % 2 == 0) {
2017-01-17 13:31:18 -05:00
$nav.addClass('use-middle');
$nav_li.eq( ($nav_li.length / 2) ).addClass('is-middle');
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Main.
var delay = 325,
locked = false;
2017-01-17 13:31:18 -05:00
// Methods.
$main._show = function(id, initial) {
2017-01-17 13:31:18 -05:00
var $article = $main_articles.filter('#' + id);
2017-01-17 13:31:18 -05:00
// No such article? Bail.
if ($article.length == 0)
return;
2017-01-17 13:31:18 -05:00
// Handle lock.
2017-01-17 13:31:18 -05:00
// Already locked? Speed through "show" steps w/o delays.
if (locked || (typeof initial != 'undefined' && initial === true)) {
2017-01-17 13:31:18 -05:00
// Mark as switching.
$body.addClass('is-switching');
2017-01-17 13:31:18 -05:00
// Mark as visible.
$body.addClass('is-article-visible');
2017-01-17 13:31:18 -05:00
// Deactivate all articles (just in case one's already active).
$main_articles.removeClass('active');
2017-01-17 13:31:18 -05:00
// Hide header, footer, languages.
$header.hide();
$footer.hide();
$languages.hide();
2017-01-17 13:31:18 -05:00
// Show main, article.
$main.show();
$article.show();
2017-01-17 13:31:18 -05:00
// Activate article.
$article.addClass('active');
2017-01-17 13:31:18 -05:00
// Unlock.
locked = false;
2017-01-17 13:31:18 -05:00
// Unmark as switching.
setTimeout(function() {
$body.removeClass('is-switching');
}, (initial ? 1000 : 0));
2017-01-17 13:31:18 -05:00
return;
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Lock.
locked = true;
2017-01-17 13:31:18 -05:00
// Article already visible? Just swap articles.
if ($body.hasClass('is-article-visible')) {
2017-01-17 13:31:18 -05:00
// Deactivate current article.
var $currentArticle = $main_articles.filter('.active');
2017-01-17 13:31:18 -05:00
$currentArticle.removeClass('active');
2017-01-17 13:31:18 -05:00
// Show article.
setTimeout(function() {
2017-01-17 13:31:18 -05:00
// Hide current article.
$currentArticle.hide();
2017-01-17 13:31:18 -05:00
// Show article.
$article.show();
2017-01-17 13:31:18 -05:00
// Activate article.
setTimeout(function() {
2017-01-17 13:31:18 -05:00
$article.addClass('active');
2017-01-17 13:31:18 -05:00
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
2017-01-17 13:31:18 -05:00
// Unlock.
setTimeout(function() {
locked = false;
}, delay);
2017-01-17 13:31:18 -05:00
}, 25);
2017-01-17 13:31:18 -05:00
}, delay);
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Otherwise, handle as normal.
else {
2017-01-17 13:31:18 -05:00
// Mark as visible.
$body
.addClass('is-article-visible');
2017-01-17 13:31:18 -05:00
// Show article.
setTimeout(function() {
2017-01-17 13:31:18 -05:00
// Hide header, footer, languages.
$header.hide();
$footer.hide();
$languages.hide();
2017-01-17 13:31:18 -05:00
// Show main, article.
$main.show();
$article.show();
2017-01-17 13:31:18 -05:00
// Activate article.
setTimeout(function() {
2017-01-17 13:31:18 -05:00
$article.addClass('active');
2017-01-17 13:31:18 -05:00
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
2017-01-17 13:31:18 -05:00
// Unlock.
setTimeout(function() {
locked = false;
}, delay);
2017-01-17 13:31:18 -05:00
}, 25);
2017-01-17 13:31:18 -05:00
}, delay);
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
};
2017-01-17 13:31:18 -05:00
$main._hide = function(addState) {
2017-01-17 13:31:18 -05:00
var $article = $main_articles.filter('.active');
2017-01-17 13:31:18 -05:00
// Article not visible? Bail.
if (!$body.hasClass('is-article-visible'))
return;
2017-01-17 13:31:18 -05:00
// Add state?
if (typeof addState != 'undefined'
&& addState === true)
history.pushState(null, null, '#');
2017-01-17 13:31:18 -05:00
// Handle lock.
2017-01-17 13:31:18 -05:00
// Already locked? Speed through "hide" steps w/o delays.
if (locked) {
2017-01-17 13:31:18 -05:00
// Mark as switching.
$body.addClass('is-switching');
2017-01-17 13:31:18 -05:00
// Deactivate article.
$article.removeClass('active');
2017-01-17 13:31:18 -05:00
// Hide article, main.
$article.hide();
$main.hide();
2017-01-17 13:31:18 -05:00
// Show footer, header.
$footer.show();
$header.show();
$languages.show();
2017-01-17 13:31:18 -05:00
// Unmark as visible.
$body.removeClass('is-article-visible');
2017-01-17 13:31:18 -05:00
// Unlock.
locked = false;
2017-01-17 13:31:18 -05:00
// Unmark as switching.
$body.removeClass('is-switching');
2017-01-17 13:31:18 -05:00
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
2017-01-17 13:31:18 -05:00
return;
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Lock.
locked = true;
2017-01-17 13:31:18 -05:00
// Deactivate article.
$article.removeClass('active');
2017-01-17 13:31:18 -05:00
// Hide article.
setTimeout(function() {
2017-01-17 13:31:18 -05:00
// Hide article, main.
$article.hide();
$main.hide();
2017-01-17 13:31:18 -05:00
// Show footer, header.
$footer.show();
$header.show();
$languages.show();
2017-01-17 13:31:18 -05:00
// Unmark as visible.
setTimeout(function() {
2017-01-17 13:31:18 -05:00
$body.removeClass('is-article-visible');
2017-01-17 13:31:18 -05:00
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
2017-01-17 13:31:18 -05:00
// Unlock.
setTimeout(function() {
locked = false;
}, delay);
2017-01-17 13:31:18 -05:00
}, 25);
2017-01-17 13:31:18 -05:00
}, delay);
2017-01-17 13:31:18 -05:00
};
2017-01-17 13:31:18 -05:00
// Articles.
$main_articles.each(function() {
2017-01-17 13:31:18 -05:00
var $this = $(this);
2017-01-17 13:31:18 -05:00
// Close.
$('<div class="close">Close</div>')
.appendTo($this)
.on('click', function() {
location.hash = '';
});
2017-01-17 13:31:18 -05:00
// Prevent clicks from inside article from bubbling.
$this.on('click', function(event) {
event.stopPropagation();
});
2017-01-17 13:31:18 -05:00
});
2017-01-17 13:31:18 -05:00
// Events.
$body.on('click', function(event) {
2017-01-17 13:31:18 -05:00
// Article visible? Hide.
if ($body.hasClass('is-article-visible'))
$main._hide(true);
2017-01-17 13:31:18 -05:00
});
2017-01-17 13:31:18 -05:00
$window.on('keyup', function(event) {
2017-01-17 13:31:18 -05:00
switch (event.keyCode) {
2017-01-17 13:31:18 -05:00
case 27:
2017-01-17 13:31:18 -05:00
// Article visible? Hide.
if ($body.hasClass('is-article-visible'))
$main._hide(true);
2017-01-17 13:31:18 -05:00
break;
2017-01-17 13:31:18 -05:00
default:
break;
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
});
2017-01-17 13:31:18 -05:00
$window.on('hashchange', function(event) {
2017-01-17 13:31:18 -05:00
// Empty hash?
if (location.hash == ''
|| location.hash == '#') {
2017-01-17 13:31:18 -05:00
// Prevent default.
event.preventDefault();
event.stopPropagation();
2017-01-17 13:31:18 -05:00
// Hide.
$main._hide();
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Otherwise, check for a matching article.
else if ($main_articles.filter(location.hash).length > 0) {
2017-01-17 13:31:18 -05:00
// Prevent default.
event.preventDefault();
event.stopPropagation();
2017-01-17 13:31:18 -05:00
// Show article.
$main._show(location.hash.substr(1));
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
});
2017-01-17 13:31:18 -05:00
// Scroll restoration.
// This prevents the page from scrolling back to the top on a hashchange.
if ('scrollRestoration' in history)
history.scrollRestoration = 'manual';
else {
2017-01-17 13:31:18 -05:00
var oldScrollPos = 0,
scrollPos = 0,
$htmlbody = $('html,body');
2017-01-17 13:31:18 -05:00
$window
.on('scroll', function() {
2017-01-17 13:31:18 -05:00
oldScrollPos = scrollPos;
scrollPos = $htmlbody.scrollTop();
2017-01-17 13:31:18 -05:00
})
.on('hashchange', function() {
$window.scrollTop(oldScrollPos);
});
2017-01-17 13:31:18 -05:00
}
2017-01-17 13:31:18 -05:00
// Initialize.
2017-01-17 13:31:18 -05:00
// Hide main, articles.
$main.hide();
$main_articles.hide();
2017-01-17 13:31:18 -05:00
// Initial article.
if (location.hash != ''
&& location.hash != '#')
$window.on('load', function() {
$main._show(location.hash.substr(1), true);
});
2017-01-17 13:31:18 -05:00
});
2017-01-17 13:31:18 -05:00
})(jQuery);