2016-08-19 09:19:06 +02:00
|
|
|
$(document).ready(function() {
|
2018-01-10 17:07:21 +01:00
|
|
|
var HITS_PER_PAGE = 10;
|
2016-09-06 11:52:26 +02:00
|
|
|
var MAX_VALUES_PER_FACET = 30;
|
|
|
|
|
|
|
|
// DOM binding
|
|
|
|
var $inputField = $('#q');
|
|
|
|
var $hits = $('#hits');
|
|
|
|
var $stats = $('#stats');
|
|
|
|
var $facets = $('#facets');
|
|
|
|
var $pagination = $('#pagination');
|
2017-12-01 16:32:57 +01:00
|
|
|
var what = '';
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
// Templates binding
|
|
|
|
var hitTemplate = Hogan.compile($('#hit-template').text());
|
|
|
|
var statsTemplate = Hogan.compile($('#stats-template').text());
|
|
|
|
var facetTemplate = Hogan.compile($('#facet-template').text());
|
|
|
|
var sliderTemplate = Hogan.compile($('#slider-template').text());
|
|
|
|
var paginationTemplate = Hogan.compile($('#pagination-template').text());
|
|
|
|
|
2018-03-13 12:24:29 +01:00
|
|
|
// defined in tutti/4_search.js
|
2017-11-24 17:47:38 +01:00
|
|
|
var search = elasticSearcher;
|
2017-12-01 16:32:57 +01:00
|
|
|
|
2017-12-29 15:11:47 +01:00
|
|
|
// what are we looking for? users? assets (default)
|
2017-12-01 16:36:08 +01:00
|
|
|
what = $inputField.attr('what');
|
|
|
|
|
2018-03-13 12:24:29 +01:00
|
|
|
function do_search(query) {
|
2018-01-05 16:26:41 +01:00
|
|
|
if (query === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
toggleIconEmptyInput(!query.trim());
|
2017-12-29 15:11:47 +01:00
|
|
|
|
2018-03-13 12:24:29 +01:00
|
|
|
search.setQuery(query, what); // what could be like "/users"
|
|
|
|
var pid = ProjectUtils.projectId();
|
|
|
|
if (pid) search.setProjectID(pid);
|
2018-01-05 16:26:41 +01:00
|
|
|
search.execute();
|
2018-03-13 12:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Input binding
|
|
|
|
$inputField.on('keyup change', function() {
|
|
|
|
var query = $inputField.val();
|
|
|
|
do_search(query);
|
2016-09-06 11:52:26 +02:00
|
|
|
}).focus();
|
|
|
|
|
2018-01-05 16:26:41 +01:00
|
|
|
search.on('results', function(content) {
|
2016-09-06 11:52:26 +02:00
|
|
|
renderStats(content);
|
|
|
|
renderHits(content);
|
2017-11-24 17:47:38 +01:00
|
|
|
renderFacets(content);
|
2016-09-06 11:52:26 +02:00
|
|
|
renderPagination(content);
|
|
|
|
renderFirstHit($(hits).children('.search-hit:first'));
|
|
|
|
});
|
|
|
|
|
2017-11-24 17:47:38 +01:00
|
|
|
/***************
|
|
|
|
* SEARCH RENDERING
|
2016-09-06 11:52:26 +02:00
|
|
|
* ***********/
|
|
|
|
|
|
|
|
function renderFirstHit(firstHit) {
|
|
|
|
|
|
|
|
firstHit.addClass('active');
|
|
|
|
firstHit.find('#search-loading').addClass('active');
|
|
|
|
|
2016-09-06 11:56:54 +02:00
|
|
|
function done() {
|
|
|
|
$('.search-loading').removeClass('active');
|
|
|
|
$('#search-error').hide();
|
|
|
|
$('#search-hit-container').show();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setTimeout(function() {
|
|
|
|
// Ignore getting that first result when there is none.
|
|
|
|
var hit_id = firstHit.attr('data-hit-id');
|
|
|
|
if (hit_id === undefined) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.get('/nodes/' + hit_id + '/view', function(dataHtml) {
|
2016-09-06 11:52:26 +02:00
|
|
|
$('#search-hit-container').html(dataHtml);
|
|
|
|
})
|
2016-09-06 11:56:54 +02:00
|
|
|
.done(done)
|
2016-09-06 11:52:26 +02:00
|
|
|
.fail(function(data) {
|
|
|
|
$('.search-loading').removeClass('active');
|
|
|
|
$('#search-hit-container').hide();
|
|
|
|
$('#search-error').show().html('Houston!\n\n' + data.status + ' ' + data.statusText);
|
|
|
|
});
|
|
|
|
}, 1000);
|
2017-11-24 17:47:38 +01:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
// Initial search
|
|
|
|
initWithUrlParams();
|
|
|
|
|
2018-01-05 17:23:40 +01:00
|
|
|
function convertTimestamp(iso8601) {
|
|
|
|
var d = new Date(iso8601)
|
|
|
|
return d.toLocaleDateString();
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
function renderStats(content) {
|
|
|
|
var stats = {
|
2017-11-24 17:47:38 +01:00
|
|
|
nbHits: numberWithDelimiter(content.count),
|
|
|
|
processingTimeMS: content.took,
|
2016-09-06 11:52:26 +02:00
|
|
|
nbHits_plural: content.nbHits !== 1
|
|
|
|
};
|
|
|
|
$stats.html(statsTemplate.render(stats));
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderHits(content) {
|
|
|
|
var hitsHtml = '';
|
|
|
|
for (var i = 0; i < content.hits.length; ++i) {
|
2018-01-05 17:23:40 +01:00
|
|
|
var created = content.hits[i].created_at;
|
2016-09-06 11:52:26 +02:00
|
|
|
if (created) {
|
2018-01-05 17:23:40 +01:00
|
|
|
content.hits[i].created_at = convertTimestamp(created);
|
2016-09-06 11:52:26 +02:00
|
|
|
}
|
2018-01-05 17:23:40 +01:00
|
|
|
var updated = content.hits[i].updated_at;
|
2016-09-06 11:52:26 +02:00
|
|
|
if (updated) {
|
2018-01-05 17:23:40 +01:00
|
|
|
content.hits[i].updated_at = convertTimestamp(updated);
|
2016-09-06 11:52:26 +02:00
|
|
|
}
|
|
|
|
hitsHtml += hitTemplate.render(content.hits[i]);
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
if (content.hits.length === 0) hitsHtml = '<p id="no-hits">We didn\'t find any items. Try searching something else.</p>';
|
|
|
|
$hits.html(hitsHtml);
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
|
2017-11-24 17:47:38 +01:00
|
|
|
function renderFacets(content) {
|
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
// If no results
|
|
|
|
if (content.hits.length === 0) {
|
|
|
|
$facets.empty();
|
2018-01-05 16:26:41 +01:00
|
|
|
facets = [];
|
2016-09-06 11:52:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:26:41 +01:00
|
|
|
var storeValue = function(values, label) {
|
|
|
|
return function(item) {
|
2018-02-01 14:28:12 +01:00
|
|
|
var refined = search.isRefined(label, item.key);
|
2018-01-05 16:26:41 +01:00
|
|
|
values.push({
|
|
|
|
facet: label,
|
|
|
|
label: item.key,
|
|
|
|
value: item.key,
|
|
|
|
count: item.doc_count,
|
|
|
|
refined: refined,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
2017-12-01 16:32:57 +01:00
|
|
|
|
2018-01-05 16:26:41 +01:00
|
|
|
var facets = [];
|
|
|
|
var aggs = content.aggs;
|
|
|
|
for (var label in aggs) {
|
2018-02-01 14:28:12 +01:00
|
|
|
var values = [];
|
|
|
|
var buckets = aggs[label].buckets;
|
2017-12-01 16:32:57 +01:00
|
|
|
|
2018-01-05 16:26:41 +01:00
|
|
|
if (buckets.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-24 17:47:38 +01:00
|
|
|
|
2018-01-05 16:26:41 +01:00
|
|
|
buckets.forEach(storeValue(values, label));
|
|
|
|
facets.push({
|
|
|
|
title: label,
|
|
|
|
values: values.slice(0),
|
|
|
|
});
|
|
|
|
}
|
2017-11-24 17:47:38 +01:00
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
// Display facets
|
|
|
|
var facetsHtml = '';
|
|
|
|
for (var indexFacet = 0; indexFacet < facets.length; ++indexFacet) {
|
|
|
|
var facet = facets[indexFacet];
|
2017-12-29 15:11:47 +01:00
|
|
|
//title, values[facet, value]
|
2017-11-24 17:47:38 +01:00
|
|
|
facetsHtml += facetTemplate.render(facet);
|
2016-09-06 11:52:26 +02:00
|
|
|
}
|
2017-11-24 17:47:38 +01:00
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
$facets.html(facetsHtml);
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
function renderPagination(content) {
|
|
|
|
// If no results
|
2017-11-24 17:47:38 +01:00
|
|
|
if (content.count === 0) {
|
2016-09-06 11:52:26 +02:00
|
|
|
$pagination.empty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:07:21 +01:00
|
|
|
var maxPages = 3;
|
|
|
|
var nbPages = Math.floor(content.count / HITS_PER_PAGE);
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
// Process pagination
|
|
|
|
var pages = [];
|
|
|
|
if (content.page > maxPages) {
|
|
|
|
pages.push({
|
|
|
|
current: false,
|
2018-01-10 17:07:21 +01:00
|
|
|
number: 0,
|
|
|
|
shownr: 1
|
2016-09-06 11:52:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
for (var p = content.page - maxPages; p < content.page + maxPages; ++p) {
|
2018-01-10 17:07:21 +01:00
|
|
|
if (p < 0 || p > nbPages) {
|
2016-09-06 11:52:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pages.push({
|
|
|
|
current: content.page === p,
|
2018-01-10 17:07:21 +01:00
|
|
|
number: p,
|
|
|
|
shownr: p+1
|
2016-09-06 11:52:26 +02:00
|
|
|
});
|
|
|
|
}
|
2017-11-24 17:47:38 +01:00
|
|
|
if (content.page + maxPages < nbPages) {
|
2016-09-06 11:52:26 +02:00
|
|
|
pages.push({
|
|
|
|
current: false,
|
2018-01-10 17:07:21 +01:00
|
|
|
number: nbPages-1,
|
|
|
|
shownr: nbPages
|
2016-09-06 11:52:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
var pagination = {
|
|
|
|
pages: pages,
|
|
|
|
};
|
2018-01-10 17:07:21 +01:00
|
|
|
if (content.page > 0) {
|
|
|
|
pagination.prev_page = {page: content.page - 1};
|
|
|
|
}
|
|
|
|
if (content.page < nbPages) {
|
|
|
|
pagination.next_page = {page: content.page + 1};
|
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
// Display pagination
|
|
|
|
$pagination.html(paginationTemplate.render(pagination));
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
// Event bindings
|
|
|
|
// Click binding
|
|
|
|
$(document).on('click', '.show-more, .show-less', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$(this).closest('ul').find('.show-more').toggle();
|
|
|
|
$(this).closest('ul').find('.show-less').toggle();
|
|
|
|
return false;
|
|
|
|
});
|
2017-11-24 17:47:38 +01:00
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
$(document).on('click', '.toggleRefine', function() {
|
2017-12-01 16:32:57 +01:00
|
|
|
search.toggleTerm($(this).data('facet'), $(this).data('value'));
|
2018-01-05 16:26:41 +01:00
|
|
|
search.execute();
|
2016-09-06 11:52:26 +02:00
|
|
|
return false;
|
|
|
|
});
|
2017-11-24 17:47:38 +01:00
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
$(document).on('click', '.gotoPage', function() {
|
2018-01-10 17:07:21 +01:00
|
|
|
const page_idx = $(this).data('page');
|
|
|
|
search.setCurrentPage(page_idx);
|
|
|
|
search.execute();
|
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
$("html, body").animate({
|
|
|
|
scrollTop: 0
|
|
|
|
}, '500', 'swing');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$(document).on('click', '.sortBy', function() {
|
|
|
|
$(this).closest('.btn-group').find('.sort-by').text($(this).text());
|
2017-11-24 17:47:38 +01:00
|
|
|
//helper.setIndex(INDEX_NAME + $(this).data('index-suffix')).search();
|
2016-09-06 11:52:26 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$(document).on('click', '#input-loop', function() {
|
|
|
|
$inputField.val('').change();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Dynamic styles
|
|
|
|
$('#facets').on("mouseenter mouseleave", ".button-checkbox", function(e) {
|
|
|
|
$(this).parent().find('.facet_link').toggleClass("hover");
|
|
|
|
});
|
2017-12-01 16:32:57 +01:00
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
$('#facets').on("mouseenter mouseleave", ".facet_link", function(e) {
|
|
|
|
$(this).parent().find('.button-checkbox button.btn').toggleClass("hover");
|
|
|
|
});
|
|
|
|
|
|
|
|
/************
|
|
|
|
* HELPERS
|
|
|
|
* ***********/
|
|
|
|
|
|
|
|
function toggleIconEmptyInput(isEmpty) {
|
|
|
|
if (isEmpty) {
|
|
|
|
$('#input-loop').addClass('glyphicon-loop');
|
|
|
|
$('#input-loop').removeClass('glyphicon-remove');
|
|
|
|
} else {
|
|
|
|
$('#input-loop').removeClass('glyphicon-loop');
|
|
|
|
$('#input-loop').addClass('glyphicon-remove');
|
|
|
|
}
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
function numberWithDelimiter(number, delimiter) {
|
|
|
|
number = number + '';
|
|
|
|
delimiter = delimiter || ',';
|
|
|
|
var split = number.split('.');
|
|
|
|
split[0] = split[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter);
|
|
|
|
return split.join('.');
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
var sortByCountDesc = function sortByCountDesc(a, b) {
|
|
|
|
return b.count - a.count;
|
2016-08-19 09:19:06 +02:00
|
|
|
};
|
2016-09-06 11:52:26 +02:00
|
|
|
var sortByName = function sortByName(a, b) {
|
|
|
|
return a.value.localeCompare(b.value);
|
2016-08-19 09:19:06 +02:00
|
|
|
};
|
2016-09-06 11:52:26 +02:00
|
|
|
var sortByRefined = function sortByRefined(sortFunction) {
|
|
|
|
return function(a, b) {
|
|
|
|
if (a.refined !== b.refined) {
|
|
|
|
if (a.refined) return -1;
|
|
|
|
if (b.refined) return 1;
|
|
|
|
}
|
|
|
|
return sortFunction(a, b);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function initWithUrlParams() {
|
|
|
|
var sPageURL = location.hash;
|
|
|
|
if (!sPageURL || sPageURL.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var sURLVariables = sPageURL.split('&');
|
|
|
|
if (!sURLVariables || sURLVariables.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var query = decodeURIComponent(sURLVariables[0].split('=')[1]);
|
|
|
|
$inputField.val(query);
|
2017-12-01 16:32:57 +01:00
|
|
|
search.setQuery(query, what);
|
2017-11-24 17:47:38 +01:00
|
|
|
|
2016-09-06 11:52:26 +02:00
|
|
|
for (var i = 2; i < sURLVariables.length; i++) {
|
|
|
|
var sParameterName = sURLVariables[i].split('=');
|
|
|
|
var facet = decodeURIComponent(sParameterName[0]);
|
|
|
|
var value = decodeURIComponent(sParameterName[1]);
|
|
|
|
}
|
|
|
|
// Page has to be set in the end to avoid being overwritten
|
|
|
|
var page = decodeURIComponent(sURLVariables[1].split('=')[1]) - 1;
|
2017-11-24 17:47:38 +01:00
|
|
|
search.setCurrentPage(page);
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
2016-09-06 11:52:26 +02:00
|
|
|
|
|
|
|
function setURLParams(state) {
|
2017-11-24 17:47:38 +01:00
|
|
|
var urlParams = '?';
|
2016-09-06 11:52:26 +02:00
|
|
|
var currentQuery = state.query;
|
|
|
|
urlParams += 'q=' + encodeURIComponent(currentQuery);
|
|
|
|
var currentPage = state.page + 1;
|
|
|
|
urlParams += '&page=' + currentPage;
|
|
|
|
location.replace(urlParams);
|
2016-08-19 09:19:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:32:57 +01:00
|
|
|
// do empty search to fill aggregations
|
2018-03-13 12:24:29 +01:00
|
|
|
do_search('');
|
2016-08-19 09:19:06 +02:00
|
|
|
});
|