Skip to content

Commit 227144d

Browse files
committed
Update example and add grid update event
1 parent 7587f62 commit 227144d

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div>
22
<app-gridstack [options]="options">
3-
<app-gridstack-item *ngFor="let item of items; index as index" [x]="item.x" [y]="item.y" [width]="item.width" [height]="item.height">
4-
Hello {{index}}
3+
<app-gridstack-item *ngFor="let item of items; index as index" [x]="item.x" [y]="item.y" [width]="item.width" [height]="item.height" [id]="item.id">
4+
Id: {{item.id}}
55
</app-gridstack-item>
66
</app-gridstack>
77
</div>

src/app/app.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ export class AppComponent implements AfterViewInit {
1515

1616
items = [
1717
{
18+
id: 'abc',
1819
x: 0,
1920
y: 0,
2021
width: 5,
2122
height: 5
2223
},
2324
{
25+
id: 'bcd',
2426
x: 10,
2527
y: 10,
2628
width: 15,
@@ -33,11 +35,22 @@ export class AppComponent implements AfterViewInit {
3335
// console.log(a);
3436
setTimeout(() => {
3537
this.items.push({
38+
id: 'efc',
3639
x: 3,
3740
y: 10,
3841
width: 5,
3942
height: 5
4043
});
44+
setTimeout(() => {
45+
// this.items[0].x = 20;
46+
this.items.push({
47+
id: 'efcw',
48+
x: 3,
49+
y: 10,
50+
width: 5,
51+
height: 5
52+
});
53+
}, 3000);
4154
}, 1000);
4255
}
4356
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
:host(*) {
22
display: block;
3+
-webkit-box-shadow: 5px 5px 3px 0px rgba(0,0,0,0.3);
4+
-moz-box-shadow: 5px 5px 3px 0px rgba(0,0,0,0.3);
5+
box-shadow: 5px 5px 3px rgba(0,0,0,0.3);
36
}

src/app/gridstack/gridstack-item/gridstack-item.component.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Component, OnInit, HostBinding, ChangeDetectionStrategy, Input, Renderer2, ElementRef, AfterViewInit, OnDestroy } from '@angular/core';
2-
import { GridstackService } from '../gridstack.service';
1+
/// <reference types="jquery" />
2+
/// <reference types="gridstack" />
33

4-
declare let jQuery: any;
4+
import { Component, ChangeDetectionStrategy, Input, Renderer2, ElementRef, AfterViewInit, OnDestroy } from '@angular/core';
5+
import { GridstackService } from '../gridstack.service';
56

67
@Component({
78
selector: 'app-gridstack-item',
@@ -16,6 +17,7 @@ export class GridstackItemComponent implements AfterViewInit, OnDestroy {
1617
if (value !== this._x) {
1718
this._x = value;
1819
this.renderer.setAttribute(this.element, 'data-gs-x', this._x.toString());
20+
this.move();
1921
}
2022
}
2123
get x(): number {
@@ -26,6 +28,7 @@ export class GridstackItemComponent implements AfterViewInit, OnDestroy {
2628
if (value !== this._y) {
2729
this._y = value;
2830
this.renderer.setAttribute(this.element, 'data-gs-y', this._y.toString());
31+
this.move();
2932
}
3033
}
3134
get y(): number {
@@ -36,6 +39,7 @@ export class GridstackItemComponent implements AfterViewInit, OnDestroy {
3639
if (value !== this._width) {
3740
this._width = value;
3841
this.renderer.setAttribute(this.element, 'data-gs-width', this._width.toString());
42+
this.resize();
3943
}
4044
}
4145
get width(): number {
@@ -46,14 +50,14 @@ export class GridstackItemComponent implements AfterViewInit, OnDestroy {
4650
if (value !== this._height) {
4751
this._height = value;
4852
this.renderer.setAttribute(this.element, 'data-gs-height', this._height.toString());
53+
this.resize();
4954
}
5055
}
5156
get height(): number {
5257
return this._height;
5358
}
5459

5560
element: HTMLElement;
56-
private gridItem: any;
5761

5862
private _x: number;
5963
private _y: number;
@@ -72,17 +76,24 @@ export class GridstackItemComponent implements AfterViewInit, OnDestroy {
7276
}
7377

7478
ngAfterViewInit() {
75-
console.log(this.gridstackService.grid);
7679
this.renderer.setAttribute(this.element, 'data-gs-id', this.id);
7780
if (this.gridstackService.grid.willItFit(this.x, this.y, this.width, this.height, true)) {
78-
this.gridItem = this.gridstackService.grid.makeWidget(this.element);
81+
this.gridstackService.grid.makeWidget(this.element);
7982
} else {
8083
console.error('Not enough free space to place the widget');
8184
}
8285
}
8386

8487
ngOnDestroy(): void {
85-
this.gridstackService.grid.remove(this.element);
88+
this.gridstackService.grid.removeWidget(this.element);
89+
}
90+
91+
private move() {
92+
this.gridstackService.grid.move(this.element, this.x, this.y);
93+
}
94+
95+
private resize() {
96+
this.gridstackService.grid.resize(this.element, this.width, this.height);
8697
}
8798

8899
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
/// <reference types="jquery" />
2+
/// <reference types="gridstack" />
3+
14
import { Injectable } from '@angular/core';
25

36
@Injectable()
47
export class GridstackService {
5-
grid: any;
8+
grid: GridStack;
69
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
/// <reference types="jquery" />
2+
/// <reference types="gridstack" />
3+
14
import {
2-
Component, HostBinding, AfterViewInit, ChangeDetectionStrategy, ElementRef, Input, ContentChildren, QueryList, Renderer2, OnInit
5+
Component, ChangeDetectionStrategy, ElementRef, Input, ContentChildren, QueryList, Renderer2, OnInit, Output, EventEmitter
36
} from '@angular/core';
47
import { GridstackItemComponent } from '../gridstack-item/gridstack-item.component';
58
import { GridstackService } from '../gridstack.service';
69

7-
declare let jQuery: any;
8-
910
@Component({
1011
selector: 'app-gridstack',
1112
templateUrl: './gridstack.component.html',
@@ -15,13 +16,12 @@ declare let jQuery: any;
1516
export class GridstackComponent implements OnInit {
1617
@ContentChildren(GridstackItemComponent) items: QueryList<GridstackItemComponent>;
1718

18-
@Input() options: any;
19+
@Input() options: GridstackOptions;
20+
@Output() change = new EventEmitter<any>();
1921

2022
previousItems: Array<HTMLElement>;
2123
element: HTMLElement;
2224

23-
grid: any;
24-
2525
constructor(
2626
private elementRef: ElementRef,
2727
private renderer: Renderer2,
@@ -33,6 +33,7 @@ export class GridstackComponent implements OnInit {
3333
ngOnInit() {
3434
jQuery(this.element).gridstack(this.options);
3535
this.gridstackService.grid = jQuery(this.element).data('gridstack');
36+
jQuery(this.element).on('change', (event, items) => this.change.emit(items));
3637
}
3738

3839
}

0 commit comments

Comments
 (0)