@@ -5,6 +5,8 @@ import {GridsterUtils} from './gridsterUtils.service';
5
5
import { GridsterItemComponent } from './gridsterItem.component' ;
6
6
import { GridsterGridComponent } from './gridsterGrid.component' ;
7
7
import { GridsterItem } from './gridsterItem.interface' ;
8
+ import { GridsterEmptyCell } from './gridsterEmptyCell.service' ;
9
+ import { GridsterCompact } from './gridsterCompact.service' ;
8
10
9
11
@Component ( {
10
12
selector : 'gridster' ,
@@ -15,7 +17,6 @@ export class GridsterComponent implements OnInit, OnDestroy {
15
17
@Input ( ) options : GridsterConfig ;
16
18
calculateLayoutDebounce : Function ;
17
19
movingItem : GridsterItem ;
18
- initialItem : GridsterItem ;
19
20
previewStyle : Function ;
20
21
el : any ;
21
22
$options : GridsterConfig ;
@@ -28,14 +29,10 @@ export class GridsterComponent implements OnInit, OnDestroy {
28
29
curColWidth : number ;
29
30
curRowHeight : number ;
30
31
windowResize : Function ;
31
- emptyCellClick : Function ;
32
- emptyCellDrop : Function ;
33
- emptyCellDrag : Function ;
34
- emptyCellMMove : Function ;
35
- emptyCellUp : Function ;
36
- emptyCellMove : Function ;
37
32
gridLines : GridsterGridComponent ;
38
33
dragInProgress : boolean ;
34
+ emptyCell : GridsterEmptyCell ;
35
+ compact : GridsterCompact ;
39
36
40
37
static checkCollisionTwoItems ( item : GridsterItem , item2 : GridsterItem ) : boolean {
41
38
return item . x < item2 . x + item2 . cols
@@ -44,7 +41,7 @@ export class GridsterComponent implements OnInit, OnDestroy {
44
41
&& item . y + item . rows > item2 . y ;
45
42
}
46
43
47
- constructor ( el : ElementRef , public renderer : Renderer2 , private cdRef : ChangeDetectorRef ) {
44
+ constructor ( el : ElementRef , public renderer : Renderer2 , public cdRef : ChangeDetectorRef ) {
48
45
this . el = el . nativeElement ;
49
46
this . $options = JSON . parse ( JSON . stringify ( GridsterConfigService ) ) ;
50
47
this . mobile = false ;
@@ -64,6 +61,8 @@ export class GridsterComponent implements OnInit, OnDestroy {
64
61
this . $options . emptyCellClickCallback = undefined ;
65
62
this . $options . emptyCellDropCallback = undefined ;
66
63
this . $options . emptyCellDragCallback = undefined ;
64
+ this . emptyCell = new GridsterEmptyCell ( this ) ;
65
+ this . compact = new GridsterCompact ( this ) ;
67
66
}
68
67
69
68
ngOnInit ( ) : void {
@@ -106,27 +105,7 @@ export class GridsterComponent implements OnInit, OnDestroy {
106
105
this . windowResize ( ) ;
107
106
this . windowResize = null ;
108
107
}
109
- if ( this . $options . enableEmptyCellClick && ! this . emptyCellClick && this . $options . emptyCellClickCallback ) {
110
- this . emptyCellClick = this . renderer . listen ( this . el , 'click' , this . emptyCellClickCb . bind ( this ) ) ;
111
- } else if ( ! this . $options . enableEmptyCellClick && this . emptyCellClick ) {
112
- this . emptyCellClick ( ) ;
113
- this . emptyCellClick = null ;
114
- }
115
- if ( this . $options . enableEmptyCellDrop && ! this . emptyCellDrop && this . $options . emptyCellDropCallback ) {
116
- this . emptyCellDrop = this . renderer . listen ( this . el , 'drop' , this . emptyCellDragDrop . bind ( this ) ) ;
117
- this . emptyCellMove = this . renderer . listen ( this . el , 'dragover' , this . emptyCellDragOver . bind ( this ) ) ;
118
- } else if ( ! this . $options . enableEmptyCellDrop && this . emptyCellDrop ) {
119
- this . emptyCellDrop ( ) ;
120
- this . emptyCellMove ( ) ;
121
- this . emptyCellMove = null ;
122
- this . emptyCellDrop = null ;
123
- }
124
- if ( this . $options . enableEmptyCellDrag && ! this . emptyCellDrag && this . $options . emptyCellDragCallback ) {
125
- this . emptyCellDrag = this . renderer . listen ( this . el , 'mousedown' , this . emptyCellMouseDown . bind ( this ) ) ;
126
- } else if ( ! this . $options . enableEmptyCellDrag && this . emptyCellDrag ) {
127
- this . emptyCellDrag ( ) ;
128
- this . emptyCellDrag = null ;
129
- }
108
+ this . emptyCell . updateOptions ( ) ;
130
109
}
131
110
132
111
optionsChanged ( ) : void {
@@ -145,115 +124,6 @@ export class GridsterComponent implements OnInit, OnDestroy {
145
124
}
146
125
}
147
126
148
- emptyCellClickCb ( e ) : void {
149
- if ( GridsterUtils . checkContentClassForEvent ( this , e ) ) {
150
- return ;
151
- }
152
- const item = this . getValidItemFromEvent ( e ) ;
153
- if ( ! item || this . movingItem ) {
154
- return ;
155
- }
156
- this . $options . emptyCellClickCallback ( event , item ) ;
157
- this . cdRef . markForCheck ( ) ;
158
- }
159
-
160
- emptyCellDragDrop ( e ) : void {
161
- const item = this . getValidItemFromEvent ( e ) ;
162
- if ( ! item ) {
163
- return ;
164
- }
165
- this . $options . emptyCellDropCallback ( event , item ) ;
166
- this . cdRef . markForCheck ( ) ;
167
- }
168
-
169
- emptyCellDragOver ( e ) : void {
170
- e . preventDefault ( ) ;
171
- e . stopPropagation ( ) ;
172
- if ( this . getValidItemFromEvent ( e ) ) {
173
- e . dataTransfer . dropEffect = 'move' ;
174
- } else {
175
- e . dataTransfer . dropEffect = 'none' ;
176
- }
177
- }
178
-
179
- emptyCellMouseDown ( e ) : void {
180
- if ( GridsterUtils . checkContentClassForEvent ( this , e ) ) {
181
- return ;
182
- }
183
- e . preventDefault ( ) ;
184
- e . stopPropagation ( ) ;
185
- const item = this . getValidItemFromEvent ( e ) ;
186
- if ( ! item ) {
187
- return ;
188
- }
189
- this . initialItem = item ;
190
- this . movingItem = item ;
191
- this . previewStyle ( ) ;
192
- this . emptyCellMMove = this . renderer . listen ( 'window' , 'mousemove' , this . emptyCellMouseMove . bind ( this ) ) ;
193
- this . emptyCellUp = this . renderer . listen ( 'window' , 'mouseup' , this . emptyCellMouseUp . bind ( this ) ) ;
194
- }
195
-
196
- emptyCellMouseMove ( e ) : void {
197
- e . preventDefault ( ) ;
198
- e . stopPropagation ( ) ;
199
- const item = this . getValidItemFromEvent ( e , this . initialItem ) ;
200
- if ( ! item ) {
201
- return ;
202
- }
203
-
204
- this . movingItem = item ;
205
- this . previewStyle ( ) ;
206
- }
207
-
208
- emptyCellMouseUp ( e ) : void {
209
- this . emptyCellMMove ( ) ;
210
- this . emptyCellUp ( ) ;
211
- const item = this . getValidItemFromEvent ( e , this . initialItem ) ;
212
- if ( ! item ) {
213
- return ;
214
- }
215
- this . movingItem = item ;
216
- this . $options . emptyCellDragCallback ( e , this . movingItem ) ;
217
- setTimeout ( function ( ) {
218
- this . movingItem = null ;
219
- this . previewStyle ( ) ;
220
- } . bind ( this ) ) ;
221
- this . cdRef . markForCheck ( ) ;
222
- }
223
-
224
- getValidItemFromEvent ( e , oldItem ?: GridsterItem ) : GridsterItem | undefined {
225
- e . preventDefault ( ) ;
226
- e . stopPropagation ( ) ;
227
- GridsterUtils . checkTouchEvent ( e ) ;
228
- const rect = this . el . getBoundingClientRect ( ) ;
229
- const x = e . clientX + this . el . scrollLeft - rect . left ;
230
- const y = e . clientY + this . el . scrollTop - rect . top ;
231
- const item : GridsterItem = {
232
- x : this . pixelsToPositionX ( x , Math . floor ) ,
233
- y : this . pixelsToPositionY ( y , Math . floor ) ,
234
- cols : this . $options . defaultItemCols ,
235
- rows : this . $options . defaultItemRows
236
- } ;
237
- if ( oldItem ) {
238
- item . cols = Math . min ( Math . abs ( oldItem . x - item . x ) + 1 , this . $options . emptyCellDragMaxCols ) ;
239
- item . rows = Math . min ( Math . abs ( oldItem . y - item . y ) + 1 , this . $options . emptyCellDragMaxRows ) ;
240
- if ( oldItem . x < item . x ) {
241
- item . x = oldItem . x ;
242
- } else if ( oldItem . x - item . x > this . $options . emptyCellDragMaxCols - 1 ) {
243
- item . x = this . movingItem . x ;
244
- }
245
- if ( oldItem . y < item . y ) {
246
- item . y = oldItem . y ;
247
- } else if ( oldItem . y - item . y > this . $options . emptyCellDragMaxRows - 1 ) {
248
- item . y = this . movingItem . y ;
249
- }
250
- }
251
- if ( this . checkCollision ( item ) ) {
252
- return ;
253
- }
254
- return item ;
255
- }
256
-
257
127
onResize ( ) : void {
258
128
this . setGridSize ( ) ;
259
129
this . calculateLayoutDebounce ( ) ;
@@ -305,7 +175,7 @@ export class GridsterComponent implements OnInit, OnDestroy {
305
175
306
176
calculateLayout ( ) : void {
307
177
// check to compact
308
- this . checkCompact ( ) ;
178
+ this . compact . checkCompact ( ) ;
309
179
310
180
this . setGridDimensions ( ) ;
311
181
if ( this . $options . outerMargin ) {
@@ -516,78 +386,4 @@ export class GridsterComponent implements OnInit, OnDestroy {
516
386
positionYToPixels ( y : number ) : number {
517
387
return y * this . curRowHeight ;
518
388
}
519
-
520
- checkCompact ( ) : void {
521
- if ( this . $options . compactType !== 'none' ) {
522
- if ( this . $options . compactType === 'compactUp' ) {
523
- this . checkCompactUp ( ) ;
524
- } else if ( this . $options . compactType === 'compactLeft' ) {
525
- this . checkCompactLeft ( ) ;
526
- } else if ( this . $options . compactType === 'compactUp&Left' ) {
527
- this . checkCompactUp ( ) ;
528
- this . checkCompactLeft ( ) ;
529
- } else if ( this . $options . compactType === 'compactLeft&Up' ) {
530
- this . checkCompactLeft ( ) ;
531
- this . checkCompactUp ( ) ;
532
- }
533
- }
534
- }
535
-
536
- checkCompactUp ( ) : boolean {
537
- let widgetMovedUp = false , widget : GridsterItemComponent , moved : boolean ;
538
- const l = this . grid . length ;
539
- for ( let i = 0 ; i < l ; i ++ ) {
540
- widget = this . grid [ i ] ;
541
- moved = this . moveUpTillCollision ( widget ) ;
542
- if ( moved ) {
543
- widgetMovedUp = true ;
544
- widget . item . y = widget . $item . y ;
545
- widget . itemChanged ( ) ;
546
- }
547
- }
548
- if ( widgetMovedUp ) {
549
- this . checkCompactUp ( ) ;
550
- return widgetMovedUp ;
551
- }
552
- }
553
-
554
- moveUpTillCollision ( itemComponent : GridsterItemComponent ) : boolean {
555
- itemComponent . $item . y -= 1 ;
556
- if ( this . checkCollision ( itemComponent . $item ) ) {
557
- itemComponent . $item . y += 1 ;
558
- return false ;
559
- } else {
560
- this . moveUpTillCollision ( itemComponent ) ;
561
- return true ;
562
- }
563
- }
564
-
565
- checkCompactLeft ( ) : boolean {
566
- let widgetMovedUp = false , widget : GridsterItemComponent , moved : boolean ;
567
- const l = this . grid . length ;
568
- for ( let i = 0 ; i < l ; i ++ ) {
569
- widget = this . grid [ i ] ;
570
- moved = this . moveLeftTillCollision ( widget ) ;
571
- if ( moved ) {
572
- widgetMovedUp = true ;
573
- widget . item . x = widget . $item . x ;
574
- widget . itemChanged ( ) ;
575
- }
576
- }
577
- if ( widgetMovedUp ) {
578
- this . checkCompactLeft ( ) ;
579
- return widgetMovedUp ;
580
- }
581
- }
582
-
583
- moveLeftTillCollision ( itemComponent : GridsterItemComponent ) : boolean {
584
- itemComponent . $item . x -= 1 ;
585
- if ( this . checkCollision ( itemComponent . $item ) ) {
586
- itemComponent . $item . x += 1 ;
587
- return false ;
588
- } else {
589
- this . moveUpTillCollision ( itemComponent ) ;
590
- return true ;
591
- }
592
- }
593
389
}
0 commit comments