UNPKG

2.16 kBJavaScriptView Raw
1export const getSize = (value) => {
2 return typeof value === 'number' ? `${value}px` : value;
3};
4/** Get the distance of the element from the top/left of the page */
5export const getOffset = (elem) => {
6 const doc = document.documentElement;
7 const body = document.body;
8 const rect = elem.getBoundingClientRect();
9 const offset = {
10 y: rect.top + (window.pageYOffset || doc.scrollTop) - (doc.clientTop || body.clientTop || 0),
11 x: rect.left + (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || body.clientLeft || 0),
12 };
13 return offset;
14};
15/**
16 * Get the position of the mouse/finger in the element
17 * @param e Trigger event
18 * @param elem Container element
19 * @param isReverse From the right/bottom
20 */
21export const getPos = (e, elem, isReverse, zoom = 1) => {
22 const event = 'targetTouches' in e ? e.targetTouches[0] : e;
23 const offset = getOffset(elem);
24 const posObj = {
25 x: event.pageX - offset.x,
26 y: event.pageY - offset.y,
27 };
28 return {
29 x: isReverse ? elem.offsetWidth * zoom - posObj.x : posObj.x,
30 y: isReverse ? elem.offsetHeight * zoom - posObj.y : posObj.y,
31 };
32};
33export const getKeyboardHandleFunc = (e, params) => {
34 if (params.hook) {
35 const result = params.hook(e);
36 if (typeof result === 'function')
37 return result;
38 if (!result)
39 return null;
40 }
41 switch (e.keyCode) {
42 case 38 /* UP */:
43 return i => (params.direction === 'ttb' ? i - 1 : i + 1);
44 case 39 /* RIGHT */:
45 return i => (params.direction === 'rtl' ? i - 1 : i + 1);
46 case 40 /* DOWN */:
47 return i => (params.direction === 'ttb' ? i + 1 : i - 1);
48 case 37 /* LEFT */:
49 return i => (params.direction === 'rtl' ? i + 1 : i - 1);
50 case 35 /* END */:
51 return () => params.max;
52 case 36 /* HOME */:
53 return () => params.min;
54 case 33 /* PAGE_UP */:
55 return i => i + 10;
56 case 34 /* PAGE_DOWN */:
57 return i => i - 10;
58 default:
59 return null;
60 }
61};
62//# sourceMappingURL=index.js.map
\No newline at end of file