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

Hide hint for long queries #26

Merged
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
27 changes: 27 additions & 0 deletions src/js/input_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var InputView = (function() {
setTimeout(that._compareQueryToInputValue, 0);
});
}

// helps with calculating the width of the input's value
this.$overflowHelper = buildOverflowHelper(this.$input);
}

utils.mixin(InputView.prototype, EventTarget, {
Expand Down Expand Up @@ -126,6 +129,12 @@ var InputView = (function() {
return (this.$input.css('direction') || 'ltr').toLowerCase();
},

isOverflow: function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like this: this.$input[0].scrollWidth > this.$input.width()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think that'll work in Firefox. https://bugzilla.mozilla.org/show_bug.cgi?id=343143

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn.

this.$overflowHelper.text(this.getInputValue());

return this.$overflowHelper.width() > this.$input.width();
},

isCursorAtEnd: function() {
var valueLength = this.$input.val().length,
selectionStart = this.$input[0].selectionStart,
Expand All @@ -150,6 +159,24 @@ var InputView = (function() {

return InputView;

function buildOverflowHelper($input) {
return $('<span></span>')
.css({
// position helper off-screen
position: 'absolute',
left: '-9999px',
visibility: 'hidden',
// use same font css as input to calculate accurate width
font: $input.css('font'),
wordSpacing: $input.css('word-spacing'),
letterSpacing: $input.css('letter-spacing'),
textIndent: $input.css('text-indent'),
textRendering: $input.css('text-rendering'),
textTransform: $input.css('text-transform')
})
.insertAfter($input);
}

function compareQueries(a, b) {
// strips leading whitespace and condenses all whitespace
a = (a || '').replace(/^\s*/g, '').replace(/\s{2,}/g, ' ').toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion src/js/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var TypeaheadView = (function() {
beginsWithQuery,
match;

if (hint && this.dropdownView.isOpen()) {
if (hint && this.dropdownView.isOpen() && !this.inputView.isOverflow()) {
inputValue = this.inputView.getInputValue();
query = inputValue
.replace(/\s{2,}/g, ' ') // condense whitespace
Expand Down
16 changes: 16 additions & 0 deletions test/input_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ describe('InputView', function() {
});
});

describe('#isOverflow', function() {
describe('when input\'s value is overflowing', function() {
it('should return false', function() {
this.$input.val(new Array(1000).join('a'));
expect(this.inputView.isOverflow()).toBe(true);
});
});

describe('when input\'s value is not overflowing', function() {
it('should return false', function() {
this.$input.val('t');
expect(this.inputView.isOverflow()).toBe(false);
});
});
});

describe('#isCursorAtEnd', function() {
beforeEach(function() {
this.$input.val('text cursor goes here');
Expand Down
12 changes: 12 additions & 0 deletions test/typeahead_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ describe('TypeaheadView', function() {
// ------------

function _updateHintSpecHelper(view, eventType) {
describe('if input\'s value is overflowing', function() {
it('should clear hint', function() {
this.inputView.isOverflow.andReturn(true);
this.inputView.getInputValue.andReturn('bl');
this.dropdownView.getFirstSuggestion.andReturn({ value: 'blah' });

this[view].trigger(eventType);

expect(this.inputView.setHintValue).not.toHaveBeenCalled();
});
});

describe('if dropdown menu is closed', function() {
it('should not show hint', function() {
this.dropdownView.isOpen.andReturn(false);
Expand Down