Skip to content

Commit

Permalink
fix(dropdown): fixed disabled tests, removed outdated (#1605)
Browse files Browse the repository at this point in the history
* fixed disabled tests, removed outdated

* fixed karma config

* fixed event creation to support IE
this "browser" just can't use ctor of Event class...

* chore(build): fixed karma config (#1606)

* fixed disabled tests, removed outdated

* fixed event creation to support IE
this "browser" just can't use ctor of Event class...
  • Loading branch information
Le0Michine authored and valorkin committed Feb 28, 2017
1 parent f21bd8d commit 29dceba
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 191 deletions.
63 changes: 0 additions & 63 deletions src/dropdown/dropdown-keyboard-nav.directive.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/dropdown/dropdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export class DropdownConfig {
/** default dropdown auto closing behavior */
public autoClose: string = NONINPUT;
/** is keyboard navigation enabled by default */
public keyboardNav: Boolean = false;
public keyboardNav: boolean = false;
}
26 changes: 21 additions & 5 deletions src/dropdown/dropdown.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@angular/core';

import { isBs3 } from '../utils/ng2-bootstrap-config';
import { dropdownService } from './dropdown.service';
import { DropdownService } from './dropdown.service';
import { DropdownConfig } from './dropdown.config';

/**
Expand All @@ -17,6 +17,7 @@ import { DropdownConfig } from './dropdown.config';
host: {'[class.show]': 'isOpen && !isBs3'}
})
export class DropdownDirective implements OnInit, OnDestroy {
private dropdownService: DropdownService;
/** if `true` dropdown will be opened */
@HostBinding('class.open')
@HostBinding('class.active')
Expand All @@ -26,6 +27,11 @@ export class DropdownDirective implements OnInit, OnDestroy {
}

public set isOpen(value: boolean) {
if (this._isOpen === !!value) {
// don't emit events
return;
}

this._isOpen = !!value;

// todo: implement after porting position
Expand All @@ -37,9 +43,9 @@ export class DropdownDirective implements OnInit, OnDestroy {
// ready
if (this.isOpen) {
this.focusToggleElement();
dropdownService.open(this);
this.dropdownService.open(this);
} else {
dropdownService.close(this);
this.dropdownService.close(this);
this.selectedOption = void 0;
}
this.onToggle.emit(this.isOpen);
Expand Down Expand Up @@ -78,15 +84,21 @@ export class DropdownDirective implements OnInit, OnDestroy {
// drop down toggle element
public toggleEl: ElementRef;
public el: ElementRef;
protected _isOpen: boolean;
protected _isOpen: boolean = false;

protected _changeDetector: ChangeDetectorRef;

public constructor(el: ElementRef, ref: ChangeDetectorRef, config: DropdownConfig) {
public constructor(
el: ElementRef,
ref: ChangeDetectorRef,
dropdownService: DropdownService,
config: DropdownConfig
) {
// @Query('dropdownMenu', {descendants: false})
// dropdownMenuList:QueryList<ElementRef>) {
this.el = el;
this._changeDetector = ref;
this.dropdownService = dropdownService;
Object.assign(this, config);
// todo: bind to route change event
}
Expand Down Expand Up @@ -118,10 +130,14 @@ export class DropdownDirective implements OnInit, OnDestroy {
}

public show():void {
/** prevent global event handling */
this.dropdownService.preventEventHandling();
this.isOpen = true;
}

public hide():void {
/** prevent global event handling */
this.dropdownService.preventEventHandling();
this.isOpen = false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/dropdown/dropdown.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { DropdownMenuDirective } from './dropdown-menu.directive';
import { DropdownToggleDirective } from './dropdown-toggle.directive';
import { DropdownDirective } from './dropdown.directive';
import { DropdownConfig } from './dropdown.config';
import { DropdownService } from './dropdown.service';

@NgModule({
declarations: [DropdownDirective, DropdownMenuDirective, DropdownToggleDirective],
exports: [DropdownDirective, DropdownMenuDirective, DropdownToggleDirective]
})
export class DropdownModule {
public static forRoot(): ModuleWithProviders {
return {ngModule: DropdownModule, providers: [DropdownConfig]};
return {ngModule: DropdownModule, providers: [DropdownConfig, DropdownService]};
}
}
81 changes: 47 additions & 34 deletions src/dropdown/dropdown.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { Injectable } from '@angular/core';

import { DropdownDirective } from './dropdown.directive';

export const ALWAYS = 'always';
export const DISABLED = 'disabled';
export const OUTSIDECLICK = 'outsideClick';
export const NONINPUT = 'nonInput';

import { DropdownDirective } from './dropdown.directive';

/* tslint:disable-next-line */
const KeyboardEvent = (global as any).KeyboardEvent as KeyboardEvent;
/* tslint:disable-next-line */
const MouseEvent = (global as any).MouseEvent as MouseEvent;

@Injectable()
export class DropdownService {
protected openScope:DropdownDirective;
private openScope:DropdownDirective;

private closeDropdownBind:EventListener = this.closeDropdown.bind(this);
private keybindFilterBind:EventListener = this.keybindFilter.bind(this);

protected closeDropdownBind:EventListener = this.closeDropdown.bind(this);
protected keybindFilterBind:EventListener = this.keybindFilter.bind(this);
private suspendedEvent: any;

public open(dropdownScope:DropdownDirective):void {
if (!this.openScope) {
Expand All @@ -39,34 +44,44 @@ export class DropdownService {
window.document.removeEventListener('keydown', this.keybindFilterBind);
}

protected closeDropdown(event:MouseEvent):void {
if (!this.openScope) {
return;
}

if (event && this.openScope.autoClose === DISABLED) {
return;
}

if (event && this.openScope.toggleEl &&
this.openScope.toggleEl.nativeElement.contains(event.target)) {
return;
}

if (event && this.openScope.autoClose === NONINPUT &&
this.openScope.menuEl &&
/input|textarea/i.test((event.target as any).tagName) &&
this.openScope.menuEl.nativeElement.contains(event.target)) {
return;
}

if (event && this.openScope.autoClose === OUTSIDECLICK &&
this.openScope.menuEl &&
this.openScope.menuEl.nativeElement.contains(event.target)) {
return;
}
public preventEventHandling(): void {
clearTimeout(this.suspendedEvent);
}

this.openScope.isOpen = false;
protected closeDropdown(event:MouseEvent):void {
this.suspendedEvent = setTimeout(() => {
if (!this.openScope) {
return;
}

if (event && this.openScope.autoClose === DISABLED) {
return;
}

if (event && this.openScope.toggleEl &&
this.openScope.toggleEl.nativeElement.contains(event.target)) {
return;
}

if (event && this.openScope.autoClose === NONINPUT &&
this.openScope.menuEl &&
/input|textarea/i.test((event.target as any).tagName) &&
this.openScope.menuEl.nativeElement.contains(event.target)) {
return;
}

if (event && this.openScope.autoClose === OUTSIDECLICK &&
this.openScope.menuEl &&
this.openScope.menuEl.nativeElement.contains(event.target)) {
return;
}

if (event) {
event.preventDefault();
event.stopPropagation();
}
this.openScope.isOpen = false;
}, 0);
}

protected keybindFilter(event:KeyboardEvent):void {
Expand All @@ -84,5 +99,3 @@ export class DropdownService {
}
}
}

export let dropdownService = new DropdownService();
8 changes: 4 additions & 4 deletions src/spec/dropdown.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* tslint:disable:max-file-line-count */
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';

import { DropdownModule } from '../dropdown/dropdown.module';
import { DropdownConfig } from '../dropdown/dropdown.config';
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('Directive: Dropdown', () => {
expect(element.querySelector('.dropdown').classList).not.toContain('open');
});

it('should close by click on nonInput menu item', () => {
it('should close by click on nonInput menu item', fakeAsync(() => {
const html = `
<div dropdown>
<button dropdownToggle>Dropdown</button>
Expand All @@ -100,10 +100,10 @@ describe('Directive: Dropdown', () => {
fixture.detectChanges();
expect(element.querySelector('.dropdown').classList).toContain('open');
element.querySelector('li').click();
tick();
fixture.detectChanges();
expect(element.querySelector('.dropdown').classList).not.toContain('open');

});
}));

it('should not close by click on input or textarea menu item', () => {
const html = `
Expand Down
Loading

0 comments on commit 29dceba

Please sign in to comment.