Remove trailing whitespace

This commit is contained in:
Wesley Moore 2010-03-19 07:24:01 +11:00
parent 9d0b51a33e
commit fa5c2476f2

View file

@ -9,64 +9,64 @@
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
/*jslint eqeqeq: true, browser: true */ /*jslint eqeqeq: true, browser: true */
/*global jQuery */ /*global jQuery */
/** /**
* Turn a text box into an auto suggest box which search's and * Turn a text box into an auto suggest box which search's and
* displays results specified in a JSON string * displays results specified in a JSON string
* *
* *
* @name jsonSuggest * @name jsonSuggest
* @type jQuery * @type jQuery
* @param searchData : [required] Can be one of three things; a JSON string which specified the search data, an object that is representative of a parsed JSON string or a function which returns either of these two things. * @param searchData : [required] Can be one of three things; a JSON string which specified the search data, an object that is representative of a parsed JSON string or a function which returns either of these two things.
* expected object format example; (either as raw object or JSON string) * expected object format example; (either as raw object or JSON string)
[ [
{ {
id: 1, id: 1,
text: 'Thomas', text: 'Thomas',
image: 'img/avator1.jpg', // optional image: 'img/avator1.jpg', // optional
extra: 'www.thomas.com' // optional extra: 'www.thomas.com' // optional
}, },
{ {
id: 2, id: 2,
text: 'Frederic', text: 'Frederic',
image: 'img/avator2.jpg', // optional image: 'img/avator2.jpg', // optional
extra: 'www.freddy.com' // optional extra: 'www.freddy.com' // optional
}, },
{ {
id: 2, id: 2,
text: 'James', text: 'James',
image: 'img/avator2.jpg', // optional image: 'img/avator2.jpg', // optional
extra: 'www.james.com' // optional extra: 'www.james.com' // optional
} }
] ]
* @param Object settings; [optional] * @param Object settings; [optional]
* minCharacters : [default 1] Number of characters that the input should accept before running a search. * 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. * 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 * 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. * strings but if a wildCard is present then results are matched from the beginning of strings.
* caseSensitive : [defautl false] True if the filter search's are to be case sensitive. * caseSensitive : [defautl 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. * 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. * maxHeight : [default 350] This is the maximum height that the results box can reach before scroll bars are shown instead of getting taller.
* highlightMatches: [default true] This will add strong tags around the text that matches the search text in each result. * 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 string * 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 string
* ajaxResults : [default false] If this is set to true then you must specify a function as the searchData construction parameter. This is because when this * ajaxResults : [default false] If this is set to true then you must specify a function as the searchData construction parameter. This is because when this
* settings is true then results are retrieved from an external function each time they are needed instead of being retrieved from the data given on * settings is true then results are retrieved from an external function each time they are needed instead of being retrieved from the data given on
* contruction. The searchData function must return a JSON string of resulting objects or the object which represents the JSON string. The function is * contruction. The searchData function must return a JSON string of resulting objects or the object which represents the JSON string. The function is
* passed the following paramenters; * passed the following paramenters;
* 1. The search text typed into the input box * 1. The search text typed into the input box
* 2. The current wildCard setting * 2. The current wildCard setting
* 3. The current caseSensitive setting * 3. The current caseSensitive setting
* 4. The current notCharacter setting * 4. The current notCharacter setting
* 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 * 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
* @author Tom Coote (www.tomcoote.co.uk) * @author Tom Coote (www.tomcoote.co.uk)
* @version 1.2.4 * @version 1.2.4
@ -75,7 +75,7 @@
(function($){ (function($){
$.fn.jsonSuggest = function(searchData, settings) { $.fn.jsonSuggest = function(searchData, settings) {
var defaults = { var defaults = {
minCharacters: 1, minCharacters: 1,
maxResults: undefined, maxResults: undefined,
wildCard: "", wildCard: "",
@ -86,36 +86,36 @@
onSelect: undefined, onSelect: undefined,
ajaxResults: false, ajaxResults: false,
width: undefined width: undefined
}; };
settings = $.extend(defaults, settings); settings = $.extend(defaults, settings);
return this.each(function() { return this.each(function() {
function regexEscape(txt, omit) { function regexEscape(txt, omit) {
var specials = ['/', '.', '*', '+', '?', '|', var specials = ['/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\']; '(', ')', '[', ']', '{', '}', '\\'];
if (omit) { if (omit) {
for (var i=0; i < specials.length; i++) { for (var i=0; i < specials.length; i++) {
if (specials[i] === omit) { specials.splice(i,1); } if (specials[i] === omit) { specials.splice(i,1); }
} }
} }
var escapePatt = new RegExp('(\\' + specials.join('|\\') + ')', 'g'); var escapePatt = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
return txt.replace(escapePatt, '\\$1'); return txt.replace(escapePatt, '\\$1');
} }
var obj = $(this), var obj = $(this),
wildCardPatt = new RegExp(regexEscape(settings.wildCard || ''),'g'), wildCardPatt = new RegExp(regexEscape(settings.wildCard || ''),'g'),
results = $('<div />'), results = $('<div />'),
currentSelection, pageX, pageY; currentSelection, pageX, pageY;
// When an item has been selected then update the input box, // When an item has been selected then update the input box,
// hide the results again and if set, call the onSelect function // hide the results again and if set, call the onSelect function
function selectResultItem(item) { function selectResultItem(item) {
obj.val(item.text); obj.val(item.text);
$(results).html('').hide(); $(results).html('').hide();
if (typeof settings.onSelect === 'function') { if (typeof settings.onSelect === 'function') {
settings.onSelect(item); settings.onSelect(item);
} }
@ -127,83 +127,83 @@
function setHoverClass(el) { function setHoverClass(el) {
$('div.resultItem', results).removeClass('hover'); $('div.resultItem', results).removeClass('hover');
$(el).addClass('hover'); $(el).addClass('hover');
currentSelection = el; currentSelection = el;
} }
// Build the results HTML based on an array of objects that matched // Build the results HTML based on an array of objects that matched
// the search criteria, highlight the matches if feature is turned on in // the search criteria, highlight the matches if feature is turned on in
// the settings. // the settings.
function buildResults(resultObjects, sFilterTxt) { function buildResults(resultObjects, sFilterTxt) {
sFilterTxt = "(" + sFilterTxt + ")"; sFilterTxt = "(" + sFilterTxt + ")";
var bOddRow = true, i, iFound = 0, var bOddRow = true, i, iFound = 0,
filterPatt = settings.caseSensitive ? new RegExp(sFilterTxt, "g") : new RegExp(sFilterTxt, "ig"); filterPatt = settings.caseSensitive ? new RegExp(sFilterTxt, "g") : new RegExp(sFilterTxt, "ig");
$(results).html('').hide(); $(results).html('').hide();
for (i = 0; i < resultObjects.length; i += 1) { for (i = 0; i < resultObjects.length; i += 1) {
var item = $('<div />'), var item = $('<div />'),
text = resultObjects[i].text; text = resultObjects[i].text;
if (settings.highlightMatches === true) { if (settings.highlightMatches === true) {
text = text.replace(filterPatt, "<strong>$1</strong>"); text = text.replace(filterPatt, "<strong>$1</strong>");
} }
$(item).append('<p class="text">' + text + '</p>'); $(item).append('<p class="text">' + text + '</p>');
if (typeof resultObjects[i].extra === 'string') { if (typeof resultObjects[i].extra === 'string') {
$(item).append('<p class="extra">' + resultObjects[i].extra + '</p>'); $(item).append('<p class="extra">' + resultObjects[i].extra + '</p>');
} }
if (typeof resultObjects[i].image === 'string') { if (typeof resultObjects[i].image === 'string') {
$(item).prepend('<img src="' + resultObjects[i].image + '" />'). $(item).prepend('<img src="' + resultObjects[i].image + '" />').
append('<br style="clear:both;" />'); append('<br style="clear:both;" />');
} }
$(item).addClass('resultItem'). $(item).addClass('resultItem').
addClass((bOddRow) ? 'odd' : 'even'). addClass((bOddRow) ? 'odd' : 'even').
click(function(n) { return function() { click(function(n) { return function() {
selectResultItem(resultObjects[n]); selectResultItem(resultObjects[n]);
};}(i)). };}(i)).
mouseover(function(el) { return function() { mouseover(function(el) { return function() {
setHoverClass(el); setHoverClass(el);
};}(item)); };}(item));
$(results).append(item); $(results).append(item);
bOddRow = !bOddRow; bOddRow = !bOddRow;
iFound += 1; iFound += 1;
if (typeof settings.maxResults === 'number' && iFound >= settings.maxResults) { if (typeof settings.maxResults === 'number' && iFound >= settings.maxResults) {
break; break;
} }
} }
if ($('div', results).length > 0) { if ($('div', results).length > 0) {
currentSelection = undefined; currentSelection = undefined;
$(results).show().css('height', 'auto'); $(results).show().css('height', 'auto');
if ($(results).height() > settings.maxHeight) { if ($(results).height() > settings.maxHeight) {
$(results).css({'overflow': 'auto', 'height': settings.maxHeight + 'px'}); $(results).css({'overflow': 'auto', 'height': settings.maxHeight + 'px'});
} }
} }
} }
// Prepare the search string based on the settings for this plugin, // Prepare the search string based on the settings for this plugin,
// run it against each item in the searchData and display any // run it against each item in the searchData and display any
// results on the page allowing selection by the user. // results on the page allowing selection by the user.
function runSuggest(e) { function runSuggest(e) {
if (this.value.length < settings.minCharacters) { if (this.value.length < settings.minCharacters) {
$(results).html('').hide(); $(results).html('').hide();
return false; return false;
} }
var resultObjects = [], var resultObjects = [],
sFilterTxt = (!settings.wildCard) ? regexEscape(this.value) : regexEscape(this.value, settings.wildCard).replace(wildCardPatt, '.*'), sFilterTxt = (!settings.wildCard) ? regexEscape(this.value) : regexEscape(this.value, settings.wildCard).replace(wildCardPatt, '.*'),
bMatch = true, bMatch = true,
filterPatt, i; filterPatt, i;
if (settings.notCharacter && sFilterTxt.indexOf(settings.notCharacter) === 0) { if (settings.notCharacter && sFilterTxt.indexOf(settings.notCharacter) === 0) {
sFilterTxt = sFilterTxt.substr(settings.notCharacter.length,sFilterTxt.length); sFilterTxt = sFilterTxt.substr(settings.notCharacter.length,sFilterTxt.length);
if (sFilterTxt.length > 0) { bMatch = false; } if (sFilterTxt.length > 0) { bMatch = false; }
@ -211,32 +211,32 @@
sFilterTxt = sFilterTxt || '.*'; sFilterTxt = sFilterTxt || '.*';
sFilterTxt = settings.wildCard ? '^' + sFilterTxt : sFilterTxt; sFilterTxt = settings.wildCard ? '^' + sFilterTxt : sFilterTxt;
filterPatt = settings.caseSensitive ? new RegExp(sFilterTxt) : new RegExp(sFilterTxt,"i"); filterPatt = settings.caseSensitive ? new RegExp(sFilterTxt) : new RegExp(sFilterTxt,"i");
// Get the results from the correct place. If settings.ajaxResults then results are retrieved from // Get the results from the correct place. If settings.ajaxResults then results are retrieved from
// an external function each time they are needed else they are retrieved from the data // an external function each time they are needed else they are retrieved from the data
// given on contruction. // given on contruction.
if (settings.ajaxResults === true) { if (settings.ajaxResults === true) {
resultObjects = searchData(this.value, settings.wildCard, resultObjects = searchData(this.value, settings.wildCard,
settings.caseSensitive, settings.caseSensitive,
settings.notCharacter); settings.notCharacter);
if (typeof resultObjects === 'string') { if (typeof resultObjects === 'string') {
resultObjects = JSON.parse(resultObjects); resultObjects = JSON.parse(resultObjects);
} }
} }
else { else {
// Look for the required match against each single search data item. When the not // Look for the required match against each single search data item. When the not
// character is used we are looking for a false match. // character is used we are looking for a false match.
for (i = 0; i < searchData.length; i += 1) { for (i = 0; i < searchData.length; i += 1) {
if (filterPatt.test(searchData[i].text) === bMatch) { if (filterPatt.test(searchData[i].text) === bMatch) {
resultObjects.push(searchData[i]); resultObjects.push(searchData[i]);
} }
} }
} }
buildResults(resultObjects, sFilterTxt); buildResults(resultObjects, sFilterTxt);
} }
// To call specific actions based on the keys pressed in the input // To call specific actions based on the keys pressed in the input
// box. Special keys are up, down and return. All other keys // box. Special keys are up, down and return. All other keys
// act as normal. // act as normal.
@ -244,7 +244,7 @@
switch (e.keyCode) { switch (e.keyCode) {
case 13: // return key case 13: // return key
$(currentSelection).trigger('click'); $(currentSelection).trigger('click');
return false; return false;
case 40: // down key case 40: // down key
if (typeof currentSelection === 'undefined') { if (typeof currentSelection === 'undefined') {
@ -253,12 +253,12 @@
else { else {
currentSelection = $(currentSelection).next().get(0); currentSelection = $(currentSelection).next().get(0);
} }
setHoverClass(currentSelection); setHoverClass(currentSelection);
if (currentSelection) { if (currentSelection) {
$(results).scrollTop(currentSelection.offsetTop); $(results).scrollTop(currentSelection.offsetTop);
} }
return false; return false;
case 38: // up key case 38: // up key
if (typeof currentSelection === 'undefined') { if (typeof currentSelection === 'undefined') {
@ -267,18 +267,18 @@
else { else {
currentSelection = $(currentSelection).prev().get(0); currentSelection = $(currentSelection).prev().get(0);
} }
setHoverClass(currentSelection); setHoverClass(currentSelection);
if (currentSelection) { if (currentSelection) {
$(results).scrollTop(currentSelection.offsetTop); $(results).scrollTop(currentSelection.offsetTop);
} }
return false; return false;
default: default:
runSuggest.apply(this, [e]); runSuggest.apply(this, [e]);
} }
} }
// Prepare the input box to show suggest results by adding in the events // Prepare the input box to show suggest results by adding in the events
// that will initiate the search and placing the element on the page // that will initiate the search and placing the element on the page
// that will show the results. // that will show the results.
@ -288,7 +288,7 @@
'left': obj.position().left + 'px', 'left': obj.position().left + 'px',
'width': settings.width || ((obj.width() + 5) + 'px') 'width': settings.width || ((obj.width() + 5) + 'px')
}).hide(); }).hide();
obj.after(results). obj.after(results).
keyup(keyListener). keyup(keyListener).
blur(function(e) { blur(function(e) {
@ -298,7 +298,7 @@
var resPos = $(results).offset(); var resPos = $(results).offset();
resPos.bottom = resPos.top + $(results).height(); resPos.bottom = resPos.top + $(results).height();
resPos.right = resPos.left + $(results).width(); resPos.right = resPos.left + $(results).width();
if (pageY < resPos.top || pageY > resPos.bottom || pageX < resPos.left || pageX > resPos.right) { if (pageY < resPos.top || pageY > resPos.bottom || pageX < resPos.left || pageX > resPos.right) {
$(results).hide(); $(results).hide();
} }
@ -308,7 +308,7 @@
'top': (obj.position().top + obj.height() + 5) + 'px', 'top': (obj.position().top + obj.height() + 5) + 'px',
'left': obj.position().left + 'px' 'left': obj.position().left + 'px'
}); });
if ($('div', results).length > 0) { if ($('div', results).length > 0) {
$(results).show(); $(results).show();
} }
@ -318,7 +318,7 @@
pageX = e.pageX; pageX = e.pageX;
pageY = e.pageY; pageY = e.pageY;
}); });
// Opera doesn't seem to assign a keyCode for the down // Opera doesn't seem to assign a keyCode for the down
// key on the keyup event. why? // key on the keyup event. why?
if ($.browser.opera) { if ($.browser.opera) {
@ -328,10 +328,10 @@
} }
}); });
} }
// Escape the not character if present so that it doesn't act in the regular expression // Escape the not character if present so that it doesn't act in the regular expression
settings.notCharacter = regexEscape(settings.notCharacter || ''); settings.notCharacter = regexEscape(settings.notCharacter || '');
// We need to get the javascript array type data from the searchData setting. // We need to get the javascript array type data from the searchData setting.
// Setting can either be a string, already an array or a function that returns one // Setting can either be a string, already an array or a function that returns one
// of those things. We only get this data if it isn't being provided using ajax on // of those things. We only get this data if it isn't being provided using ajax on