From 3e2968310b9c4310d321be19f8741e566d319f92 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 3 Jan 2014 13:00:36 -0800 Subject: [PATCH] Ensure "New Relation..." is always available (fixes #2066) --- js/id/ui/raw_membership_editor.js | 1 + js/lib/d3.combobox.js | 11 +++++++++-- test/spec/lib/d3.combobox.js | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/js/id/ui/raw_membership_editor.js b/js/id/ui/raw_membership_editor.js index fbda18c5f..03a25f255 100644 --- a/js/id/ui/raw_membership_editor.js +++ b/js/id/ui/raw_membership_editor.js @@ -152,6 +152,7 @@ iD.ui.RawMembershipEditor = function(context) { .attr('type', 'text') .attr('class', 'member-entity-input') .call(d3.combobox() + .minItems(1) .fetcher(function(value, callback) { callback(relations(value)); }) diff --git a/js/lib/d3.combobox.js b/js/lib/d3.combobox.js index 9b169374d..d59e463e1 100644 --- a/js/lib/d3.combobox.js +++ b/js/lib/d3.combobox.js @@ -1,7 +1,8 @@ d3.combobox = function() { var event = d3.dispatch('accept'), data = [], - suggestions = []; + suggestions = [], + minItems = 2; var fetcher = function(val, cb) { cb(data.filter(function(d) { @@ -192,7 +193,7 @@ d3.combobox = function() { } function render() { - if (suggestions.length > 1 && document.activeElement === input.node()) { + if (suggestions.length >= minItems && document.activeElement === input.node()) { show(); } else { hide(); @@ -258,5 +259,11 @@ d3.combobox = function() { return combobox; }; + combobox.minItems = function(_) { + if (!arguments.length) return minItems; + minItems = _; + return combobox; + }; + return d3.rebind(combobox, event, 'on'); }; diff --git a/test/spec/lib/d3.combobox.js b/test/spec/lib/d3.combobox.js index e048e1082..a0fa5d66c 100644 --- a/test/spec/lib/d3.combobox.js +++ b/test/spec/lib/d3.combobox.js @@ -95,6 +95,13 @@ describe("d3.combobox", function() { expect(body.selectAll('.combobox-option').size()).to.equal(0); }); + it("shows menu on focus if it would contain at least minItems items", function() { + combobox.minItems(1); + input.property('value', 'f').call(combobox.data(data)); + input.node().focus(); + expect(body.selectAll('.combobox-option').size()).to.equal(1); + }); + it("shows all entries when clicking on the caret", function() { input.property('value', 'foo').call(combobox.data(data)); happen.mousedown(body.selectAll('.combobox-caret').node());