function split_query(query) { query = query.split(' '); query = query.map(function (tag) { return tag.trim(); }); query = query.filter(Boolean); return query; } function complete(field, suggestions, tag) { var query = $(field).val(); query = split_query(query); if (query.length == 0) return; query[query.length - 1] = tag + ' '; $(field).val(query.join(' ')); $(suggestions).hide(); } function suggest(suggestions, suggestion_content, field, query) { query = split_query(query); if (query.length == 0) return; query = query[query.length - 1]; if (query.length < 2) return; if (query[query.length - 1] == ' ' || query[query.length - 1] == '-') { $(suggestions).hide(); return; } api_unchecked('suggest', {tag: query}, function (data) { if (data.error !== undefined) { popup('danger', data.error); $(suggestions).hide(); return; } $(suggestion_content).html(''); for (var i = 0; i < data.tags_list.length; i++) { var html = ''; html += ''; html += data.tags_list[i]; html += ''; $(suggestion_content).append(html); } if (data.tags_list.length) $(suggestions).show(); }); }