suggest.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. function split_query(query) {
  2. query = query.split(' ');
  3. query = query.map(function (tag) {
  4. return tag.trim();
  5. });
  6. query = query.filter(Boolean);
  7. return query;
  8. }
  9. function complete(field, suggestions, tag) {
  10. var query = $(field).val();
  11. query = split_query(query);
  12. if (query.length == 0)
  13. return;
  14. query[query.length - 1] = tag + ' ';
  15. $(field).val(query.join(' '));
  16. $(suggestions).hide();
  17. }
  18. function suggest(suggestions, suggestion_content, field, query) {
  19. query = split_query(query);
  20. if (query.length == 0)
  21. return;
  22. query = query[query.length - 1];
  23. if (query.length < 2)
  24. return;
  25. if (query[query.length - 1] == ' ' || query[query.length - 1] == '-') {
  26. $(suggestions).hide();
  27. return;
  28. }
  29. api_unchecked('suggest', {tag: query}, function (data) {
  30. if (data.error !== undefined) {
  31. popup('danger', data.error);
  32. $(suggestions).hide();
  33. return;
  34. }
  35. $(suggestion_content).html('');
  36. for (var i = 0; i < data.tags_list.length; i++) {
  37. var html = '';
  38. html += '<a class="dropdown-item" onclick="complete(\'' + field + '\', \'' + suggestions + '\', \'' + data.tags_list[i] + '\');">';
  39. html += data.tags_list[i];
  40. html += '</a>';
  41. $(suggestion_content).append(html);
  42. }
  43. if (data.tags_list.length)
  44. $(suggestions).show();
  45. });
  46. }