Skip to content

Commit 608f43f

Browse files
committed
fix(selection): reset selected on drop
1 parent 02a3730 commit 608f43f

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {Injectable} from '@angular/core';
2+
import {Subject} from 'rxjs';
3+
4+
@Injectable({
5+
providedIn: 'root'
6+
})
7+
export class NgsgEventsService {
8+
public dropped$ = new Subject();
9+
}

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
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';
1616
import {NgsgSelectionService} from './ngsg-selection.service';
1717
import {NgsgClassService} from './ngsg-class.service';
1818
import {NgsgElementsHelper} from './ngsg-elements.helper';
19+
import {NgsgEventsService} from './ngsg-events.service';
20+
import {Subject} from 'rxjs';
21+
import {takeUntil} from 'rxjs/operators';
1922

2023
const 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

Comments
 (0)