FEATURE: Sorting per column on topic list

This commit is contained in:
Justin DiRose 2019-11-18 11:03:49 -06:00
parent 312cbf883e
commit 1dd582e1b1
8 changed files with 99 additions and 4 deletions

View File

@ -9,6 +9,8 @@ module KnowledgeExplorer
tags: params[:tags],
category: params[:category],
search_term: params[:search],
ascending: params[:ascending],
order: params[:order],
page: params[:page]
}

View File

@ -1,3 +1,25 @@
import discourseComputed from "discourse-common/utils/decorators";
export default Ember.Component.extend({
classNames: "knowledge-explorer-topic-list"
classNames: "knowledge-explorer-topic-list",
@discourseComputed("order")
sortTitle(order) {
return order === "title";
},
@discourseComputed("order")
sortActivity(order) {
return order === "activity";
},
actions: {
sortListActivity() {
this.sortBy("activity");
return false;
},
sortListTitle() {
this.sortBy("title");
return false;
}
}
});

View File

@ -18,8 +18,10 @@ function mergeCategories(results) {
export default Ember.Controller.extend({
application: Ember.inject.controller(),
queryParams: {
ascending: "ascending",
filterCategories: "category",
filterTags: "tags",
orderColumn: "order",
searchTerm: "search",
selectedTopic: "topic"
},
@ -36,6 +38,8 @@ export default Ember.Controller.extend({
selectedTopic: null,
topic: null,
expandedFilters: false,
ascending: null,
orderColumn: null,
@on("init")
_setupFilters() {
@ -149,6 +153,21 @@ export default Ember.Controller.extend({
this.send("refreshModel");
},
sortBy(column) {
const order = this.orderColumn;
const ascending = this.ascending;
if (column === "title") {
this.set("orderColumn", "title");
} else if (column === "activity") {
this.set("orderColumn", "activity");
}
if (!ascending && order) this.set("ascending", true);
else this.set("ascending", "");
this.send("refreshModel");
},
loadMore() {
if (this.canLoadMore) {
this.set("isLoadingMore", true);
@ -172,7 +191,9 @@ export default Ember.Controller.extend({
const params = this.getProperties(
"filterCategories",
"filterTags",
"searchTerm"
"searchTerm",
"ascending",
"orderColumn"
);
KnowledgeExplorer.list(params).then(result => {

View File

@ -11,6 +11,8 @@ export default {
filters.push(`category=${params.filterCategories}`);
if (params.filterTags) filters.push(`tags=${params.filterTags}`);
if (params.searchTerm) filters.push(`search=${params.searchTerm}`);
if (params.ascending) filters.push("ascending=true");
if (params.orderColumn) filters.push(`order=${params.orderColumn}`);
if (params.page) filters.push(`page=${params.page}`);
let promise = ajax(`/knowledge-explorer.json?${filters.join("&")}`);

View File

@ -1,8 +1,26 @@
{{#load-more selector=".topic-list tr" action=loadMore}}
<table class="topic-list">
<thead>
<th>Topic</th>
<th>Activity</th>
<th {{action "sortListTitle"}}>
Topic
{{#if sortTitle}}
{{#if ascending}}
{{d-icon 'angle-up'}}
{{else}}
{{d-icon 'angle-down'}}
{{/if}}
{{/if}}
</th>
<th {{action "sortListActivity"}}>
Activity
{{#if sortActivity}}
{{#if ascending}}
{{d-icon 'angle-up'}}
{{else}}
{{d-icon 'angle-down'}}
{{/if}}
{{/if}}
</th>
</thead>
<tbody>
{{#each topics as |topic|}}

View File

@ -62,6 +62,9 @@
{{#unless emptySearchResults}}
{{knowledge-explorer-topic-list
topics=topics
ascending=ascending
order=orderColumn
sortBy=(action "sortBy")
selectTopic=(action "setSelectedTopic")
loadMore=(action "loadMore")
loading=isLoadingMore

View File

@ -115,6 +115,18 @@
.knowledge-explorer-topic-list {
flex-basis: 100%;
th {
min-width: 5em;
&:hover {
background-color: $primary-low;
}
.d-icon {
vertical-align: middle;
}
}
.badge-wrapper .badge-category .category-name {
// extra protection for ultra-long category names
max-width: 30vw;

View File

@ -49,6 +49,20 @@ module KnowledgeExplorer
results = results.where('lower(title) LIKE ?', "%#{@filters[:search_term].downcase}%")
end
if @filters[:order] == "title"
if @filters[:ascending].present?
results = results.reorder('title')
else
results = results.reorder('title DESC')
end
elsif @filters[:order] == "activity"
if @filters[:ascending].present?
results = results.reorder('last_posted_at')
else
results = results.reorder('last_posted_at DESC')
end
end
tags = tag_count(results)
categories = categories_count(results)
@ -120,6 +134,7 @@ module KnowledgeExplorer
filters.push("tags=#{@filters[:tags]}") if @filters[:tags].present?
filters.push("category=#{@filters[:category]}") if @filters[:category].present?
filters.push("search=#{@filters[:search_term]}") if @filters[:search_term].present?
filters.push("sort=#{@filters[:sort]}") if @filters[:sort].present?
if @filters[:page].present?
filters.push("page=#{@filters[:page].to_i + 1}")