-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwide.js
67 lines (58 loc) · 1.97 KB
/
wide.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
/*
* Wide 0.9
* https://github.com/gmasson/widejs/
* License MIT
*/
/* Capturar elementos através do ID */
function wId(id) {
return document.getElementById(id);
}
/* Captura os elementos através do ID */
var wideContent = wId(wideResultArea);
var wideResultTemplate = wId(wideResultTemplate);
var wideSearchInput = wId(wideSearchInput);
/* Função para mostrar os itens na tela */
function wShowItems(search = '') {
/*
Filtra os itens de acordo com o texto digitado na busca por titulo e descricao
OBS: aqui você pode incluir mais critérios de busca
*/
const wFilteredData = database.filter(item =>
item.title.toLowerCase().includes(search)
|| item.descr.toLowerCase().includes(search)
|| item.tags.toLowerCase().includes(search)
);
wFilteredData.forEach(function (dataItem) {
/* Clonando conteudo do template (não funciona no querido Internet Explorer) */
var item = document.importNode(wideResultTemplate.content, true);
/* Aqui indicamos os dados e onde serão inseridos */
item.getElementById(wideResultImg).setAttribute("src", dataItem.img);
item.getElementById(wideResultImg).setAttribute("alt", dataItem.img_alt);
item.getElementById(wideResultTitle).textContent = dataItem.title;
item.getElementById(wideResultDescr).textContent = dataItem.descr;
item.getElementById(wideResultLink).setAttribute("href", dataItem.link_url);
item.getElementById(wideResultLink).textContent = dataItem.link_name;
wideContent.appendChild(item);
});
}
/* Limpa os itens para fazer uma nova busca */
function wClearItems() {
wideContent.innerHTML = '';
}
/* Com o parametro 'all', é exibido todos cadastros, mesmo que o campo esteja vazio */
/* Busca itens quando o input for modificado */
wideSearchInput.addEventListener('input', function(e) {
const value = e.target.value.toLowerCase();
if (value == '') {
wClearItems();
if (wideShowAll == 'all') {
wShowItems();
}
} else {
wClearItems();
wShowItems(value);
}
});
if (wideShowAll == 'all') {
wShowItems();
}