/*jshint curly: true, eqeqeq: true, undef: true, devel: true, browser: true */ /*global jQuery */ /** * Turn a text box into an auto suggest box which search's and * displays results specified in a JSON string * * * @name jsonSuggest * @type jQuery plugin * @author Tom Coote (tomcoote.co.uk) * @version 2.0.1 * @copyright Copyright 2011 Tom Coote * @license released under the BSD (3-clause) licences * * @param settings ; * url : [default ''] A URL that will return a JSON response. Called via $.getJSON, it is passed a * data dictionary containing the user typed search phrase. It must return a JSON string that * represents the array of results to display. * data : [default []]An array or JSON string representation of an array of data to search through. * Example of the array format is as follows: [ { id: 1, text: 'Thomas', image: 'img/avator1.jpg', // optional extra: 'www.thomas.com' // optional }, { id: 2, text: 'Frederic', image: 'img/avator2.jpg', // optional extra: 'www.freddy.com' // optional }, { id: 2, text: 'James', image: 'img/avator2.jpg', // optional extra: 'www.james.com' // optional } ] * minCharacters : [default 1] Number of characters that the input should accept before running a search. * maxResults : [default undefined] If set then no more results than this number will be found. * wildCard : [default ''] A character to be used as a match all wildcard when searching. Leaving empty * will mean results are matched inside strings but if a wildCard is present then results are * matched from the beginning of strings. * caseSensitive : [default false] True if the filter search's are to be case sensitive. * notCharacter : [default !] The character to use at the start of any search text to specify that the results * should NOT contain the following text. * maxHeight : [default 350] This is the maximum height that the results box can reach before scroll bars * are shown instead of getting taller. * width: [default undefined] If set this will become the width of the results box else the box will be * the same width as the input. * highlightMatches : [default true] This will add strong tags around the text that matches the search text in each result. * onSelect : [default undefined] Function that gets called once a result has been selected, gets passed in * the object version of the result as specified in the JSON data. * */ (function($) { $.fn.NCBOAutocomplete = function(settings) { var defaults = { url: '', data: [], minCharacters: 1, maxResults: undefined, wildCard: '', caseSensitive: false, notCharacter: '!', maxHeight: 350, highlightMatches: true, onSelect: undefined, width: 550, property: 'text', searchParameter: 'search', additionalParameters: {}, resultAttribute: null, searchTextSuffix: "", searchFromRoot: null }, getJSONTimeout; settings = $.extend(defaults, settings); // Handle search in subtree for cases where the root node is not the actual root of the ontology if (settings.searchFromRoot != null) { settings.additionalParameters.subtree_id = settings.searchFromRoot settings.additionalParameters.ontology = settings.additionalParameters.ontologies } return this.each(function() { /** * Escape some text so that it can be used inside a regular expression * without implying regular expression rules iself. */ function regexEscape(txt, omit) { var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\']; if (omit) { for (var i = 0; i < specials.length; i++) { if (specials[i] === omit) { specials.splice(i,1); } } } var escapePatt = new RegExp('(\\' + specials.join('|\\') + ')', 'g'); return txt.replace(escapePatt, '\\$1'); } var obj = $(this), wildCardPatt = new RegExp(regexEscape(settings.wildCard || ''),'g'), results = $('