2d8c7a9b5db6469d77c37e59f94d45b14cfddbb1
[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   var findModals = function (target) {
9     var i;
10     var 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   var getModal = function (event) {
18     var modalToggle = findModals(event.target);
19     if (!modalToggle || !modalToggle.hash) return;
20     return document.querySelector(modalToggle.hash);
21   };
22
23   window.addEventListener('touchend', function (event) {
24     var modal = getModal(event);
25     if (modal) modal.classList.toggle('active');
26   });
27
28   window.addEventListener('click', function (event) { 
29     if (getModal(event)) event.preventDefault();
30   });
31 }();