ratchet/js/popovers.js

72 строки
1.7 KiB
JavaScript
Исходник Обычный вид История

2012-11-05 04:03:39 +04:00
/* ----------------------------------
2014-02-25 08:18:44 +04:00
* POPOVER v2.0.0
2012-11-05 04:03:39 +04:00
* Licensed under The MIT License
* http://opensource.org/licenses/MIT
* ---------------------------------- */
!function () {
var popover;
var findPopovers = function (target) {
var i, popovers = document.querySelectorAll('a');
for (; target && target !== document; target = target.parentNode) {
for (i = popovers.length; i--;) { if (popovers[i] === target) return target; }
}
};
var onPopoverHidden = function () {
popover.style.display = 'none';
popover.removeEventListener('webkitTransitionEnd', onPopoverHidden);
}
var backdrop = function () {
var element = document.createElement('div');
element.classList.add('backdrop');
element.addEventListener('touchend', function () {
popover.addEventListener('webkitTransitionEnd', onPopoverHidden);
popover.classList.remove('visible');
2014-02-16 01:06:24 +04:00
popover.parentNode.removeChild(backdrop);
2012-11-05 04:03:39 +04:00
});
return element;
}();
var getPopover = function (e) {
var anchor = findPopovers(e.target);
if (!anchor || !anchor.hash || (anchor.hash.indexOf("/") > 0)) return;
2012-11-05 04:03:39 +04:00
2013-10-21 19:22:04 +04:00
try {
popover = document.querySelector(anchor.hash);
}
catch (error) {
popover = null;
}
if (popover == null) return;
2012-11-05 04:03:39 +04:00
if (!popover || !popover.classList.contains('popover')) return;
return popover;
}
var showHidePopover = function (e) {
2012-11-05 04:03:39 +04:00
var popover = getPopover(e);
if (!popover) return;
popover.style.display = 'block';
popover.offsetHeight;
popover.classList.add('visible');
popover.parentNode.appendChild(backdrop);
};
2012-11-05 04:03:39 +04:00
window.addEventListener('touchend', showHidePopover);
window.addEventListener('click', showHidePopover);
2012-11-05 04:03:39 +04:00
}();