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 @@ ...@@ -7,6 +7,13 @@
model_gadget : null, model_gadget : null,
result_gadget : null result_gadget : null
}) })
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("getUrlParameter", "getUrlParameter")
.declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod("redirect", "redirect")
.ready(function () { .ready(function () {
var model_gadget, var model_gadget,
result_gadget, result_gadget,
...@@ -24,33 +31,93 @@ ...@@ -24,33 +31,93 @@
model_gadget : model_gadget, model_gadget : model_gadget,
result_gadget : result 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, var gadget = this,
drop_list = document.getElementById("select_index"); index_list,
return gadget.state.model_gadget.get_available_indices() drop_list = gadget.element.querySelector("#select_index");
.push(function (results) {
results.forEach((item, i) => { return new RSVP.Queue()
var option_item = document.createElement("option"); .push(function () {
option_item.value = item; return RSVP.all([
option_item.appendChild(document.createTextNode(item)); gadget.getUrlFor({command: 'history_previous'})
drop_list.appendChild(option_item); ]);
})
.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, var gadget = this,
chosen_index; 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) { if (chosen_index === null) {
return gadget.state.result_gadget.addItem( return gadget.state.result_gadget.addItem(
{ {
...@@ -63,7 +130,7 @@ ...@@ -63,7 +130,7 @@
return gadget.state.result_gadget.clear() return gadget.state.result_gadget.clear()
.push(function () { .push(function () {
console.log("starting search"); 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) { .push(function (result) {
console.log("search done"); console.log("search done");
...@@ -131,20 +198,12 @@ ...@@ -131,20 +198,12 @@
.onEvent("submit", function (event) { .onEvent("submit", function (event) {
var gadget = this, var gadget = this,
top = document.getElementById("logo-search"), search_key = document.getElementById("search_input").value,
search_key = document.getElementById("search_input").value; index = document.getElementById("select_index").value;
if(top) { return gadget.redirect({"command": "display", "options": {
gadget.element.removeChild(top); page: "mynij_search",
} q: search_key,
return gadget.state.result_gadget.startLoading() index: index
.push(function () { }});
return gadget.state.result_gadget.showResultBox();
})
.push(function () {
return gadget.search(search_key)
})
.push(function () {
return gadget.add_searx_results(search_key);
});
}); });
}(window, document, rJS, RSVP)); }(window, document, rJS, RSVP));
\ No newline at end of file
...@@ -148,8 +148,9 @@ ...@@ -148,8 +148,9 @@
results = []; results = [];
return gadget.get_index(index_name) return gadget.get_index(index_name)
.push(function (index) { .push(function (index) {
var limit = 100;
console.log(index.info()); console.log(index.info());
return index.search(search_key); return index.search(search_key, limit);
}) })
.push(function (index_results) { .push(function (index_results) {
return index_results; return index_results;
......
...@@ -59,16 +59,16 @@ ...@@ -59,16 +59,16 @@
loader.style.display = "none"; loader.style.display = "none";
body = document.createElement('p'); body = document.createElement('p');
body.className = "body"; body.className = "body";
item.body = new DOMParser().parseFromString( //item.body = new DOMParser().parseFromString(
item.body, // item.body,
"text/html" // "text/html"
).body.textContent || ""; // ).body.textContent || "";
if (key === "" || item.body === "") { if (key === "" || item.content === "") {
body.innerHTML = ""; body.innerHTML = "";
list.appendChild(list_item); list.appendChild(list_item);
} else { } else {
//return gadget.cut_description(item.body, item.description, key) //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) { .push(function (result) {
var array = [...result.matchAll(key)], var array = [...result.matchAll(key)],
i; i;
...@@ -139,8 +139,14 @@ ...@@ -139,8 +139,14 @@
}) })
.declareMethod("cut_description", function (body, key) { .declareMethod("cut_description", function (body, key) {
//function(body, description, key) //function(body, description, key)
var result = new RegExp( var result,
'[^.?!]*' + ' ' + key + ' ' + '[^.?!]*[.?!]', 'gm').exec(body); 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 (result === null) {
//if (description !== "") return description; else.... //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