55 EventEmitter ,
66 HostListener ,
77 Input ,
8- NgZone ,
8+ NgZone , OnDestroy ,
99 OnInit ,
1010 Output
1111} from '@angular/core' ;
@@ -16,44 +16,54 @@ import {NgsgSortService} from './ngsg-sort.service';
1616import { NgsgSelectionService } from './ngsg-selection.service' ;
1717import { NgsgClassService } from './ngsg-class.service' ;
1818import { NgsgElementsHelper } from './ngsg-elements.helper' ;
19+ import { NgsgEventsService } from './ngsg-events.service' ;
20+ import { Subject } from 'rxjs' ;
21+ import { takeUntil } from 'rxjs/operators' ;
1922
2023const selector = '[ngSortgridItem]' ;
2124
2225@Directive ( { selector} )
23- export class NgsgItemDirective implements OnInit , AfterViewInit {
24- private DEFAULT_GROUP = 'defaultGroup' ;
25-
26- @Input ( )
27- private ngSortGridGroup : string = this . DEFAULT_GROUP ;
28- private selected = false ;
29-
26+ export class NgsgItemDirective implements OnInit , AfterViewInit , OnDestroy {
27+ @Input ( ) private ngSortGridGroup : string = this . DEFAULT_GROUP ;
3028 @Input ( ) ngSortGridItems ;
3129
3230 @Output ( ) sorted = new EventEmitter < any > ( ) ;
3331
32+ private DEFAULT_GROUP = 'defaultGroup' ;
33+ private selected = false ;
34+ private destroy$ = new Subject ( ) ;
35+
3436 constructor (
3537 public el : ElementRef ,
3638 private sortService : NgsgSortService ,
3739 private selectionService : NgsgSelectionService ,
3840 private reflectService : NgsgReflectService ,
3941 private classService : NgsgClassService ,
40- private ngsgStore : NgsgStoreService
42+ private ngsgStore : NgsgStoreService ,
43+ private ngsgEventService : NgsgEventsService
4144 ) {
4245 }
4346
4447 ngOnInit ( ) : void {
45- // TODO handle classes as input
4648 if ( ! this . ngSortGridItems ) {
4749 console . error ( `Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
4850 otherwhise the ordered items will not be emitted in the (sorted) event` ) ;
4951 }
5052 this . ngsgStore . initState ( this . ngSortGridGroup , this . ngSortGridItems , { } ) ;
53+ this . ngsgEventService . dropped$ . pipe (
54+ takeUntil ( this . destroy$ )
55+ ) . subscribe ( ( ) => this . selected = false ) ;
5156 }
5257
5358 ngAfterViewInit ( ) : void {
5459 this . el . nativeElement . draggable = true ;
5560 }
5661
62+ ngOnDestroy ( ) : void {
63+ this . destroy$ . next ( ) ;
64+ this . destroy$ . complete ( ) ;
65+ }
66+
5767 @HostListener ( 'dragstart' , [ '$event' ] )
5868 dragStart ( event ) : void {
5969 if ( ! this . occuredOnHost ( event ) ) {
@@ -89,6 +99,7 @@ export class NgsgItemDirective implements OnInit, AfterViewInit {
8999 const reflectedChanges = this . reflectService . reflectChanges ( this . ngSortGridGroup , event . target ) ;
90100 this . sorted . next ( reflectedChanges ) ;
91101 this . ngsgStore . resetSelectedItems ( this . ngSortGridGroup ) ;
102+ this . ngsgEventService . dropped$ . next ( ) ;
92103 }
93104
94105 @HostListener ( 'click' , [ '$event' ] )
0 commit comments