Commit 8f574152 authored by Alain Takoudjou's avatar Alain Takoudjou

Mynij Search: add query in search page url, show searched text body in result

parent 2f822e9b
......@@ -7,6 +7,13 @@
model_gadget : null,
result_gadget : null
})
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("getUrlParameter", "getUrlParameter")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod("redirect", "redirect")
.ready(function () {
var model_gadget,
result_gadget,
......@@ -24,33 +31,93 @@
model_gadget : model_gadget,
result_gadget : result
});
})
.push(function () {
setTimeout(function () {
return gadget.fill_available_indices();
}, 100);
});
})
.declareMethod("fill_available_indices", function () {
.onStateChange(function () {
})
.declareMethod("render", function (options) {
var gadget = this,
drop_list = document.getElementById("select_index");
return gadget.state.model_gadget.get_available_indices()
.push(function (results) {
results.forEach((item, i) => {
var option_item = document.createElement("option");
option_item.value = item;
option_item.appendChild(document.createTextNode(item));
drop_list.appendChild(option_item);
index_list,
drop_list = gadget.element.querySelector("#select_index");
return new RSVP.Queue()
.push(function () {
return RSVP.all([
gadget.getUrlFor({command: 'history_previous'})
]);
})
.push(function (url_list) {
return gadget.updateHeader({
page_title: "Mynij Search"
//selection_url: url_list[0]
});
})
.push(function () {
return gadget.state.model_gadget.get_available_indices();
})
.push(function (results) {
index_list = results;
while (drop_list.firstChild) {
drop_list.removeChild(drop_list.firstChild);
}
results.forEach((item, i) => {
var option_item = document.createElement("option");
option_item.value = item;
option_item.appendChild(document.createTextNode(item));
drop_list.appendChild(option_item);
});
})
.push(function () {
var limit = options.limit | 100,
index = options.index;
if (options.q) {
if (index_list.indexOf(index) === -1)
index = undefined;
else
drop_list.value = index;
gadget.element.querySelector("#search_input").value = options.q;
return gadget.defer_search(options.q, index, limit);
} else {
return gadget.clear();
}
});
})
});
.declareJob("clear", function () {
this.element.querySelector("#search_input").value = "";
return this.state.result_gadget.clear();
})
.declareMethod("search", function (key) {
.declareJob("defer_search", function (search_text, index, limit) {
var gadget = this,
top = document.getElementById("logo-search");
if(top) {
gadget.element.removeChild(top);
}
return gadget.state.result_gadget.startLoading()
.push(function () {
return gadget.state.result_gadget.showResultBox();
})
.push(function () {
return gadget.search(search_text, limit)
})
.push(function () {
return gadget.add_searx_results(search_text);
});
})
.declareMethod("search", function (key, limit, index) {
var gadget = this,
chosen_index;
chosen_index = document.getElementById("select_index").selectedOptions[0];
if (index) {
chosen_index = index;
} else {
chosen_index = document.getElementById("select_index").value;
}
if (chosen_index === null) {
return gadget.state.result_gadget.addItem(
{
......@@ -63,7 +130,7 @@
return gadget.state.result_gadget.clear()
.push(function () {
console.log("starting search");
return gadget.state.model_gadget.search(key, chosen_index.value);
return gadget.state.model_gadget.search(key, chosen_index, limit);
})
.push(function (result) {
console.log("search done");
......@@ -131,20 +198,12 @@
.onEvent("submit", function (event) {
var gadget = this,
top = document.getElementById("logo-search"),
search_key = document.getElementById("search_input").value;
if(top) {
gadget.element.removeChild(top);
}
return gadget.state.result_gadget.startLoading()
.push(function () {
return gadget.state.result_gadget.showResultBox();
})
.push(function () {
return gadget.search(search_key)
})
.push(function () {
return gadget.add_searx_results(search_key);
});
search_key = document.getElementById("search_input").value,
index = document.getElementById("select_index").value;
return gadget.redirect({"command": "display", "options": {
page: "mynij_search",
q: search_key,
index: index
}});
});
}(window, document, rJS, RSVP));
\ No newline at end of file
......@@ -148,8 +148,9 @@
results = [];
return gadget.get_index(index_name)
.push(function (index) {
var limit = 100;
console.log(index.info());
return index.search(search_key);
return index.search(search_key, limit);
})
.push(function (index_results) {
return index_results;
......
......@@ -59,16 +59,16 @@
loader.style.display = "none";
body = document.createElement('p');
body.className = "body";
item.body = new DOMParser().parseFromString(
item.body,
"text/html"
).body.textContent || "";
if (key === "" || item.body === "") {
//item.body = new DOMParser().parseFromString(
// item.body,
// "text/html"
// ).body.textContent || "";
if (key === "" || item.content === "") {
body.innerHTML = "";
list.appendChild(list_item);
} else {
//return gadget.cut_description(item.body, item.description, key)
return gadget.cut_description(item.body, key)
return gadget.cut_description(item.content, key)
.push(function (result) {
var array = [...result.matchAll(key)],
i;
......@@ -139,8 +139,14 @@
})
.declareMethod("cut_description", function (body, key) {
//function(body, description, key)
var result = new RegExp(
'[^.?!]*' + ' ' + key + ' ' + '[^.?!]*[.?!]', 'gm').exec(body);
var result,
key_list = key.split(' '),
search_key = key;
if (key_list.length > 0)
search_key = [key].concat(key_list).join("|");
result = new RegExp(
'[^;?.?!]*\s?(' + search_key + ')\s?[^.?!]*[.?!]', 'i').exec(body);
if (result === null) {
//if (description !== "") return description; else....
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment