REFACTOR: Complete filtering on back end for complexity purposes

This commit is contained in:
Justin DiRose 2019-09-10 13:36:02 -05:00
parent 9ecad98b17
commit 6057e054c4
5 changed files with 40 additions and 100 deletions

View File

@ -16,11 +16,7 @@ module KnowledgeExplorer
category_topic_lists = get_topics_from_categories(knowledge_explorer_categories)
end
if filters[:tags]
tag_topic_lists = get_topics_from_tags(tags_by_filter(filters[:tags]))
else
tag_topic_lists = get_topics_from_tags(knowledge_explorer_tags)
end
tag_topic_lists = get_topics_from_tags(knowledge_explorer_tags)
# Deduplicate results
@ -43,6 +39,11 @@ module KnowledgeExplorer
end
end
if filters[:tags]
tag_filter = filters[:tags].split(' ')
topics = topics.select { |topic| (topic[:tags] & tag_filter).size >= 1}
end
topics = count_tags(topics)
render json: topics
@ -77,8 +78,11 @@ module KnowledgeExplorer
topics.each do |topic|
topic[:tags].each do |tag|
if params[:tags]
active = params[:tags].include?(tag)
end
if tags.none? { |item| item[:id].to_s == tag }
tags << { id: tag, count: 1 }
tags << { id: tag, count: 1 , active: active || false }
else
tag_index = tags.index(tags.find { |item| item[:id].to_s == tag })
tags[tag_index][:count] += 1
@ -123,7 +127,9 @@ module KnowledgeExplorer
def tags_by_filter(tags)
selected_tags = tags.split(' ')
Tag.where('name IN (?)', selected_tags)
if (selected_tags)
return Tag.where('name IN (?)', selected_tags)
end
end
end
end

View File

@ -10,7 +10,11 @@ export default Ember.Component.extend({
}
// query the search api
knowledgeExplorer.search(this.get("filteredTags"), term).then(result => {
const tags = this.get("filterTags") || null;
if (tags && tags.includes(" ")) {
tags.join(" ");
}
knowledgeExplorer.search(tags, term).then(result => {
this.set("searchResults", result.topics || []);
});
},

View File

@ -3,26 +3,15 @@ import {
observes
} from "ember-addons/ember-computed-decorators";
function arrayContainsArray(superset, subset) {
if (0 === subset.length) {
return false;
}
return subset.every(v => superset.indexOf(v) >= 0);
}
export default Ember.Controller.extend({
application: Ember.inject.controller(),
queryParams: {
filterCategory: "category",
filterTags: "tags"
},
filtered: false,
filterTags: null,
filterCategory: null,
filteredTags: null,
filteredTopics: null,
searchTerm: null,
searchResults: null,
@ -41,63 +30,8 @@ export default Ember.Controller.extend({
return results.length;
},
@observes("filterTags")
filterByTags() {
const filterTags = this.get("filterTags");
if (filterTags != null) {
const filter = filterTags.split(" ");
const topics = this.get("topics");
const tags = this.get("tags");
if (filter != "") {
const filteredTopics = topics.filter(topic =>
arrayContainsArray(topic.tags, filter)
);
const filteredTags = [];
// add active tags
filter.forEach(tag => {
let t = {
id: tag,
count: 0,
active: true
};
filteredTags.push(t);
});
filteredTopics.forEach(topic => {
let topicTags = topic.tags;
topicTags.forEach(tag => {
if (filterTags.includes(tag)) {
//increment count for active tags
let index = filteredTags.findIndex(t => t.id === tag);
filteredTags[index].count++;
} else if (filteredTags.findIndex(t => t.id === tag) != -1) {
//increment count for seen subtags
let index = filteredTags.findIndex(t => t.id === tag);
filteredTags[index].count++;
} else {
//add entry for unseen subtag
let t = {
id: tag,
count: 1
};
filteredTags.push(t);
}
});
});
this.set("filteredTags", filteredTags);
this.set("filteredTopics", filteredTopics);
this.set("filtered", true);
} else {
this.set("filteredTags", null);
this.set("filteredTopics", null);
this.set("filterTags", null);
this.set("filtered", false);
}
}
@computed("filterTags")
filtered(filterTags) {
return !!filterTags;
}
});

View File

@ -1,8 +1,18 @@
import { ajax } from "discourse/lib/ajax";
export default Ember.Route.extend({
model() {
return ajax("/knowledge-explorer.json");
queryParams: {
filterTags: {
refreshModel: true
}
},
model(params) {
if (params.filterTags) {
const tags = params.filterTags;
return ajax(`/knowledge-explorer.json?tags=${tags}`);
} else {
return ajax("/knowledge-explorer.json");
}
},
setupController(controller, model) {
@ -10,6 +20,5 @@ export default Ember.Route.extend({
tags: model.tags,
topics: model.topics
});
},
}
});

View File

@ -1,26 +1,13 @@
<div class="knowledge-explorer">
<div class="knowledge-explorer-filter">
{{knowledge-explorer-search searchResults=searchResults searchTerm=searchTerm filteredTags=filteredTags}}
{{knowledge-explorer-search searchResults=searchResults searchTerm=searchTerm filterTags=filterTags}}
</div>
<div class="knowledge-explorer-browse">
{{#if filteredTags}}
{{knowledge-explorer-tag-list
tags=filteredTags
filterTags=filterTags
}}
{{else}}
{{knowledge-explorer-tag-list
tags=tags
filterTags=filterTags
}}
{{/if}}
{{#if filtered}}
{{#if filteredTopics}}
{{knowledge-explorer-topic-list topics=filteredTopics}}
{{else}}
<div class="no-result">No results match the current filter</div>
{{/if}}
{{else if hasSearchResults}}
{{knowledge-explorer-tag-list
tags=tags
filterTags=filterTags
}}
{{#if hasSearchResults}}
{{#if emptySearchResults}}
<div class="no-result">{{i18n 'search.no_results'}}</div>
{{else}}