This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete.coffee
74 lines (67 loc) · 2.37 KB
/
autocomplete.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
CORS_PROXY = "http://127.0.0.1:3001/"
#DEVELOPMENT
window.availableTags = []
window.taglist = ->
arr = []
availableTags.forEach (item)->
arr.push [item.label, item.desc, item.value]
return arr
window.queries = {}
window.lastQuery = ""
window.lastQueried = ""
window.lang = ""
$ ->
$("#tags").autocomplete(
source: availableTags
# focus: (event, ui) ->
# $("#item").val ui.item.label
# false
select: (event, ui) ->
# console.log ui.item
# console.log ui.item.label
$("#item").val ui.item.label
$("#item-id").val ui.item.id
$("#item-description").html ui.item.desc
$("#item-icon").attr "src", "images/" + ui.item.icon
false
).data("ui-autocomplete")._renderItem = (ul, item) ->
$("<li>").append("<a>" + item.label + "<br>" + item.desc + "</a>").appendTo ul
$('#tags').on 'keyup', ->
window.lastQuery = $('#tags').val()
unless window.lastQuery.length < 2 or queries[window.lastQuery]? or /^Q[0-9]*$/.test(window.lastQuery) or window.timeout != null
lang = $('#language').val()
getJSONWikidataSearchResults window.lastQuery, lang
queries[window.lastQuery] = {}
timeoutSetter()
window.lastQueried = window.lastQuery
window.timeout = null
timeoutSetter = ->
window.timeout = "not null"
f = ->
console.log new Date()
window.timeout = null
if window.lastQueried != window.lastQuery
window.lastQueried = window.lastQuery
console.log "QUERY SAVER!"
console.log window.lastQuery
getJSONWikidataSearchResults window.lastQuery, lang
timeoutSetter()
# console.log "NULLED!"
# console.log window.lastQueried
# console.log window.lastQuery
# console.log window.lastQueried != window.lastQuery
setTimeout(f, 500)
wikidataSearch = (query, language = "en", format="json")->
return "https://www.wikidata.org/w/api.php?action=wbsearchentities&language=#{language}&format=#{format}&search=#{query}"
getJSONWikidataSearchResults = (query, language)->
$.getJSON CORS_PROXY + wikidataSearch(query, language, "json") , (data)->
if data.search?
data.search.forEach (result)->
if result.label?
formatedResult =
value: result.id
label: result.label
desc: result.description
# icon: ""
availableTags.push(formatedResult)
queries[query][result.id] = result