Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit f604312

Browse files
committed
Fix some issues with the grid. Add remove items to the demo page
1 parent 6216678 commit f604312

File tree

4 files changed

+28
-44
lines changed

4 files changed

+28
-44
lines changed

src/NgGrid.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export declare class NgGridItem {
126126
private _added;
127127
config: any;
128128
constructor(_ngEl: ElementRef, _renderer: Renderer, _ngGrid: NgGrid);
129+
onInit(): void;
129130
canDrag(e: any): boolean;
130131
canResize(e: any): string;
131132
onMouseMove(e: any): void;

src/NgGrid.ts

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export class NgGrid {
272272
ngItem.setGridPosition(newPos.col, newPos.row);
273273
this._items.push(ngItem);
274274
this._addToGrid(ngItem);
275+
ngItem.recalculateSelf();
275276
}
276277

277278
public removeItem(ngItem: NgGridItem): void {
@@ -281,6 +282,8 @@ export class NgGrid {
281282
this._items.splice(x, 1);
282283

283284
// Update position of all items
285+
this._updateSize();
286+
this._cascadeGrid();
284287
this._items.forEach((item) => item.recalculateSelf());
285288
}
286289

@@ -321,12 +324,14 @@ export class NgGrid {
321324
if (item != null) {
322325
if (this.resizeEnable && item.canResize(e) != null) {
323326
this._resizeStart(e);
327+
return false;
324328
} else if (this.dragEnable && item.canDrag(e)) {
325329
this._dragStart(e);
330+
return false;
326331
}
327332
}
328333

329-
return false;
334+
return true;
330335
}
331336

332337
private _resizeStart(e: any): void {
@@ -365,7 +370,7 @@ export class NgGrid {
365370
}
366371
}
367372

368-
private _onMouseMove(e: any): boolean {
373+
private _onMouseMove(e: any): void {
369374
if (e.buttons == 0 && this.isDragging) {
370375
this._dragStop(e);
371376
} else if (e.buttons == 0 && this.isResizing) {
@@ -382,8 +387,6 @@ export class NgGrid {
382387
item.onMouseMove(e);
383388
}
384389
}
385-
386-
return false;
387390
}
388391

389392
private _drag(e: any): void {
@@ -468,11 +471,13 @@ export class NgGrid {
468471
private _onMouseUp(e: any): boolean {
469472
if (this.isDragging) {
470473
this._dragStop(e);
474+
return false;
471475
} else if (this.isResizing) {
472476
this._resizeStop(e);
477+
return false;
473478
}
474479

475-
return false;
480+
return true;
476481
}
477482

478483
private _dragStop(e: any): void {
@@ -864,9 +869,8 @@ export class NgGrid {
864869

865870
private _createPlaceholder(pos: {col: number, row:number}, dims: {x: number, y: number}) {
866871
var me = this;
867-
console.log(pos, dims);
872+
868873
this._loader.loadNextToLocation((<Type>NgGridPlaceholder), this._items[0].getElement()).then(componentRef => {
869-
console.log(componentRef);
870874
me._placeholderRef = componentRef;
871875
var placeholder = componentRef.instance;
872876
// me._placeholder.setGrid(me);
@@ -929,66 +933,40 @@ export class NgGridItem {
929933
this.setConfig(v);
930934

931935
if (!this._added) {
932-
this._ngGrid.addItem(this);
933936
this._added = true;
937+
this._ngGrid.addItem(this);
934938
}
935939

936940
this._recalculateDimensions();
937941
this._recalculatePosition();
938942
}
939943

940944
// Constructor
941-
constructor(private _ngEl: ElementRef, private _renderer: Renderer, private _ngGrid:NgGrid) {//@Host()
945+
constructor(private _ngEl: ElementRef, private _renderer: Renderer, private _ngGrid:NgGrid) {}
946+
947+
// Public methods
948+
public onInit() {
942949
this._renderer.setElementClass(this._ngEl, 'grid-item', true);
943950
if (this._ngGrid.autoStyle) this._renderer.setElementStyle(this._ngEl, 'position', 'absolute');
944951
this._recalculateDimensions();
945952
this._recalculatePosition();
946953
}
947954

948-
// Public methods
949955
public canDrag(e: any): boolean {
950956
if (this._dragHandle) {
951-
var foundHandle: boolean;
952957
var parent = e.target.parentElement;
953958

954-
var last: any = e.target;
955-
956-
while (parent) {
957-
if (parent.querySelector(this._dragHandle) == last) {
958-
foundHandle = true;
959-
break;
960-
}
961-
962-
last = parent;
963-
parent = parent.parentElement;
964-
}
965-
966-
return foundHandle;
959+
return parent.querySelector(this._dragHandle) == e.target;
967960
}
968961

969962
return true;
970963
}
971964

972965
public canResize(e: any): string {
973966
if (this._resizeHandle) {
974-
var foundHandle: boolean;
975-
var paths: Array<any> = e.path;
976-
paths.pop(); // Get rid of #document
977-
978-
var last: any = null;
979-
980-
for (var x in paths) {
981-
if (last !== null) {
982-
if (paths[x].querySelector(this._resizeHandle) == last) {
983-
foundHandle = true;
984-
break;
985-
}
986-
}
987-
988-
last = paths[x];
989-
}
990-
991-
return foundHandle ? 'both' : null;
967+
var parent = e.target.parentElement;
968+
969+
return parent.querySelector(this._resizeHandle) == e.target ? 'both' : null;
992970
} else {
993971
var mousePos = this._getMousePosition(e);
994972

@@ -1187,8 +1165,8 @@ class NgGridPlaceholder {
11871165
private _col: number;
11881166
private _row: number;
11891167

1190-
constructor (private _renderer: Renderer, private _ngEl: ElementRef, @Host() private _ngGrid: NgGrid) {
1191-
this._renderer.setElementClass(this._ngEl, 'placeholder', true);
1168+
constructor (private _renderer: Renderer, private _ngEl: ElementRef, private _ngGrid: NgGrid) {
1169+
this._renderer.setElementClass(this._ngEl, 'grid-placeholder', true);
11921170
if (this._ngGrid.autoStyle) this._renderer.setElementStyle(this._ngEl, 'position', 'absolute');
11931171
}
11941172

src/app.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ <h1>Angular 2 Grid Demo</h1>
9999
</label>
100100

101101
Col: {{ curItem.col }}, Row: {{ curItem.row }}, Size X: {{ curItem.sizex }}, Size Y: {{ curItem.sizey }}
102+
<a (click)="removeBox()" class="btn btn-primary">Remove Box</a>
102103
</div>
103104
</form>
104105
<a (click)="addBox()" class="btn btn-primary pull-right">Add Box</a>

src/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class MyAppComponent {
5050
this.boxes.push(this.curNum++);
5151
}
5252

53+
removeBox() {
54+
this.boxes.splice(this.curItemCheck, 1);
55+
}
56+
5357
updateItem(index: number, pos: { col: number, row: number, sizex: number, sizey: number }) {
5458
this.itemPositions[index] = pos;
5559
if (this.curItemCheck == index) this.curItem = pos;

0 commit comments

Comments
 (0)