Merge pull request #833 from pafcu/oneautocompleter

Fixes for the new autocompletion
This commit is contained in:
pafcu 2015-01-08 18:25:38 +02:00
commit 80c77b7f92
4 changed files with 62 additions and 35 deletions

View File

@ -293,9 +293,10 @@ function navbar_complete(&$a) {
$url = $directory['url'] . '/dirsearch'; $url = $directory['url'] . '/dirsearch';
} }
$count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
if($url) { if($url) {
$query = $url . '?f=' ; $query = $url . '?f=' ;
$query .= '&name=' . urlencode($search) . '&limit=50' . (($address) ? '&address=' . urlencode($search) : ''); $query .= '&name=' . urlencode($search) . "&limit=$count" . (($address) ? '&address=' . urlencode($search) : '');
$x = z_fetch_url($query); $x = z_fetch_url($query);
if($x['success']) { if($x['success']) {

View File

@ -1,8 +1,9 @@
<?php <?php
// Autocomplete for saved searches. Should probably be put in the same place as the other autocompletes
function search_ac_init(&$a){ function search_ac_init(&$a){
if(!local_user()) if(!local_user())
return ""; killme();
$start = (x($_REQUEST,'start')?$_REQUEST['start']:0); $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
@ -13,17 +14,6 @@ function search_ac_init(&$a){
$search = $_REQUEST['query']; $search = $_REQUEST['query'];
} }
$sql_extra = '';
$x = array();
$x['query'] = $search;
$x['photos'] = array();
$x['links'] = array();
$x['suggestions'] = array();
$x['data'] = array();
// Priority to people searches // Priority to people searches
if ($search) { if ($search) {
@ -32,18 +22,23 @@ function search_ac_init(&$a){
} }
$r = q("SELECT `abook_id`, `xchan_name`, `xchan_photo_s`, `xchan_url` FROM `abook` left join xchan on abook_xchan = xchan_hash WHERE abook_channel = %d $r = q("SELECT `abook_id`, `xchan_name`, `xchan_photo_s`, `xchan_url`, `xchan_addr` FROM `abook` left join xchan on abook_xchan = xchan_hash WHERE abook_channel = %d
$people_sql_extra $people_sql_extra
ORDER BY `xchan_name` ASC ", ORDER BY `xchan_name` ASC ",
intval(local_user()) intval(local_user())
); );
$results = array();
if($r) { if($r) {
foreach($r as $g) { foreach($r as $g) {
$x['photos'][] = $g['xchan_photo_s']; $results[] = array(
$x['links'][] = $g['xchan_url']; "photo" => $g['xchan_photo_s'],
$x['suggestions'][] = '@' . $g['xchan_name']; "name" => '@'.$g['xchan_name'],
$x['data'][] = 'cid=' . intval($g['abook_id']); "id" => $g['abook_id'],
"link" => $g['xchan_url'],
"label" => '',
"nick" => '',
);
} }
} }
@ -53,15 +48,24 @@ function search_ac_init(&$a){
if(count($r)) { if(count($r)) {
foreach($r as $g) { foreach($r as $g) {
$x['photos'][] = $a->get_baseurl() . '/images/hashtag.png'; $results[] = array(
$x['links'][] = $g['url']; "photo" => $a->get_baseurl() . '/images/hashtag.png',
$x['suggestions'][] = '#' . $g['term']; "name" => '#'.$g['term'],
$x['data'][] = intval($g['tid']); "id" => $g['tid'],
"link" => $g['url'],
"label" => '',
"nick" => '',
);
} }
} }
header("content-type: application/json"); header("content-type: application/json");
echo json_encode($x); $o = array(
'start' => $start,
'count' => $count,
'items' => $results,
);
echo json_encode($o);
logger('search_ac: ' . print_r($x,true)); logger('search_ac: ' . print_r($x,true));

View File

@ -5,11 +5,16 @@
*/ */
function contact_search(term, callback, backend_url, type, extra_channels) { function contact_search(term, callback, backend_url, type, extra_channels) {
// Check if there is a cached result that contains the same information we would get with a full server-side search // Check if there is a cached result that contains the same information we would get with a full server-side search
// Assume type hasn't changed
for(t in contact_search.cache) { var bt = backend_url+type;
if(term.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results if(!(bt in contact_search.cache)) contact_search.cache[bt] = {};
var lterm = term.toLowerCase(); // Ignore case
for(t in contact_search.cache[bt]) {
if(lterm.indexOf(t) >= 0) { // A more broad search has been performed already, so use those results
// Filter old results locally // Filter old results locally
var matching = contact_search.cache[t].filter(function (x) { return (x.name.indexOf(term) >= 0 || x.nick.indexOf(term) >= 0); }); var matching = contact_search.cache[bt][t].filter(function (x) { return (x.name.toLowerCase().indexOf(lterm) >= 0 || x.nick.toLowerCase().indexOf(lterm) >= 0); });
matching.unshift({taggable:false, text: term, replace: term});
callback(matching); callback(matching);
return; return;
} }
@ -33,20 +38,34 @@ function contact_search(term, callback, backend_url, type, extra_channels) {
success:function(data){ success:function(data){
// Cache results if we got them all (more information would not improve results) // Cache results if we got them all (more information would not improve results)
// data.count represents the maximum number of items // data.count represents the maximum number of items
if(data.items.length < data.count) { if(data.items.length -1 < data.count) {
contact_search.cache[term] = data.items; contact_search.cache[bt][lterm] = data.items;
} }
callback(data.items); var items = data.items.slice(0);
items.unshift({taggable:false, text: term, replace: term});
callback(items);
}, },
}).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong. }).fail(function () {callback([]); }); // Callback must be invoked even if something went wrong.
} }
contact_search.cache = {}; contact_search.cache = {};
function contact_format(item) { function contact_format(item) {
return "<div class='{0}' title='{4}'><img src='{1}'>{2} ({3})</div>".format(item.taggable, item.photo, item.name, ((item.label) ? item.nick + ' ' + item.label : item.nick), item.link ) // Show contact information if not explicitly told to show something else
if(typeof item.text === 'undefined') {
var desc = ((item.label) ? item.nick + ' ' + item.label : item.nick)
if(desc) desc = ' ('+desc+')';
return "<div class='{0}' title='{4}'><img src='{1}'>{2}{3}</div>".format(item.taggable, item.photo, item.name, desc, item.link)
}
else
return "<div>"+item.text+"</div>"
} }
function editor_replace(item) { function editor_replace(item) {
if(typeof item.replace !== 'undefined') {
return '$1$2'+item.replace;
}
// $2 ensures that prefix (@,@!) is preserved // $2 ensures that prefix (@,@!) is preserved
var id = item.id; var id = item.id;
// 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way. // 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
@ -57,6 +76,9 @@ function editor_replace(item) {
} }
function basic_replace(item) { function basic_replace(item) {
if(typeof item.replace !== 'undefined')
return '$1'+item.replace;
return '$1'+item.name+' '; return '$1'+item.name+' ';
} }
@ -88,7 +110,7 @@ function submit_form(e) {
replace: function(item) { return "$1"+item['text'] + ' '; }, replace: function(item) { return "$1"+item['text'] + ' '; },
} }
this.attr('autocomplete','off'); this.attr('autocomplete','off');
this.textcomplete([contacts,smilies],{className:'acpopup'}); this.textcomplete([contacts,smilies],{className:'acpopup',zIndex:1050});
}; };
})( jQuery ); })( jQuery );
@ -107,7 +129,7 @@ function submit_form(e) {
template: contact_format, template: contact_format,
} }
this.attr('autocomplete','off'); this.attr('autocomplete','off');
var a = this.textcomplete([contacts],{className:'acpopup',maxCount:100}); var a = this.textcomplete([contacts],{className:'acpopup',maxCount:100,zIndex: 1050});
a.on('textComplete:select', function(e,value,strategy) { submit_form(this); }); a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });
@ -130,7 +152,7 @@ function submit_form(e) {
} }
this.attr('autocomplete','off'); this.attr('autocomplete','off');
var a = this.textcomplete([contacts],{className:'acpopup'}); var a = this.textcomplete([contacts],{className:'acpopup',zIndex:1050});
if(autosubmit) if(autosubmit)
a.on('textComplete:select', function(e,value,strategy) { submit_form(this); }); a.on('textComplete:select', function(e,value,strategy) { submit_form(this); });

View File

@ -1,5 +1,5 @@
$(document).ready(function() { $(document).ready(function() {
$("#search-text").contact_autocomplete(baseurl + '/acl'); $("#search-text").contact_autocomplete(baseurl + '/search_ac');
$('.jslider-scale ins').addClass('hidden-xs'); $('.jslider-scale ins').addClass('hidden-xs');
}); });