REFACTOR: Complete filtering on back end for complexity purposes
This commit is contained in:
parent
9ecad98b17
commit
6057e054c4
|
@ -16,11 +16,7 @@ module KnowledgeExplorer
|
||||||
category_topic_lists = get_topics_from_categories(knowledge_explorer_categories)
|
category_topic_lists = get_topics_from_categories(knowledge_explorer_categories)
|
||||||
end
|
end
|
||||||
|
|
||||||
if filters[:tags]
|
tag_topic_lists = get_topics_from_tags(knowledge_explorer_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
|
|
||||||
|
|
||||||
# Deduplicate results
|
# Deduplicate results
|
||||||
|
|
||||||
|
@ -43,6 +39,11 @@ module KnowledgeExplorer
|
||||||
end
|
end
|
||||||
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)
|
topics = count_tags(topics)
|
||||||
|
|
||||||
render json: topics
|
render json: topics
|
||||||
|
@ -77,8 +78,11 @@ module KnowledgeExplorer
|
||||||
|
|
||||||
topics.each do |topic|
|
topics.each do |topic|
|
||||||
topic[:tags].each do |tag|
|
topic[:tags].each do |tag|
|
||||||
|
if params[:tags]
|
||||||
|
active = params[:tags].include?(tag)
|
||||||
|
end
|
||||||
if tags.none? { |item| item[:id].to_s == tag }
|
if tags.none? { |item| item[:id].to_s == tag }
|
||||||
tags << { id: tag, count: 1 }
|
tags << { id: tag, count: 1 , active: active || false }
|
||||||
else
|
else
|
||||||
tag_index = tags.index(tags.find { |item| item[:id].to_s == tag })
|
tag_index = tags.index(tags.find { |item| item[:id].to_s == tag })
|
||||||
tags[tag_index][:count] += 1
|
tags[tag_index][:count] += 1
|
||||||
|
@ -123,7 +127,9 @@ module KnowledgeExplorer
|
||||||
|
|
||||||
def tags_by_filter(tags)
|
def tags_by_filter(tags)
|
||||||
selected_tags = tags.split(' ')
|
selected_tags = tags.split(' ')
|
||||||
Tag.where('name IN (?)', selected_tags)
|
if (selected_tags)
|
||||||
|
return Tag.where('name IN (?)', selected_tags)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,11 @@ export default Ember.Component.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
// query the search api
|
// 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 || []);
|
this.set("searchResults", result.topics || []);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,26 +3,15 @@ import {
|
||||||
observes
|
observes
|
||||||
} from "ember-addons/ember-computed-decorators";
|
} 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({
|
export default Ember.Controller.extend({
|
||||||
application: Ember.inject.controller(),
|
application: Ember.inject.controller(),
|
||||||
queryParams: {
|
queryParams: {
|
||||||
filterCategory: "category",
|
filterCategory: "category",
|
||||||
filterTags: "tags"
|
filterTags: "tags"
|
||||||
},
|
},
|
||||||
filtered: false,
|
|
||||||
filterTags: null,
|
filterTags: null,
|
||||||
filterCategory: null,
|
filterCategory: null,
|
||||||
|
|
||||||
filteredTags: null,
|
|
||||||
filteredTopics: null,
|
|
||||||
|
|
||||||
searchTerm: null,
|
searchTerm: null,
|
||||||
searchResults: null,
|
searchResults: null,
|
||||||
|
|
||||||
|
@ -41,63 +30,8 @@ export default Ember.Controller.extend({
|
||||||
return results.length;
|
return results.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
@observes("filterTags")
|
@computed("filterTags")
|
||||||
filterByTags() {
|
filtered(filterTags) {
|
||||||
const filterTags = this.get("filterTags");
|
return !!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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
|
||||||
export default Ember.Route.extend({
|
export default Ember.Route.extend({
|
||||||
model() {
|
queryParams: {
|
||||||
return ajax("/knowledge-explorer.json");
|
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) {
|
setupController(controller, model) {
|
||||||
|
@ -10,6 +20,5 @@ export default Ember.Route.extend({
|
||||||
tags: model.tags,
|
tags: model.tags,
|
||||||
topics: model.topics
|
topics: model.topics
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,26 +1,13 @@
|
||||||
<div class="knowledge-explorer">
|
<div class="knowledge-explorer">
|
||||||
<div class="knowledge-explorer-filter">
|
<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>
|
||||||
<div class="knowledge-explorer-browse">
|
<div class="knowledge-explorer-browse">
|
||||||
{{#if filteredTags}}
|
{{knowledge-explorer-tag-list
|
||||||
{{knowledge-explorer-tag-list
|
tags=tags
|
||||||
tags=filteredTags
|
filterTags=filterTags
|
||||||
filterTags=filterTags
|
}}
|
||||||
}}
|
{{#if hasSearchResults}}
|
||||||
{{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}}
|
|
||||||
{{#if emptySearchResults}}
|
{{#if emptySearchResults}}
|
||||||
<div class="no-result">{{i18n 'search.no_results'}}</div>
|
<div class="no-result">{{i18n 'search.no_results'}}</div>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
|
Loading…
Reference in New Issue