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
1313import { NgsgReflectService } from './ngsg-reflect.service' ;
@@ -19,11 +19,12 @@ import {NgsgElementsHelper} from './ngsg-elements.helper';
1919import { NgsgEventsService } from './ngsg-events.service' ;
2020import { Subject } from 'rxjs' ;
2121import { takeUntil } from 'rxjs/operators' ;
22+ import { group } from '@angular/animations' ;
2223
2324const 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 ) ;
0 commit comments