Skip to content

Commit 8ab2d31

Browse files
Cherrgzeripath
authored andcommitted
fix topic bar to allow prefixes (#7325)
* - do not select if escape is pressed - allow prefixes by adding current request content to result list - remove html-tags before insert on page fix #7126 Signed-off-by: Michael Gnehr <[email protected]> * sort current query to top Signed-off-by: Michael Gnehr <[email protected]> * remove already added topics from dropdown list Signed-off-by: Michael Gnehr <[email protected]> * protoct against xss thanks to @silverwind Signed-off-by: Michael Gnehr <[email protected]>
1 parent ff85dd3 commit 8ab2d31

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

public/js/index.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,6 +2847,7 @@ function initTopicbar() {
28472847

28482848
topicDropdown.dropdown({
28492849
allowAdditions: true,
2850+
forceSelection: false,
28502851
fields: { name: "description", value: "data-value" },
28512852
saveRemoteData: false,
28522853
label: {
@@ -2864,18 +2865,50 @@ function initTopicbar() {
28642865
throttle: 500,
28652866
cache: false,
28662867
onResponse: function(res) {
2867-
var formattedResponse = {
2868+
let formattedResponse = {
28682869
success: false,
28692870
results: [],
28702871
};
2872+
const stripTags = function (text) {
2873+
return text.replace(/<[^>]*>?/gm, "");
2874+
};
2875+
2876+
let query = stripTags(this.urlData.query.trim());
2877+
let found_query = false;
2878+
let current_topics = [];
2879+
topicDropdown.find('div.label.visible.topic,a.label.visible').each(function(_,e){ current_topics.push(e.dataset.value); });
28712880

28722881
if (res.topics) {
2873-
formattedResponse.success = true;
2874-
for (var i=0;i < res.topics.length;i++) {
2875-
formattedResponse.results.push({"description": res.topics[i].Name, "data-value": res.topics[i].Name})
2882+
let found = false;
2883+
for (let i=0;i < res.topics.length;i++) {
2884+
// skip currently added tags
2885+
if (current_topics.indexOf(res.topics[i].Name) != -1){
2886+
continue;
2887+
}
2888+
2889+
if (res.topics[i].Name.toLowerCase() === query.toLowerCase()){
2890+
found_query = true;
2891+
}
2892+
formattedResponse.results.push({"description": res.topics[i].Name, "data-value": res.topics[i].Name});
2893+
found = true;
28762894
}
2895+
formattedResponse.success = found;
28772896
}
28782897

2898+
if (query.length > 0 && !found_query){
2899+
formattedResponse.success = true;
2900+
formattedResponse.results.unshift({"description": query, "data-value": query});
2901+
} else if (query.length > 0 && found_query) {
2902+
formattedResponse.results.sort(function(a, b){
2903+
if (a.description.toLowerCase() === query.toLowerCase()) return -1;
2904+
if (b.description.toLowerCase() === query.toLowerCase()) return 1;
2905+
if (a.description > b.description) return -1;
2906+
if (a.description < b.description) return 1;
2907+
return 0;
2908+
});
2909+
}
2910+
2911+
28792912
return formattedResponse;
28802913
},
28812914
},

0 commit comments

Comments
 (0)