Commit 3f4879aa authored by Gabriela Bittencourt's avatar Gabriela Bittencourt
Browse files

Make a prototype of autocomplete feature

The results are displayed in purr data console.
It's printed the first 10 results of the search.
parent 63e7e797
Pipeline #3706 canceled with stage
......@@ -521,6 +521,7 @@ var canvas_events = (function() {
if (evt.keyCode === 13) {
grow_svg_for_element(textbox());
}
pdgui.autocomplete(document.getElementById("new_object_textentry").className, textbox().innerText);
//evt.preventDefault();
return false;
},
......
......@@ -564,10 +564,18 @@ function obj_exact_match(title) {
return completion_index.search("=\"" + title + "\"", objs_completion_field);
}
function search_obj(title) {
return completion_index.search("\"" + title + "\"", objs_completion_field);
}
function arg_exact_match(title, arg) {
return completion_index.search({$and: [{"title": "=\"" + title + "\""}, {"args.text": "=\"" + arg + "\""}]});
}
function search_arg(title, arg) {
return completion_index.search({$and: [{"title": "=\"" + title + "\""}, {"args.text": "\"" + arg + "\""}]})[0].matches;
}
function index_obj_completion(obj_or_msg, obj_or_msg_text) {
var title, arg;
if (obj_or_msg === "msg") {
......@@ -614,8 +622,41 @@ function write_completion_index() {
}
}
function autocomplete(obj_class, text) {
if (text.length !== 0) {
let title, arg;
if (obj_class === "obj") {
let text_array = text.split(" ");
title = text_array[0].toString();
arg = text_array.slice(1, text_array.length);
arg = (arg.length !== 0) ? arg.toString().replace(/\,/g, " ") : "";
} else {
title = "msg";
arg = text;
}
let n = 10;
post(" -------- First " + n + " results -------- ");
let results = (arg.length > 0) ? (search_arg(title, arg).slice(1,)) : (search_obj(title));
if (results.length > n) results = results.slice(0,n);
if (results.length > 0) {
results.forEach(function (f,i,a) {
let suggestion;
if (arg.length < 1) { // autocomplete title
suggestion = f.item.title;
} else { // autocomplete argument
suggestion = ((obj_class==="obj")?(title+" "):"") + f.value;
}
post("- " + suggestion);
})
} else {
post("No suggestions found!");
}
}
}
exports.index_obj_completion = index_obj_completion;
exports.write_completion_index = write_completion_index;
exports.autocomplete = autocomplete;
// Modules
......
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