filter_searchTrigger

Code format, 
allow search event always,
allow names in object mode   like [ { keyCode:'tab', ctrl:false}], possible values see tskeyCodes
This commit is contained in:
CiTRO33 2018-05-10 17:21:22 +02:00 committed by GitHub
parent d28dd21673
commit abaff4bc7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@
$.extend( tskeyCodes, { $.extend( tskeyCodes, {
backSpace : 8, backSpace : 8,
tab : 9,
escape : 27, escape : 27,
space : 32, space : 32,
left : 37, left : 37,
@ -459,24 +460,24 @@
c.$table.triggerHandler( 'filterFomatterUpdate' ); c.$table.triggerHandler( 'filterFomatterUpdate' );
}, 100); }, 100);
} }
// check if search is included because enter trigger a search event
if(event.type === 'search'){ /* if ( event.type === 'search' ) {
var triggerSearch = false; var triggerSearch = false,
var searchTrigger = wo.filter_searchTrigger === false ? ['search', 'blur' , 13 ] : wo.filter_searchTrigger; searchTrigger = wo.filter_searchTrigger === false ? [ 'blur', tskeyCodes.enter ] : wo.filter_searchTrigger;
// skip search if not included // skip search if not included
for (var t in searchTrigger){ for ( var t in searchTrigger ) {
// events var stType = typeof searchTrigger[t],
var stType = typeof searchTrigger[t]; trigger = searchTrigger[t];
if(stType === "string" && event.type === searchTrigger[t]){ // only events required
if ( stType === "string" && event.type === trigger ) {
triggerSearch = true; triggerSearch = true;
break; break;
} }
} }
if(!triggerSearch){ if( !triggerSearch ) {
return; return;
} }
} } */
// pass true ( skipFirst ) to prevent the tablesorter.setFilters function from skipping the first // pass true ( skipFirst ) to prevent the tablesorter.setFilters function from skipping the first
// input ensures all inputs are updated when a search is triggered on the table // input ensures all inputs are updated when a search is triggered on the table
// $( 'table' ).trigger( 'search', [...] ); // $( 'table' ).trigger( 'search', [...] );
@ -856,7 +857,7 @@
var column = parseInt( $( this ).attr( 'data-column' ), 10 ), var column = parseInt( $( this ).attr( 'data-column' ), 10 ),
liveSearch = typeof wo.filter_liveSearch === 'boolean' ? wo.filter_liveSearch : liveSearch = typeof wo.filter_liveSearch === 'boolean' ? wo.filter_liveSearch :
ts.getColumnData( table, wo.filter_liveSearch, column ), ts.getColumnData( table, wo.filter_liveSearch, column ),
searchTrigger = wo.filter_searchTrigger ; searchTrigger = wo.filter_searchTrigger;
if ( typeof liveSearch === 'undefined' ) { if ( typeof liveSearch === 'undefined' ) {
liveSearch = wo.filter_liveSearch.fallback || false; liveSearch = wo.filter_liveSearch.fallback || false;
} }
@ -871,67 +872,44 @@
( typeof liveSearch === 'number' && this.value.length < liveSearch ) || ( typeof liveSearch === 'number' && this.value.length < liveSearch ) ||
// let return & backspace continue on, but ignore arrows & non-valid characters // let return & backspace continue on, but ignore arrows & non-valid characters
( event.which !== tskeyCodes.enter && event.which !== tskeyCodes.backSpace && ( event.which !== tskeyCodes.enter && event.which !== tskeyCodes.backSpace &&
( event.which < tskeyCodes.space || ( event.which >= tskeyCodes.left && event.which <= tskeyCodes.down ) ) ( event.which < tskeyCodes.space || ( event.which >= tskeyCodes.left && event.which <= tskeyCodes.down ) ) ) &&
) && searchTrigger === false ) ) { searchTrigger === false ) ) {
return; return;
// live search // live search
} else if ( liveSearch === false ) { } else if ( liveSearch === false ) {
if ( searchTrigger === false ) {
if (searchTrigger === false) {
if ( this.value !== '' && event.which !== tskeyCodes.enter ) { if ( this.value !== '' && event.which !== tskeyCodes.enter ) {
return; return;
} }
} else { } else {
var skipSearch = true; var skipSearch = true;
// only allow keyCode // on keyup event only keycodes needed to be checked
for ( var t in searchTrigger ){ for ( var t in searchTrigger ) {
// events var stType = typeof searchTrigger[t],
var stType = typeof searchTrigger[t]; trigger = searchTrigger[t];
if(stType === "number" && event.which === searchTrigger[t]){ // single keycode
// keycodes if ( stType === 'number' && event.which === trigger ) {
skipSearch = false; skipSearch = false;
break; break;
} else if(stType === "object" ){ } else if ( stType === 'object' ) {
if(searchTrigger[t].hasOwnProperty('keyCode') && event.which === searchTrigger[t].keyCode){ // keyCode property must be in the object
if ( typeof trigger.keyCode === 'number' && event.which === trigger.keyCode ||
tskeyCodes[ trigger.keyCode ] !== undefined &&
event.which === tskeyCodes[ trigger.keyCode ] ) {
// check modifier // check modifier
var ctrl = true, alt = true, shift = true; var ctrl = typeof trigger.ctrl === 'undefined' ? true : trigger.ctrl === event.ctrlKey,
if(searchTrigger[t].hasOwnProperty('ctrl')){ alt = typeof trigger.alt === 'undefined' ? true : trigger.alt === event.altKey,
if(searchTrigger[t].ctrl === event.ctrlKey ){ shift = typeof trigger.shift === 'undefined' ? true : trigger.shift === event.shiftKey;
ctrl = true; if( ctrl && alt && shift ){
}else{
ctrl = false;
}
}
if(searchTrigger[t].hasOwnProperty('alt')){
if(searchTrigger[t].alt === event.altKey ){
alt = true;
}else{
alt = false;
}
}
if(searchTrigger[t].hasOwnProperty('shift')){
if( searchTrigger[t].shift === event.shiftKey ){
shift = true;
}
else{
shift = false;
}
}
if(ctrl && alt && shift){
skipSearch = false; skipSearch = false;
} }
} }
} }
} }
if(skipSearch){ if( skipSearch ) {
return; return;
} }
} }
} }
// change event = no delay; last true flag tells getFilters to skip newest timed input // change event = no delay; last true flag tells getFilters to skip newest timed input
tsf.searching( table, true, true, column ); tsf.searching( table, true, true, column );
@ -944,35 +922,43 @@
liveSearch = typeof wo.filter_liveSearch === 'boolean' ? liveSearch = typeof wo.filter_liveSearch === 'boolean' ?
wo.filter_liveSearch : wo.filter_liveSearch :
ts.getColumnData( table, wo.filter_liveSearch, column ), ts.getColumnData( table, wo.filter_liveSearch, column ),
searchTrigger = wo.filter_searchTrigger === false ? ['search', 'blur' , 13 ] : wo.filter_searchTrigger, searchTrigger = wo.filter_searchTrigger === false ? ['blur' , tskeyCodes.enter ] : wo.filter_searchTrigger,
triggerSearch = false; triggerSearch = false;
if (table.config.widgetOptions.filter_initialized) { if ( table.config.widgetOptions.filter_initialized ) {
// Only if liveSearch is disabled // Only if liveSearch is disabled
if(liveSearch === false){ if ( liveSearch === false ) {
for (var t in searchTrigger){ if ( eventType === 'search' ) {
// events triggerSearch = true;
var stType = typeof searchTrigger[t]; } else {
if(stType === "string" && eventType === searchTrigger[t]){ for ( var t in searchTrigger ) {
triggerSearch = true; var stType = typeof searchTrigger[t],
break; trigger = searchTrigger[t];
} else if(stType === "number" && event.which === searchTrigger[t] ){ // events
// keycodes if ( stType === 'string' && eventType === trigger) {
triggerSearch = true;
break;
} else if( stType === "object" ){
if( searchTrigger[t].hasOwnProperty('keyCode') && event.which === searchTrigger[t].keyCode ){
// same keycode
triggerSearch = true; triggerSearch = true;
// check modifier break;
if( searchTrigger[t].hasOwnProperty('ctrl') && searchTrigger[t].ctrl !== event.ctrlKey ){ } else if ( stType === 'number' && event.which === trigger ) {
triggerSearch = false; // keys
} triggerSearch = true;
if( searchTrigger[t].hasOwnProperty('alt') && searchTrigger[t].alt !== event.altKey ){ break;
triggerSearch = false; } else if ( stType === 'object' ){
} // key combinations
if( searchTrigger[t].hasOwnProperty('shift') && searchTrigger[t].shift !== event.shiftKey ){ if ( typeof trigger.keyCode === 'number' && event.which === trigger.keyCode ||
triggerSearch = false; tskeyCodes[ trigger.keyCode ] !== undefined &&
event.which === tskeyCodes[ trigger.keyCode ] ) {
// same keycode
triggerSearch = true;
// check modifier
if ( typeof trigger.ctrl !== 'undefined' && trigger.ctrl !== event.ctrlKey ) {
triggerSearch = false;
}
if ( typeof trigger.alt !== 'undefined' && trigger.alt !== event.altKey ) {
triggerSearch = false;
}
if ( typeof trigger.shift !== 'undefined' && trigger.shift !== event.shiftKey ) {
triggerSearch = false;
}
} }
} }
} }
@ -984,10 +970,10 @@
// don't allow 'change' or 'input' event to process if the input value // don't allow 'change' or 'input' event to process if the input value
// is the same - fixes #685 // is the same - fixes #685
this.value !== c.lastSearch[column]){ this.value !== c.lastSearch[column]){
triggerSearch = true; triggerSearch = true;
} }
if(triggerSearch) { if ( triggerSearch ) {
event.preventDefault(); event.preventDefault();
// init search with no delay // init search with no delay
$( this ).attr( 'data-lastSearchTime', new Date().getTime() ); $( this ).attr( 'data-lastSearchTime', new Date().getTime() );