diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java index d1b9656..d672bf6 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java @@ -8,6 +8,7 @@ import java.io.PrintWriter; import java.util.Map; import java.util.TreeMap; +import java.util.stream.Collectors; public class IndexParser extends Compiler { protected int idx = -1; @@ -61,7 +62,8 @@ public void putFeaturePhrase(String featureId, String rawPhrase){ public void buildIndex(PrintWriter out) throws IOException { out.print("let indexPhrases = "); - mapper.writeValue(out, phrases); + mapper.writeValue(out, phrases.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); out.println(";"); out.print("let indexMap = "); diff --git a/core/src/main/resources/htmlTemplate/dist/feature-pasta.min.js b/core/src/main/resources/htmlTemplate/dist/feature-pasta.min.js index 28220c5..7ee5e99 100644 --- a/core/src/main/resources/htmlTemplate/dist/feature-pasta.min.js +++ b/core/src/main/resources/htmlTemplate/dist/feature-pasta.min.js @@ -562,22 +562,22 @@ $("#btn-diff").click(function(){ }); // TYPE HEAD -var substringMatcher = function(strs) { +var substringMatcher = function(indexPhrases, indexMap) { return function findMatches(q, cb) { var matches = []; var substrRegex = new RegExp(q, 'i'); - $.each(strs, function (featureId, indiceItem) { - $.each(indiceItem.values, function (i, txt) { - if (substrRegex.test(txt)) { + for(let i in indexMap){ + for(let j in indexMap[i].phrases){ + if(substrRegex.test(indexPhrases[indexMap[i].phrases[j]])){ matches.push({ - "url": featureId, - "name": indiceItem.name, - "txt": txt + "url": indexPhrases[i], + "name": indexPhrases[indexMap[i].title], + "txt": indexPhrases[indexMap[i].phrases[j]] }); } - }); - }); + } + } cb(matches); }; @@ -590,7 +590,7 @@ $('#busca').typeahead({ displayKey: 'txt', name: 'txt', display: 'txt', - source: substringMatcher(indice), + source: substringMatcher(indexPhrases, indexMap), templates: { suggestion: Handlebars.compile("

{{name}}

{{txt}}
") } diff --git a/core/src/main/resources/htmlTemplate/js/template-feature-pasta.js b/core/src/main/resources/htmlTemplate/js/template-feature-pasta.js index 8efef12..461d948 100644 --- a/core/src/main/resources/htmlTemplate/js/template-feature-pasta.js +++ b/core/src/main/resources/htmlTemplate/js/template-feature-pasta.js @@ -18,22 +18,22 @@ $("#btn-diff").click(function(){ }); // TYPE HEAD -var substringMatcher = function(strs) { +var substringMatcher = function(indexPhrases, indexMap) { return function findMatches(q, cb) { var matches = []; var substrRegex = new RegExp(q, 'i'); - $.each(strs, function (featureId, indiceItem) { - $.each(indiceItem.values, function (i, txt) { - if (substrRegex.test(txt)) { + for(let i in indexMap){ + for(let j in indexMap[i].phrases){ + if(substrRegex.test(indexPhrases[indexMap[i].phrases[j]])){ matches.push({ - "url": featureId, - "name": indiceItem.name, - "txt": txt + "url": indexPhrases[i], + "name": indexPhrases[indexMap[i].title], + "txt": indexPhrases[indexMap[i].phrases[j]] }); } - }); - }); + } + } cb(matches); }; @@ -46,7 +46,7 @@ $('#busca').typeahead({ displayKey: 'txt', name: 'txt', display: 'txt', - source: substringMatcher(indice), + source: substringMatcher(indexPhrases, indexMap), templates: { suggestion: Handlebars.compile("

{{name}}

{{txt}}
") } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/IndexParserTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/IndexParserTest.java index 2eec08c..19697db 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/IndexParserTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/IndexParserTest.java @@ -90,7 +90,7 @@ public void buildIndex() throws IOException { } Assertions.assertThat(sw.toString()) - .contains("let indexPhrases = {\"a new func\":1,\"checkpoint\":2,\"f-id\":0};") + .contains("let indexPhrases = {\"0\":\"f-id\",\"1\":\"a new func\",\"2\":\"checkpoint\"};") .contains("let indexMap = {\"0\":{\"title\":2,\"phrases\":[1]}};"); } }