mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 13:24:27 +01:00
Introduce a basic front to display facets
This commit is contained in:
parent
2cd8675734
commit
a8e3269ad6
@ -1,5 +1,6 @@
|
|||||||
var request = null;
|
var request = null;
|
||||||
var timeoutID = null;
|
var timeoutID = null;
|
||||||
|
var selected_facets = {};
|
||||||
|
|
||||||
$('#query, #facet').on('input', function () {
|
$('#query, #facet').on('input', function () {
|
||||||
var query = $('#query').val();
|
var query = $('#query').val();
|
||||||
@ -20,7 +21,7 @@ $('#query, #facet').on('input', function () {
|
|||||||
}),
|
}),
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
success: function (data, textStatus, request) {
|
success: function (data, textStatus, request) {
|
||||||
results.innerHTML = '';
|
documents.innerHTML = '';
|
||||||
facets.innerHTML = '';
|
facets.innerHTML = '';
|
||||||
|
|
||||||
let timeSpent = request.getResponseHeader('Time-Ms');
|
let timeSpent = request.getResponseHeader('Time-Ms');
|
||||||
@ -30,16 +31,29 @@ $('#query, #facet').on('input', function () {
|
|||||||
time.classList.remove('fade-in-out');
|
time.classList.remove('fade-in-out');
|
||||||
|
|
||||||
for (facet_name in data.facets) {
|
for (facet_name in data.facets) {
|
||||||
|
// Append an header to the list of facets
|
||||||
|
let upperCaseName = facet_name.charAt(0).toUpperCase() + facet_name.slice(1);
|
||||||
|
$("<h3></h3>").text(upperCaseName).appendTo($('#facets'));
|
||||||
|
|
||||||
|
// Create a div for a bulma select
|
||||||
|
const header = document.createElement('div');
|
||||||
|
let div = $("<div></div>").addClass('select is-multiple');
|
||||||
|
|
||||||
|
// Create the select element
|
||||||
|
let select = $(`<select data-facet-name='${facet_name}' multiple size=\"8\"></select>`);
|
||||||
for (value of data.facets[facet_name]) {
|
for (value of data.facets[facet_name]) {
|
||||||
const elem = document.createElement('span');
|
let option = $('<option></option>')
|
||||||
elem.classList.add("tag");
|
.text(value)
|
||||||
elem.setAttribute('data-name', facet_name);
|
.attr('value', value)
|
||||||
elem.setAttribute('data-value', value);
|
.attr('title', value);
|
||||||
elem.innerHTML = `${facet_name}:${value}`;
|
select.append(option);
|
||||||
facets.appendChild(elem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.append(select);
|
||||||
|
$('#facets').append(div);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (element of data.documents) {
|
for (element of data.documents) {
|
||||||
const elem = document.createElement('li');
|
const elem = document.createElement('li');
|
||||||
elem.classList.add("document");
|
elem.classList.add("document");
|
||||||
@ -65,22 +79,16 @@ $('#query, #facet').on('input', function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
elem.appendChild(ol);
|
elem.appendChild(ol);
|
||||||
results.appendChild(elem);
|
documents.appendChild(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When we click on a tag we append the facet value
|
// When we click on a facet value we change the global values
|
||||||
// at the end of the facet query.
|
// to make sure that we don't loose the selection between requests.
|
||||||
$('#facets .tag').on('click', function () {
|
$('#facets select').on('change', function(e) {
|
||||||
let name = $(this).attr("data-name");
|
let facet_name = $(this).attr('data-facet-name');
|
||||||
let value = $(this).attr("data-value");
|
selected_facets[facet_name] = $(this).val();
|
||||||
|
|
||||||
let facet_query = $('#facet').val().trim();
|
|
||||||
if (facet_query === "") {
|
|
||||||
$('#facet').val(`${name} = "${value}"`).trigger('input');
|
|
||||||
} else {
|
|
||||||
$('#facet').val(`${facet_query} AND ${name} = "${value}"`).trigger('input');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
},
|
},
|
||||||
beforeSend: function () {
|
beforeSend: function () {
|
||||||
if (request !== null) {
|
if (request !== null) {
|
||||||
|
@ -1,24 +1,5 @@
|
|||||||
#results {
|
#results {
|
||||||
max-width: 900px;
|
|
||||||
margin: 20px auto 0 auto;
|
margin: 20px auto 0 auto;
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#facets .tag {
|
|
||||||
margin-right: 1em;
|
|
||||||
margin-bottom: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
#facets {
|
|
||||||
max-width: 900px;
|
|
||||||
margin: 20px auto 0 auto;
|
|
||||||
padding: 0;
|
|
||||||
max-height: 16em;
|
|
||||||
overflow: scroll;
|
|
||||||
}
|
|
||||||
|
|
||||||
#facets .tag:hover {
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#logo-white {
|
#logo-white {
|
||||||
@ -80,14 +61,6 @@
|
|||||||
opacity: 0.7;
|
opacity: 0.7;
|
||||||
}
|
}
|
||||||
|
|
||||||
.content {
|
|
||||||
max-width: 65%;
|
|
||||||
flex: 0 0 65%;
|
|
||||||
box-sizing: border-box;
|
|
||||||
padding-left: 10px;
|
|
||||||
color: rgba(0,0,0,.9);
|
|
||||||
}
|
|
||||||
|
|
||||||
.content mark {
|
.content mark {
|
||||||
background-color: hsl(204, 86%, 88%);
|
background-color: hsl(204, 86%, 88%);
|
||||||
color: hsl(204, 86%, 25%);
|
color: hsl(204, 86%, 25%);
|
||||||
|
@ -84,14 +84,20 @@
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section id="facets">
|
<section id="results" class="container">
|
||||||
<!-- facet values -->
|
<div class="columns">
|
||||||
</section>
|
<div class="column is-one-fifth">
|
||||||
|
<div id="facets" class="content">
|
||||||
|
<!-- facets matching requests -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<section>
|
<div class="column is-four-fifths">
|
||||||
<ol id="results" class="content">
|
<ol id="documents" class="content">
|
||||||
<!-- documents matching requests -->
|
<!-- documents matching requests -->
|
||||||
</ol>
|
</ol>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
Reference in New Issue
Block a user