Skip to content

Commit 18c279d

Browse files
committed
implements jbaysolutions#35, release 1.0.3
1 parent d44e7e3 commit 18c279d

File tree

10 files changed

+185
-53
lines changed

10 files changed

+185
-53
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
1.0.3 (Aug 16, 2017)
4+
5+
* merged #85, support for dragIgnoreFrom and resizeIgnoreFrom
6+
* implemented #35, resize, resized, move and moved events
7+
8+
39
1.0.2 (Apr 18, 2017)
410

511
* fixed #40 properly

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,83 @@ npm install vue-grid-layout
118118
````
119119

120120

121+
### Events
122+
123+
Move and resize event listeners can be added to each grid-item, so that the parent Vue can be notified when a grid element is being moved or resized.
124+
Moved and resized event listeners can be added, if the only notification needed is when an item is finished moving or resizing.
125+
126+
Working example [here](https://jbaysolutions.github.io/vue-grid-layout/examples/02-events.html)
127+
128+
````html
129+
130+
<grid-layout
131+
:layout="layout"
132+
:col-num="12"
133+
:row-height="30"
134+
:is-draggable="true"
135+
:is-resizable="true"
136+
:vertical-compact="true"
137+
:margin="[10, 10]"
138+
:use-css-transforms="true"
139+
>
140+
141+
<grid-item v-for="item in layout"
142+
:x="item.x"
143+
:y="item.y"
144+
:w="item.w"
145+
:h="item.h"
146+
:i="item.i"
147+
@resize="resizeEvent"
148+
@move="moveEvent"
149+
@resized="resizedEvent"
150+
@moved="movedEvent">
151+
{{item.i}}
152+
</grid-item>
153+
</grid-layout>
154+
````
155+
156+
* Move event: every time an item is being moved and changes position
157+
158+
```javascript
159+
moveEvent: function(i, newX, newY){
160+
console.log("MOVE i=" + i + ", X=" + newX + ", Y=" + newY);
161+
},
162+
```
163+
164+
* Resize event: every time an item is being resized and changes size
165+
166+
```javascript
167+
resizeEvent: function(i, newH, newW){
168+
console.log("RESIZE i=" + i + ", H=" + newH + ", W=" + newW);
169+
},
170+
```
171+
172+
* Moved event: every time an item is finished being moved and changes position
173+
174+
```javascript
175+
movedEvent: function(i, newX, newY){
176+
console.log("MOVED i=" + i + ", X=" + newX + ", Y=" + newY);
177+
},
178+
```
179+
180+
* Resized event: every time an item is finished being resized and changes size
181+
182+
```javascript
183+
/**
184+
*
185+
* @param i the item id/index
186+
* @param newH new height in grid rows
187+
* @param newW new width in grid columns
188+
* @param newHPx new height in pixels
189+
* @param newWPx new width in pixels
190+
*
191+
*/
192+
resizedEvent: function(i, newH, newW, newHPx, newWPx){
193+
console.log("RESIZED i=" + i + ", H=" + newH + ", W=" + newW + ", H(px)=" + newHPx + ", W(px)=" + newWPx);
194+
},
195+
```
196+
197+
121198
## Contribute
122199

123200
If you have a feature request, please add it as an issue or make a pull request.

dist/vue-grid-layout.js

Lines changed: 45 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-grid-layout.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-grid-layout.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ <h1>Vue Grid Layout</h1>
4343
:h.sync="item.h"
4444
:min-w="2"
4545
:min-h="2"
46-
:i="item.i">
46+
:i="item.i"
47+
@resize="resize"
48+
@move="move"
49+
@resized="resized"
50+
@moved="moved"
51+
>
4752
<test-element :text="item.i"></test-element>
4853
</grid-item>
4954
</grid-layout>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-grid-layout",
3-
"version": "1.1.1",
3+
"version": "1.0.3",
44
"description": "A draggable and resizable grid layout, as a Vue component.",
55
"keywords": [
66
"grid",

src/GridItem.vue

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@
176176
lastY: NaN,
177177
lastW: NaN,
178178
lastH: NaN,
179-
style: {}
179+
style: {} ,
180+
181+
previousW: null,
182+
previousH: null,
183+
previousX: null,
184+
previousY: null,
180185
}
181186
},
182187
ready: function() {
@@ -191,7 +196,7 @@
191196
192197
var self = this;
193198
if (this.isDraggable) {
194-
if (this.interactObj == null) {
199+
if (this.interactObj === null || this.interactObj === undefined) {
195200
this.interactObj = interact(this.$els.item, {ignoreFrom: this.dragIgnoreFrom});
196201
}
197202
this.interactObj
@@ -202,7 +207,7 @@
202207
});
203208
}
204209
if (this.isResizable) {
205-
if (this.interactObj == null) {
210+
if (this.interactObj === null) {
206211
this.interactObj = interact(this.$els.item, {ignoreFrom: this.resizeIgnoreFrom});
207212
}
208213
this.interactObj
@@ -279,12 +284,15 @@
279284
handleResize: function(event) {
280285
const position = getControlPosition(event);
281286
// Get the current drag point from the event. This is used as the offset.
282-
if (position == null) return; // not possible but satisfies flow
287+
if (position === null) return; // not possible but satisfies flow
283288
const {x, y} = position;
284289
285290
const newSize = {width: 0, height: 0};
286291
switch (event.type) {
287292
case "resizestart":
293+
this.previousW = this.w;
294+
this.previousH = this.h;
295+
288296
var pos = this.calcPosition(this.x, this.y, this.w, this.h);
289297
newSize.width = pos.width;
290298
newSize.height = pos.height;
@@ -341,6 +349,13 @@
341349
this.lastW = x;
342350
this.lastH = y;
343351
352+
if (this.w !== pos.w || this.h !== pos.h) {
353+
this.$dispatch("resize", this.i, pos.h, pos.w);
354+
}
355+
if (event.type === "resizeend" && (this.previousW !== this.w || this.previousH !== this.h)) {
356+
this.$dispatch("resized", this.i, pos.h, pos.w, newSize.height, newSize.width);
357+
}
358+
344359
this.$dispatch("resizeEvent", event.type, this.i, this.x, this.y, this.h, this.w);
345360
},
346361
handleDrag(event) {
@@ -349,14 +364,17 @@
349364
const position = getControlPosition(event);
350365
351366
// Get the current drag point from the event. This is used as the offset.
352-
if (position == null) return; // not possible but satisfies flow
367+
if (position === null) return; // not possible but satisfies flow
353368
const {x, y} = position;
354369
355370
var shouldUpdate = false;
356371
357372
const newPosition = {top: 0, left: 0};
358373
switch (event.type) {
359374
case "dragstart":
375+
this.previousX = this.x;
376+
this.previousY = this.y;
377+
360378
var parentRect = event.target.offsetParent.getBoundingClientRect();
361379
var clientRect = event.target.getBoundingClientRect();
362380
newPosition.left = clientRect.left - parentRect.left;
@@ -395,6 +413,13 @@
395413
this.lastX = x;
396414
this.lastY = y;
397415
416+
if (this.x !== pos.x || this.y !== pos.y) {
417+
this.$dispatch("move", this.i, pos.x, pos.y);
418+
}
419+
if (event.type === "dragend" && (this.previousX !== this.x || this.previousY !== this.y)) {
420+
this.$dispatch("moved", this.i, pos.x, pos.y);
421+
}
422+
398423
this.$dispatch("dragEvent", event.type, this.i, this.x, this.y, this.w, this.h);
399424
},
400425
calcPosition: function(x, y, w, h) {

src/GridLayout.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
},
170170
events: {
171171
dragEvent: function (eventName, id, x, y, w, h) {
172-
if (eventName == "dragmove" || eventName == "dragstart") {
172+
if (eventName === "dragmove" || eventName === "dragstart") {
173173
this.isDragging = true;
174174
this.placeholder.i = id;
175175
this.placeholder.x = x;
@@ -190,7 +190,7 @@
190190
this.updateHeight();
191191
},
192192
resizeEvent: function (eventName, id, x, y, h, w) {
193-
if (eventName == "resizestart" || eventName == "resizemove") {
193+
if (eventName === "resizestart" || eventName === "resizemove") {
194194
this.isDragging = true;
195195
this.placeholder.i = id;
196196
this.placeholder.x = x;

src/app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,19 @@ new Vue({
7272
var item = {"x":0,"y":0,"w":2,"h":2,"i":this.index+"", whatever: "bbb"};
7373
this.index++;
7474
this.layout.push(item);
75-
}
75+
} ,
76+
move: function(i, newX, newY){
77+
console.log("MOVE i=" + i + ", X=" + newX + ", Y=" + newY);
78+
},
79+
resize: function(i, newH, newW){
80+
console.log("RESIZE i=" + i + ", H=" + newH + ", W=" + newW);
81+
},
82+
moved: function(i, newX, newY){
83+
console.log("### MOVED i=" + i + ", X=" + newX + ", Y=" + newY);
84+
},
85+
resized: function(i, newH, newW){
86+
console.log("### RESIZED i=" + i + ", H=" + newH + ", W=" + newW);
87+
},
7688
},
7789
});
7890

0 commit comments

Comments
 (0)