@cocreate/position 1.1.45 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/demo/index.html +4 -4
- package/docs/index.html +1 -1
- package/package.json +3 -3
- package/src/position.js +337 -326
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.2.1](https://github.com/CoCreate-app/CoCreate-position/compare/v1.2.0...v1.2.1) (2023-03-16)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies' ([b8da26d](https://github.com/CoCreate-app/CoCreate-position/commit/b8da26dbe70f40863d1da3069ad7dae04bc21a22))
|
|
7
|
+
|
|
8
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-position/compare/v1.1.45...v1.2.0) (2023-03-16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* replaced get-value and set-value with a super charged version of CoCreate-events ([201232d](https://github.com/CoCreate-app/CoCreate-position/commit/201232def69cca0f44bc0ed0ec8e71757b9ee653))
|
|
14
|
+
|
|
1
15
|
## [1.1.45](https://github.com/CoCreate-app/CoCreate-position/compare/v1.1.44...v1.1.45) (2023-02-01)
|
|
2
16
|
|
|
3
17
|
|
package/demo/index.html
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
margin: 0px;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
[drag-
|
|
13
|
+
[drag-handle] {
|
|
14
14
|
top: 0px;
|
|
15
15
|
left: 0px;
|
|
16
16
|
width: 100%;
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
<body>
|
|
28
28
|
|
|
29
29
|
<div class="position:fixed height:300px width:300px background:whitesmoke" positionable>
|
|
30
|
-
<div drag-
|
|
30
|
+
<div drag-handle></div>
|
|
31
31
|
</div>
|
|
32
32
|
|
|
33
|
-
<div class="position:fixed height:300px width:300px background:brown"
|
|
34
|
-
<div drag-
|
|
33
|
+
<div class="position:fixed height:300px width:300px background:brown" moveable>
|
|
34
|
+
<div drag-handle></div>
|
|
35
35
|
</div>
|
|
36
36
|
|
|
37
37
|
|
package/docs/index.html
CHANGED
|
@@ -144,7 +144,7 @@
|
|
|
144
144
|
</div>
|
|
145
145
|
|
|
146
146
|
<div id="demo-preview" class="position:relative overflow:auto background-color:white">
|
|
147
|
-
<div
|
|
147
|
+
<div class="demopreview" class="padding:20px"></div>
|
|
148
148
|
</div>
|
|
149
149
|
|
|
150
150
|
<div class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/position",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Make any element draggable and movable. Customizable via attributes, great for popups, alerts, multitasking and displaying multiple views.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"position",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"webpack-log": "^3.0.1"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@cocreate/docs": "^1.
|
|
64
|
-
"@cocreate/observer": "^1.
|
|
63
|
+
"@cocreate/docs": "^1.6.0",
|
|
64
|
+
"@cocreate/observer": "^1.6.0"
|
|
65
65
|
}
|
|
66
66
|
}
|
package/src/position.js
CHANGED
|
@@ -1,336 +1,347 @@
|
|
|
1
1
|
function position(el, container, options) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
/** define constant **/
|
|
7
|
-
this.dragAreaHeight = 50;
|
|
8
|
-
this.MARGIN = 5;
|
|
9
|
-
this.FULLSCREEN_MARGIN = -60;
|
|
10
|
-
this.NO_SNAP = -100;
|
|
11
|
-
this.SNAP_MARGIN = -10;
|
|
12
|
-
this.RIGHT_SCROL = 5;
|
|
13
|
-
|
|
14
|
-
this.container = container;
|
|
15
|
-
if (!this.container.positionIndex)
|
|
16
|
-
this.container.positionIndex = 10
|
|
17
|
-
|
|
18
|
-
this.el = el;
|
|
19
|
-
this.boundStatus = {};
|
|
20
|
-
|
|
21
|
-
this.selectedElement;
|
|
22
|
-
// this.selectedClass = "selectedPosition";
|
|
23
|
-
/**
|
|
24
|
-
* x: mouse x in element
|
|
25
|
-
* y: mouse y in element
|
|
26
|
-
* cx: x in document
|
|
27
|
-
* cy: y in document
|
|
28
|
-
* */
|
|
29
|
-
this.point = {};
|
|
30
|
-
this.rect = {};
|
|
31
|
-
this.clickedInfo = null;
|
|
32
|
-
this.preSnapped = null;
|
|
33
|
-
this.isSnap = false;
|
|
34
|
-
|
|
35
|
-
this._createGhost(this.container.getAttribute("data-ghost-class"));
|
|
36
|
-
this._initEvent();
|
|
37
|
-
this._selectElement()
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
position.prototype = {
|
|
41
|
-
constructor: position,
|
|
42
|
-
|
|
43
|
-
_initEvent: function () {
|
|
44
|
-
let self = this;
|
|
45
|
-
this.iframe = this.el.querySelector('iframe')
|
|
46
|
-
if (this.iframe) {
|
|
47
|
-
this.iframe.addEventListener('load', function () {
|
|
48
|
-
const iframeContent = self.iframe.contentDocument;
|
|
49
|
-
iframeContent.addEventListener('mousedown', function (e) {
|
|
50
|
-
self._selectElement(e.detail.element)
|
|
51
|
-
})
|
|
52
|
-
});
|
|
2
|
+
if (!(el && el.nodeType && el.nodeType === 1)) {
|
|
3
|
+
return;
|
|
53
4
|
}
|
|
54
5
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
this.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.el.addEventListener('touchend', function (e) {
|
|
80
|
-
self._onUp(e.touches[0]);
|
|
81
|
-
}, { passive: false });
|
|
82
|
-
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
_onMove: function (e) {
|
|
86
|
-
this.__getBoundStatus(e)
|
|
87
|
-
this.__animate();
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
_onDown: function (e) {
|
|
91
|
-
this.__getBoundStatus(e);
|
|
92
|
-
|
|
93
|
-
this.clickedInfo = {
|
|
94
|
-
x: this.point.x,
|
|
95
|
-
y: this.point.y,
|
|
96
|
-
cx: this.point.cx,
|
|
97
|
-
cy: this.point.cy,
|
|
98
|
-
w: this.rect.width,
|
|
99
|
-
h: this.rect.height,
|
|
100
|
-
isMoving: this._isMovable(),
|
|
101
|
-
boundStatus: this.boundStatus,
|
|
102
|
-
isChangeStart: true
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
|
|
106
|
-
_onUp: function (e) {
|
|
107
|
-
if (e) {
|
|
108
|
-
this.__getBoundStatus(e);
|
|
109
|
-
}
|
|
110
|
-
if (!this.clickedInfo) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if (this.clickedInfo.isMoving && !this.isParked) {
|
|
114
|
-
let p_w = this.el.parentNode.offsetWidth, p_h = this.el.parentNode.offsetHeight;
|
|
115
|
-
let snap_info = null;
|
|
116
|
-
|
|
117
|
-
if (this.__between(this.rect.top, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
118
|
-
this.__between(this.rect.left, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
119
|
-
this.__between(p_w - this.rect.right, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
120
|
-
this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.FULLSCREEN_MARGIN)) {
|
|
121
|
-
snap_info = { x: 0, y: 0, w: '100%', h: '100%' }
|
|
122
|
-
} else if (this.__between(this.rect.top, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
123
|
-
snap_info = { x: 0, y: 0, w: '100%', h: '50%' }
|
|
124
|
-
} else if (this.__between(this.rect.left, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
125
|
-
snap_info = { x: 0, y: 0, w: '50%', h: '100%' }
|
|
126
|
-
} else if (this.__between(p_w - this.rect.right, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
127
|
-
snap_info = { x: '50%', y: 0, w: '50%', h: '100%' }
|
|
128
|
-
} else if (this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
129
|
-
snap_info = { x: 0, y: '50%', w: '100%', h: '50%' }
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (snap_info && !this.isSnap) {
|
|
133
|
-
this.__setBound(this.el, snap_info.x, snap_info.y, snap_info.w, snap_info.h);
|
|
134
|
-
this.preSnapped = { x: this.rect.x, y: this.rect.y, width: this.rect.width, height: this.rect.height };
|
|
135
|
-
this.isSnap = true;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
let ghost_info = {
|
|
139
|
-
x: this.rect.x,
|
|
140
|
-
y: this.rect.y,
|
|
141
|
-
w: this.rect.width,
|
|
142
|
-
h: this.rect.height
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this._ghostProcess(ghost_info)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (this.clickedInfo.isMoving) {
|
|
149
|
-
this.createEvent('element-moveend');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
this.clickedInfo = null;
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
__setBound: function (el, x, y, w, h) {
|
|
157
|
-
el.style.left = x;
|
|
158
|
-
el.style.top = y;
|
|
159
|
-
el.style.width = w;
|
|
160
|
-
el.style.height = h;
|
|
161
|
-
},
|
|
162
|
-
|
|
163
|
-
__setRectInfo: function () {
|
|
164
|
-
let bound = this.el.getBoundingClientRect();
|
|
165
|
-
let parentRect = this.el.parentNode.getBoundingClientRect();
|
|
6
|
+
/** define constant **/
|
|
7
|
+
this.dragAreaHeight = 50;
|
|
8
|
+
this.MARGIN = 5;
|
|
9
|
+
this.FULLSCREEN_MARGIN = -60;
|
|
10
|
+
this.NO_SNAP = -100;
|
|
11
|
+
this.SNAP_MARGIN = -10;
|
|
12
|
+
this.RIGHT_SCROL = 5;
|
|
13
|
+
this.dragHandle = el.querySelector('[drag-handle]')
|
|
14
|
+
this.container = container;
|
|
15
|
+
if (!this.container.positionIndex)
|
|
16
|
+
this.container.positionIndex = 10
|
|
17
|
+
|
|
18
|
+
this.el = el;
|
|
19
|
+
this.boundStatus = {};
|
|
20
|
+
|
|
21
|
+
this.selectedElement;
|
|
22
|
+
// this.selectedClass = "selectedPosition";
|
|
23
|
+
/**
|
|
24
|
+
* x: mouse x in element
|
|
25
|
+
* y: mouse y in element
|
|
26
|
+
* cx: x in document
|
|
27
|
+
* cy: y in document
|
|
28
|
+
* */
|
|
29
|
+
this.point = {};
|
|
166
30
|
this.rect = {};
|
|
167
|
-
this.
|
|
168
|
-
this.
|
|
169
|
-
this.
|
|
170
|
-
this.rect.height = bound.height;
|
|
171
|
-
this.rect.top = bound.top - parentRect.top;
|
|
172
|
-
this.rect.bottom = bound.bottom - parentRect.top;
|
|
173
|
-
this.rect.left = bound.left - parentRect.left;
|
|
174
|
-
this.rect.right = bound.right - parentRect.left;
|
|
175
|
-
},
|
|
176
|
-
|
|
177
|
-
__getBoundStatus: function (e) {
|
|
178
|
-
let bound = this.el.getBoundingClientRect();
|
|
179
|
-
let parentRect = this.el.parentNode.getBoundingClientRect();
|
|
180
|
-
let x = e.clientX - bound.left;// - parentRect.left;
|
|
181
|
-
let y = e.clientY - bound.top;// - parentRect.top;
|
|
182
|
-
|
|
183
|
-
this.__setRectInfo();
|
|
184
|
-
|
|
185
|
-
this.point.x = x;
|
|
186
|
-
this.point.y = y;
|
|
187
|
-
this.point.cx = e.clientX - parentRect.left;
|
|
188
|
-
this.point.cy = e.clientY - parentRect.top;
|
|
189
|
-
|
|
190
|
-
this.boundStatus = {
|
|
191
|
-
isTop: y < this.MARGIN && y > -this.MARGIN,
|
|
192
|
-
isLeft: x < this.MARGIN && x > -this.MARGIN,
|
|
193
|
-
isRight: x >= bound.width - this.RIGHT_SCROL && x <= bound.width + this.MARGIN + (this.MARGIN - this.RIGHT_SCROL),
|
|
194
|
-
isBottom: y >= bound.height - this.MARGIN && y <= bound.height + this.MARGIN
|
|
195
|
-
}
|
|
31
|
+
this.clickedInfo = null;
|
|
32
|
+
this.preSnapped = null;
|
|
33
|
+
this.isSnap = false;
|
|
196
34
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return x >= min && x <= max;
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
__animate: function () {
|
|
205
|
-
let c_info = this.clickedInfo;
|
|
206
|
-
|
|
207
|
-
let eventName = null;
|
|
208
|
-
|
|
209
|
-
if (c_info && c_info.isMoving) {
|
|
210
|
-
/**
|
|
211
|
-
* Ghost Process
|
|
212
|
-
**/
|
|
213
|
-
|
|
214
|
-
let p_w = this.el.parentNode.offsetWidth, p_h = this.el.parentNode.offsetHeight;
|
|
215
|
-
let ghost_info = null;
|
|
216
|
-
|
|
217
|
-
if (this.__between(this.rect.top, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
218
|
-
this.__between(this.rect.left, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
219
|
-
this.__between(p_w - this.rect.right, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
220
|
-
this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.FULLSCREEN_MARGIN)) {
|
|
221
|
-
ghost_info = { x: 0, y: 0, w: p_w, h: p_h, type: "show" }
|
|
222
|
-
} else if (this.__between(this.rect.top, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
223
|
-
ghost_info = { x: 0, y: 0, w: p_w, h: p_h / 2, type: "show" }
|
|
224
|
-
} else if (this.__between(this.rect.left, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
225
|
-
ghost_info = { x: 0, y: 0, w: p_w / 2, h: p_h, type: "show" }
|
|
226
|
-
} else if (this.__between(p_w - this.rect.right, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
227
|
-
ghost_info = { x: p_w / 2, y: 0, w: p_w / 2, h: p_h, type: "show" }
|
|
228
|
-
} else if (this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
229
|
-
ghost_info = { x: 0, y: p_h / 2, w: p_w, h: p_h / 2, type: "show" }
|
|
230
|
-
} else {
|
|
231
|
-
ghost_info = { x: this.rect.left, y: this.rect.top, w: this.rect.width, h: this.rect.height, type: "hide" }
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (ghost_info && !this.isParked && !this.isSnap) {
|
|
235
|
-
this._ghostProcess(ghost_info)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (this.isSnap) {
|
|
239
|
-
this.el.style.left = this.point.cx + 'px';
|
|
240
|
-
this.el.style.top = this.point.cy + 'px';
|
|
241
|
-
this.el.style.width = this.preSnapped.width + 'px';
|
|
242
|
-
this.el.style.height = this.preSnapped.height + 'px';
|
|
243
|
-
this.isSnap = false;
|
|
244
|
-
} else {
|
|
245
|
-
this.el.style.top = (this.point.cy - c_info.y) + 'px';
|
|
246
|
-
this.el.style.left = (this.point.cx - c_info.x) + 'px';
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
eventName = "element-moving";
|
|
250
|
-
if (c_info.isChangeStart) {
|
|
251
|
-
this.clickedInfo.isChangeStart = false;
|
|
252
|
-
eventName = "element-movestart";
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
this.createEvent(eventName)
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
},
|
|
259
|
-
|
|
260
|
-
minMax() {
|
|
261
|
-
if (!this.isSnap) {
|
|
262
|
-
this.isSnap = true;
|
|
263
|
-
this.preSnapped = { x: this.rect.x, y: this.rect.y, width: this.rect.width, height: this.rect.height };
|
|
264
|
-
this.__setBound(this.el, 0, 0, "100%", "100%");
|
|
265
|
-
} else {
|
|
266
|
-
this.isSnap = false;
|
|
267
|
-
this.el.style.left = this.preSnapped.x + 'px';
|
|
268
|
-
this.el.style.top = this.preSnapped.y + 'px';
|
|
269
|
-
this.el.style.width = this.preSnapped.width + 'px';
|
|
270
|
-
this.el.style.height = this.preSnapped.height + 'px';
|
|
271
|
-
}
|
|
272
|
-
},
|
|
273
|
-
|
|
274
|
-
_isMovable() {
|
|
275
|
-
let width = this.rect.width;
|
|
276
|
-
return this.point.x > 0 && this.point.x < width && this.point.y > 0 && this.point.y < this.dragAreaHeight;
|
|
277
|
-
},
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Position Events
|
|
281
|
-
* move: element-moving, element-moveend, element-movestart
|
|
282
|
-
**/
|
|
283
|
-
createEvent(eventName) {
|
|
284
|
-
const event = new CustomEvent(eventName, { detail: { element: this.el, instance: this } });
|
|
285
|
-
this.el.dispatchEvent(event);
|
|
286
|
-
},
|
|
287
|
-
|
|
288
|
-
_createGhost: function (ghostClass) {
|
|
289
|
-
let node = document.createElement("div");
|
|
290
|
-
if (!ghostClass) {
|
|
291
|
-
ghostClass = "snap-ghost";
|
|
292
|
-
}
|
|
35
|
+
this._createGhost(this.container.getAttribute("data-ghost-class"));
|
|
36
|
+
this._initEvent();
|
|
37
|
+
this._selectElement()
|
|
38
|
+
}
|
|
293
39
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (this.el) {
|
|
323
|
-
this.createEvent('elementSelected');
|
|
324
|
-
this.el.style.zIndex = this.container.positionIndex += 1;
|
|
325
|
-
this.el.classList.add('selectedPosition');
|
|
326
|
-
}
|
|
327
|
-
},
|
|
40
|
+
position.prototype = {
|
|
41
|
+
constructor: position,
|
|
42
|
+
|
|
43
|
+
_initEvent: function () {
|
|
44
|
+
let self = this;
|
|
45
|
+
this.iframe = this.el.querySelector('iframe')
|
|
46
|
+
if (this.iframe) {
|
|
47
|
+
this.iframe.addEventListener('load', function () {
|
|
48
|
+
const iframeContent = self.iframe.contentDocument;
|
|
49
|
+
iframeContent.addEventListener('mousedown', function (e) {
|
|
50
|
+
self._selectElement(e.detail.element)
|
|
51
|
+
})
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let positionableEl = this.el
|
|
56
|
+
if (this.dragHandle)
|
|
57
|
+
positionableEl = this.dragHandle
|
|
58
|
+
|
|
59
|
+
positionableEl.addEventListener('touchstart', function (e) {
|
|
60
|
+
self._selectElement(e.detail.element)
|
|
61
|
+
self._onDown(e.touches[0]);
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
positionableEl.addEventListener('mousedown', function (e) {
|
|
65
|
+
self._selectElement(e.detail.element)
|
|
66
|
+
self._onDown(e);
|
|
67
|
+
})
|
|
328
68
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
69
|
+
positionableEl.addEventListener('mousemove', function (e) {
|
|
70
|
+
if (self.clickedInfo && self.clickedInfo.isMoving) {
|
|
71
|
+
e.preventDefault();
|
|
72
|
+
self._onMove(e);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
positionableEl.addEventListener('mouseup', function (e) {
|
|
77
|
+
if (self.clickedInfo && self.clickedInfo.isMoving) {
|
|
78
|
+
self._onUp(e);
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
}
|
|
81
|
+
}, true);
|
|
82
|
+
|
|
83
|
+
positionableEl.addEventListener('touchmove', function (e) {
|
|
84
|
+
self._onMove(e.touches[0]);
|
|
85
|
+
}, { passive: false });
|
|
86
|
+
|
|
87
|
+
positionableEl.addEventListener('touchend', function (e) {
|
|
88
|
+
self._onUp(e.touches[0]);
|
|
89
|
+
}, { passive: false });
|
|
90
|
+
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
_isMovable() {
|
|
94
|
+
if (this.dragHandle)
|
|
95
|
+
return true
|
|
96
|
+
let width = this.rect.width;
|
|
97
|
+
return this.point.x > 0 && this.point.x < width && this.point.y > 0 && this.point.y < this.dragAreaHeight;
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
_onDown: function (e) {
|
|
101
|
+
this.__getBoundStatus(e);
|
|
102
|
+
|
|
103
|
+
this.clickedInfo = {
|
|
104
|
+
x: this.point.x,
|
|
105
|
+
y: this.point.y,
|
|
106
|
+
cx: this.point.cx,
|
|
107
|
+
cy: this.point.cy,
|
|
108
|
+
w: this.rect.width,
|
|
109
|
+
h: this.rect.height,
|
|
110
|
+
isMoving: this._isMovable(),
|
|
111
|
+
boundStatus: this.boundStatus,
|
|
112
|
+
isChangeStart: true
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
_onMove: function (e) {
|
|
117
|
+
this.__getBoundStatus(e)
|
|
118
|
+
this.__animate();
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
_onUp: function (e) {
|
|
122
|
+
if (!this.clickedInfo) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (e) {
|
|
126
|
+
this.__getBoundStatus(e);
|
|
127
|
+
}
|
|
128
|
+
if (this.clickedInfo.isMoving && !this.isParked) {
|
|
129
|
+
let p_w = this.el.parentNode.offsetWidth, p_h = this.el.parentNode.offsetHeight;
|
|
130
|
+
let snap_info = null;
|
|
131
|
+
|
|
132
|
+
if (this.__between(this.rect.top, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
133
|
+
this.__between(this.rect.left, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
134
|
+
this.__between(p_w - this.rect.right, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
135
|
+
this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.FULLSCREEN_MARGIN)) {
|
|
136
|
+
snap_info = { x: 0, y: 0, w: '100%', h: '100%' }
|
|
137
|
+
} else if (this.__between(this.rect.top, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
138
|
+
snap_info = { x: 0, y: 0, w: '100%', h: '50%' }
|
|
139
|
+
} else if (this.__between(this.rect.left, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
140
|
+
snap_info = { x: 0, y: 0, w: '50%', h: '100%' }
|
|
141
|
+
} else if (this.__between(p_w - this.rect.right, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
142
|
+
snap_info = { x: '50%', y: 0, w: '50%', h: '100%' }
|
|
143
|
+
} else if (this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
144
|
+
snap_info = { x: 0, y: '50%', w: '100%', h: '50%' }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (snap_info && !this.isSnap) {
|
|
148
|
+
this.__setBound(this.el, snap_info.x, snap_info.y, snap_info.w, snap_info.h);
|
|
149
|
+
this.preSnapped = { x: this.rect.x, y: this.rect.y, width: this.rect.width, height: this.rect.height };
|
|
150
|
+
this.isSnap = true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let ghost_info = {
|
|
154
|
+
x: this.rect.x,
|
|
155
|
+
y: this.rect.y,
|
|
156
|
+
w: this.rect.width,
|
|
157
|
+
h: this.rect.height
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this._ghostProcess(ghost_info)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (this.clickedInfo.isMoving) {
|
|
164
|
+
this.createEvent('element-moveend');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
this.clickedInfo = null;
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
__setBound: function (el, x, y, w, h) {
|
|
172
|
+
el.style.left = x;
|
|
173
|
+
el.style.top = y;
|
|
174
|
+
el.style.width = w;
|
|
175
|
+
el.style.height = h;
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
__setRectInfo: function () {
|
|
179
|
+
let bound = this.el.getBoundingClientRect();
|
|
180
|
+
let parentRect = this.el.parentNode.getBoundingClientRect();
|
|
181
|
+
this.rect = {};
|
|
182
|
+
this.rect.x = bound.x - parentRect.x;
|
|
183
|
+
this.rect.y = bound.y - parentRect.y;
|
|
184
|
+
this.rect.width = bound.width;
|
|
185
|
+
this.rect.height = bound.height;
|
|
186
|
+
this.rect.top = bound.top - parentRect.top;
|
|
187
|
+
this.rect.bottom = bound.bottom - parentRect.top;
|
|
188
|
+
this.rect.left = bound.left - parentRect.left;
|
|
189
|
+
this.rect.right = bound.right - parentRect.left;
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
__getBoundStatus: function (e) {
|
|
193
|
+
let bound = this.el.getBoundingClientRect();
|
|
194
|
+
let parentRect = this.el.parentNode.getBoundingClientRect();
|
|
195
|
+
let x = e.clientX - bound.left;// - parentRect.left;
|
|
196
|
+
let y = e.clientY - bound.top;// - parentRect.top;
|
|
197
|
+
|
|
198
|
+
this.__setRectInfo();
|
|
199
|
+
|
|
200
|
+
this.point.x = x;
|
|
201
|
+
this.point.y = y;
|
|
202
|
+
this.point.cx = e.clientX - parentRect.left;
|
|
203
|
+
this.point.cy = e.clientY - parentRect.top;
|
|
204
|
+
|
|
205
|
+
this.boundStatus = {
|
|
206
|
+
isTop: y < this.MARGIN && y > -this.MARGIN,
|
|
207
|
+
isLeft: x < this.MARGIN && x > -this.MARGIN,
|
|
208
|
+
isRight: x >= bound.width - this.RIGHT_SCROL && x <= bound.width + this.MARGIN + (this.MARGIN - this.RIGHT_SCROL),
|
|
209
|
+
isBottom: y >= bound.height - this.MARGIN && y <= bound.height + this.MARGIN
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return this.boundStatus;
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
__between: function (x, min, max) {
|
|
216
|
+
return x >= min && x <= max;
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
__animate: function () {
|
|
220
|
+
let c_info = this.clickedInfo;
|
|
221
|
+
|
|
222
|
+
let eventName = null;
|
|
223
|
+
|
|
224
|
+
if (c_info && c_info.isMoving) {
|
|
225
|
+
/**
|
|
226
|
+
* Ghost Process
|
|
227
|
+
**/
|
|
228
|
+
|
|
229
|
+
let p_w = this.el.parentNode.offsetWidth, p_h = this.el.parentNode.offsetHeight;
|
|
230
|
+
let ghost_info = null;
|
|
231
|
+
|
|
232
|
+
if (this.__between(this.rect.top, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
233
|
+
this.__between(this.rect.left, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
234
|
+
this.__between(p_w - this.rect.right, this.NO_SNAP, this.FULLSCREEN_MARGIN) ||
|
|
235
|
+
this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.FULLSCREEN_MARGIN)) {
|
|
236
|
+
ghost_info = { x: 0, y: 0, w: p_w, h: p_h, type: "show" }
|
|
237
|
+
} else if (this.__between(this.rect.top, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
238
|
+
ghost_info = { x: 0, y: 0, w: p_w, h: p_h / 2, type: "show" }
|
|
239
|
+
} else if (this.__between(this.rect.left, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
240
|
+
ghost_info = { x: 0, y: 0, w: p_w / 2, h: p_h, type: "show" }
|
|
241
|
+
} else if (this.__between(p_w - this.rect.right, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
242
|
+
ghost_info = { x: p_w / 2, y: 0, w: p_w / 2, h: p_h, type: "show" }
|
|
243
|
+
} else if (this.__between(p_h - this.rect.bottom, this.NO_SNAP, this.SNAP_MARGIN)) {
|
|
244
|
+
ghost_info = { x: 0, y: p_h / 2, w: p_w, h: p_h / 2, type: "show" }
|
|
245
|
+
} else {
|
|
246
|
+
ghost_info = { x: this.rect.left, y: this.rect.top, w: this.rect.width, h: this.rect.height, type: "hide" }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (ghost_info && !this.isParked && !this.isSnap) {
|
|
250
|
+
this._ghostProcess(ghost_info)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (this.isSnap) {
|
|
254
|
+
this.el.style.left = this.point.cx + 'px';
|
|
255
|
+
this.el.style.top = this.point.cy + 'px';
|
|
256
|
+
this.el.style.width = this.preSnapped.width + 'px';
|
|
257
|
+
this.el.style.height = this.preSnapped.height + 'px';
|
|
258
|
+
this.isSnap = false;
|
|
259
|
+
} else {
|
|
260
|
+
this.el.style.top = (this.point.cy - c_info.y) + 'px';
|
|
261
|
+
this.el.style.left = (this.point.cx - c_info.x) + 'px';
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
eventName = "element-moving";
|
|
265
|
+
if (c_info.isChangeStart) {
|
|
266
|
+
this.clickedInfo.isChangeStart = false;
|
|
267
|
+
eventName = "element-movestart";
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
this.createEvent(eventName)
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
minMax() {
|
|
276
|
+
if (!this.isSnap) {
|
|
277
|
+
this.isSnap = true;
|
|
278
|
+
this.preSnapped = { x: this.rect.x, y: this.rect.y, width: this.rect.width, height: this.rect.height };
|
|
279
|
+
this.__setBound(this.el, 0, 0, "100%", "100%");
|
|
280
|
+
} else {
|
|
281
|
+
this.isSnap = false;
|
|
282
|
+
this.el.style.left = this.preSnapped.x + 'px';
|
|
283
|
+
this.el.style.top = this.preSnapped.y + 'px';
|
|
284
|
+
this.el.style.width = this.preSnapped.width + 'px';
|
|
285
|
+
this.el.style.height = this.preSnapped.height + 'px';
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Position Events
|
|
292
|
+
* move: element-moving, element-moveend, element-movestart
|
|
293
|
+
**/
|
|
294
|
+
createEvent(eventName) {
|
|
295
|
+
const event = new CustomEvent(eventName, { detail: { element: this.el, instance: this } });
|
|
296
|
+
this.el.dispatchEvent(event);
|
|
297
|
+
},
|
|
298
|
+
|
|
299
|
+
_createGhost: function (ghostClass) {
|
|
300
|
+
let node = document.createElement("div");
|
|
301
|
+
if (!ghostClass) {
|
|
302
|
+
ghostClass = "snap-ghost";
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
node.classList.add(ghostClass);
|
|
306
|
+
this.container.appendChild(node);
|
|
307
|
+
this.ghostEl = node;
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
_ghostProcess: function (info) {
|
|
311
|
+
this.ghostEl.style.left = info.x + 'px';
|
|
312
|
+
this.ghostEl.style.top = info.y + 'px';
|
|
313
|
+
this.ghostEl.style.width = info.w + 'px';
|
|
314
|
+
this.ghostEl.style.height = info.h + 'px';
|
|
315
|
+
|
|
316
|
+
if (info.type === "show") {
|
|
317
|
+
if (this.ghostEl.style.display === 'none') {
|
|
318
|
+
this.ghostEl.style.display = 'block'
|
|
319
|
+
}
|
|
320
|
+
this.ghostEl.style.opacity = 0.2;
|
|
321
|
+
} else {
|
|
322
|
+
this.ghostEl.style.opacity = 0;
|
|
323
|
+
if (this.ghostEl.style.display !== 'none') {
|
|
324
|
+
this.ghostEl.style.display = 'none'
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return;
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
_selectElement: function (el) {
|
|
331
|
+
this._releaseSelect();
|
|
332
|
+
this.container['selectedPositionElement'] = this.el
|
|
333
|
+
if (this.el) {
|
|
334
|
+
this.createEvent('elementSelected');
|
|
335
|
+
this.el.style.zIndex = this.container.positionIndex += 1;
|
|
336
|
+
this.el.classList.add('selectedPosition');
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
|
|
340
|
+
_releaseSelect: function () {
|
|
341
|
+
let el = this.container['selectedPositionElement']
|
|
342
|
+
if (el)
|
|
343
|
+
el.classList.remove("selectedPosition");
|
|
344
|
+
},
|
|
334
345
|
}
|
|
335
346
|
|
|
336
347
|
export default position;
|