2016-05-08 08:14:40 +02:00
|
|
|
import {root} from './root';
|
|
|
|
|
2014-11-22 09:06:08 +01:00
|
|
|
/**
|
|
|
|
* DOM event utilities
|
|
|
|
*/
|
|
|
|
|
2016-06-02 06:13:56 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
export const addEvt = (obj, type, func, capture) => {
|
|
|
|
if (obj.addEventListener) {
|
|
|
|
obj.addEventListener(type, func, capture);
|
|
|
|
}
|
|
|
|
else if (obj.attachEvent) {
|
|
|
|
obj.attachEvent('on' + type, func);
|
|
|
|
} else {
|
|
|
|
obj['on' + type] = func;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-11-19 13:42:37 +01:00
|
|
|
export const removeEvt = (obj, type, func, capture) => {
|
2016-12-27 11:20:16 +01:00
|
|
|
if (obj.removeEventListener) {
|
2016-06-02 06:13:56 +02:00
|
|
|
obj.removeEventListener(type, func, capture);
|
2016-12-27 11:20:16 +01:00
|
|
|
} else if (obj.detachEvent) {
|
|
|
|
obj.detachEvent('on' + type, func);
|
2016-06-02 06:13:56 +02:00
|
|
|
} else {
|
|
|
|
obj['on' + type] = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevents further propagation of the current event in the bubbling phase
|
|
|
|
*
|
|
|
|
* @param {Event} evt Event on the DOM
|
|
|
|
*/
|
2016-12-27 11:20:16 +01:00
|
|
|
export const stopEvt = (evt) => {
|
2016-06-02 06:13:56 +02:00
|
|
|
if (!evt) {
|
|
|
|
evt = root.event;
|
|
|
|
}
|
|
|
|
if (evt.stopPropagation) {
|
|
|
|
evt.stopPropagation();
|
|
|
|
} else {
|
|
|
|
evt.cancelBubble = true;
|
2014-11-22 09:06:08 +01:00
|
|
|
}
|
|
|
|
};
|
2016-06-02 06:13:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels the event if it is cancelable, without stopping further
|
|
|
|
* propagation of the event.
|
|
|
|
*
|
|
|
|
* @param {Event} evt Event on the DOM
|
|
|
|
*/
|
2016-12-27 11:20:16 +01:00
|
|
|
export const cancelEvt = (evt) => {
|
2016-06-02 06:13:56 +02:00
|
|
|
if (!evt) {
|
|
|
|
evt = root.event;
|
|
|
|
}
|
|
|
|
if (evt.preventDefault) {
|
|
|
|
evt.preventDefault();
|
|
|
|
} else {
|
|
|
|
evt.returnValue = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the object that dispatched the event
|
|
|
|
*
|
|
|
|
* @param {Event} evt Event on the DOM
|
|
|
|
* @returns {DOMElement}
|
|
|
|
*/
|
2016-12-27 11:20:16 +01:00
|
|
|
export const targetEvt = (evt) => {
|
2016-06-02 06:13:56 +02:00
|
|
|
if (!evt) {
|
|
|
|
evt = root.event;
|
|
|
|
}
|
|
|
|
return evt.target || evt.srcElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Unicode value of pressed key
|
|
|
|
*
|
|
|
|
* @param {Event} evt Event on the DOM
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
2016-12-27 11:20:16 +01:00
|
|
|
export const keyCode = (evt) => {
|
2016-06-02 06:13:56 +02:00
|
|
|
return evt.charCode ? evt.charCode :
|
|
|
|
(evt.keyCode ? evt.keyCode : (evt.which ? evt.which : 0));
|
|
|
|
};
|
2018-01-29 13:20:52 +01:00
|
|
|
|
2018-02-02 12:26:28 +01:00
|
|
|
/**
|
2018-02-04 11:36:53 +01:00
|
|
|
* Check code of pressed key is one of the expected key codes
|
2018-02-02 12:26:28 +01:00
|
|
|
*
|
|
|
|
* @param {Event} evt key event
|
|
|
|
* @param {Array} keyCodes list of keycodes to check
|
|
|
|
*/
|
|
|
|
export const isKeyPressed = (evt, keyCodes = []) => {
|
|
|
|
return keyCodes.indexOf(keyCode(evt)) !== -1;
|
|
|
|
};
|
2018-05-20 13:43:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind passed function to passed scope
|
|
|
|
* @param {Function} fn function
|
|
|
|
* @param {Object} scope object instance
|
|
|
|
*/
|
|
|
|
export function bound(fn, scope) {
|
|
|
|
let boundFnName = `${fn.name}_bound`;
|
|
|
|
if (!scope[boundFnName]) {
|
|
|
|
scope[boundFnName] = fn.bind(scope);
|
|
|
|
}
|
|
|
|
return scope[boundFnName];
|
|
|
|
}
|