-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Sensor.js
154 lines (136 loc) · 3.42 KB
/
Sensor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const defaultDelay = {
mouse: 0,
drag: 0,
touch: 100,
};
/**
* Base sensor class. Extend from this class to create a new or custom sensor
* @class Sensor
* @module Sensor
*/
export default class Sensor {
/**
* Sensor constructor.
* @constructs Sensor
* @param {HTMLElement[]|NodeList|HTMLElement} containers - Containers
* @param {Object} options - Options
*/
constructor(containers = [], options = {}) {
/**
* Current containers
* @property containers
* @type {HTMLElement[]}
*/
this.containers = [...containers];
/**
* Current options
* @property options
* @type {Object}
*/
this.options = {...options};
/**
* Current drag state
* @property dragging
* @type {Boolean}
*/
this.dragging = false;
/**
* Current container
* @property currentContainer
* @type {HTMLElement}
*/
this.currentContainer = null;
/**
* Draggables original source element
* @property originalSource
* @type {HTMLElement}
*/
this.originalSource = null;
/**
* The event of the initial sensor down
* @property startEvent
* @type {Event}
*/
this.startEvent = null;
/**
* The delay of each sensor
* @property delay
* @type {Object}
*/
this.delay = calcDelay(options.delay);
}
/**
* Attaches sensors event listeners to the DOM
* @return {Sensor}
*/
attach() {
return this;
}
/**
* Detaches sensors event listeners to the DOM
* @return {Sensor}
*/
detach() {
return this;
}
/**
* Adds container to this sensor instance
* @param {...HTMLElement} containers - Containers you want to add to this sensor
* @example draggable.addContainer(document.body)
*/
addContainer(...containers) {
this.containers = [...this.containers, ...containers];
}
/**
* Removes container from this sensor instance
* @param {...HTMLElement} containers - Containers you want to remove from this sensor
* @example draggable.removeContainer(document.body)
*/
removeContainer(...containers) {
this.containers = this.containers.filter(
(container) => !containers.includes(container),
);
}
/**
* Triggers event on target element
* @param {HTMLElement} element - Element to trigger event on
* @param {SensorEvent} sensorEvent - Sensor event to trigger
*/
trigger(element, sensorEvent) {
const event = document.createEvent('Event');
event.detail = sensorEvent;
event.initEvent(sensorEvent.type, true, true);
element.dispatchEvent(event);
this.lastEvent = sensorEvent;
return sensorEvent;
}
}
/**
* Calculate the delay of each sensor through the delay in the options
* @param {undefined|Number|Object} optionsDelay - the delay in the options
* @return {Object}
*/
function calcDelay(optionsDelay) {
const delay = {};
if (optionsDelay === undefined) {
return {...defaultDelay};
}
if (typeof optionsDelay === 'number') {
for (const key in defaultDelay) {
if (Object.prototype.hasOwnProperty.call(defaultDelay, key)) {
delay[key] = optionsDelay;
}
}
return delay;
}
for (const key in defaultDelay) {
if (Object.prototype.hasOwnProperty.call(defaultDelay, key)) {
if (optionsDelay[key] === undefined) {
delay[key] = defaultDelay[key];
} else {
delay[key] = optionsDelay[key];
}
}
}
return delay;
}