| 1 |
jQuery( document ).ready( function( $ ) { |
| 2 |
var fep_delay = ( function() { |
| 3 |
var timer = 0; |
| 4 |
return function( callback, ms ) { |
| 5 |
clearTimeout ( timer ); |
| 6 |
timer = setTimeout( callback, ms ); |
| 7 |
}; |
| 8 |
})(); |
| 9 |
|
| 10 |
$( document ).on( 'keyup', '#fep-message-top', function() { |
| 11 |
fep_delay( function() { |
| 12 |
$( '#fep-result' ).hide(); |
| 13 |
var display_name = $( '#fep-message-top' ).val(); |
| 14 |
var data = { |
| 15 |
q: display_name |
| 16 |
}; |
| 17 |
if ( ! display_name ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
$( '#fep-message-top' ).addClass( 'fep-loading-gif' ); |
| 21 |
$.ajax({ |
| 22 |
url: fep_script.root +'/users/autosuggestion/', |
| 23 |
method: 'get', |
| 24 |
data: data, |
| 25 |
dataType: 'json', |
| 26 |
beforeSend: function ( xhr ) { |
| 27 |
xhr.setRequestHeader( 'X-WP-Nonce', fep_script.nonce ); |
| 28 |
} |
| 29 |
}).done( function( response ) { |
| 30 |
$( '#fep-result' ).html('<ul></ul>'); |
| 31 |
if ( $.isEmptyObject( response ) ) { |
| 32 |
$( '#fep-result ul' ).append('<li>' + fep_script.no_match + '</li>'); |
| 33 |
} else { |
| 34 |
$.each( response.slice(0,5), function(i, v) { |
| 35 |
$( '#fep-result ul' ).append('<li><a href="#" data-user_nicename="' + v.nicename + '" data-user_name="' + v.name + '">' + v.name + '</a></li>'); |
| 36 |
}); |
| 37 |
} |
| 38 |
$( '#fep-result' ).show(); |
| 39 |
}).always( function() { |
| 40 |
$( '#fep-message-top' ).removeClass( 'fep-loading-gif' ); |
| 41 |
}); |
| 42 |
}, 300 ); |
| 43 |
}); |
| 44 |
|
| 45 |
$( document ).on( 'click', '#fep-result a', function(e) { |
| 46 |
e.preventDefault(); |
| 47 |
$( '#fep-message-to' ).val( $(this).data('user_nicename') ); |
| 48 |
$( '#fep-message-top' ).val( $(this).data('user_name') ); |
| 49 |
$( '#fep-result' ).hide(); |
| 50 |
}); |
| 51 |
}); |
| 52 |
|