-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
138 lines (131 loc) · 5.28 KB
/
script.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var wikiLink = "https://en.wikipedia.org/w/api.php?action=opensearch&limit=5&namespace=0&redirects=resolve&format=json"; //make limit as options (user can chose home many results to show)
var searchText = "&search=";
var link = '';
window.onload = function() {
//define all variables
var checkLive = document.getElementById("check");
checkLive.checked = false; //just to be on safe side checked set as false
var checked = false;
var search = document.getElementById("search");
var submit = document.getElementById("submit");
var liveResults = document.getElementById("livesearch");
var results = document.getElementById("results");
/*
all functions to be used in the code
*/
//event adder
function addListener(element, event, fn, capture) {
if (!capture) {capture=false}
element.addEventListener(event, fn, capture);
console.log("event added to " + element.id + "; Capture: " + capture);
};
//event remover
function removeListener(element, event, fn, capture) {
if (!capture) {capture=false}
element.removeEventListener(event, fn, capture);
console.log("event removed from " + element.id);
};
function hideShowElementBlock(element, type) {
element.style.display=type;
};
function clearValue(element) {
if (element.nodeType == 1) {
if (element.type) {
element.value="";
} else {
element.innerHTML="";
}
console.log("Cleared: " + element.id);
}
}
/*
the following is main computing
*/
function liveResultsData(event) {
var code = event.which||event.keyCode||event.charCode;
console.log(search.value.length);
if (search.value.length>1) {
clearValue(liveResults);
link = wikiLink + searchText + search.value;
console.log(link);
hideShowElementBlock(liveResults, "block");
getJSONP(link, function(json) {
var names = json[1];
var desc = json[2];
var links = json[3];
if (!names.length) {liveResults.innerHTML="<li>Not Found</li>";}
for (var i = 0; i < names.length; i++) {
var li, a, name;
li = document.createElement("LI");
a = document.createElement("A");
li.setAttribute("id", "item"+i+1);
a.setAttribute("href", links[i]);
a.setAttribute("target", "_blank")
name = document.createTextNode(names[i]);
a.appendChild(name);
li.appendChild(a);
liveResults.appendChild(li);
};
})
} else {
clearValue(liveResults);
hideShowElementBlock(liveResults, "none");
};
};
function populateResults() {
clearValue(results);hideShowElementBlock(liveResults, "none"); //just to be on the safe side
if (search.value.length) {
link = wikiLink + searchText + search.value;
getJSONP(link, function(json){
var names = json[1];
var desc = json[2];
var links = json[3];
if (!names.length) {results.innerHTML="<h3>Not Found</h3>";}
for (var i = 0; i < names.length; i++) {
var h, p, a, div;
div = document.createElement("DIV");
h = document.createElement("H3");
a = document.createElement("A");
p = document.createElement("P");
h.appendChild(document.createTextNode(names[i]));
p.appendChild(document.createTextNode(desc[i]));
a.setAttribute("href", links[i]);
a.setAttribute("target", "_blank");
div.setAttribute("id", "id"+i+1);
a.appendChild(h);
a.appendChild(p);
div.appendChild(a);
results.appendChild(div);
}
})
}
};
addListener(submit, "click", populateResults, false);
addListener(checkLive, "click", function() {
if (checkLive.checked) {
removeListener(submit, "click", populateResults, false);
hideShowElementBlock(submit, "none");
(function() { //just little experements with self-executive fn
clearValue(results);
clearValue(liveResults);
clearValue(search);
})();
addListener(search, "keyup", liveResultsData)
return false;
}
(function() {
clearValue(results);
clearValue(liveResults);
clearValue(search);
})();
addListener(submit, "click", populateResults, false);
hideShowElementBlock(submit, "block");
removeListener(search, "keyup", liveResultsData);
}, false);
//clear everything to be in the safe side
(function() {
clearValue(results);
clearValue(liveResults);
clearValue(search);
})();
}