-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (47 loc) · 1.72 KB
/
script.js
File metadata and controls
55 lines (47 loc) · 1.72 KB
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
$(document).ready(function () {
marvel_movie_list = null;
var promise = new Promise(function (resolve, reject) {
$.getJSON('marvel_movie.json', function (json) {
marvel_movie_list = json['items'];
marvel_movie_list.sort(function (a, b) { return new Date(a['release_date']) - new Date(b['release_date']) })
resolve('Movie List')
});
});
function fuzzyMatch(text, search) {
search = search.replace(/\ /g, '').toLowerCase();
var tokens = text.split('');
var searchPosition = 0;
$.each(tokens, function (i, textChar) {
if (textChar.toLowerCase() == search[searchPosition]) {
tokens[i] = '<strong style="color:red">' + textChar + '</strong>';
searchPosition++;
if (searchPosition >= search.length) {
return false;
}
}
});
if (searchPosition != search.length)
return '';
return tokens.join('');
}
function refreshSearch() {
var search = $("#searchVal").val();
var results = []
var id = 1;
$.each(marvel_movie_list, function (i, movie_list) {
var result = fuzzyMatch(movie_list['title'], search);
if (result) {
results.push('<tr><td>' + id + '</td><td>' + result + '</td><td>' + movie_list['release_date'] + '</td></tr>');
id++;
}
});
var resultHTML = results.join('\n');
$("#movieList tbody").html(resultHTML);
}
$(function () {
promise.then(function (successMsg) {
refreshSearch();
$("#searchVal").keyup(refreshSearch);
})
})
});