Skip to content

Commit 0db7f1e

Browse files
committed
fix(asyncpipe): support async pipe
1 parent daa80b3 commit 0db7f1e

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

projects/ng-sortgrid/src/lib/ngsg-item.directive.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
EventEmitter,
66
HostListener,
77
Input,
8-
NgZone, OnDestroy,
8+
NgZone, OnChanges, OnDestroy,
99
OnInit,
10-
Output
10+
Output, SimpleChanges
1111
} from '@angular/core';
1212

1313
import {NgsgReflectService} from './ngsg-reflect.service';
@@ -19,11 +19,12 @@ import {NgsgElementsHelper} from './ngsg-elements.helper';
1919
import {NgsgEventsService} from './ngsg-events.service';
2020
import {Subject} from 'rxjs';
2121
import {takeUntil} from 'rxjs/operators';
22+
import {group} from '@angular/animations';
2223

2324
const selector = '[ngSortgridItem]';
2425

2526
@Directive({selector})
26-
export class NgsgItemDirective implements OnInit, AfterViewInit, OnDestroy {
27+
export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDestroy {
2728
@Input() ngSortGridGroup = 'defaultGroup';
2829
@Input() ngSortGridItems;
2930

@@ -43,16 +44,22 @@ export class NgsgItemDirective implements OnInit, AfterViewInit, OnDestroy {
4344
}
4445

4546
ngOnInit(): void {
46-
if (!this.ngSortGridItems) {
47-
console.warn(`Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
48-
otherwhise the ordered items will not be emitted in the (sorted) event`);
49-
}
50-
this.ngsgStore.initState(this.ngSortGridGroup, this.ngSortGridItems, {});
5147
this.ngsgEventService.dropped$.pipe(
5248
takeUntil(this.destroy$)
5349
).subscribe(() => this.selected = false);
5450
}
5551

52+
ngOnChanges(changes: SimpleChanges): void {
53+
const sortGridItemChanges = changes.ngSortGridItems;
54+
const sortGridItems = sortGridItemChanges.currentValue ? sortGridItemChanges.currentValue : [];
55+
56+
if (!this.ngsgStore.hasGroup(this.ngSortGridGroup)) {
57+
this.ngsgStore.initState(this.ngSortGridGroup, sortGridItems);
58+
return;
59+
}
60+
this.ngsgStore.setItems(this.ngSortGridGroup, sortGridItems);
61+
}
62+
5663
ngAfterViewInit(): void {
5764
this.el.nativeElement.draggable = true;
5865
}
@@ -94,6 +101,13 @@ export class NgsgItemDirective implements OnInit, AfterViewInit, OnDestroy {
94101
if (!this.ngsgStore.hasSelectedItems(this.ngSortGridGroup)) {
95102
return;
96103
}
104+
105+
if (!this.ngsgStore.hasItems(this.ngSortGridGroup)) {
106+
console.warn(`Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
107+
otherwhise the ordered items can not be emitted in the (sorted) event`);
108+
return;
109+
}
110+
97111
this.sortService.endSort();
98112
const element = !this.occuredOnHost(event) ? NgsgElementsHelper.findHost(event.target, selector) : event.target;
99113
const reflectedChanges = this.reflectService.reflectChanges(this.ngSortGridGroup, element);

projects/ng-sortgrid/src/lib/ngsg-store.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ export class NgsgStoreService {
2929
}
3030

3131
public setItems(group: string, items: any): void {
32+
console.log('Items', items);
3233
this.state.get(group).items = [...items];
3334
}
3435

3536
public getItems(group: string): any[] {
3637
return this.state.get(group).items;
3738
}
3839

40+
public hasItems(group: string): boolean {
41+
return this.getItems(group).length > 0;
42+
}
43+
44+
public hasGroup(group: string): boolean {
45+
return this.state.has(group);
46+
}
47+
3948
public getSelectedItems(group: string): NgsgDragelement[] {
4049
return this.state.get(group).selectedItems;
4150
}

0 commit comments

Comments
 (0)