From: Ben Schwarz Date: Sun, 23 Dec 2012 22:55:27 +0000 (+1100) Subject: Added script for modals X-Git-Url: http://git.roojs.org/?p=ratchet;a=commitdiff_plain;h=5731c1c8151a566ef8982f5f8a62d045819b610d Added script for modals * Follows existing conventions throughout --- diff --git a/lib/js/modals.js b/lib/js/modals.js new file mode 100644 index 0000000..d938c53 --- /dev/null +++ b/lib/js/modals.js @@ -0,0 +1,40 @@ +/* ---------------------------------- + * MODAL v1.0.0 + * Licensed under The MIT License + * http://opensource.org/licenses/MIT + * ---------------------------------- */ + +!function () { + findModals = function ( target ) { + var i, + modals = document.querySelectorAll( 'a' ); + + for ( ; target && target !== document; target = target.parentNode ) { + for ( i = modals.length; i--; ) { if ( modals[i] === target ) return target; } + } + }; + + getModal = function ( event ) { + var modal, + modalToggle = findModals( event.target ); + + if ( !modalToggle || !modalToggle.hash ) return; + + modal = document.querySelector( modalToggle.hash ) + + if ( !modal ) return; + return modal; + }; + + window.addEventListener( 'touchend', function ( event ) { + var modal = getModal( event ); + + if ( !modal ) return; + modal.offsetHeight; + modal.classList.toggle( 'active' ); + } ); + + window.addEventListener( 'click', function ( event ) { + if ( getModal( event ) ) event.preventDefault(); + } ); +}(); \ No newline at end of file