1 | import {root} from './root';
|
2 |
|
3 | /**
|
4 | * DOM event utilities
|
5 | */
|
6 |
|
7 | /**
|
8 | * Add event handler for specified event on passed element
|
9 | *
|
10 | * @param {DOMElement} obj Element
|
11 | * @param {String} type Event type
|
12 | * @param {Function} Handler
|
13 | * @param {Boolean} capture Specifiy whether the event should be executed in
|
14 | * the capturing or in the bubbling phase
|
15 | */
|
16 | export const addEvt = (obj, type, func, capture) => {
|
17 | if (obj.addEventListener) {
|
18 | obj.addEventListener(type, func, capture);
|
19 | }
|
20 | else if (obj.attachEvent) {
|
21 | obj.attachEvent('on' + type, func);
|
22 | } else {
|
23 | obj['on' + type] = func;
|
24 | }
|
25 | };
|
26 |
|
27 | /**
|
28 | * Remove event handler for specified event on passed element
|
29 | *
|
30 | * @param {DOMElement} obj Element
|
31 | * @param {String} type Event type
|
32 | * @param {Function} Handler
|
33 | * @param {Boolean} capture Specifiy whether the event should be executed in
|
34 | * the capturing or in the bubbling phase
|
35 | */
|
36 | export const removeEvt = (obj, type, func, capture) => {
|
37 | if (obj.removeEventListener) {
|
38 | obj.removeEventListener(type, func, capture);
|
39 | } else if (obj.detachEvent) {
|
40 | obj.detachEvent('on' + type, func);
|
41 | } else {
|
42 | obj['on' + type] = null;
|
43 | }
|
44 | };
|
45 |
|
46 | /**
|
47 | * Prevents further propagation of the current event in the bubbling phase
|
48 | *
|
49 | * @param {Event} evt Event on the DOM
|
50 | */
|
51 | export const stopEvt = (evt) => {
|
52 | if (!evt) {
|
53 | evt = root.event;
|
54 | }
|
55 | if (evt.stopPropagation) {
|
56 | evt.stopPropagation();
|
57 | } else {
|
58 | evt.cancelBubble = true;
|
59 | }
|
60 | };
|
61 |
|
62 | /**
|
63 | * Cancels the event if it is cancelable, without stopping further
|
64 | * propagation of the event.
|
65 | *
|
66 | * @param {Event} evt Event on the DOM
|
67 | */
|
68 | export const cancelEvt = (evt) => {
|
69 | if (!evt) {
|
70 | evt = root.event;
|
71 | }
|
72 | if (evt.preventDefault) {
|
73 | evt.preventDefault();
|
74 | } else {
|
75 | evt.returnValue = false;
|
76 | }
|
77 | };
|
78 |
|
79 | /**
|
80 | * Reference to the object that dispatched the event
|
81 | *
|
82 | * @param {Event} evt Event on the DOM
|
83 | * @returns {DOMElement}
|
84 | */
|
85 | export const targetEvt = (evt) => {
|
86 | if (!evt) {
|
87 | evt = root.event;
|
88 | }
|
89 | return evt.target || evt.srcElement;
|
90 | };
|
91 |
|
92 | /**
|
93 | * Returns the Unicode value of pressed key
|
94 | *
|
95 | * @param {Event} evt Event on the DOM
|
96 | * @returns {Number}
|
97 | */
|
98 | export const keyCode = (evt) => {
|
99 | return evt.charCode ? evt.charCode :
|
100 | (evt.keyCode ? evt.keyCode : (evt.which ? evt.which : 0));
|
101 | };
|
102 |
|
103 | /**
|
104 | * Check code of pressed key is one of the expected key codes
|
105 | *
|
106 | * @param {Event} evt key event
|
107 | * @param {Array} keyCodes list of keycodes to check
|
108 | */
|
109 | export const isKeyPressed = (evt, keyCodes = []) => {
|
110 | return keyCodes.indexOf(keyCode(evt)) !== -1;
|
111 | };
|
112 |
|
113 | /**
|
114 | * Bind passed function to passed scope
|
115 | * @param {Function} fn function
|
116 | * @param {Object} scope object instance
|
117 | */
|
118 | export function bound(fn, scope) {
|
119 | let boundFnName = `${fn.name}_bound`;
|
120 | if (!scope[boundFnName]) {
|
121 | scope[boundFnName] = fn.bind(scope);
|
122 | }
|
123 | return scope[boundFnName];
|
124 | }
|