From 0b239c8f37975fc4d02971453f27fd38325e01ee Mon Sep 17 00:00:00 2001 From: Corey Johnson & Kevin Sawicki Date: Wed, 12 Dec 2012 14:52:31 -0800 Subject: [PATCH] Debounce filter input on select lists --- spec/app/select-list-spec.coffee | 8 ++++++++ src/app/select-list.coffee | 4 +++- src/extensions/outline-view/spec/outline-view-spec.coffee | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/app/select-list-spec.coffee b/spec/app/select-list-spec.coffee index d219c6aaf..f017c5051 100644 --- a/spec/app/select-list-spec.coffee +++ b/spec/app/select-list-spec.coffee @@ -35,6 +35,8 @@ describe "SelectList", -> it "filters the elements in the list based on the scoreElement function and selects the first item", -> miniEditor.insertText('la') + window.advanceClock(selectList.inputThrottle) + expect(list.find('li').length).toBe 2 expect(list.find('li:contains(Alpha)')).toExist() expect(list.find('li:contains(Delta)')).toExist() @@ -44,11 +46,15 @@ describe "SelectList", -> it "displays an error if there are no matches, removes error when there are matches", -> miniEditor.insertText('nothing will match this') + window.advanceClock(selectList.inputThrottle) + expect(list.find('li').length).toBe 0 expect(selectList.error).not.toBeHidden() expect(selectList).toHaveClass("error") miniEditor.setText('la') + window.advanceClock(selectList.inputThrottle) + expect(list.find('li').length).toBe 2 expect(selectList.error).not.toBeVisible() expect(selectList).not.toHaveClass("error") @@ -109,6 +115,8 @@ describe "SelectList", -> describe "when there is no item selected (because the list is empty)", -> it "does not trigger the confirmed hook", -> miniEditor.insertText("i will never match anything") + window.advanceClock(selectList.inputThrottle) + expect(list.find('li')).not.toExist() miniEditor.trigger 'core:confirm' expect(selectList.confirmed).not.toHaveBeenCalled() diff --git a/src/app/select-list.coffee b/src/app/select-list.coffee index 9906a8f6a..29a937218 100644 --- a/src/app/select-list.coffee +++ b/src/app/select-list.coffee @@ -2,6 +2,7 @@ $ = require 'jquery' { View } = require 'space-pen' Editor = require 'editor' fuzzyFilter = require 'fuzzy-filter' +_ = require 'underscore' module.exports = class SelectList extends View @@ -15,13 +16,14 @@ class SelectList extends View @viewClass: -> 'select-list' maxItems: Infinity + inputThrottle: 50 filteredArray: null cancelling: false initialize: -> requireStylesheet 'select-list.css' - @miniEditor.getBuffer().on 'change', => @populateList() + @miniEditor.getBuffer().on 'change', _.debounce((=> @populateList()), @inputThrottle) @miniEditor.on 'focusout', => @cancel() unless @cancelling @on 'core:move-up', => @selectPreviousItem() @on 'core:move-down', => @selectNextItem() diff --git a/src/extensions/outline-view/spec/outline-view-spec.coffee b/src/extensions/outline-view/spec/outline-view-spec.coffee index 646bdb6b2..e058e4c09 100644 --- a/src/extensions/outline-view/spec/outline-view-spec.coffee +++ b/src/extensions/outline-view/spec/outline-view-spec.coffee @@ -45,6 +45,8 @@ describe "OutlineView", -> runs -> outlineView.miniEditor.setText("nothing will match this") + window.advanceClock(outlineView.inputThrottle) + expect(rootView.find('.outline-view')).toExist() expect(outlineView.list.children('li').length).toBe 0 expect(outlineView.error).toBeVisible() @@ -53,6 +55,8 @@ describe "OutlineView", -> # Should remove error outlineView.miniEditor.setText("") + window.advanceClock(outlineView.inputThrottle) + expect(outlineView.list.children('li').length).toBe 2 expect(outlineView).not.toHaveClass "error" expect(outlineView.error).not.toBeVisible()