From 55eccb34938734150607eb3b0fd59c3fba95bfd0 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Thu, 16 Jun 2016 19:34:46 +0200 Subject: [PATCH] feat(range): add debounce input for ionChange event Closes #6894 --- src/components/range/range.ts | 20 ++++++++++++++--- src/components/range/test/basic/page1.html | 2 +- src/components/searchbar/searchbar.ts | 17 +++++++++------ src/util/debouncer.ts | 25 ++++++++++++++++++++++ 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 src/util/debouncer.ts diff --git a/src/components/range/range.ts b/src/components/range/range.ts index 3bcd6ed47c5..554819bfb72 100644 --- a/src/components/range/range.ts +++ b/src/components/range/range.ts @@ -5,6 +5,7 @@ import {Form} from '../../util/form'; import {isTrueProperty, isNumber, isString, isPresent, clamp} from '../../util/util'; import {Item} from '../item/item'; import {pointerCoord, Coordinates, raf} from '../../util/dom'; +import {Debouncer} from '../../util/debouncer'; const RANGE_VALUE_ACCESSOR = new Provider( @@ -214,6 +215,7 @@ export class Range { private _snaps: boolean = false; private _removes: Function[] = []; private _mouseRemove: Function; + private _debouncer: Debouncer = new Debouncer(0); /** * @private @@ -293,6 +295,17 @@ export class Range { this._pin = isTrueProperty(val); } + /** + * @input {number} If true, a pin with integer value is shown when the knob is pressed. Defaults to `false`. + */ + @Input() + get debounce(): number { + return this._debouncer.wait; + } + set debounce(val: number) { + this._debouncer.wait = val; + } + /** * @input {boolean} Show two knobs. Defaults to `false`. */ @@ -519,9 +532,10 @@ export class Range { this.value = newVal; } - this.onChange(this.value); - - this.ionChange.emit(this); + this._debouncer.debounce(() => { + this.onChange(this.value); + this.ionChange.emit(this); + }); } this.updateBar(); diff --git a/src/components/range/test/basic/page1.html b/src/components/range/test/basic/page1.html index ace0d3ada0a..6e9e8cb5d34 100644 --- a/src/components/range/test/basic/page1.html +++ b/src/components/range/test/basic/page1.html @@ -19,7 +19,7 @@ - + diff --git a/src/components/searchbar/searchbar.ts b/src/components/searchbar/searchbar.ts index 44237c589ce..0c05acaaa0c 100644 --- a/src/components/searchbar/searchbar.ts +++ b/src/components/searchbar/searchbar.ts @@ -3,6 +3,7 @@ import {NgControl} from '@angular/common'; import {Config} from '../../config/config'; import {isPresent} from '../../util/util'; +import {Debouncer} from '../../util/debouncer'; /** @@ -46,10 +47,10 @@ import {isPresent} from '../../util/util'; }) export class Searchbar { private _value: string|number = ''; - private _tmr: any; private _shouldBlur: boolean = true; private _isActive: boolean = false; private _searchbarInput: ElementRef; + private _debouncer: Debouncer = new Debouncer(250); /** * @input {string} Set the the cancel button text. Default: `"Cancel"`. @@ -64,7 +65,13 @@ export class Searchbar { /** * @input {number} How long, in milliseconds, to wait to trigger the `input` event after each keystroke. Default `250`. */ - @Input() debounce: number = 250; + @Input() + get debounce(): number { + return this._debouncer.wait; + } + set debounce(val: number) { + this._debouncer.wait = val; + } /** * @input {string} Set the input's placeholder. Default `"Search"`. @@ -268,13 +275,11 @@ export class Searchbar { */ inputChanged(ev: any) { let value = ev.target.value; - - clearTimeout(this._tmr); - this._tmr = setTimeout(() => { + this._debouncer.debounce(() => { this._value = value; this.onChange(this._value); this.ionInput.emit(ev); - }, Math.round(this.debounce)); + }); } /** diff --git a/src/util/debouncer.ts b/src/util/debouncer.ts new file mode 100644 index 00000000000..327de2eeddf --- /dev/null +++ b/src/util/debouncer.ts @@ -0,0 +1,25 @@ + +export class Debouncer { + private timer: number = null; + callback: Function; + + constructor(public wait: number) { } + + debounce(callback: Function) { + this.callback = callback; + this.schedule(); + } + + schedule() { + if (this.timer) { + clearTimeout(this.timer); + this.timer = null; + } + if (this.wait <= 0) { + this.callback(); + } else { + this.timer = setTimeout(this.callback, this.wait); + } + } + +}