Skip to content

Commit

Permalink
Fixes issue where search wont close when clicked on message #3039
Browse files Browse the repository at this point in the history
  • Loading branch information
jlukic committed Nov 30, 2015
1 parent d34b18b commit 6d34ae2
Showing 1 changed file with 62 additions and 88 deletions.
150 changes: 62 additions & 88 deletions src/definitions/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
;
}
}
},

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
},
Expand All @@ -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
Expand All @@ -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();
},
Expand Down Expand Up @@ -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;
}
Expand All @@ -393,9 +401,6 @@ $.fn.search = function(parameters) {
},

set: {
clearable: function() {
$remove.addClass(className.active);
},
focus: function() {
$module.addClass(className.focus);
},
Expand All @@ -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') {
Expand All @@ -435,9 +425,6 @@ $.fn.search = function(parameters) {
},

remove: {
clearable: function() {
$remove.removeClass(className.active);
},
loading: function() {
$module.removeClass(className.loading);
},
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 : {
Expand All @@ -1177,8 +1153,7 @@ $.fn.search.settings = {
metadata: {
cache : 'cache',
results : 'results',
result : 'result',
value : 'value'
result : 'result'
},

regExp: {
Expand All @@ -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'
},

Expand Down Expand Up @@ -1275,8 +1249,8 @@ $.fn.search.settings = {

// each item inside category
$.each(category.results, function(index, result) {
if(response[fields.url]) {
html += '<a class="result" href="' + response[fields.url] + '">';
if(result[fields.url]) {
html += '<a class="result" href="' + result[fields.url] + '">';
}
else {
html += '<a class="result">';
Expand Down Expand Up @@ -1326,8 +1300,8 @@ $.fn.search.settings = {

// each result
$.each(response[fields.results], function(index, result) {
if(response[fields.url]) {
html += '<a class="result" href="' + response[fields.url] + '">';
if(result[fields.url]) {
html += '<a class="result" href="' + result[fields.url] + '">';
}
else {
html += '<a class="result">';
Expand Down Expand Up @@ -1368,4 +1342,4 @@ $.fn.search.settings = {
}
};

})( jQuery, window , document );
})( jQuery, window, document );

0 comments on commit 6d34ae2

Please sign in to comment.