-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (98 loc) · 3.11 KB
/
index.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
function getRandomColor() {
var letters = 'BCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * letters.length)];
}
return color;
}
function randomDivBg(){
console.log('from randomDivBg')
document.querySelectorAll('div').forEach(function(div){
console.log(div)
console.log(div.style)
console.log(div.textContent)
div.style.background = getRandomColor()
})
}
document.onchange = function(){
console.log('from document onchange')
}
async function preQuery() {
const client = new MeiliSearch({ host: 'http://127.0.0.1:7700' });
// client.getStats().then((response)=>{ console.log(response.indexes)})
client.getIndexes({ limit: 999 }).then((r)=>{
console.log(r)
console.log(r.results)
r.results.forEach( (v,k) => {
console.log(k,v)
console.log(k,v.uid)
// add buttons per index
let button = document.createElement('button')
button.textContent = v.uid
// button.id = v.uid+'_id'
button.onclick = query
console.log(button)
let button_container = document.getElementById('index_chooser')
button_container.append(button)
})
})
// let res = await client.getIndexes({ limit: 3 })
// console.log(res)
console.log('from preQuery')
}
function query2(){
let button_container = document.getElementById('index_chooser')
button_container.append('from query2')
console.log(this.innerHTML)
// console.log(this.getAttribute('id'))
button_container.append(this)
}
function query(index_Name) {
const search = instantsearch({
indexName: this.innerHTML,
searchClient: instantMeiliSearch(
"http://localhost:7700"
)
});
search.addWidgets([
instantsearch.widgets.searchBox({
container: "#searchbox",
}),
instantsearch.widgets.configure({ hitsPerPage: 8 }),
instantsearch.widgets.hits({
container: "#hits",
escapeHTML: true,
transformItems(items) {
console.log(items)
for(let i=0; i<items.length; i++){
// items[i]._highlightResult.content.value = items[i]._highlightResult.content.value.replace(/<em>/g, '<span style="background-color:yellow">').replace(/<\/em>/g, '</span>')
items[i]._highlightResult.content.value = items[i]._highlightResult.content.value.replace(/__LINEBREAK__/g, '<br>')
// items[i]._highlightResult.content.value = `<div style = "background-color:yellow">\
// ${items[i]._highlightResult.content.value}</div>`
}
return items
// return items.map(item => ({
// ...item,
// name: item.name.toUpperCase(),
// }));
},
templates: {
item: `
<div>
<div class="hit-name">
{{#helpers.highlight}}{ "attribute": "date" }{{/helpers.highlight}}
</div>
<div class="hit-name">
{{#helpers.highlight}}{ "attribute": "content" }{{/helpers.highlight}}
</div>
</div>
`
}
}),
instantsearch.widgets.pagination({
container: "#pagination"
})
]);
search.start();
}