FEATURE: show category-to-tag relationships on tags index page

This commit is contained in:
Neil Lalonde 2016-05-31 16:27:13 -04:00
parent 09e991b3c3
commit a6aab00663
7 changed files with 69 additions and 14 deletions

View File

@ -0,0 +1,21 @@
export default Ember.Component.extend({
classNameBindings: [':tag-list', 'categoryClass'],
sortedTags: Ember.computed.sort('tags', 'sortProperties'),
title: function() {
if (this.get('titleKey')) { return I18n.t(this.get('titleKey')); }
}.property('titleKey'),
category: function() {
if (this.get('categoryId')) {
return Discourse.Category.findById(this.get('categoryId'));
}
}.property('categoryId'),
categoryClass: function() {
if (this.get('category')) {
return "tag-list-" + this.get('category.fullSlug');
}
}.property('category')
});

View File

@ -1,8 +1,6 @@
export default Ember.Controller.extend({
sortProperties: ['count:desc', 'id'],
sortedTags: Ember.computed.sort('model', 'sortProperties'),
actions: {
sortByCount() {
this.set('sortProperties', ['count:desc', 'id']);

View File

@ -0,0 +1,13 @@
{{#if title}}
<h3>{{title}}</h3>
{{/if}}
{{#if category}}
{{category-title-link category=category}}
{{/if}}
{{#each sortedTags as |tag|}}
<div class='tag-box'>
{{discourse-tag tag.id}} <span class='tag-count'>x {{tag.count}}</span>
</div>
{{/each}}
<div class="clearfix" />
<hr/>

View File

@ -2,18 +2,17 @@
{{discourse-banner user=currentUser banner=site.banner}}
</div>
<h2>{{i18n "tagging.all_tags"}}</h2>
<h2>{{i18n "tagging.tags"}}</h2>
<div class='tag-sort-options'>
{{i18n "tagging.sort_by"}}
<a {{action "sortByCount"}}>{{i18n "tagging.sort_by_count"}}</a>
<a {{action "sortById"}}>{{i18n "tagging.sort_by_name"}}</a>
</div>
<div class="clearfix" />
<div class='tag-list'>
{{#each sortedTags as |tag|}}
<div class='tag-box'>
{{discourse-tag tag.id}} <span class='tag-count'>x {{tag.count}}</span>
</div>
{{/each}}
</div>
{{#each model.extras.categories as |category|}}
{{tag-list tags=category.tags sortProperties=sortProperties categoryId=category.id}}
{{/each}}
{{tag-list tags=model sortProperties=sortProperties titleKey="tagging.all_tags"}}

View File

@ -8,6 +8,10 @@
margin-top: 2em;
}
#list-area .tag-list h3 {
margin-bottom: 20px;
}
.tag-box {
display: inline-block;
width: 300px;
@ -180,8 +184,11 @@ header .discourse-tag {color: $tag-color !important; }
padding-top: 2px;
}
.tag-sort-options a {
text-decoration: underline;
.tag-sort-options {
margin-bottom: 20px;
a {
text-decoration: underline;
}
}
.autocomplete {

View File

@ -12,15 +12,23 @@ class TagsController < ::ApplicationController
before_filter :set_category_from_params, except: [:index, :update, :destroy, :tag_feed, :search, :notifications, :update_notifications]
def index
categories = Category.where("id in (select category_id from category_tags)")
.where("id in (?)", guardian.allowed_category_ids)
.preload(:tags)
category_tag_counts = categories.map { |c| {id: c.id, tags: self.class.tag_counts_json(Tag.category_tags_by_count_query(c, limit: 300).count)} }
tag_counts = self.class.tags_by_count(guardian, limit: 300).count
@tags = tag_counts.map {|t, c| { id: t, text: t, count: c } }
@tags = self.class.tag_counts_json(tag_counts)
respond_to do |format|
format.html do
render :index
end
format.json do
render json: { tags: @tags }
render json: {
tags: @tags,
extras: { categories: category_tag_counts }
}
end
end
end
@ -154,6 +162,10 @@ class TagsController < ::ApplicationController
guardian.filter_allowed_categories(Tag.tags_by_count_query(opts))
end
def self.tag_counts_json(tag_counts)
tag_counts.map {|t, c| { id: t, text: t, count: c } }
end
def set_category_from_params
slug_or_id = params[:category]
return true if slug_or_id.nil?

View File

@ -15,6 +15,11 @@ class Tag < ActiveRecord::Base
q
end
def self.category_tags_by_count_query(category, opts={})
tags_by_count_query(opts).where("tags.id in (select tag_id from category_tags where category_id = ?)", category.id)
.where("topics.category_id = ?", category.id)
end
def self.top_tags(limit_arg=nil)
self.tags_by_count_query(limit: limit_arg || SiteSetting.max_tags_in_filter_list)
.count