diff --git a/packages/composer-playground/src/app/app.module.ts b/packages/composer-playground/src/app/app.module.ts index 47c5575f16..6df9da9602 100644 --- a/packages/composer-playground/src/app/app.module.ts +++ b/packages/composer-playground/src/app/app.module.ts @@ -55,6 +55,7 @@ import { ViewCertificateComponent } from './view-certificate'; import { FileDragDropDirective } from './directives/file-drag-drop'; import { CheckOverFlowDirective } from './directives/check-overflow'; import { FocusHereDirective } from './directives/focus-here'; +import { DebounceDirective } from './directives/debounce'; import { PerfectScrollbarModule } from 'ngx-perfect-scrollbar'; import { AdminService } from './services/admin.service'; @@ -153,7 +154,8 @@ type StoreType = { TransactionComponent, VersionCheckComponent, ViewCertificateComponent, - WelcomeComponent + WelcomeComponent, + DebounceDirective ], imports: [ // import Angular's modules BrowserAnimationsModule, diff --git a/packages/composer-playground/src/app/directives/debounce/debounce.directive.spec.ts b/packages/composer-playground/src/app/directives/debounce/debounce.directive.spec.ts new file mode 100644 index 0000000000..48d6db54bb --- /dev/null +++ b/packages/composer-playground/src/app/directives/debounce/debounce.directive.spec.ts @@ -0,0 +1,59 @@ +/* tslint:disable:no-unused-variable */ +/* tslint:disable:no-unused-expression */ +/* tslint:disable:no-var-requires */ +/* tslint:disable:max-classes-per-file */ +import { ComponentFixture, TestBed, async, fakeAsync, tick, inject } from '@angular/core/testing'; +import { FormsModule } from '@angular/forms'; +import { Component, Renderer, } from '@angular/core'; +import { By } from '@angular/platform-browser'; + +import * as sinon from 'sinon'; +import * as chai from 'chai'; +import { DebounceDirective } from './debounce.directive'; + +let should = chai.should(); + +@Component({ + selector: 'test', + template: ` + + ` +}) + +class TestComponent { + value: string = ''; + clicked: boolean = false; + onChange = sinon.stub(); +} + +describe('DebounceDirective', () => { + let component: TestComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [TestComponent, DebounceDirective], + imports: [FormsModule] + }) + .compileComponents(); + + fixture = TestBed.createComponent(TestComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + })); + + it('should create the directive', async(() => { + component.should.be.ok; + })); + + it('should call onChange after 500ms', async(fakeAsync(() => { + component = fixture.componentInstance; + let divEl = fixture.debugElement.query(By.css('input')); + let event = {}; + + divEl.nativeElement.dispatchEvent(new Event('keyup')); + fixture.detectChanges(); + tick(500); + component.onChange.should.have.been.called; + }))); +}); diff --git a/packages/composer-playground/src/app/directives/debounce/debounce.directive.ts b/packages/composer-playground/src/app/directives/debounce/debounce.directive.ts new file mode 100644 index 0000000000..25e70834ae --- /dev/null +++ b/packages/composer-playground/src/app/directives/debounce/debounce.directive.ts @@ -0,0 +1,31 @@ +import { + EventEmitter, + ElementRef, + OnInit, + Directive, + Input, + Output +} from '@angular/core'; +import { Observable } from 'rxjs'; +import { NgModel } from '@angular/forms'; + +@Directive({ + selector: '[debounce]' +}) +export class DebounceDirective implements OnInit { + @Input() delay: number = 500; + @Output() debounceFunc: EventEmitter = new EventEmitter(); + + constructor(private elementRef: ElementRef, private model: NgModel) { + } + + ngOnInit(): void { + const eventStream = Observable.fromEvent(this.elementRef.nativeElement, 'keyup') + .map(() => { + return this.model.value; + }) + .debounceTime(this.delay); + eventStream.subscribe((input) => this.debounceFunc.emit(input)); + } + +} diff --git a/packages/composer-playground/src/app/directives/debounce/index.ts b/packages/composer-playground/src/app/directives/debounce/index.ts new file mode 100644 index 0000000000..bc8b591ace --- /dev/null +++ b/packages/composer-playground/src/app/directives/debounce/index.ts @@ -0,0 +1 @@ +export * from './debounce.directive'; diff --git a/packages/composer-playground/src/app/editor-file/editor-file.component.html b/packages/composer-playground/src/app/editor-file/editor-file.component.html index 54c0b0bae2..541768e4f5 100644 --- a/packages/composer-playground/src/app/editor-file/editor-file.component.html +++ b/packages/composer-playground/src/app/editor-file/editor-file.component.html @@ -1,8 +1,8 @@ - +