@@ -10,21 +10,24 @@ var testLayout = [
10
10
{ "x" :4 , "y" :5 , "w" :2 , "h" :5 , "i" :"8" } ,
11
11
{ "x" :5 , "y" :10 , "w" :4 , "h" :3 , "i" :"9" } ,
12
12
] ;
13
+ var mouseXY = { "x" :null , "y" :null } ;
14
+ var DragPos = { "x" :null , "y" :null , "w" :1 , "h" :1 , "i" :null } ;
15
+
16
+ document . addEventListener ( "dragover" , function ( e ) {
17
+ mouseXY . x = e . clientX ;
18
+ mouseXY . y = e . clientY ;
19
+ } , false ) ;
13
20
14
- let DragPos = { "x" :null , "y" :null , "w" :1 , "h" :1 , "i" :null } ;
15
21
new Vue ( {
16
22
el : '#app' ,
17
23
data : {
18
24
layout : testLayout ,
19
25
} ,
20
26
methods : {
21
- dragstart :function ( e ) {
22
- } ,
23
27
drag :function ( e ) {
24
-
25
28
let parentRect = document . getElementById ( 'content' ) . getBoundingClientRect ( ) ;
26
29
let mouseInGrid = false ;
27
- if ( ( ( e . x > parentRect . left ) && ( e . x < parentRect . right ) ) && ( ( e . y > parentRect . top ) && ( e . y < parentRect . bottom ) ) ) {
30
+ if ( ( ( mouseXY . x > parentRect . left ) && ( mouseXY . x < parentRect . right ) ) && ( ( mouseXY . y > parentRect . top ) && ( mouseXY . y < parentRect . bottom ) ) ) {
28
31
mouseInGrid = true ;
29
32
}
30
33
if ( mouseInGrid == true && ( this . layout . findIndex ( item => item . i === 'drop' ) ) == - 1 ) {
@@ -43,8 +46,8 @@ new Vue({
43
46
} catch {
44
47
}
45
48
let el = this . $refs . gridLayout . $children [ index ] ;
46
- el . dragging = { "top" :e . y - parentRect . top , "left" :e . x - parentRect . left } ;
47
- let new_pos = el . calcXY ( e . y - parentRect . top , e . x - parentRect . left ) ;
49
+ el . dragging = { "top" :mouseXY . y - parentRect . top , "left" :mouseXY . x - parentRect . left } ;
50
+ let new_pos = el . calcXY ( mouseXY . y - parentRect . top , mouseXY . x - parentRect . left ) ;
48
51
49
52
if ( mouseInGrid == true ) {
50
53
this . $refs . gridLayout . dragEvent ( 'dragstart' , 'drop' , new_pos . x , new_pos . y , 1 , 1 ) ;
@@ -58,15 +61,18 @@ new Vue({
58
61
59
62
} ,
60
63
dragend :function ( e ) {
64
+
61
65
let parentRect = document . getElementById ( 'content' ) . getBoundingClientRect ( ) ;
62
66
let mouseInGrid = false ;
63
- if ( ( ( e . x > parentRect . left ) && ( e . x < parentRect . right ) ) && ( ( e . y > parentRect . top ) && ( e . y < parentRect . bottom ) ) ) {
67
+ if ( ( ( mouseXY . x > parentRect . left ) && ( mouseXY . x < parentRect . right ) ) && ( ( mouseXY . y > parentRect . top ) && ( mouseXY . y < parentRect . bottom ) ) ) {
64
68
mouseInGrid = true ;
65
69
}
66
70
if ( mouseInGrid == true ) {
67
71
alert ( `Dropped element props:\n${ JSON . stringify ( DragPos , [ 'x' , 'y' , 'w' , 'h' ] , 2 ) } ` ) ;
72
+ this . $refs . gridLayout . dragEvent ( 'dragend' , 'drop' , DragPos . x , DragPos . y , 1 , 1 ) ;
73
+ this . layout = this . layout . filter ( obj => obj . i !== 'drop' ) ;
68
74
69
- // UNCOMMENT below if you want to add a GridLayout item
75
+ // UNCOMMENT below if you want to add a grid- item
70
76
/*
71
77
this.layout.push({
72
78
x: DragPos.x,
@@ -81,8 +87,9 @@ new Vue({
81
87
} catch {
82
88
}
83
89
*/
90
+
84
91
}
85
- } ,
92
+ } ,
86
93
} ,
87
94
} ) ;
88
95
0 commit comments