This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
108 lines (93 loc) · 3.36 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
(function () {
function sortArrayByObjectProperty(obj, property) {
return obj.sort(function (a, b) {
if (a[property].toLowerCase() < b[property].toLowerCase()) {
return -1;
}
if (a[property].toLowerCase() > b[property].toLowerCase()) {
return 1;
}
return 0;
});
}
function sortObjectByKeys(rows) {
return Object.keys(rows).sort().reduce(function (obj, key) {
obj[key] = rows[key];
return obj;
}, {});
}
function summarize(rows, property) {
var arr = {};
rows.forEach(function (e) {
var key = e[property]
if (!key || key === '?') {
key = 'Uncategorized';
}
if (!arr.hasOwnProperty(key)) {
arr[key] = 0;
}
arr[key]++;
})
return sortObjectByKeys(arr);
}
var app = new Vue({
el: '#app',
data: {
error: '',
keyword: '',
items: [],
categories: {},
results: [],
},
methods: {
search: function (keyword) {
if (!keyword) {
this.results = [];
return;
}
this.error = ''
var items = this.items.filter(function (item) {
return ("" + item.client_id).toLowerCase().indexOf(keyword.toLowerCase()) !== -1 || ("" + item.name).toLowerCase().indexOf(keyword.toLowerCase()) !== -1
})
this.results = items.map(function (item) {
var keys = ['client_id', 'name']
for (keyIndex in keys) {
var key = keys[keyIndex]
var value = "" + item[key]
var index = value.toLowerCase().indexOf(keyword.toLowerCase())
if (index !== -1) {
item[key] = value.substring(0, index)
item[key] += "<strong>" + value.substring(index, index + keyword.length) + "</strong>"
item[key] += value.substring(index + keyword.length)
}
}
return item
})
if (!this.results || !this.results.length) {
this.error = `item "<strong>${keyword}</strong>" not found.`
}
},
showCategory: function (category) {
this.keyword = '';
this.error = '';
this.results = this.items.filter(function (e) {
return e.type === category;
})
if (!this.results) {
this.error = `item ${keyword} not found.`
}
}
},
beforeCreate: function () {
this.error = '';
fetch('./data.json').then(function (response) {
return response.json()
}).then(function (response) {
this.items = Object.freeze(sortArrayByObjectProperty(response, 'name'))
this.categories = Object.freeze(summarize(response, 'type'))
}.bind(this)).catch(function (e) {
this.error = e.toString()
})
}
})
})()