Kill old search code that never got fully UX'ified.
parent
437fbab959
commit
2b8022cd66
|
@ -294,188 +294,5 @@ $(document).ready(function () {
|
|||
win.on('resize', apply_stickynav);
|
||||
apply_stickynav();
|
||||
}
|
||||
|
||||
|
||||
/// Search
|
||||
/// Here be dragons, this is beta quality code. Beware.
|
||||
|
||||
if (build.is_rtd_theme()) {
|
||||
searchLanding();
|
||||
}
|
||||
|
||||
$(document).on({
|
||||
mouseenter: function(ev) {
|
||||
var tooltip = $(ev.target).next();
|
||||
tooltip.show();
|
||||
},
|
||||
mouseleave: function(ev) {
|
||||
var tooltip = $(ev.target).next();
|
||||
tooltip.hide();
|
||||
}
|
||||
}, '.result-count');
|
||||
|
||||
$(document).on('submit', '#rtd-search-form', function (ev) {
|
||||
//ev.preventDefault();
|
||||
clearSearch();
|
||||
var query = $("#rtd-search-form input[name='q']").val();
|
||||
getSearch(query, true);
|
||||
});
|
||||
|
||||
$(document).on('click', '.search-result', function (ev) {
|
||||
ev.preventDefault();
|
||||
//console.log(ev.target)
|
||||
html = $(ev.target).next().html();
|
||||
displayContent(html);
|
||||
});
|
||||
|
||||
function searchLanding() {
|
||||
// Highlight based on highlight GET arg
|
||||
var params = $.getQueryParameters();
|
||||
var query = (params.q) ? params.q[0].split(/\s+/) : [];
|
||||
var clear = true;
|
||||
/* Don't "search" on highlight phrases
|
||||
if (!query.length) {
|
||||
// Only clear on q
|
||||
clear = false
|
||||
var query = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
|
||||
}
|
||||
*/
|
||||
if (query.length) {
|
||||
query = query.join(" ");
|
||||
console.log("Searching based on GET arg for: " + query);
|
||||
$("#rtd-search-form input[name='q']").val(query);
|
||||
getSearch(query, clear);
|
||||
}
|
||||
}
|
||||
|
||||
function getSearch(query, clear) {
|
||||
var get_data = {
|
||||
project: READTHEDOCS_DATA['project'],
|
||||
version: READTHEDOCS_DATA['version'],
|
||||
format: "jsonp",
|
||||
q: query
|
||||
};
|
||||
|
||||
// Search results
|
||||
$.ajax({
|
||||
url: API_HOST + "/api/v2/search/section/",
|
||||
crossDomain: true,
|
||||
xhrFields: {
|
||||
withCredentials: true,
|
||||
},
|
||||
dataType: "jsonp",
|
||||
data: get_data,
|
||||
success: function (data) {
|
||||
clearSearch(clear);
|
||||
hits = data.results.hits.hits;
|
||||
if (!hits.length) {
|
||||
resetState();
|
||||
} else {
|
||||
displaySearch(hits, query);
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
console.log('Error searching');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function displayContent(html) {
|
||||
var content = $('.rst-content');
|
||||
content.html(html);
|
||||
}
|
||||
|
||||
function displaySearch(hits, query) {
|
||||
FIRSTRUN = {};
|
||||
current = $(".toctree-l1.current > a");
|
||||
for (var index in hits) {
|
||||
var hit = hits[index];
|
||||
var path = hit.fields.path;
|
||||
var pageId = hit.fields.page_id;
|
||||
var title = hit.fields.title;
|
||||
var content = hit.fields.content;
|
||||
var highlight = hit.highlight.content;
|
||||
var score = hit._score;
|
||||
|
||||
var li = $(".toctree-l1 > a[href^='" + path + "']");
|
||||
|
||||
/*
|
||||
// This doesn't work :)
|
||||
if (!li.length && $(current.next().children()[0]).text() == title) {
|
||||
li = current
|
||||
console.log("Current page: " + title)
|
||||
} else {
|
||||
console.log("Not: " + title)
|
||||
}
|
||||
*/
|
||||
|
||||
var ul = li.next();
|
||||
|
||||
console.log(path);
|
||||
|
||||
// Display content for first result
|
||||
if (index === 0) {
|
||||
// Don't display content for now, so we show sphinx results
|
||||
//displayContent(content)
|
||||
}
|
||||
|
||||
// Clear out subheading with result content
|
||||
if (!FIRSTRUN[path]) {
|
||||
li.show();
|
||||
li.attr("href", li.attr('href') + "?highlight=" + query);
|
||||
li.parent().addClass("current");
|
||||
li.append("<i style='position:absolute;right:30px;top:6px;' class='fa fa-search result-icon'></i>");
|
||||
ul.empty();
|
||||
FIRSTRUN[path] = true;
|
||||
}
|
||||
|
||||
// Dedupe
|
||||
if (!FIRSTRUN[path+title]) {
|
||||
ul.append('<li class="toctree-l2">' + '<a class="reference internal search-result" pageId="' + pageId + '">' + title + '</a>' + '<span style="display: none;" class="data">' + content + '</span>' + '</li>');
|
||||
if (score > 1) {
|
||||
$(".toctree-l2 ");
|
||||
inserted = $('.toctree-l2 > [pageId="' + pageId + '"]');
|
||||
inserted.append("<i style='position:absolute;right:30px;top:6px;' class='fa fa-fire'></i>");
|
||||
}
|
||||
FIRSTRUN[path+title] = true;
|
||||
}
|
||||
}
|
||||
// Hide non-showing bits
|
||||
$.each($(".toctree-l1 > a"), function (index, el) {
|
||||
hide = true;
|
||||
if ($(el).attr('href') === "") {
|
||||
// Current page
|
||||
hide = false;
|
||||
}
|
||||
for (var key in FIRSTRUN) {
|
||||
if ($(el).attr('href').indexOf(key) === 0) {
|
||||
hide = false;
|
||||
}
|
||||
}
|
||||
if (hide) {
|
||||
$(el).hide();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function resetState() {
|
||||
$.each($(".toctree-l1 > a"), function (index, el) {
|
||||
var el = $(el);
|
||||
el.show();
|
||||
el.parent().show();
|
||||
});
|
||||
|
||||
}
|
||||
function clearSearch(empty) {
|
||||
$('.result-icon').remove();
|
||||
$.each($(".toctree-l1 > a"), function (index, el) {
|
||||
var el = $(el);
|
||||
if (empty) {
|
||||
el.parent().removeClass('current');
|
||||
el.next().empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue