forked from jifalops/drag-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag-resize.js
484 lines (432 loc) · 14.3 KB
/
drag-resize.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
CSS custom property | Description | Default
------------------- | ----------- | -------
`--drag-resize-handle-size` | draggable edge/corner size | 8px;
`--drag-resize-corner-color` | color for resizable corner | '';
`--drag-resize-edge-padding` | padding for resizable edges | '';
`--drag-resize-edge-border` | border for resizable edges | '';
`--drag-resize-corner-border` | border for resizable corners | '';
`--drag-resize-container` | Mixin applied to resize container | {};
`--drag-resize-move-cursor` | move cursor | move;
`--drag-resize-top-cursor` | top cursor | ns-resize;
`--drag-resize-bottom-cursor` | bottom cursor | ns-resize;
`--drag-resize-left-cursor` | left cursor | ew-resize;
`--drag-resize-right-cursor` | right cursor | ew-resize;
`--drag-resize-top-left-cursor` | top-left cursor | nw-resize;
`--drag-resize-top-right-cursor` | top-right cursor | ne-resize;
`--drag-resize-bottom-left-cursor` | bottom-left cursor | sw-resize;
`--drag-resize-bottom-right-cursor` | bottom-right cursor | se-resize;
*/
import { PolymerElement,html } from '../@polymer/polymer/polymer-element.js';
import { GestureEventListeners } from '../@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import { FlattenedNodesObserver } from '../@polymer/polymer/lib/utils/flattened-nodes-observer.js';
import '../@polymer/iron-flex-layout/iron-flex-layout.js';
import '../@webcomponents/shadycss/entrypoints/apply-shim.js';
/**
* `drag-resize`
* A touch friendly draggable and/or resizable container.
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class DragResize extends GestureEventListeners(PolymerElement) {
static get properties() {
return {
/**
* Which directions the container can be dragged.
* Any combination of `up`, `down`, `left`, or `right`; or `none`.
*/
drag: {
type: String,
value: 'up down left right',
observer: '_dragChanged',
reflectToAttribute: true
},
/**
* Which edges allow resize.
* Any combination of `top`, `bottom`, `left`, or `right`; or `none`.
* A corner is enabled when both of its adjacent sides are enabled.
*/
resize: {
type: String,
value: 'top bottom left right',
observer: '_resizeChanged',
reflectToAttribute: true
},
/**
* True if draggable in at least one direction.
*/
draggable: {
type: Boolean,
value: false,
readOnly: true,
reflectToAttribute: true
},
/**
* True if resizable on at least one edge.
*/
resizable: {
type: Boolean,
value: false,
readOnly: true,
reflectToAttribute: true
},
left: {
type: Number,
notify: true
},
top: {
type: Number,
notify: true
},
width: {
type: Number,
notify: true
},
height: {
type: Number,
notify: true
},
scale: {
type: Number,
value: 1
}
};
}
static get template() {
return html`
<style>
:host {
display: inline-block;
--drag-resize-handle-size: 8px;
--drag-resize-move-cursor: move;
--drag-resize-top-cursor: ns-resize;
--drag-resize-bottom-cursor: ns-resize;
--drag-resize-left-cursor: ew-resize;
--drag-resize-right-cursor: ew-resize;
--drag-resize-top-left-cursor: nw-resize;
--drag-resize-top-right-cursor: ne-resize;
--drag-resize-bottom-left-cursor: sw-resize;
--drag-resize-bottom-right-cursor: se-resize;
--drag-resize-corner-background: white;
--drag-resize-border-color: #333;
--drag-resize-border-width: 2px;
--drag-resize-edge-border: var(--drag-resize-border-width) dashed var(--drag-resize-border-color);
--drag-resize-corner-border: var(--drag-resize-border-width) solid var(--drag-resize-border-color);
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
position: relative;
box-sizing: border-box;
display: block;
@apply --drag-resize-container;
}
:host([resize~=top]) {
padding-top: var(--drag-resize-edge-padding);
}
:host([resize~=bottom]) {
padding-bottom: var(--drag-resize-edge-padding);
}
:host([resize~=left]) {
padding-left: var(--drag-resize-edge-padding);
}
:host([resize~=right]) {
padding-right: var(--drag-resize-edge-padding);
}
:host([draggable]) #overlay {
cursor: var(--drag-resize-move-cursor);
z-index: 1000;
}
:host div {
position: absolute;
}
#overlay {
top: 0;
left: 0;
bottom: 0;
right: 0;
box-sizing: border-box;
margin: calc(-1 * var(--drag-resize-border-width));
}
.top.edge, .bottom.edge {
height: var(--drag-resize-handle-size);
left: 0;
right: 0;
}
.left.edge, .right.edge {
width: var(--drag-resize-handle-size);
top: 0;
bottom: 0;
}
.corner {
width: calc(var(--drag-resize-handle-size));
height: calc(var(--drag-resize-handle-size));
background: var(--drag-resize-corner-background);
border: var(--drag-resize-corner-border);
}
.top.edge {
top: 0;
border-top: var(--drag-resize-edge-border);
cursor: var(--drag-resize-top-cursor);
}
.bottom.edge {
bottom: 0;
border-bottom: var(--drag-resize-edge-border);
cursor: var(--drag-resize-bottom-cursor);
}
.left.edge {
left: 0;
border-left: var(--drag-resize-edge-border);
cursor: var(--drag-resize-left-cursor);
}
.right.edge {
right: 0;
border-right: var(--drag-resize-edge-border);
cursor: var(--drag-resize-right-cursor);
}
.top.corner {
top: calc(var(--drag-resize-handle-size) / -2);
}
.bottom.corner {
bottom: calc(var(--drag-resize-handle-size) / -2);
}
.left.corner {
left: calc(var(--drag-resize-handle-size) / -2);
}
.right.corner {
right: calc(var(--drag-resize-handle-size) / -2);
}
.top.left {
cursor: var(--drag-resize-top-left-cursor);
}
.top.right {
cursor: var(--drag-resize-top-right-cursor);
}
.bottom.left {
cursor: var(--drag-resize-bottom-left-cursor);
}
.bottom.right {
cursor: var(--drag-resize-bottom-right-cursor);
}
</style>
<slot></slot>
<div id="overlay" draggable on-track="_onDrag">
<div draggable class="top edge" hidden$="[[!resizeTop]]" on-track="_onResize"></div>
<div draggable class="right edge" hidden$="[[!resizeRight]]" on-track="_onResize"></div>
<div draggable class="bottom edge" hidden$="[[!resizeBottom]]" on-track="_onResize"></div>
<div draggable class="left edge" hidden$="[[!resizeLeft]]" on-track="_onResize"></div>
<div draggable class="top left corner" hidden$="[[!resizeTopLeft]]" on-track="_onResize"></div>
<div draggable class="top right corner" hidden$="[[!resizeTopRight]]" on-track="_onResize"></div>
<div draggable class="bottom left corner" hidden$="[[!resizeBottomLeft]]" on-track="_onResize"></div>
<div draggable class="bottom right corner" hidden$="[[!resizeBottomRight]]" on-track="_onResize"></div>
</div>
`;
}
static get observers() {
return [
'updateSize(width,height)',
'updatePosition(top,left)'
];
}
updateSize(width,height) {
if (this.box && typeof width != "undefined" && typeof height != "undefined") {
this.box.style.height = height + 'px';
this.box.style.width = width + 'px';
}
}
updatePosition(top,left) {
if (typeof top != "undefined" && typeof left != "undefined") {
this.style.top = `${top}px`;
this.style.left = `${left}px`;
}
}
connectedCallback() {
super.connectedCallback();
let slot = this.shadowRoot.querySelector('slot');
this._observer = new FlattenedNodesObserver(slot,
info => {
this.box = slot.assignedElements({flatten:true})
.filter(n => n.hasAttribute('drag-content'))[0];
if (typeof this.top != "undefined")
this.initialTop = this.top;
else
this.initialTop = this.offsetTop
if (typeof this.left != "undefined")
this.initialLeft = this.left;
else
this.initialLeft = this.offsetLeft
if (typeof this.height != "undefined")
this.initialHeight = this.height;
else if(this.box)
this.initialHeight = this.box.offsetHeight
if (typeof this.width != "undefined")
this.initialWidth = this.width;
else if(this.box)
this.initialWidth = this.box.offsetWidth
this.top = this.initialTop;
this.left = this.initialLeft;
this.updatePosition(this.top,this.left);
this.height = this.initialHeight;
this.width = this.initialWidth;
this.updateSize(this.width,this.height);
});
}
disconnectedCallback() {
super.disconnectedCallback();
this._observer.disconnect();
}
reset() {
if (!this.box) return;
this.height = this.initialHeight;
this.width = this.initialWidth;
this.top = this.initialTop;
this.left = this.initialLeft;
}
_resizeChanged(edges) {
if (typeof edges != 'string') {
this._setDraggable(false);
return;
}
this.resizeTop = edges.includes('top');
this.resizeBottom = edges.includes('bottom');
this.resizeLeft = edges.includes('left');
this.resizeRight = edges.includes('right');
this.resizeTopLeft = this.resizeTop && this.resizeLeft;
this.resizeTopRight = this.resizeTop && this.resizeRight;
this.resizeBottomLeft = this.resizeBottom && this.resizeLeft;
this.resizeBottomRight = this.resizeBottom && this.resizeRight;
this._setResizable(this.resizeTop || this.resizeBottom
|| this.resizeLeft || this.resizeRight);
}
_dragChanged(drag) {
if (typeof drag != 'string') {
this._setResizable(false);
return;
}
this.dragUp = drag.includes('up');
this.dragDown = drag.includes('down');
this.dragLeft = drag.includes('left');
this.dragRight = drag.includes('right');
this._setDraggable(this.dragUp || this.dragDown
|| this.dragLeft || this.dragRight);
}
_onResize(e, track) {
if (!this.resizable) return;
// Stop from dragging as well.
e.stopPropagation();
// Fix for half-pixel ddx/y when touching.
const dx = Math.round((track.ddx || 0)/this.scale);
const dy = Math.round((track.ddy || 0)/this.scale);
// if (track.state == 'start') {
// console.log('resizing', e.target.id, this.box);
// }
// Remember top-left point between runs.
if (this.top === undefined) this.top = this.box.offsetTop || 0;
if (this.left === undefined) this.left = this.box.offsetLeft || 0;
if (this.height === undefined) this.height = this.box.offsetHeight || 0;
if (this.width === undefined) this.width = this.box.offsetWidth || 0;
// Values to change
let top = Number(this.top);
let left = Number(this.left);
let height = Number(this.height);
let width = Number(this.width);
let changed = false;
// If this line is moved to the top, resizing fails.
const translate = this._sidesToTranslate(this.box);
if (e.target.classList.contains('top')) {
height += -dy;
if (height < 0) height = 0;
else if (translate.top) top += dy;
changed = true;
} else if (e.target.classList.contains('bottom')) {
height += dy;
if (height < 0) height = 0;
else if (translate.bottom) top += -dy;
changed = true;
}
if (e.target.classList.contains('left')) {
width += -dx;
if (width < 0) width = 0;
else if (translate.left) left += dx;
changed = true;
} else if (e.target.classList.contains('right')) {
width += dx;
if (width < 0) width = 0;
else if (translate.right) left += dx;
changed = true;
}
if (changed) {
this.fire('resize', track);
this.top = top;
this.left = left;
this.width = width;
this.height = height;
}
if (track.state == 'end') {
e.target.blur();
}
}
_onDrag(e, track) {
if (!this.draggable) return;
const dx = Math.round((track.dx || 0)/this.scale);
const dy = Math.round((track.dy || 0)/this.scale);
// Remember top-left point between runs.
if (this.top === undefined) this.top = this.box.offsetTop || 0;
if (this.left === undefined) this.left = this.box.offsetLeft || 0;
if (track.state == 'start') {
// console.log('dragging...');
this._dragStartTop = Number(this.top);
this._dragStartLeft = Number(this.left);
}
// Values to change
let top = this._dragStartTop;
let left = this._dragStartLeft;
let changed = false;
if ((this.dragUp && dy <= 0) || this.dragDown && dy >= 0) {
top += dy;
changed = true;
}
if ((this.dragLeft && dx <= 0) || this.dragRight && dx >= 0) {
left += dx;
changed = true;
}
// console.log(top, left, dx, dy)
if (changed) {
this.fire('drag', track);
this.top = top;
this.left = left;
}
}
_sidesToTranslate(element) {
let top = this.offsetTop;
let left = this.offsetLeft;
return {
top: this.offsetTop == top,
left: this.offsetLeft == left,
bottom: this.offsetTop == top - 2,
right: this.offsetLeft == left - 2,
};
}
/** Copy the Polymer 1.x fire implementation. */
fire(name, data, options = {}) {
options.detail = data;
if (options.bubbles === undefined) options.bubbles = true;
if (options.composed === undefined) options.composed = true;
this.dispatchEvent(new CustomEvent(name, options));
}
/**
* Triggered when the element is resized.
* @event resize
* @param {track} object The on-track event detail.
*/
/**
* Triggered when the element is dragged.
* @event drag
* @param {track} object The on-track event detail.
*/
}
window.customElements.define('drag-resize', DragResize);