@@ -10,6 +10,13 @@ import { GridsterUtils } from './gridsterUtils.service';
10
10
11
11
const GRIDSTER_ITEM_RESIZABLE_HANDLER_CLASS = 'gridster-item-resizable-handler' ;
12
12
13
+ enum Direction {
14
+ UP = 'UP' ,
15
+ DOWN = 'DOWN' ,
16
+ LEFT = 'LEFT' ,
17
+ RIGHT = 'RIGHT'
18
+ }
19
+
13
20
export class GridsterDraggable {
14
21
gridsterItem : GridsterItemComponentInterface ;
15
22
gridster : GridsterComponentInterface ;
@@ -163,21 +170,86 @@ export class GridsterDraggable {
163
170
dragMove = ( e : MouseEvent ) : void => {
164
171
e . stopPropagation ( ) ;
165
172
e . preventDefault ( ) ;
166
- GridsterUtils . checkTouchEvent ( e ) ;
167
- this . offsetLeft = this . gridster . el . scrollLeft - this . gridster . el . offsetLeft ;
168
- this . offsetTop = this . gridster . el . scrollTop - this . gridster . el . offsetTop ;
169
- scroll (
170
- this . gridster ,
171
- this . left ,
172
- this . top ,
173
- this . width ,
174
- this . height ,
175
- e ,
176
- this . lastMouse ,
177
- this . calculateItemPositionFromMousePosition
178
- ) ;
179
173
180
- this . calculateItemPositionFromMousePosition ( e ) ;
174
+ // get the directions of the mouse event
175
+ let directions = this . getDirections ( e ) ;
176
+
177
+ if ( this . gridster . options . enableBoundaryControl ) {
178
+ // prevent moving up at the top of gridster
179
+ if (
180
+ directions . includes ( Direction . UP ) &&
181
+ this . gridsterItem . el . getBoundingClientRect ( ) . top <=
182
+ this . gridster . el . getBoundingClientRect ( ) . top + this . margin
183
+ ) {
184
+ directions = directions . filter ( direction => direction != Direction . UP ) ;
185
+ e = new MouseEvent ( e . type , {
186
+ clientX : e . clientX ,
187
+ clientY : this . lastMouse . clientY
188
+ } ) ;
189
+ }
190
+ // prevent moving left at the leftmost column of gridster
191
+ if (
192
+ directions . includes ( Direction . LEFT ) &&
193
+ this . gridsterItem . el . getBoundingClientRect ( ) . left <=
194
+ this . gridster . el . getBoundingClientRect ( ) . left + this . margin
195
+ ) {
196
+ directions = directions . filter (
197
+ direction => direction != Direction . LEFT
198
+ ) ;
199
+ e = new MouseEvent ( e . type , {
200
+ clientX : this . lastMouse . clientX ,
201
+ clientY : e . clientY
202
+ } ) ;
203
+ }
204
+ // prevent moving right at the rightmost column of gridster
205
+ if (
206
+ directions . includes ( Direction . RIGHT ) &&
207
+ this . gridsterItem . el . getBoundingClientRect ( ) . right >=
208
+ this . gridster . el . getBoundingClientRect ( ) . right - this . margin
209
+ ) {
210
+ directions = directions . filter (
211
+ direction => direction != Direction . RIGHT
212
+ ) ;
213
+ e = new MouseEvent ( e . type , {
214
+ clientX : this . lastMouse . clientX ,
215
+ clientY : e . clientY
216
+ } ) ;
217
+ }
218
+ // prevent moving down at the bottom of gridster
219
+ if (
220
+ directions . includes ( Direction . DOWN ) &&
221
+ this . gridsterItem . el . getBoundingClientRect ( ) . bottom >=
222
+ this . gridster . el . getBoundingClientRect ( ) . bottom - this . margin
223
+ ) {
224
+ directions = directions . filter (
225
+ direction => direction != Direction . DOWN
226
+ ) ;
227
+ e = new MouseEvent ( e . type , {
228
+ clientX : e . clientX ,
229
+ clientY : this . lastMouse . clientY
230
+ } ) ;
231
+ }
232
+ }
233
+
234
+ // do not change item location when there is no direction to go
235
+ if ( directions . length ) {
236
+ GridsterUtils . checkTouchEvent ( e ) ;
237
+ this . offsetLeft =
238
+ this . gridster . el . scrollLeft - this . gridster . el . offsetLeft ;
239
+ this . offsetTop = this . gridster . el . scrollTop - this . gridster . el . offsetTop ;
240
+ scroll (
241
+ this . gridster ,
242
+ this . left ,
243
+ this . top ,
244
+ this . width ,
245
+ this . height ,
246
+ e ,
247
+ this . lastMouse ,
248
+ this . calculateItemPositionFromMousePosition
249
+ ) ;
250
+
251
+ this . calculateItemPositionFromMousePosition ( e ) ;
252
+ }
181
253
} ;
182
254
183
255
calculateItemPositionFromMousePosition = ( e : MouseEvent ) : void => {
@@ -472,4 +544,25 @@ export class GridsterDraggable {
472
544
cancelTouchCancel ( ) ;
473
545
}
474
546
} ;
547
+
548
+ /**
549
+ * Returns the list of directions for given mouse event
550
+ * @param e Mouse event
551
+ * */
552
+ private getDirections ( e : MouseEvent ) {
553
+ const directions : string [ ] = [ ] ;
554
+ if ( this . lastMouse . clientY > e . clientY ) {
555
+ directions . push ( Direction . UP ) ;
556
+ }
557
+ if ( this . lastMouse . clientY < e . clientY ) {
558
+ directions . push ( Direction . DOWN ) ;
559
+ }
560
+ if ( this . lastMouse . clientX < e . clientX ) {
561
+ directions . push ( Direction . RIGHT ) ;
562
+ }
563
+ if ( this . lastMouse . clientX > e . clientX ) {
564
+ directions . push ( Direction . LEFT ) ;
565
+ }
566
+ return directions ;
567
+ }
475
568
}
0 commit comments