Skip to content

Commit 16007dc

Browse files
committed
move compact and addEmptyCell logic to separate services
1 parent df4f664 commit 16007dc

5 files changed

+249
-215
lines changed

.angular-cli.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
"defaults": {
5555
"styleExt": "css",
5656
"component": {}
57-
}
57+
},
58+
"packageManager": "npm"
5859
}

src/lib/gridster.component.ts

Lines changed: 9 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {GridsterUtils} from './gridsterUtils.service';
55
import {GridsterItemComponent} from './gridsterItem.component';
66
import {GridsterGridComponent} from './gridsterGrid.component';
77
import {GridsterItem} from './gridsterItem.interface';
8+
import {GridsterEmptyCell} from './gridsterEmptyCell.service';
9+
import {GridsterCompact} from './gridsterCompact.service';
810

911
@Component({
1012
selector: 'gridster',
@@ -15,7 +17,6 @@ export class GridsterComponent implements OnInit, OnDestroy {
1517
@Input() options: GridsterConfig;
1618
calculateLayoutDebounce: Function;
1719
movingItem: GridsterItem;
18-
initialItem: GridsterItem;
1920
previewStyle: Function;
2021
el: any;
2122
$options: GridsterConfig;
@@ -28,14 +29,10 @@ export class GridsterComponent implements OnInit, OnDestroy {
2829
curColWidth: number;
2930
curRowHeight: number;
3031
windowResize: Function;
31-
emptyCellClick: Function;
32-
emptyCellDrop: Function;
33-
emptyCellDrag: Function;
34-
emptyCellMMove: Function;
35-
emptyCellUp: Function;
36-
emptyCellMove: Function;
3732
gridLines: GridsterGridComponent;
3833
dragInProgress: boolean;
34+
emptyCell: GridsterEmptyCell;
35+
compact: GridsterCompact;
3936

4037
static checkCollisionTwoItems(item: GridsterItem, item2: GridsterItem): boolean {
4138
return item.x < item2.x + item2.cols
@@ -44,7 +41,7 @@ export class GridsterComponent implements OnInit, OnDestroy {
4441
&& item.y + item.rows > item2.y;
4542
}
4643

47-
constructor(el: ElementRef, public renderer: Renderer2, private cdRef: ChangeDetectorRef) {
44+
constructor(el: ElementRef, public renderer: Renderer2, public cdRef: ChangeDetectorRef) {
4845
this.el = el.nativeElement;
4946
this.$options = JSON.parse(JSON.stringify(GridsterConfigService));
5047
this.mobile = false;
@@ -64,6 +61,8 @@ export class GridsterComponent implements OnInit, OnDestroy {
6461
this.$options.emptyCellClickCallback = undefined;
6562
this.$options.emptyCellDropCallback = undefined;
6663
this.$options.emptyCellDragCallback = undefined;
64+
this.emptyCell = new GridsterEmptyCell(this);
65+
this.compact = new GridsterCompact(this);
6766
}
6867

6968
ngOnInit(): void {
@@ -106,27 +105,7 @@ export class GridsterComponent implements OnInit, OnDestroy {
106105
this.windowResize();
107106
this.windowResize = null;
108107
}
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();
130109
}
131110

132111
optionsChanged(): void {
@@ -145,115 +124,6 @@ export class GridsterComponent implements OnInit, OnDestroy {
145124
}
146125
}
147126

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-
257127
onResize(): void {
258128
this.setGridSize();
259129
this.calculateLayoutDebounce();
@@ -305,7 +175,7 @@ export class GridsterComponent implements OnInit, OnDestroy {
305175

306176
calculateLayout(): void {
307177
// check to compact
308-
this.checkCompact();
178+
this.compact.checkCompact();
309179

310180
this.setGridDimensions();
311181
if (this.$options.outerMargin) {
@@ -516,78 +386,4 @@ export class GridsterComponent implements OnInit, OnDestroy {
516386
positionYToPixels(y: number): number {
517387
return y * this.curRowHeight;
518388
}
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-
}
593389
}

src/lib/gridsterCompact.service.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {Injectable} from '@angular/core';
2+
import {GridsterComponent} from './gridster.component';
3+
import {GridsterItemComponent} from './gridsterItem.component';
4+
5+
@Injectable()
6+
export class GridsterCompact {
7+
8+
constructor(private gridster: GridsterComponent) {
9+
}
10+
11+
checkCompact(): void {
12+
if (this.gridster.$options.compactType !== 'none') {
13+
if (this.gridster.$options.compactType === 'compactUp') {
14+
this.checkCompactUp();
15+
} else if (this.gridster.$options.compactType === 'compactLeft') {
16+
this.checkCompactLeft();
17+
} else if (this.gridster.$options.compactType === 'compactUp&Left') {
18+
this.checkCompactUp();
19+
this.checkCompactLeft();
20+
} else if (this.gridster.$options.compactType === 'compactLeft&Up') {
21+
this.checkCompactLeft();
22+
this.checkCompactUp();
23+
}
24+
}
25+
}
26+
27+
checkCompactUp(): boolean {
28+
let widgetMovedUp = false, widget: GridsterItemComponent, moved: boolean;
29+
const l = this.gridster.grid.length;
30+
for (let i = 0; i < l; i++) {
31+
widget = this.gridster.grid[i];
32+
moved = this.moveUpTillCollision(widget);
33+
if (moved) {
34+
widgetMovedUp = true;
35+
widget.item.y = widget.$item.y;
36+
widget.itemChanged();
37+
}
38+
}
39+
if (widgetMovedUp) {
40+
this.checkCompactUp();
41+
return widgetMovedUp;
42+
}
43+
}
44+
45+
moveUpTillCollision(itemComponent: GridsterItemComponent): boolean {
46+
itemComponent.$item.y -= 1;
47+
if (this.gridster.checkCollision(itemComponent.$item)) {
48+
itemComponent.$item.y += 1;
49+
return false;
50+
} else {
51+
this.moveUpTillCollision(itemComponent);
52+
return true;
53+
}
54+
}
55+
56+
checkCompactLeft(): boolean {
57+
let widgetMovedUp = false, widget: GridsterItemComponent, moved: boolean;
58+
const l = this.gridster.grid.length;
59+
for (let i = 0; i < l; i++) {
60+
widget = this.gridster.grid[i];
61+
moved = this.moveLeftTillCollision(widget);
62+
if (moved) {
63+
widgetMovedUp = true;
64+
widget.item.x = widget.$item.x;
65+
widget.itemChanged();
66+
}
67+
}
68+
if (widgetMovedUp) {
69+
this.checkCompactLeft();
70+
return widgetMovedUp;
71+
}
72+
}
73+
74+
moveLeftTillCollision(itemComponent: GridsterItemComponent): boolean {
75+
itemComponent.$item.x -= 1;
76+
if (this.gridster.checkCollision(itemComponent.$item)) {
77+
itemComponent.$item.x += 1;
78+
return false;
79+
} else {
80+
this.moveUpTillCollision(itemComponent);
81+
return true;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)