1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-19 06:47:00 +02:00
TableFilter/src/event.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-05-08 08:14:40 +02:00
import {root} from './root';
2014-11-22 09:06:08 +01:00
/**
* DOM event utilities
*/
2015-05-30 14:23:33 +02:00
export default {
/**
* Add event handler for specified event on passed element
*
* @param {DOMElement} obj Element
* @param {String} type Event type
* @param {Function} Handler
* @param {Boolean} capture Specifiy whether the event should be executed in
* the capturing or in the bubbling phase
*/
add(obj, type, func, capture) {
if (obj.addEventListener) {
2014-11-23 11:31:55 +01:00
obj.addEventListener(type, func, capture);
2014-11-22 09:06:08 +01:00
}
else if (obj.attachEvent) {
obj.attachEvent('on' + type, func);
2014-11-23 11:31:55 +01:00
} else {
obj['on' + type] = func;
2014-11-22 09:06:08 +01:00
}
},
/**
* Remove event handler for specified event on passed element
*
* @param {DOMElement} obj Element
* @param {String} type Event type
* @param {Function} Handler
* @param {Boolean} capture Specifiy whether the event should be executed in
* the capturing or in the bubbling phase
*/
remove(obj, type, func, capture) {
if (obj.detachEvent) {
obj.detachEvent('on' + type, func);
2014-11-22 09:06:08 +01:00
}
else if (obj.removeEventListener) {
2014-11-23 11:31:55 +01:00
obj.removeEventListener(type, func, capture);
2014-11-22 09:06:08 +01:00
} else {
obj['on' + type] = null;
2014-11-22 09:06:08 +01:00
}
},
/**
* Prevents further propagation of the current event in the bubbling phase
*
* @param {Event} evt Event on the DOM
*/
stop(evt) {
if (!evt) {
2016-05-08 08:14:40 +02:00
evt = root.event;
2014-11-22 09:06:08 +01:00
}
if (evt.stopPropagation) {
2014-11-22 09:06:08 +01:00
evt.stopPropagation();
} else {
evt.cancelBubble = true;
}
},
/**
* Cancels the event if it is cancelable, without stopping further
* propagation of the event.
*
* @param {Event} evt Event on the DOM
*/
cancel(evt) {
if (!evt) {
2016-05-08 08:14:40 +02:00
evt = root.event;
2014-11-22 09:06:08 +01:00
}
if (evt.preventDefault) {
2014-11-22 09:06:08 +01:00
evt.preventDefault();
} else {
evt.returnValue = false;
}
2015-04-06 11:21:49 +02:00
},
/**
* Reference to the object that dispatched the event
*
* @param {Event} evt Event on the DOM
* @returns {DOMElement}
*/
target(evt) {
if (!evt) {
evt = root.event;
}
return evt.target || evt.srcElement;
2015-04-20 09:20:28 +02:00
},
/**
* Returns the Unicode value of pressed key
*
* @param {Event} evt Event on the DOM
* @returns {Number}
*/
keyCode(evt) {
2015-04-06 11:21:49 +02:00
return evt.charCode ? evt.charCode :
(evt.keyCode ? evt.keyCode : (evt.which ? evt.which : 0));
2014-11-22 09:06:08 +01:00
}
};