Skip to content

Commit f94419e

Browse files
committed
fix(nesteddom): remove findHost of Elementhelper
1 parent 7e8cd51 commit f94419e

File tree

4 files changed

+22
-119
lines changed

4 files changed

+22
-119
lines changed

projects/ng-sortgrid/src/lib/ngsg-elements.helper.spec.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,4 @@ describe('NgsgElementsHelper', () => {
1818
expect(index).toBe(2);
1919
});
2020

21-
it('must find the element that matches the selector', () => {
22-
const selector = 'sample-selector';
23-
const result = {
24-
matches: (s) => s === selector,
25-
name: 'parentElement'
26-
};
27-
const element = {
28-
parentElement: {
29-
matches: () => false,
30-
parentElement: result
31-
}
32-
} as any;
33-
const findHostSpy = spyOn(NgsgElementsHelper, 'findHost');
34-
findHostSpy.and.callThrough();
35-
36-
NgsgElementsHelper.findHost(element, selector);
37-
expect(findHostSpy).toHaveBeenCalledTimes(2);
38-
});
39-
40-
it('must retunr the element that matches the selector', () => {
41-
const selector = 'sample-selector';
42-
const result = {
43-
matches: (s) => s === selector,
44-
name: 'parentElement'
45-
};
46-
const element = {
47-
parentElement: {
48-
matches: () => false,
49-
parentElement: result
50-
}
51-
} as any;
52-
const findHostSpy = spyOn(NgsgElementsHelper, 'findHost');
53-
findHostSpy.and.callThrough();
54-
55-
const actual = NgsgElementsHelper.findHost(element, selector);
56-
expect((actual as any)).toEqual(result);
57-
});
58-
5921
});

projects/ng-sortgrid/src/lib/ngsg-elements.helper.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,4 @@ export class NgsgElementsHelper {
44
const allElements = element.parentNode.children;
55
return Array.prototype.indexOf.call(allElements, element);
66
}
7-
8-
public static findHost(element: Element, selector: string): Element {
9-
const parentElement = element.parentElement;
10-
if (parentElement.matches(selector)) {
11-
return parentElement;
12-
}
13-
return this.findHost(parentElement, selector);
14-
}
157
}

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

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,16 @@ describe('NgsgItemDirective', () => {
8080
});
8181

8282
it('should call sort with the host if the event occured on the host', () => {
83-
const event = {target: {matches: () => true}};
8483
ngsgStore.hasSelectedItems.and.returnValue(true);
8584

86-
sut.dragEnter(event);
87-
expect(ngsgSortService.sort).toHaveBeenCalledWith(event.target);
88-
});
89-
90-
it('should call sort with the host, even if the event did not occure on it', () => {
91-
const event = {target: {matches: () => false}};
92-
const host = 'Some element' as any;
93-
ngsgStore.hasSelectedItems.and.returnValue(true);
94-
NgsgElementsHelper.findHost = () => host;
95-
96-
sut.dragEnter(event);
97-
expect(ngsgSortService.sort).toHaveBeenCalledWith(host);
85+
sut.dragEnter();
86+
expect(ngsgSortService.sort).toHaveBeenCalledWith(elementRef.nativeElement);
9887
});
9988

10089
it('should sort the items if the event occured on the host and on the correct group', () => {
10190
ngsgStore.hasSelectedItems.and.returnValue(true);
102-
const event = {
103-
target: {
104-
matches: () => true
105-
}
106-
};
107-
sut.dragEnter(event);
108-
expect(ngsgSortService.sort).toHaveBeenCalledWith(event.target);
91+
sut.dragEnter();
92+
expect(ngsgSortService.sort).toHaveBeenCalledWith(elementRef.nativeElement);
10993
});
11094

11195
it('must call event preventDefault', () => {
@@ -122,46 +106,28 @@ describe('NgsgItemDirective', () => {
122106

123107
it('should not call endSort if the group does not contain selectedItems', () => {
124108
ngsgStore.hasSelectedItems.and.returnValue(false);
125-
sut.drop({});
109+
sut.drop();
126110
expect(ngsgSortService.endSort).not.toHaveBeenCalled();
127111
});
128112

129113
it('should sort if the group contains selectedItems', () => {
130114
ngsgStore.hasSelectedItems.and.returnValue(true);
131115
ngsgStore.hasItems.and.returnValue(true);
132-
sut.drop({target: {matches: () => true}});
116+
sut.drop();
133117
expect(ngsgSortService.endSort).toHaveBeenCalled();
134118
});
135119

136120
it('should call the reflection service with the host if the event occured on it', () => {
137121
const group = 'test-group';
138-
const event = {target: {matches: () => true}};
139122
sut.ngSortGridGroup = group;
140123
ngsgStore.hasSelectedItems.and.returnValue(true);
141124

142-
sut.drop(event);
143-
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, event.target);
144-
});
145-
146-
it('should call the reflection service with the host even if the event did not occured on it', () => {
147-
const group = 'test-group';
148-
const event = {target: {matches: () => false}};
149-
const host = 'Some element' as any;
150-
NgsgElementsHelper.findHost = () => host;
151-
sut.ngSortGridGroup = group;
152-
ngsgStore.hasSelectedItems.and.returnValue(true);
153-
154-
sut.drop(event);
155-
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, host);
125+
sut.drop();
126+
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, elementRef.nativeElement);
156127
});
157128

158129
it('should get the reflected changes from the reflection service and emit them', done => {
159130
const group = 'test-group';
160-
const event = {
161-
target: {
162-
matches: () => true
163-
}
164-
};
165131
const reflectedChanges = ['item two', 'item one', 'item three'];
166132

167133
ngsgStore.hasSelectedItems.and.returnValue(true);
@@ -173,40 +139,26 @@ describe('NgsgItemDirective', () => {
173139
expect(reflectedChanges).toEqual(changes);
174140
done();
175141
});
176-
sut.drop(event);
177-
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, event.target);
142+
sut.drop();
143+
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, elementRef.nativeElement);
178144
});
179145

180146
it('should reset the selected items on drop', () => {
181-
const event = {target: {matches: () => true}};
182-
sut.drop(event);
147+
sut.drop();
183148
expect(ngsgStore.resetSelectedItems).toHaveBeenCalled();
184149
});
185150

186151
it('should stream the dropped event on the eventservice', done => {
187-
const event = {target: {matches: () => true}};
188152
ngsgEventService.dropped$.subscribe(() => done());
189-
sut.drop(event);
153+
sut.drop();
190154
});
191155

192156
it('should call the selctionservice with the host if the event occured on the host', () => {
193157
const group = 'test-group';
194-
const event = {target: {matches: () => true}};
195-
sut.ngSortGridGroup = group;
196-
197-
sut.clicked(event);
198-
expect(ngsgSelectionService.updateSelectedDragItem).toHaveBeenCalledWith(group, event.target, true);
199-
});
200-
201-
it('should call the selctionservice with the host, even if the event did not occure on it', () => {
202-
const group = 'test-group';
203-
const event = {target: {matches: () => false}};
204-
const host = 'Some element' as any;
205-
NgsgElementsHelper.findHost = () => host;
206158
sut.ngSortGridGroup = group;
207159

208-
sut.clicked(event);
209-
expect(ngsgSelectionService.updateSelectedDragItem).toHaveBeenCalledWith(group, host, true);
160+
sut.clicked();
161+
expect(ngsgSelectionService.updateSelectedDragItem).toHaveBeenCalledWith(group, elementRef.nativeElement, true);
210162
});
211163

212164
it(`should init the state with empty items if group has yet not been
@@ -260,7 +212,7 @@ describe('NgsgItemDirective', () => {
260212
const consoleWarnSpy = spyOn(console, 'warn');
261213
ngsgStore.hasItems.and.returnValue(false);
262214

263-
sut.drop(event);
215+
sut.drop();
264216
expect(consoleWarnSpy).toHaveBeenCalledWith(expectedWarniningMessage);
265217
});
266218
});

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
7878
this.sortService.initSort(this.ngSortGridGroup);
7979
}
8080

81-
@HostListener('dragenter', ['$event'])
82-
dragEnter(event): void {
81+
@HostListener('dragenter')
82+
dragEnter(): void {
8383
if (!this.ngsgStore.hasSelectedItems(this.ngSortGridGroup)) {
8484
return;
8585
}
86-
const element = !this.occuredOnHost(event) ? NgsgElementsHelper.findHost(event.target, selector) : event.target;
87-
this.sortService.sort(element);
86+
this.sortService.sort(this.el.nativeElement);
8887
}
8988

9089
@HostListener('dragover', ['$event'])
@@ -97,7 +96,7 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
9796
}
9897

9998
@HostListener('drop', ['$event'])
100-
drop(event): void {
99+
drop(): void {
101100
if (!this.ngsgStore.hasSelectedItems(this.ngSortGridGroup)) {
102101
return;
103102
}
@@ -109,18 +108,16 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
109108
}
110109

111110
this.sortService.endSort();
112-
const element = !this.occuredOnHost(event) ? NgsgElementsHelper.findHost(event.target, selector) : event.target;
113-
const reflectedChanges = this.reflectService.reflectChanges(this.ngSortGridGroup, element);
111+
const reflectedChanges = this.reflectService.reflectChanges(this.ngSortGridGroup, this.el.nativeElement);
114112
this.sorted.next(reflectedChanges);
115113
this.ngsgStore.resetSelectedItems(this.ngSortGridGroup);
116114
this.ngsgEventService.dropped$.next();
117115
}
118116

119117
@HostListener('click', ['$event'])
120-
clicked(event): void {
121-
const element = !this.occuredOnHost(event) ? NgsgElementsHelper.findHost(event.target, selector) : event.target;
118+
clicked(): void {
122119
this.selected = !this.selected;
123-
this.selectionService.updateSelectedDragItem(this.ngSortGridGroup, element, this.selected);
120+
this.selectionService.updateSelectedDragItem(this.ngSortGridGroup, this.el.nativeElement, this.selected);
124121
}
125122

126123
private occuredOnHost(event): boolean {

0 commit comments

Comments
 (0)