Sha256: 84e18ca17e4e6750d00989cba0c54c013279029f9e0247d3e4d8c6fa4d25ee86

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

import 'typeahead.js/dist/typeahead.jquery.js'

IQVOC.autocomplete = (function($) {

// `field` is the input field to be augmented
// `source` is a function expected to calculate the results - it is invoked with
// the respective query string and a callback and expected to invoke that
// callback with an array of `{ value, label }` objects
// `options` is an object with optional members `displayKey`, `noResultsMsg` and
// `onSelect`
// TODO: built-in support for loading indicator?
function augment(field, source, options) {
  field = field.jquery ? field : $(field);

  options = options || {};
  options.noResultsMsg = options.noResultsMsg || "no results";
  options.displayKey = options.displayKey || "value";

  field.typeahead({
    minLength: 2,
    highlight: true
  }, {
    source: source,
    displayKey: options.displayKey,
    templates: {
      empty: function() {
        var el = $("<p />").text(options.noResultsMsg);
        return $("<div />").append(el).html();
      },
      suggestion: function(item) {
        var el = $("<p />").text(item.label);
        return $("<div />").append(el).html();
      }
    }
  }).bind("typeahead:selected", function(ev, selected, name) {
    if(options.onSelect) {
      options.onSelect.call(this, ev, selected); // TODO: document
    }
  });
}

return augment;

}(jQuery));

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
iqvoc-4.14.5 app/assets/javascripts/iqvoc/autocomplete.js
iqvoc-4.14.4 app/assets/javascripts/iqvoc/autocomplete.js
iqvoc-4.13.2 app/assets/javascripts/iqvoc/autocomplete.js
iqvoc-4.13.0 app/assets/javascripts/iqvoc/autocomplete.js