Working tag filtering of topics
This commit is contained in:
parent
9eb97cbb55
commit
2c9b904e15
|
@ -4,21 +4,26 @@ module KnowledgeExplorer
|
||||||
before_action :init_guardian
|
before_action :init_guardian
|
||||||
|
|
||||||
def index
|
def index
|
||||||
category_topic_lists = []
|
|
||||||
tag_topic_lists = []
|
|
||||||
|
|
||||||
knowledge_explorer_categories.each do |c|
|
filters = {
|
||||||
if topic_list = TopicQuery.new(current_user, category: c.id).list_latest
|
tags: params[:tags],
|
||||||
category_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
|
category: params[:category]
|
||||||
end
|
}
|
||||||
|
|
||||||
|
if filters[:category]
|
||||||
|
category_topic_lists = get_topics_from_categories(category_by_filter(filters[:category]))
|
||||||
|
else
|
||||||
|
category_topic_lists = get_topics_from_categories(knowledge_explorer_categories)
|
||||||
end
|
end
|
||||||
|
|
||||||
knowledge_explorer_tags.each do |t|
|
if filters[:tags]
|
||||||
if topic_list = TopicQuery.new(current_user, tags: t.name).list_latest
|
tag_topic_lists = get_topics_from_tags(tags_by_filter(filters[:tags]))
|
||||||
tag_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
|
else
|
||||||
end
|
tag_topic_lists = get_topics_from_tags(knowledge_explorer_tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Deduplicate results
|
||||||
|
|
||||||
topics = []
|
topics = []
|
||||||
|
|
||||||
category_topic_lists.each do |list|
|
category_topic_lists.each do |list|
|
||||||
|
@ -43,6 +48,30 @@ module KnowledgeExplorer
|
||||||
render json: topics
|
render json: topics
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_topics_from_categories(categories)
|
||||||
|
category_topic_lists = []
|
||||||
|
|
||||||
|
categories.each do |c|
|
||||||
|
if topic_list = TopicQuery.new(current_user, category: c.id).list_latest
|
||||||
|
category_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
category_topic_lists
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_topics_from_tags(tags)
|
||||||
|
tag_topic_lists = []
|
||||||
|
|
||||||
|
tags.each do |t|
|
||||||
|
if topic_list = TopicQuery.new(current_user, tags: t.name).list_latest
|
||||||
|
tag_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tag_topic_lists
|
||||||
|
end
|
||||||
|
|
||||||
def count_tags(topics)
|
def count_tags(topics)
|
||||||
tags = []
|
tags = []
|
||||||
|
|
||||||
|
@ -79,5 +108,18 @@ module KnowledgeExplorer
|
||||||
|
|
||||||
Tag.where('name IN (?)', selected_tags)
|
Tag.where('name IN (?)', selected_tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def category_by_filter(category_filter)
|
||||||
|
selected_category = category_filter
|
||||||
|
|
||||||
|
category = Category.where('slug IN (?)', selected_category)
|
||||||
|
|
||||||
|
category.select { |c| @guardian.can_see_category?(c) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def tags_by_filter(tags)
|
||||||
|
selected_tags = tags.split(' ')
|
||||||
|
Tag.where('name IN (?)', selected_tags)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,5 @@ export default Ember.Component.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
this.set("filterTags", filter);
|
this.set("filterTags", filter);
|
||||||
this.send("refreshRoute");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,15 +1,44 @@
|
||||||
import {
|
import { observes } from "ember-addons/ember-computed-decorators";
|
||||||
default as computed,
|
|
||||||
observes,
|
function arrayContainsArray(superset, subset) {
|
||||||
on
|
if (0 === subset.length) {
|
||||||
} from "ember-addons/ember-computed-decorators";
|
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,
|
||||||
|
|
||||||
|
filteredTopics: null,
|
||||||
|
|
||||||
|
@observes("filterTags")
|
||||||
|
filterByTags() {
|
||||||
|
debugger;
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
this.set("filteredTopics", filteredTopics);
|
||||||
|
this.set("filtered", true);
|
||||||
|
} else {
|
||||||
|
this.set("filteredTopics", null);
|
||||||
|
this.set("filterTags", null);
|
||||||
|
this.set("filtered", false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,5 +10,6 @@ export default Ember.Route.extend({
|
||||||
tags: model.tags,
|
tags: model.tags,
|
||||||
topics: model.topics
|
topics: model.topics
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,6 +4,14 @@
|
||||||
tags=tags
|
tags=tags
|
||||||
filterTags=filterTags
|
filterTags=filterTags
|
||||||
searchResults=searchResults}}
|
searchResults=searchResults}}
|
||||||
{{knowledge-explorer-topic-list topics=topics}}
|
{{#if filtered}}
|
||||||
|
{{#if filteredTopics}}
|
||||||
|
{{knowledge-explorer-topic-list topics=filteredTopics}}
|
||||||
|
{{else}}
|
||||||
|
<div class="no-result">No results match the current filter</div>
|
||||||
|
{{/if}}
|
||||||
|
{{else}}
|
||||||
|
{{knowledge-explorer-topic-list topics=topics}}
|
||||||
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue