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,15 +460,16 @@
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;
} }
@ -475,8 +477,7 @@
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', [...] );
@ -871,53 +872,34 @@
( 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;
}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 ){ if( ctrl && alt && shift ){
skipSearch = false; skipSearch = false;
} }
@ -927,11 +909,7 @@
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,39 +922,47 @@
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 ) {
if ( eventType === 'search' ) {
triggerSearch = true;
} else {
for ( var t in searchTrigger ) { for ( var t in searchTrigger ) {
var stType = typeof searchTrigger[t],
trigger = searchTrigger[t];
// events // events
var stType = typeof searchTrigger[t]; if ( stType === 'string' && eventType === trigger) {
if(stType === "string" && eventType === searchTrigger[t]){
triggerSearch = true; triggerSearch = true;
break; break;
} else if(stType === "number" && event.which === searchTrigger[t] ){ } else if ( stType === 'number' && event.which === trigger ) {
// keycodes // keys
triggerSearch = true; triggerSearch = true;
break; break;
} else if( stType === "object" ){ } else if ( stType === 'object' ){
if( searchTrigger[t].hasOwnProperty('keyCode') && event.which === searchTrigger[t].keyCode ){ // key combinations
if ( typeof trigger.keyCode === 'number' && event.which === trigger.keyCode ||
tskeyCodes[ trigger.keyCode ] !== undefined &&
event.which === tskeyCodes[ trigger.keyCode ] ) {
// same keycode // same keycode
triggerSearch = true; triggerSearch = true;
// check modifier // check modifier
if( searchTrigger[t].hasOwnProperty('ctrl') && searchTrigger[t].ctrl !== event.ctrlKey ){ if ( typeof trigger.ctrl !== 'undefined' && trigger.ctrl !== event.ctrlKey ) {
triggerSearch = false; triggerSearch = false;
} }
if( searchTrigger[t].hasOwnProperty('alt') && searchTrigger[t].alt !== event.altKey ){ if ( typeof trigger.alt !== 'undefined' && trigger.alt !== event.altKey ) {
triggerSearch = false; triggerSearch = false;
} }
if( searchTrigger[t].hasOwnProperty('shift') && searchTrigger[t].shift !== event.shiftKey ){ if ( typeof trigger.shift !== 'undefined' && trigger.shift !== event.shiftKey ) {
triggerSearch = false; triggerSearch = false;
} }
} }
} }
} }
}
// change & input events must be ignored if liveSearch !== true // change & input events must be ignored if liveSearch !== true
} else if ( ( eventType === 'change' || eventType === 'input' ) && } else if ( ( eventType === 'change' || eventType === 'input' ) &&
// prevent search if liveSearch is a number // prevent search if liveSearch is a number