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.js
100 lines (87 loc) · 2.86 KB
/
autocomplete.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Generated by CoffeeScript 1.7.1
(function() {
var CORS_PROXY, getJSONWikidataSearchResults, timeoutSetter, wikidataSearch;
CORS_PROXY = "http://127.0.0.1:3001/";
window.availableTags = [];
window.taglist = function() {
var arr;
arr = [];
availableTags.forEach(function(item) {
return arr.push([item.label, item.desc, item.value]);
});
return arr;
};
window.queries = {};
window.lastQuery = "";
window.lastQueried = "";
window.lang = "";
$(function() {
$("#tags").autocomplete({
source: availableTags,
select: function(event, ui) {
$("#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);
return false;
}
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return $("<li>").append("<a>" + item.label + "<br>" + item.desc + "</a>").appendTo(ul);
};
return $('#tags').on('keyup', function() {
var lang;
window.lastQuery = $('#tags').val();
if (!(window.lastQuery.length < 2 || (queries[window.lastQuery] != null) || /^Q[0-9]*$/.test(window.lastQuery) || window.timeout !== null)) {
lang = $('#language').val();
getJSONWikidataSearchResults(window.lastQuery, lang);
queries[window.lastQuery] = {};
timeoutSetter();
return window.lastQueried = window.lastQuery;
}
});
});
window.timeout = null;
timeoutSetter = function() {
var f;
window.timeout = "not null";
f = function() {
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);
return timeoutSetter();
}
};
return setTimeout(f, 500);
};
wikidataSearch = function(query, language, format) {
if (language == null) {
language = "en";
}
if (format == null) {
format = "json";
}
return "https://www.wikidata.org/w/api.php?action=wbsearchentities&language=" + language + "&format=" + format + "&search=" + query;
};
getJSONWikidataSearchResults = function(query, language) {
return $.getJSON(CORS_PROXY + wikidataSearch(query, language, "json"), function(data) {
if (data.search != null) {
return data.search.forEach(function(result) {
var formatedResult;
if (result.label != null) {
formatedResult = {
value: result.id,
label: result.label,
desc: result.description
};
availableTags.push(formatedResult);
return queries[query][result.id] = result;
}
});
}
});
};
}).call(this);