typeahead.js icon indicating copy to clipboard operation
typeahead.js copied to clipboard

Custom event suggestions

Open jharding opened this issue 13 years ago • 39 comments

I was pretty conservative with what custom events I added in v0.9.0, now it's time to get a little crazier. Below is a combination of events I think would be nice to have and some events I've seen people asking for:

  • autocompleted – when the user tabs to select the suggestion the hint is showing.
  • rendered – suggestions for a given query were retrieved and rendered.
  • prefetch failed – requesting or processing the prefetched data fails for whatever reason.
  • nothing left – no suggestions were found for a given query.
  • suggestion highlighted – the user mouses or keys over a suggestion in the dropdown menu.
  • changed – emulate the native change event of an input element.

I'm not sure implementing all of these is plausible, but I at least wanted to document them so a discussion could be had.

Any other custom events you'd like to see implemented?

jharding avatar Mar 24 '13 07:03 jharding

In regards to the autocompleted event, I could see an argument being made that it should be lumped into typehead:selected

jharding avatar Mar 24 '13 07:03 jharding

Eh scratch that, typeahead:autocompleted should be separate from typeahead:selected.

jharding avatar Mar 24 '13 18:03 jharding

First of all: awesome work! Keep it up! I would be happy about an event being triggered as soon as a remote request is being fired and another one when data comes back from the remote source.

fnwbr avatar Mar 25 '13 12:03 fnwbr

@FLOKER just so I understand the use case, how would you use those events?

jharding avatar Mar 25 '13 23:03 jharding

I know that my remote source takes about 4 - 10 seconds to answer the request typeahead.js sends. And I would like to show something like a spinning wheel or any kind of indicator.

For example:

$('.typeahead').typeahead({ 
   ...
}).on('typeahead:requestSentOff', function() {
   showSpinningWheel();
}).on('typeahead:remoteDataReceived', function() {
   hideSpinningWheel();
});

I don't really know if typeahead:remoteDataReceived is actually the same like typeahead:opened, but I noticed that typeahead:opened gets already fired when I focus my input field, even without having entered something... Thanks in advance!

Edit: I just noticed the perfect example of what I would like to do is the GitHub issue-search in projects. Just if you go to Issues and type something in the upper right hand searchbox, you notice the small indicator on the left. :grin:

fnwbr avatar Mar 26 '13 08:03 fnwbr

Should the typeahead:opened fire on focus if the actual .tt-dropdown-menu is not displayed?

joelataylor avatar Mar 28 '13 17:03 joelataylor

@joelataylor first some context. The dropdown menu has 2 properties that determine whether or not it is visible: isOpened and isEmpty. The only time the dropdown menu is visible is isOpened === true && isEmpty === false. typeahead:opened gets triggered when isOpened toggles from false to true.

The events that can toggle isOpened to true:

  • input gains focus
  • input has focus and the query changes
  • input has focus and either the up or down arrow are pressed

The events that can toggle isOpened to false:

  • input loses focus
  • the esc key is pressed

With this being the case, I think the way typeahead:opened is currently triggered is fine. What needs to be added is some sort of event that is triggered when suggestions become visible. I'm guessing the typeahead:rendered event I mentioned previously in this thread will fill that void.

jharding avatar Mar 31 '13 06:03 jharding

@FLOKER sorry I thought I replied to your last comment. I like the idea of being able to replicate how the GitHub issue search works. I'll try and think of a good way to achieve that functionality.

jharding avatar Mar 31 '13 06:03 jharding

@jharding thanks. I think something like the previously mentioned typeahead:rendered would work to hide for example the spinning wheel indicator. You would only have to think about an event being fired when the query to the remote server is being made.

$('.typeahead').typeahead({ 
   ...
}).on('typeahead:remoteRequestSentOff', function() {
   // gets fired when typeahead makes a query to the remote source
   showSpinningWheel();
}).on('typeahead:rendered', function() {
   hideSpinningWheel();
});

fnwbr avatar Mar 31 '13 09:03 fnwbr

@jharding thanks for the explanation. So if I could check the isEmpty property I could properly toggle - although the opened event would only fire once. I'd have to setup a keyup listener. Hmmm ... well I look forward to the new events!! :)

joelataylor avatar Mar 31 '13 14:03 joelataylor

I could really use the custom typeahead:suggestionhighlighted. I want to recreate part of Yahoo's 'Search Assist' search box, but right now it's only possible to capture the arrow key events to find out which result is highlighted:

http://www.visualmedia.nl/html5/supersearch/

tinusg avatar Apr 02 '13 23:04 tinusg

How are you going about capturing the key events?

jharding avatar Apr 02 '13 23:04 jharding

Alright, I see you're listening for keydown. Is there a reason why you can't do something similar with mouseenter?

jharding avatar Apr 02 '13 23:04 jharding

There must be a reason, because this doesn't trigger the handler:

        $('.typeahead').on('mouseover', '.tt-suggestion', function(e){
            console.log('fire');
            setPreview();
        });                             

I'm looking at the source of typeahead.js right now to figure out where it goes wrong, but jQuery and events are not my friends..

tinusg avatar Apr 02 '13 23:04 tinusg

Try:

$('.twitter-typeahead').on('mouseover', '.tt-suggestion', function(e){
  console.log('fire');
  setPreview();
});  

jharding avatar Apr 02 '13 23:04 jharding

Thanks Mr. Harding, you pushed me in the right direction. It was actually:

        $('.tt-dropdown-menu').on('mouseover', '.tt-suggestion', function(e){
            setPreview();
        });                             

The mockup now works like a charm!

tinusg avatar Apr 02 '13 23:04 tinusg

Hi

Is there a way of obtaining the Dataset name of the selected datum from custom events 'typeahead:selected' or 'typeahead:autocompleted'? I want to have multiple datasets for a single input and have callback functionality specific to each dataset.

Thanks Ste

steswinbank avatar Apr 12 '13 13:04 steswinbank

@steswinbank the only way you can do that right now is by adding additional information to the datums.

$('.typeahead').typeahead({
  name: 'dataset1',
  local: [ 
    { value: 'val1', dataset: 'dataset1' }, 
    { value: 'val2', dataset: 'dataset1' }
  ]
});

jharding avatar Apr 13 '13 23:04 jharding

I need what @steswinbank said too, and I really don't want to waste bandwidth for an attribute that is already known to typeahead.

meh avatar Apr 14 '13 18:04 meh

I've found a way of obtaining the dataset, but this is a pretty fragile hack relying on the markup not changing structure: o.getDatasetName = function (inputEl) { return $(inputEl).parent().find("div.tt-suggestion").parent().parent().attr("class").substr(11) }

This returns a string with the dataset name.

steswinbank avatar Apr 16 '13 15:04 steswinbank