Skip to content
This repository was archived by the owner on Jul 26, 2019. It is now read-only.

Commit 45abab3

Browse files
author
DangerRuss
committed
Making it 1.6.x compatible
1 parent 0b53641 commit 45abab3

File tree

6 files changed

+155
-106
lines changed

6 files changed

+155
-106
lines changed

client.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$(window).on('action:ajaxify.end', function () {
2+
require(['translator'], function(translator) {
3+
translator.translate('[[topic:most_votes]]', function(most_votes) {
4+
var sortEl = $('.category [component="thread/sort"] ul');
5+
sortEl.append('<li><a href="#" class="most_votes" data-sort="most_votes"><i class="fa fa-fw ' + (config.categoryTopicSort === 'most_votes' ? 'fa-check' : '') + '"></i> ' + most_votes + '</a></li>');
6+
});
7+
});
8+
});

languages/en-GB/global.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"votes": "Votes"
3+
}

languages/ru/global.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"votes": "Понравилось"
3+
}

library.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ socketAdmin.plugins.categorySortByVotes.reindex = function(socket, data, callbac
2626
var map = {};
2727
postsFields.forEach(function(postField, index) {
2828
if (postField['upvotes'] || postField['downvotes'])
29-
map[postField['pid']] = postField['upvotes'] - postField['downvotes'];
29+
map[postField['pid']] = (postField['upvotes'] - postField['downvotes']) || 0;
3030
else
31-
map[postField['pid']] = postField['votes'];
31+
map[postField['pid']] = postField['votes'] || 0;
3232
});
3333

3434
data.forEach(function (topic, index){
@@ -65,22 +65,6 @@ theme.getSettings = function (data, callback) {
6565
}
6666
callback(null, data);
6767
};
68-
69-
theme.beforeSearch = function (data, callback){
70-
function tmp(err, settings){
71-
if (settings.categoryTopicSort === 'most_votes') {
72-
data.reverse = true;
73-
data.set = 'cid:' + data.cid + ':tids:votes';
74-
}
75-
callback(null, data);
76-
}
77-
if (data.settings) {
78-
tmp(null, data.settings);
79-
} else {
80-
user.getSettings(data['uid'], tmp);
81-
}
82-
};
83-
8468
theme.init = function (data, callback) {
8569
data.router.get('/admin/plugins/category-sort-by-votes', data.middleware.admin.buildHeader, renderAdmin);
8670
data.router.get('/api/admin/plugins/category-sort-by-votes', renderAdmin);
@@ -130,7 +114,7 @@ theme.addAdminNavigation = function(header, callback) {
130114
header.plugins.push({
131115
route: '/plugins/category-sort-by-votes',
132116
icon: 'fa-edit',
133-
name: 'Category Sort'
117+
name: 'Category Sort By Votes'
134118
});
135119

136120
callback(null, header);
@@ -144,9 +128,9 @@ theme.addTopicsVotesInCategory = function(params, callback) {
144128
posts.getPostsFields(mainPids, ['votes', 'upvotes', 'downvotes'], function(err, postsFields) {
145129
postsFields.forEach(function(postFields, index) {
146130
if (postFields['upvotes'] || postFields['downvotes'])
147-
params.topics[index].votes = postFields['upvotes'] - postFields['downvotes'];
131+
params.topics[index].votes = (postFields['upvotes'] - postFields['downvotes']) || 0;
148132
else
149-
params.topics[index].votes = postFields['votes'];
133+
params.topics[index].votes = postFields['votes'] || 0;
150134
});
151135

152136
callback(null, params);
@@ -160,4 +144,18 @@ theme.createTopic = function (data, callback) {
160144
callback(null, data);
161145
}
162146

147+
theme.getSortedSetRangeDirection = (data, callback) => {
148+
if (data.sort==='most_votes') {
149+
data.direction = 'highest-to-lowest';
150+
}
151+
return callback(null, data);
152+
}
153+
154+
theme.buildTopicsSortedSet = (data, callback) => {
155+
if (data.data.sort==='most_votes') {
156+
data.set="cid:" + data.data.cid + ":tids:votes";
157+
}
158+
return callback(null, data);
159+
}
160+
163161
module.exports = theme;

plugin.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "nodebb-plugin-category-sort-by-votes",
3-
"name": "Sort by Votes",
3+
"name": "Category Sort by Votes",
44
"description": "Adds functionality to sort topics by votes in category list view",
55
"url": "https://github.com/wktang/nodebb-plugin-category-sort-by-votes",
66
"library": "./library.js",
@@ -9,11 +9,17 @@
99
{ "hook": "filter:topics.get", "method": "addTopicsVotesInCategory" },
1010
{ "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
1111
{ "hook": "filter:user.getSettings", "method": "getSettings" },
12-
{ "hook": "filter:category.topics.prepare", "method": "beforeSearch" },
12+
{ "hook": "filter:categories.getSortedSetRangeDirection", "method": "getSortedSetRangeDirection"},
13+
{ "hook": "filter:categories.buildTopicsSortedSet", "method": "buildTopicsSortedSet"},
1314
{ "hook": "action:post.downvote", "method": "downvote" },
1415
{ "hook": "action:post.upvote", "method": "upvote" },
1516
{ "hook": "action:post.unvote", "method": "unvote" },
1617
{ "hook": "filter:topic.create", "method": "createTopic" }
1718
],
18-
"templates": "./public/templates"
19+
"templates": "./public/templates",
20+
"scripts": [
21+
"./client.js"
22+
],
23+
"languages": "languages",
24+
"defaultLang": "en-GB"
1925
}
Lines changed: 113 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,124 @@
1-
<ul component="category" id="topics-container" data-nextstart="{nextStart}">
2-
<meta itemprop="itemListOrder" content="descending">
3-
<!-- BEGIN topics -->
4-
<li component="category/topic" class="category-item {function.generateTopicClass}" itemprop="itemListElement" <!-- IMPORT partials/data/category.tpl -->>
5-
<meta itemprop="name" content="{function.stripTags, title}">
1+
<ul component="category" class="topic-list" itemscope itemtype="http://www.schema.org/ItemList" data-nextstart="{nextStart}" data-set="{set}">
2+
<meta itemprop="itemListOrder" content="descending">
3+
<!-- BEGIN topics -->
4+
<li component="category/topic" class="row clearfix category-item {function.generateTopicClass}" <!-- IMPORT partials/data/category.tpl -->>
5+
<meta itemprop="name" content="{function.stripTags, title}">
66

7-
<div class="category-body">
8-
<div class="row">
7+
<div class="col-md-6 col-sm-9 col-xs-10 content">
8+
<div class="avatar pull-left" title="{topics.user.username}">
9+
<!-- IF showSelect -->
10+
<div class="select" component="topic/select">
11+
<!-- IF topics.thumb -->
12+
<img src="{topics.thumb}" class="user-img" />
13+
<!-- ELSE -->
14+
<!-- IF topics.user.picture -->
15+
<img component="user/picture" data-uid="{topics.user.uid}" src="{topics.user.picture}" class="user-img" />
16+
<!-- ELSE -->
17+
<div class="user-icon" style="background-color: {topics.user.icon:bgColor};">{topics.user.icon:text}</div>
18+
<!-- ENDIF topics.user.picture -->
19+
<!-- ENDIF topics.thumb -->
20+
<i class="fa fa-check"></i>
21+
</div>
22+
<!-- ENDIF showSelect -->
923

10-
<div class="col-md-7 col-sm-9">
24+
<!-- IF !showSelect -->
25+
<a href="<!-- IF topics.user.userslug -->{config.relative_path}/user/{topics.user.userslug}<!-- ELSE -->#<!-- ENDIF topics.user.userslug -->" class="pull-left">
26+
<!-- IF topics.thumb -->
27+
<img src="{topics.thumb}" class="user-img" />
28+
<!-- ELSE -->
29+
<!-- IF topics.user.picture -->
30+
<img component="user/picture" data-uid="{topics.user.uid}" src="{topics.user.picture}" class="user-img" />
31+
<!-- ELSE -->
32+
<div class="user-icon" style="background-color: {topics.user.icon:bgColor};">{topics.user.icon:text}</div>
33+
<!-- ENDIF topics.user.picture -->
34+
<!-- ENDIF topics.thumb -->
35+
</a>
36+
<!-- ENDIF !showSelect -->
1137

12-
<!-- IF showSelect -->
13-
<i class="fa fa-fw fa-square-o pull-left select pointer" component="topic/select"></i>
14-
<!-- ENDIF showSelect -->
38+
</div>
1539

16-
<div class="category-profile-pic">
17-
<a href="<!-- IF topics.user.userslug -->{config.relative_path}/user/{topics.user.userslug}<!-- ELSE -->#<!-- ENDIF topics.user.userslug -->">
18-
<!-- IF topics.thumb -->
19-
<img src="{topics.thumb}" class="user-img" title="{topics.user.username}" />
20-
<!-- ELSE -->
21-
<!-- IF topics.user.picture -->
22-
<img component="user/picture" data-uid="{topics.user.uid}" src="{topics.user.picture}" class="user-img" title="{topics.user.username}" />
23-
<!-- ELSE -->
24-
<div class="user-icon" style="background-color: {topics.user.icon:bgColor};" title="{topics.user.username}">{topics.user.icon:text}</div>
25-
<!-- ENDIF topics.user.picture -->
26-
<!-- ENDIF topics.thumb -->
27-
</a>
28-
</div>
29-
<div class="category-text">
30-
<p><strong><i component="topic/pinned" class="fa fa-thumb-tack<!-- IF !topics.pinned --> hide<!-- ENDIF !topics.pinned -->"></i> <i component="topic/locked" class="fa fa-lock<!-- IF !topics.locked --> hide<!-- ENDIF !topics.locked -->"></i></strong>
31-
<!-- IF !topics.noAnchor -->
32-
<a component="topic/header" href="{config.relative_path}/topic/{topics.slug}" itemprop="url" class="topic-title">{topics.title}</a><br />
33-
<!-- ELSE -->
34-
<a component="topic/header" itemprop="url" class="topic-title">{topics.title}</a><br />
35-
<!-- ENDIF !topics.noAnchor -->
40+
<h2 component="topic/header" class="title">
41+
<i component="topic/pinned" class="fa fa-thumb-tack <!-- IF !topics.pinned -->hide<!-- ENDIF !topics.pinned -->" title="[[topic:pinned]]"></i>
42+
<i component="topic/locked" class="fa fa-lock <!-- IF !topics.locked -->hide<!-- ENDIF !topics.locked -->" title="[[topic:locked]]"></i>
43+
<i component="topic/moved" class="fa fa-arrow-circle-right <!-- IF !topics.oldCid -->hide<!-- ENDIF !topics.oldCid -->" title="[[topic:moved]]"></i>
44+
<!-- BEGIN icons -->@value<!-- END icons -->
3645

37-
<small>
38-
<a href="{config.relative_path}/category/{topics.category.slug}"><i class="fa {topics.category.icon}"></i> {topics.category.name}</a> &bull; <span class="timeago" title="{topics.timestampISO}"></span>
39-
<!-- IF !topics.unreplied -->
40-
<span class="hidden-md hidden-lg">
41-
<br/>
42-
<a href="{config.relative_path}/topic/{topics.slug}/{topics.teaser.index}"><span class="timeago" title="{topics.teaser.timestampISO}"></span></a>
43-
</span>
44-
<!-- ENDIF !topics.unreplied -->
45-
<br/>
46-
<!-- IMPORT partials/category_tags.tpl -->
47-
</small>
48-
</p>
49-
</div>
50-
</div>
46+
<!-- IF !topics.noAnchor -->
47+
<a href="{config.relative_path}/topic/{topics.slug}<!-- IF topics.bookmark -->/{topics.bookmark}<!-- ENDIF topics.bookmark -->" itemprop="url">{topics.title}</a><br />
48+
<!-- ELSE -->
49+
<span>{topics.title}</span><br />
50+
<!-- ENDIF !topics.noAnchor -->
5151

52-
<div class="col-md-1 hidden-sm hidden-xs category-stat">
53-
<!-- IF topics.votes -->
54-
<strong class="human-readable-number" title="{topics.votes}">{topics.votes}</strong><br />
55-
<!-- ELSE -->
56-
<strong class="human-readable-number" title="0">0</strong><br />
57-
<!-- ENDIF topics.votes -->
58-
<small>VOTES</small>
59-
</div>
52+
<!-- IF !template.category -->
53+
<small>
54+
<a href="{config.relative_path}/category/{topics.category.slug}"><span class="fa-stack fa-lg"><i style="color:{topics.category.bgColor};"class="fa fa-circle fa-stack-2x"></i><i style="color:{topics.category.color};" class="fa {topics.category.icon} fa-stack-1x"></i></span> {topics.category.name}</a> &bull;
55+
</small>
56+
<!-- ENDIF !template.category -->
6057

61-
<div class="col-xs-1 category-stat hidden-xs">
62-
<strong class="human-readable-number" title="{topics.postcount}">{topics.postcount}</strong><br />
63-
<small>[[global:posts]]</small>
64-
</div>
58+
<!-- IF topics.tags.length -->
59+
<span class="tag-list hidden-xs">
60+
<!-- BEGIN tags -->
61+
<a href="{config.relative_path}/tags/{topics.tags.value}"><span class="tag" style="<!-- IF topics.tags.color -->color: {topics.tags.color};<!-- ENDIF topics.tags.color --><!-- IF topics.tags.bgColor -->background-color: {topics.tags.bgColor};<!-- ENDIF topics.tags.bgColor -->">{topics.tags.value}</span></a>
62+
<!-- END tags -->
63+
<small>&bull;</small>
64+
</span>
65+
<!-- ENDIF topics.tags.length -->
6566

66-
<div class="col-xs-1 category-stat hidden-xs">
67-
<strong class="human-readable-number" title="{topics.viewcount}">{topics.viewcount}</strong><br />
68-
<small>[[global:views]]</small>
69-
</div>
67+
<small class="hidden-xs"><span class="timeago" title="{topics.timestampISO}"></span> &bull; <a href="<!-- IF topics.user.userslug -->{config.relative_path}/user/{topics.user.userslug}<!-- ELSE -->#<!-- ENDIF topics.user.userslug -->">{topics.user.username}</a></small>
68+
<small class="visible-xs-inline">
69+
<!-- IF topics.teaser.timestamp -->
70+
<span class="timeago" title="{topics.teaser.timestampISO}"></span>
71+
<!-- ELSE -->
72+
<span class="timeago" title="{topics.timestampISO}"></span>
73+
<!-- ENDIF topics.teaser.timestamp -->
74+
</small>
75+
</h2>
76+
</div>
7077

71-
<div class="col-xs-2 category-stat replies hidden-sm hidden-xs">
72-
<!-- IF topics.unreplied -->
73-
<p class="no-replies"><a href="{config.relative_path}/topic/{topics.slug}" itemprop="url">[[category:no_replies]]</a></p>
74-
<!-- ELSE -->
75-
<a href="<!-- IF topics.teaser.user.userslug -->{config.relative_path}/user/{topics.teaser.user.userslug}<!-- ELSE -->#<!-- ENDIF topics.teaser.user.userslug -->">
76-
<!-- IF topics.teaser.user.picture -->
77-
<img class="teaser-pic" src="{topics.teaser.user.picture}" title="{topics.teaser.user.username}"/>
78-
<!-- ELSE -->
79-
<div class="teaser-pic user-icon" style="background-color: {topics.teaser.user.icon:bgColor};" title="{topics.teaser.user.username}">{topics.teaser.user.icon:text}</div>
80-
<!-- ENDIF topics.teaser.user.picture -->
81-
</a>
82-
<a href="{config.relative_path}/topic/{topics.slug}/{topics.teaser.index}">
83-
<span class="timeago" title="{topics.teaser.timestampISO}"></span>
84-
</a>
78+
<div class="mobile-stat col-xs-2 visible-xs text-right">
79+
<span class="human-readable-number">{topics.postcount}</span> <a href="{config.relative_path}/topic/{topics.slug}/{topics.teaser.index}"><i class="fa fa-arrow-circle-right"></i></a>
80+
</div>
8581

86-
<!-- ENDIF topics.unreplied -->
87-
</div>
88-
</div>
89-
</div>
82+
<div class="col-md-1 hidden-sm hidden-xs stats">
83+
<span class="human-readable-number" title="{topics.postcount}">{topics.postcount}</span><br />
84+
<small>[[global:posts]]</small>
85+
</div>
9086

91-
</li>
92-
<!-- END topics -->
93-
</ul>
87+
<div class="col-md-1 hidden-sm hidden-xs stats">
88+
<span class="human-readable-number" title="{topics.viewcount}">{topics.viewcount}</span><br />
89+
<small>[[global:views]]</small>
90+
</div>
91+
<div class="col-md-1 hidden-sm hidden-xs stats">
92+
<span class="human-readable-number" title="{topics.votes}">{topics.votes}</span><br />
93+
<small>[[global:votes]]</small>
94+
</div>
95+
<div class="col-md-3 col-sm-3 teaser hidden-xs" component="topic/teaser">
96+
<div class="card" style="border-color: {topics.category.bgColor}">
97+
<!-- IF topics.unreplied -->
98+
<p>
99+
[[category:no_replies]]
100+
</p>
101+
<!-- ELSE -->
102+
<!-- IF topics.teaser.pid -->
103+
<p>
104+
<a href="{config.relative_path}/user/{topics.teaser.user.userslug}">
105+
<!-- IF topics.teaser.user.picture -->
106+
<img title="{topics.teaser.user.username}" class="user-img" src="{topics.teaser.user.picture}" />
107+
<!-- ELSE -->
108+
<span title="{topics.teaser.user.username}" class="user-icon user-img" style="background-color: {topics.teaser.user.icon:bgColor};">{topics.teaser.user.icon:text}</span>
109+
<!-- ENDIF topics.teaser.user.picture -->
110+
</a>
111+
<a class="permalink" href="{config.relative_path}/topic/{topics.slug}/{topics.teaser.index}">
112+
<span class="timeago" title="{topics.teaser.timestampISO}"></span>
113+
</a>
114+
</p>
115+
<div class="post-content">
116+
{topics.teaser.content}
117+
</div>
118+
<!-- ENDIF topics.teaser.pid -->
119+
<!-- ENDIF topics.unreplied -->
120+
</div>
121+
</div>
122+
</li>
123+
<!-- END topics -->
124+
</ul>

0 commit comments

Comments
 (0)