Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Dropdown any parent #667

Merged
merged 3 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/modules/dropdown/dropdown-adapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CLS_NO_SCROLL = 'sky-dropdown-no-scroll';
@Injectable()
export class SkyDropdownAdapterService {
public dropdownClose = new EventEmitter<any>();
private scrollListener: Function;
private scrollListeners: Array<Function> = [];

constructor() {
}
Expand All @@ -32,7 +32,7 @@ export class SkyDropdownAdapterService {

// Do not need to disable scroll when in full screen dropdown mode
if (!isFullScreen) {
this.scrollListener = this.setupParentScrollHandler(dropdownEl, windowObj, renderer);
this.scrollListeners = this.setupParentScrollHandler(dropdownEl, windowObj, renderer);
}

}
Expand All @@ -46,8 +46,12 @@ export class SkyDropdownAdapterService {
this.setDropdownDefaults(menuEl, renderer, windowObj, false);
this.dropdownClose.emit(undefined);

if (this.scrollListener) {
this.scrollListener();
if (this.scrollListeners.length > 0) {
for (let i = 0; i < this.scrollListeners.length; i++) {
this.scrollListeners[i]();
}

this.scrollListeners = [];
}
}
}
Expand Down Expand Up @@ -97,13 +101,15 @@ export class SkyDropdownAdapterService {
private setupParentScrollHandler(
dropdownEl: ElementRef,
windowObj: Window,
renderer: Renderer): Function {
renderer: Renderer): Array<Function> {

let parentEl = this.getScrollableParentEl(dropdownEl, windowObj);
let parentEls = this.getAllScrollableParentEl(dropdownEl, windowObj);
let listeners: Array<Function> = [];
/* istanbul ignore else */
/* sanity check */
if (parentEl) {
let listener: any;
for (let i = 0; i < parentEls.length; i++) {
let listener: Function;
let parentEl = parentEls[i];
if (parentEl === document.body) {
listener = renderer.listenGlobal('window', 'scroll', () => {
this.dropdownClose.emit(undefined);
Expand All @@ -117,8 +123,10 @@ export class SkyDropdownAdapterService {
});
}

return listener;
listeners.push(listener);

}
return listeners;
}

private setDropdownDefaults(
Expand Down Expand Up @@ -246,14 +254,15 @@ export class SkyDropdownAdapterService {
};
}

private getScrollableParentEl(el: ElementRef, windowObj: Window): HTMLElement {
private getAllScrollableParentEl(el: ElementRef, windowObj: Window): Array<HTMLElement> {
let overflowY: string,
result: Array<HTMLElement> = [document.body],
parentEl = el.nativeElement.parentNode;

while (parentEl !== undefined && parentEl instanceof HTMLElement) {
if (parentEl === document.body) {
return parentEl;
}
while (
parentEl !== undefined &&
parentEl instanceof HTMLElement &&
parentEl !== document.body) {

overflowY = windowObj.getComputedStyle(parentEl, undefined).overflowY;

Expand All @@ -264,14 +273,16 @@ export class SkyDropdownAdapterService {
case 'AUTO':
case 'HIDDEN':
case 'SCROLL':
return parentEl;
result.push(parentEl);
break;
default:
break;
}
}

parentEl = parentEl.parentNode;
}
return result;
}

private getMenuEl(dropdownEl: ElementRef): HTMLElement {
Expand Down
22 changes: 22 additions & 0 deletions src/modules/dropdown/dropdown.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ describe('Dropdown component', () => {

expect(dropdownMenu3).not.toBeVisible();
});

it('should close dropdown on multiple parent scroll', () => {

let fixture = TestBed.createComponent(DropdownParentTestComponent);
let el: HTMLElement = fixture.nativeElement;

fixture.detectChanges();

let dropdown1BtnEl = el.querySelector('#dropdown-1 .sky-dropdown-button') as HTMLElement;

dropdown1BtnEl.click();
fixture.detectChanges();

let windowScrollEvt = document.createEvent('CustomEvent');
windowScrollEvt.initEvent('scroll', false, false);

window.dispatchEvent(windowScrollEvt);

let dropdownMenu1 = el.querySelector('#dropdown-1 .sky-dropdown-menu') as HTMLElement;

expect(dropdownMenu1).not.toBeVisible();
});
});

describe('postition tests', () => {
Expand Down