Browse Source

Abstraction abstraction abstraction #1634

pull/1698/merge
jlukic 9 years ago
parent
commit
f8fa373482
1 changed files with 72 additions and 34 deletions
  1. 106
      src/definitions/modules/search.js

106
src/definitions/modules/search.js

@ -32,6 +32,7 @@ $.fn.search = function(parameters) {
settings = $.extend(true, {}, $.fn.search.settings, parameters), settings = $.extend(true, {}, $.fn.search.settings, parameters),
className = settings.className, className = settings.className,
regExp = settings.regExp,
selector = settings.selector, selector = settings.selector,
error = settings.error, error = settings.error,
namespace = settings.namespace, namespace = settings.namespace,
@ -137,30 +138,33 @@ $.fn.search = function(parameters) {
module.debug('Search result selected'); module.debug('Search result selected');
var var
$result = $(this), $result = $(this),
$title = $result.find('.title'),
title = $title.html()
$title = $result.find(selector.title).eq(0),
$link = $result.find('a[href]').eq(0),
href = $link.attr('href') || false,
target = $link.attr('target') || false,
name = ($title.length > 0)
? $title.text()
: false,
title = $title.html(),
returnedValue
; ;
if(settings.onSelect == 'default' || settings.onSelect.call(this, event) == 'default') {
var
$link = $result.find('a[href]').eq(0),
$title = $result.find(selector.title).eq(0),
href = $link.attr('href') || false,
target = $link.attr('target') || false,
name = ($title.length > 0)
? $title.text()
: false
;
module.hideResults();
if(name) {
$prompt.val(name);
if( $.isFunction(settings.onSelect) ) {
if(settings.onSelect.call(element) === false) {
module.debug('onSelect Callback cancelled default actions');
return;
} }
if(href) {
if(target == '_blank' || event.ctrlKey) {
window.open(href);
}
else {
window.location.href = (href);
}
}
module.hideResults();
if(name) {
module.set.value(name);
}
if(href) {
module.verbose('Opening search link found in result', $link);
if(target == '_blank' || event.ctrlKey) {
window.open(href);
}
else {
window.location.href = (href);
} }
} }
} }
@ -192,7 +196,7 @@ $.fn.search = function(parameters) {
; ;
} }
// result shortcuts // result shortcuts
if($results.filter(':visible').length > 0) {
if( module.is.visible() ) {
if(keyCode == keys.enter) { if(keyCode == keys.enter) {
module.verbose('Enter key pressed, selecting active result'); module.verbose('Enter key pressed, selecting active result');
if( $result.filter('.' + activeClass).length > 0 ) { if( $result.filter('.' + activeClass).length > 0 ) {
@ -267,10 +271,36 @@ $.fn.search = function(parameters) {
}, },
searchHTML searchHTML
; ;
module.verbose('First request, initializing API');
$module.api(apiSettings); $module.api(apiSettings);
} }
}, },
can: {
transition: function() {
return settings.transition && $.fn.transition !== undefined && $module.transition('is supported');
}
},
is: {
empty: function() {
return ($results.html() === '');
},
visible: function() {
return ($results.filter(':visible').length > 0);
},
focused: function() {
return ($prompt.filter(':focus').length > 0);
}
},
set: {
value: function(value) {
module.verbose('Setting search input value', value);
$prompt.val(value);
}
},
query: function() { query: function() {
var var
searchTerm = $prompt.val(), searchTerm = $prompt.val(),
@ -305,12 +335,12 @@ $.fn.search = function(parameters) {
search: { search: {
local: function(searchTerm) { local: function(searchTerm) {
var var
results = [],
results = [],
fullTextResults = [], fullTextResults = [],
searchFields = $.isArray(settings.searchFields) searchFields = $.isArray(settings.searchFields)
? settings.searchFields ? settings.searchFields
: [settings.searchFields], : [settings.searchFields],
searchRegExp = new RegExp('(?:\s|^)' + searchTerm, 'i'),
searchRegExp = new RegExp(regExp.exact + searchTerm, 'i'),
fullTextRegExp = new RegExp(searchTerm, 'i'), fullTextRegExp = new RegExp(searchTerm, 'i'),
searchHTML searchHTML
; ;
@ -444,17 +474,21 @@ $.fn.search = function(parameters) {
}, },
addResults: function(html) { addResults: function(html) {
if(settings.onResultsAdd == 'default' || settings.onResultsAdd.call($results, html) == 'default') {
$results
.html(html)
;
if( $.isFunction(settings.onResultsAdd) ) {
if( settings.onResultsAdd.call($results, html) === false ) {
module.debug('onResultsAdd callback cancelled default action');
return false;
}
} }
$results
.html(html)
;
module.showResults(); module.showResults();
}, },
showResults: function() { showResults: function() {
if( ($results.filter(':visible').length === 0) && ($prompt.filter(':focus').length > 0) && $results.html() !== '') {
if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported') && !$results.transition('is inward')) {
if( !module.is.visible() && module.is.focused() && !module.is.empty() ) {
if( module.can.transition() ) {
module.debug('Showing results with css animations'); module.debug('Showing results with css animations');
$results $results
.transition({ .transition({
@ -475,8 +509,8 @@ $.fn.search = function(parameters) {
} }
}, },
hideResults: function() { hideResults: function() {
if($results.filter(':visible').length > 0) {
if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported') && !$results.transition('is outward')) {
if( module.is.visible() ) {
if( module.can.transition() ) {
module.debug('Hiding results with css animations'); module.debug('Hiding results with css animations');
$results $results
.transition({ .transition({
@ -490,7 +524,7 @@ $.fn.search = function(parameters) {
module.debug('Hiding results with javascript'); module.debug('Hiding results with javascript');
$results $results
.stop() .stop()
.fadeIn(settings.duration, settings.easing)
.fadeOut(settings.duration, settings.easing)
; ;
} }
settings.onResultsClose.call($results); settings.onResultsClose.call($results);
@ -767,6 +801,10 @@ $.fn.search.settings = {
method : 'The method you called is not defined.' method : 'The method you called is not defined.'
}, },
regExp: {
exact: '(?:\s|^)'
},
selector : { selector : {
prompt : '.prompt', prompt : '.prompt',
searchButton : '.search.button', searchButton : '.search.button',

Loading…
Cancel
Save