-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrog.Module.js
More file actions
206 lines (166 loc) · 4.6 KB
/
Copy pathDrog.Module.js
File metadata and controls
206 lines (166 loc) · 4.6 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*!
* Drog.js v1.2.2
* Copyright (c) 2026, Emanuel Rojas Vásquez
* MIT License
* https://github.com/erovas/Drog.js
* Converted to ES Module.
*/
const Xi = '-Xi';
const Yi = '-Yi';
const Xf = '-Xf';
const Yf = '-Yf';
const Xt = '-x';
const Yt = '-y';
const MOUSEDOWN = 'mousedown';
const TOUCHSTART = 'touchstart';
const MOUSEMOVE = 'mousemove';
const TOUCHMOVE = 'touchmove';
const MOUSEUP = 'mouseup';
const TOUCHEND = 'touchend';
const FATHER = '-f';
const PASSIVE = { passive: false };
const IS_DROG = '-d';
const DATA_DROG = '[data-drog]';
//#region Helpers
/**
*
* @param {HTMLElement | Document} element
* @param {string} type
* @param {EventListenerOrEventListenerObject} listener
* @param {boolean | AddEventListenerOptions} options
*/
function addEvent(element, type, listener, options = false) {
element.addEventListener(type, listener, options);
}
/**
*
* @param {HTMLElement | Document} element
* @param {string} type
* @param {EventListenerOrEventListenerObject} listener
* @param {boolean | AddEventListenerOptions} options
*/
function removeEvent(element, type, listener, options = false) {
element.removeEventListener(type, listener, options);
}
/**
*
* @param {HTMLElement} element
* @param {string} selectors
*/
function querySelector(element, selectors){
return element.querySelector(DATA_DROG) || element;
}
//#endregion
//#region Methods
/**
* Enables drag functionality on a DOM element.
* @param {HTMLElement} element - The DOM element to make draggable
* @returns
*/
function on(element) {
if (element[IS_DROG])
return;
const target = querySelector(element, DATA_DROG);
// save reference original element into target
target[FATHER] = element;
// Save element position
element[Xt] = 0;
element[Yt] = 0;
element.style.zIndex = 10;
target.style.touchAction = "none";
// signing element
element[IS_DROG] = true;
addEvent(target, MOUSEDOWN, drogInit);
addEvent(target, TOUCHSTART, drogInit, PASSIVE);
}
/**
* Disables drag functionality on a DOM element.
* @param {HTMLElement} element - The DOM element to remove drag from
* @returns
*/
function off(element) {
if (!element[IS_DROG])
return;
const target = querySelector(element, DATA_DROG);
element.style.zIndex = '';
element.style.transform = '';
target.style.touchAction = '';
// deleting references
target[FATHER] = null;
// unsigning element
element[IS_DROG] = null;
removeEvent(target, MOUSEDOWN, drogInit);
removeEvent(target, TOUCHSTART, drogInit);
}
/**
* Programmatically moves a draggable element to specific coordinates.
* @param {HTMLElement} element - The draggable DOM element
* @param {Number} x - X coordinate in pixels.
* @param {Number} y - Y coordinate in pixels
* @returns
*/
function move(element, x, y) {
if (!element[IS_DROG])
return;
x = parseFloat(x) || 0;
y = parseFloat(y) || 0;
element[Xt] = x;
element[Yt] = y;
element.style.transform = 'translate(' + x + 'px,' + y + 'px)';
}
//#endregion
//#region Events
/**
*
* @param {MouseEvent|TouchEvent} e
* @returns
*/
function drogInit(e) {
if (e.which > 1)
return;
const that = this;
const element = that[FATHER];
// get the mouse cursor position at startup:
element[Xi] = e.clientX || e.targetTouches[0].clientX;
element[Yi] = e.clientY || e.targetTouches[0].clientY;
// call a function whenever the cursor moves:
if (e.type === TOUCHSTART) {
addEvent(that, TOUCHMOVE, drogMove, PASSIVE);
addEvent(that, TOUCHEND, drogEnd, PASSIVE);
}
else {
addEvent(that, MOUSEMOVE, drogMove);
addEvent(that, MOUSEUP, drogEnd);
}
}
/**
*
* @param {MouseEvent|TouchEvent} e
*/
function drogMove(e) {
e.preventDefault();
const element = this[FATHER];
// calculate the new cursor position:
element[Xf] = e.clientX || e.targetTouches[0].clientX;
element[Yf] = e.clientY || e.targetTouches[0].clientY;
element[Xt] -= element[Xi] - element[Xf];
element[Yt] -= element[Yi] - element[Yf];
element[Xi] = element[Xf];
element[Yi] = element[Yf];
element.style.transform = 'translate(' + element[Xt] + 'px,' + element[Yt] + 'px)';
}
function drogEnd(e) {
const that = this;
// stop moving when mouse/touch is released:
removeEvent(that, MOUSEMOVE, drogMove);
removeEvent(that, MOUSEUP, drogEnd);
removeEvent(that, TOUCHMOVE, drogMove, PASSIVE);
removeEvent(that, TOUCHEND, drogEnd, PASSIVE);
}
//#endregion
const Drog = {
on,
off,
move
};
export default Drog;