diff --git a/src/definitions/modules/search.js b/src/definitions/modules/search.js index d27b2bd73..97d7cffd8 100644 --- a/src/definitions/modules/search.js +++ b/src/definitions/modules/search.js @@ -46,8 +46,6 @@ $.fn.search = function(parameters) { $module = $(this), $prompt = $module.find(selector.prompt), - $input = $module.find(selector.input), - $remove = $module.find(selector.remove), $searchButton = $module.find(selector.searchButton), $results = $module.find(selector.results), $result = $module.find(selector.result), @@ -107,11 +105,6 @@ $.fn.search = function(parameters) { .on('mouseup' + eventNamespace, selector.results, module.event.result.mouseup) .on('click' + eventNamespace, selector.result, module.event.result.click) ; - if(module.is.searchSelection()) { - $module - .on('click' + eventNamespace, selector.remove, module.event.remove.click) - ; - } } }, @@ -141,20 +134,29 @@ $.fn.search = function(parameters) { }, blur: function(event) { var - pageLostFocus = (document.activeElement === this) - ; - if(!pageLostFocus && !module.resultsClicked) { - if( settings.forceSelection && module.is.searchSelection() ) { - module.set.value( module.get.selectedValue() ); + pageLostFocus = (document.activeElement === this), + callback = function() { + module.cancel.query(); + module.remove.focus(); + module.timer = setTimeout(module.hideResults, settings.hideDelay); } - module.cancel.query(); - module.remove.focus(); - module.timer = setTimeout(module.hideResults, settings.hideDelay); + ; + if(pageLostFocus) { + return; } - }, - remove: { - click: function() { - module.clear.selectedValue(); + if(module.resultsClicked) { + module.debug('Determining if user action caused search to close'); + $module + .one('click', selector.results, function(event) { + if( !module.is.animating() && !module.is.hidden() ) { + callback(); + } + }) + ; + } + else { + module.debug('Input blurred without user action, closing results'); + callback(); } }, result: { @@ -189,7 +191,7 @@ $.fn.search = function(parameters) { } module.hideResults(); if(value) { - module.set.selectedValue(value); + module.set.value(value); } if(href) { module.verbose('Opening search link found in result', $link); @@ -224,9 +226,7 @@ $.fn.search = function(parameters) { // search shortcuts if(keyCode == keys.escape) { module.verbose('Escape key pressed, blurring search field'); - $prompt - .trigger('blur') - ; + module.trigger.blur(); } if( module.is.visible() ) { if(keyCode == keys.enter) { @@ -315,15 +315,15 @@ $.fn.search = function(parameters) { }, is: { - input : function() { - $module.is('input'); + animating: function() { + return $results.hasClass(className.animating); + }, + hidden: function() { + return $results.hasClass(className.hidden); }, empty: function() { return ($results.html() === ''); }, - searchSelection: function() { - return $module.hasClass(className.selection); - }, visible: function() { return ($results.filter(':visible').length > 0); }, @@ -332,6 +332,20 @@ $.fn.search = function(parameters) { } }, + trigger: { + blur: function() { + var + events = document.createEvent('HTMLEvents'), + promptElement = $prompt[0] + ; + if(promptElement) { + module.verbose('Triggering native blur event'); + events.initEvent('blur', false, false); + promptElement.dispatchEvent(events); + } + } + }, + get: { inputEvent: function() { var @@ -344,12 +358,6 @@ $.fn.search = function(parameters) { ; return inputEvent; }, - selectedValue: function() { - return ($input.length > 0) - ? $input.val() || '' - : $module.data(metadata.value) || '' - ; - }, value: function() { return $prompt.val(); }, @@ -377,7 +385,7 @@ $.fn.search = function(parameters) { $.each(results, function(index, category) { if($.isArray(category.results)) { result = module.search.object(value, category.results, lookupFields)[0]; - // dont continue searching if a result is found + // don't continue searching if a result is found if(result) { return false; } @@ -393,9 +401,6 @@ $.fn.search = function(parameters) { }, set: { - clearable: function() { - $remove.addClass(className.active); - }, focus: function() { $module.addClass(className.focus); }, @@ -408,21 +413,6 @@ $.fn.search = function(parameters) { .val(value) ; }, - selectedValue: function(value) { - if($input.length > 0) { - $input.val(value); - } - else { - $module.data(metadata.value, value); - } - if(value === '') { - module.remove.clearable(); - } - else { - module.set.clearable(); - } - module.set.value(value); - }, type: function(type) { type = type || settings.type; if(settings.type == 'category') { @@ -435,9 +425,6 @@ $.fn.search = function(parameters) { }, remove: { - clearable: function() { - $remove.removeClass(className.active); - }, loading: function() { $module.removeClass(className.loading); }, @@ -472,8 +459,8 @@ $.fn.search = function(parameters) { else { module.error(error.source); } - settings.onSearchQuery.call(element, searchTerm); } + settings.onSearchQuery.call(element, searchTerm); } else { module.hideResults(); @@ -648,19 +635,10 @@ $.fn.search = function(parameters) { numCharacters = searchTerm.length ; return (numCharacters >= settings.minCharacters); - }, - selectedValue: function() { - return module.get.selectedValue() - ? true - : false - ; } }, clear: { - selectedValue: function() { - module.set.selectedValue(''); - }, cache: function(value) { var cache = $module.data(metadata.cache) @@ -1120,9 +1098,6 @@ $.fn.search.settings = { searchFullText : true, // whether to include fuzzy results in local search - forceSelection : true, - // whether to remove unmatched input values from a selection search - automatic : true, // whether to add events to prompt automatically @@ -1154,13 +1129,14 @@ $.fn.search.settings = { onResultsClose : function(){}, className: { - active : 'active', - empty : 'empty', - focus : 'focus', - loading : 'loading', - results : 'results', - selection: 'selection', - pressed : 'down' + animating : 'animating', + active : 'active', + empty : 'empty', + focus : 'focus', + hidden : 'hidden', + loading : 'loading', + results : 'results', + pressed : 'down' }, error : { @@ -1177,8 +1153,7 @@ $.fn.search.settings = { metadata: { cache : 'cache', results : 'results', - result : 'result', - value : 'value' + result : 'result' }, regExp: { @@ -1196,19 +1171,18 @@ $.fn.search.settings = { price : 'price', // result price results : 'results', // array of results (standard) title : 'title', // result title + url : 'url', // result url action : 'action', // "view more" object name actionText : 'text', // "view more" text actionURL : 'url' // "view more" url }, selector : { - category : '.category', - input : '> input[type="hidden"]', prompt : '.prompt', - remove : '> .icon.input > .remove.icon', - result : '.result', - results : '.results', searchButton : '.search.button', + results : '.results', + category : '.category', + result : '.result', title : '.title, .name' }, @@ -1275,8 +1249,8 @@ $.fn.search.settings = { // each item inside category $.each(category.results, function(index, result) { - if(response[fields.url]) { - html += ''; + if(result[fields.url]) { + html += ''; } else { html += ''; @@ -1326,8 +1300,8 @@ $.fn.search.settings = { // each result $.each(response[fields.results], function(index, result) { - if(response[fields.url]) { - html += ''; + if(result[fields.url]) { + html += ''; } else { html += ''; @@ -1368,4 +1342,4 @@ $.fn.search.settings = { } }; -})( jQuery, window , document ); +})( jQuery, window, document );