-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient-side.js
80 lines (63 loc) · 2.22 KB
/
client-side.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
var feedback = document.getElementById('feedback');
var searchform = document.getElementById('searchform');
var results = document.getElementById('results'),
searchform.addEventListener('submit', function (e) {
e.preventDefault();
results.innerHTML = '';
var f = 'query=' + document.getElementById('search-box').value;
return submitForm(f);
});
function submitForm (formdata) {
var xhr = new XMLHttpRequest();
xhr.open(searchform.method, searchform.action, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
var data = JSON.parse(xhr.responseText);
var d = data.length;
if (d === 0) {
feedback.textContent = 'Sorry, no matches found.';
feedback.style.color = 'crimson';
}
while (d--) {
addMatchingPost(data[d].ref);
}
};
xhr.onerror = function () {
feedback.innerHTML = '<span class="error">Sorry, there was a technical error. Please try again later.</span>';
};
xhr.send(formdata);
}
function addMatchingPost (idno) {
var apicall = new XMLHttpRequest();
apicall.open('GET', ghost.url.api('posts'), true);
apicall.onload = function () {
var results = document.getElementById('results');
var posts = JSON.parse(apicall.responseText).posts;
var j = posts.length;
var htmlstring;
var len;
var info;
while (j--) {
if (posts[j].id === idno) {
htmlstring += (
'<a class="search-result" href="' + posts[j].url + '">' +
'<h3>' + posts[j].title + '</h3>' +
'<p>' + posts[j].meta_description + '</p>' +
'<time datetime="' + (new Date(posts[j].published_at)) + '">' +
(new Date(posts[j].published_at)).toLocaleDateString() +
'</time>' +
'</a>'
);
}
}
results.insertAdjacentHTML('beforebegin', htmlstring);
len = document.querySelectorAll('.search-result').length;
info = len === 1 ? ' result found' : ' results found';
feedback.textContent = len + info;
feedback.style.color = 'green';
};
apicall.onerror = function () {
feedback.innerHTML = '<span class="error">Sorry, there was a technical error. Please try again later.</span>';
};
apicall.send();
}