forked from mauricius/vue-draggable-resizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHowTo.stories.js
292 lines (269 loc) · 7.51 KB
/
HowTo.stories.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
import VueDraggableResizable from '../../src/components/vue-draggable-resizable.vue';
export default {
title: 'How To',
component: VueDraggableResizable
};
export const FrameSelection = () => ({
components: { VueDraggableResizable },
template: `
<div
id="components-container"
@mousedown="mouseDown"
@mouseup="mouseUpfn"
@mousemove="mouseMove">
<vue-draggable-resizable
class-name-active="my-active-class"
v-for="element in elements"
:style="selectStyle(element.isSelected)"
:key="element.id"
:x="element.x"
:y="element.y"
:w="element.w"
:h="element.h"
:resizable="false"
@dragging="(left, top) => dragging(element.id, left, top)"
@dragstop="(left, top) => dragstop(element.id, left, top)"
>
<p>{{ element.text }}</p>
</vue-draggable-resizable>
<!--frame selection-->
<div
:style="{width: rectWidth + 'px',height:rectHeight + 'px',left:rectX+'px',top:rectY+'px'}"
v-show="rectShow"
class="select-box-dashed">
</div>
<div id="toolbar">
Number of selected elements: <span>{{selectedItemNum}}</span>
</div>
</div>
`,
data() {
return {
draggingId: null,
prevOffsetX: 0,
prevOffsetY: 0,
elements: [
{id: 1, x: 10, y: 10, w: 200, h: 200, isSelect: false, text: 'Element 1'},
{id: 2, x: 250, y: 200, w: 200, h: 200, isSelect: false, text: 'Element 2'},
{id: 3, x: 500, y: 300, w: 200, h: 200, isSelect: false, text: 'Element 3'}
],
//frame selection
selectedItemNum:0,
startX: 0, //X of the mouse (begin to move)
startY: 0,
initX: 0, //X of the mouse (moving)
initY: 0,
scrollX: 0,
scrollY: 0,
rectX: 0, //X of the rectangle selection
rectY: 0,
rectWidth: 0, //width of the rectangle selection
rectHeight: 0,
rectShow: false, //weather to show the rectangle selection
}
},
methods: {
dragging(id, left, top) {
this.draggingId = id;
const offsetX = left - this.draggingElement.x;
const offsetY = top - this.draggingElement.y;
const deltaX = this.deltaX(offsetX);
const deltaY = this.deltaY(offsetY);
// if the dragging element is not been selected,clear all selected elements
if(this.draggingElement.isSelected===false){
this.elements.map((el) => { el.isSelected = false; });
this.selectedItemNum=0;
return;
};
this.elements.map(el => {
if (el.id !== id && el.isSelected===true) {
el.x += deltaX;
el.y += deltaY;
}
return el;
});
},
dragstop(id, left, top) {
this.elements.map(el => {
if (el.id === id) {
el.x = left;
el.y = top;
}
return el;
});
this.draggingId = null;
this.prevOffsetX = 0;
this.prevOffsetY = 0;
},
deltaX(offsetX) {
const ret = offsetX - this.prevOffsetX;
this.prevOffsetX = offsetX;
return ret;
},
deltaY(offsetY) {
const ret = offsetY - this.prevOffsetY;
this.prevOffsetY = offsetY;
return ret;
},
mouseDown(e) {
if (e.target.id !== 'components-container') {
return;
}
//clear all selected elements
if (this.selectedItemNum > 0) {
this.elements.map((el) => { el.isSelected = false; });
this.selectedItemNum=0;
}
this.rectSelect = true;//begin to draw the rectangle
this.rectWidth =0;
this.rectHeight=0;
this.startX = e.offsetX;
this.startY = e.offsetY;
this.scrollX = e.clientX - e.offsetX;
this.scrollY = e.clientY - e.offsetY;
},
mouseMove(e) {
if (!this.rectSelect) {
return;
}
this.rectShow = true;
this.initX = e.clientX - this.scrollX;
this.initY = e.clientY - this.scrollY;
this.rectX = Math.min(this.initX, this.startX);
this.rectY = Math.min(this.initY, this.startY);
this.rectWidth = Math.abs(this.initX - this.startX);
this.rectHeight = Math.abs(this.initY - this.startY);
this.clearBubble(e);
},
mouseUpfn(e) {
if (this.rectSelect) {
if (this.rectWidth > 10 && this.rectHeight > 10) {
this.elements.map((el)=>{
const elLeft=el.x;
const elRight=el.x+el.w;
const elTop=el.y;
const elBottom=el.y+el.h;
if (elLeft > this.rectX && elTop > this.rectY && (elRight < (this.rectX + this.rectWidth)) && (elBottom < (this.rectY + this.rectHeight))) {
el.isSelected = true;
this.selectedItemNum+=1;
}
});
}
this.rectShow = false;
this.rectSelect = false;
this.clearBubble(e);
}
},
clearBubble(e) {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
},
selectStyle(isSelected) {
return isSelected? "border:1px solid blue !important" :null;
}
},
computed: {
draggingElement: function () {
if (! this.draggingId) return;
return this.elements.find(el => el.id === this.draggingId);
},
}
})
export const DragMultiple = () => ({
components: { VueDraggableResizable },
template: `
<div>
<vue-draggable-resizable
class-name-active="my-active-class"
v-for="element in elements"
:key="element.id"
:x="element.x"
:y="element.y"
:w="200"
:h="200"
:resizable="false"
@dragging="(left, top) => dragging(element.id, left, top)"
@dragstop="(left, top) => dragstop(element.id, left, top)"
>
<p>{{ element.text }} {{ sync ? '(synchronized)' : null }}</p>
</vue-draggable-resizable>
</div>
`,
data() {
return {
sync: false,
draggingId: null,
prevOffsetX: 0,
prevOffsetY: 0,
elements: [
{id: 1, x: 0, y: 0, text: 'Element 1'},
{id: 2, x: 200, y: 200, text: 'Element 2'}
]
}
},
mounted() {
window.addEventListener('keydown', ev => {
if (ev.keyCode === 17) {
this.sync = true;
}
});
window.addEventListener('keyup', ev => {
if (ev.keyCode === 17) {
this.sync = false;
}
});
},
methods: {
dragging(id, left, top) {
this.draggingId = id;
if (! this.sync) return;
const offsetX = left - this.draggingElement.x;
const offsetY = top - this.draggingElement.y;
const deltaX = this.deltaX(offsetX);
const deltaY = this.deltaY(offsetY);
this.elements.map(el => {
if (el.id !== id) {
el.x += deltaX;
el.y += deltaY;
}
return el;
});
},
dragstop(id, left, top) {
this.elements.map(el => {
if (el.id === id) {
el.x = left;
el.y = top;
}
return el;
});
this.draggingId = null;
this.prevOffsetX = 0;
this.prevOffsetY = 0;
},
deltaX(offsetX) {
const ret = offsetX - this.prevOffsetX;
this.prevOffsetX = offsetX;
return ret;
},
deltaY(offsetY) {
const ret = offsetY - this.prevOffsetY;
this.prevOffsetY = offsetY;
return ret;
}
},
computed: {
draggingElement: function () {
if (! this.draggingId) return;
return this.elements.find(el => el.id === this.draggingId);
}
}
})