d938c53c3b260de4705b493d9bb2e04a6f0f5108
[ratchet] / lib / js / modals.js
1 /* ----------------------------------
2  * MODAL v1.0.0
3  * Licensed under The MIT License
4  * http://opensource.org/licenses/MIT
5  * ---------------------------------- */
6
7 !function () {
8   findModals = function ( target ) {
9     var i, 
10         modals = document.querySelectorAll( 'a' );
11
12     for ( ; target && target !== document; target = target.parentNode ) {
13       for ( i = modals.length; i--; ) { if ( modals[i] === target ) return target; }
14     }
15   };
16
17   getModal = function ( event ) {
18     var modal,
19         modalToggle = findModals( event.target );
20
21     if ( !modalToggle || !modalToggle.hash ) return;
22
23     modal = document.querySelector( modalToggle.hash )
24     
25     if ( !modal ) return;
26     return modal;
27   };
28
29   window.addEventListener( 'touchend', function ( event ) {
30     var modal = getModal( event );
31
32     if ( !modal ) return; 
33     modal.offsetHeight;
34     modal.classList.toggle( 'active' );
35   } );
36
37   window.addEventListener( 'click', function ( event ) { 
38     if ( getModal( event ) ) event.preventDefault();
39   } );
40 }();