Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comp(dropdown) fixed disabled tests, removed outdated #1605

Merged
merged 7 commits into from
Feb 28, 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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ script:
- npm run build
- rm -rf node_modules/ng2-bootstrap
- npm i ./dist
#angular-cli test coverage is broken
#- npm run test-coverage
- npm run test-coverage
- npm run test

after_success:
Expand Down
6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ const customLaunchers = require('./scripts/sauce-browsers').customLaunchers;
module.exports = function (config) {
const configuration = {
basePath: '',
frameworks: ['jasmine', 'angular-cli'],
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-remap-istanbul'),
require('angular-cli/plugins/karma')
require('@angular/cli/plugins/karma')
],
files: [
{pattern: './scripts/test.ts', watched: false}
],
preprocessors: {
'./scripts/test.ts': ['angular-cli']
'./scripts/test.ts': ['@angular/cli']
},
remapIstanbulReporter: {
reports: {
Expand Down
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