// Detects when user clicks outside of specified element.
function ClickOutsideTracker() {
var isMouseOver = null;
var element;
var onClickOutside;
this.assign = function(_element, _onClickOutside) {
isMouseOver = false;
element = _element;
onClickOutside = _onClickOutside;
element.mouseenter(function () {
isMouseOver = true;
}).mouseleave(function () {
isMouseOver = false;
});
if (isIOS()) {
$(document.body).click(globalOnClickHandler);
?
} else {
$(document.body).mousedown(globalOnClickHandler);
}
return this;
};
function globalOnClickHandler() {
if (!isMouseOver) {
onClickOutside();
}
}
this.unassign = function() {
element.off("mouseenter");
element.off("mouseleave");
if (isIOS()) {
$(document.body).off("click");
} else {
$(document.body).off("mousedown");
}
}
}
$(document).on('click', function(event) {
if(!($(event.target).closest(element).size() > 0 || $(event.target).is(element))) {
console.log('Click outside');
} else
console.log('Click inside on',event.target);
});