aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/frontend/sortcomments.js
blob: 26d0c5899610a2701196c72cfe665f1d30f1f19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Comment sorting (sortcomments.js)
HashOver.prototype.sortComments = function (method)
{
	var sortArray = [];
	var defaultName = this.setup['default-name'];

	// Returns the sum number of replies in a comment thread
	function replyPropertySum (comment, callback)
	{
		var sum = 0;

		// Check if there are replies to the current comment
		if (comment.replies !== undefined) {
			// If so, run through them adding up the number of replies
			for (var i = 0, il = comment.replies.length; i < il; i++) {
				sum += replyPropertySum (comment.replies[i], callback);
			}
		}

		// Calculate the sum based on the give callback
		sum += callback (comment);

		return sum;
	}

	// Calculation callback for `replyPropertySum` function
	function replyCounter (comment)
	{
		return (comment.replies) ? comment.replies.length : 0;
	}

	// Calculation callback for `replyPropertySum` function
	function netLikes (comment)
	{
		return (comment.likes || 0) - (comment.dislikes || 0);
	}

	// Sort methods
	switch (method) {
		// Sort all comment in reverse order
		case 'descending': {
			// Get all comments
			var tmpArray = this.getAllComments (this.instance.comments.primary);

			// And reverse the comments
			sortArray = tmpArray.reverse ();

			break;
		}

		// Sort all comments by date
		case 'by-date': {
			sortArray = this.getAllComments (this.instance.comments.primary).sort (function (a, b) {
				if (a['sort-date'] === b['sort-date']) {
					return 1;
				}

				return b['sort-date'] - a['sort-date'];
			});

			break;
		}

		// Sort all comment by net number of likes
		case 'by-likes': {
			sortArray = this.getAllComments (this.instance.comments.primary).sort (function (a, b) {
				a.likes = a.likes || 0;
				b.likes = b.likes || 0;
				a.dislikes = a.dislikes || 0;
				b.dislikes = b.dislikes || 0;

				return (b.likes - b.dislikes) - (a.likes - a.dislikes);
			});

			break;
		}

		// Sort all comment by number of replies
		case 'by-replies': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And sort them by number of replies
			sortArray = tmpArray.sort (function (a, b) {
				var ac = (!!a.replies) ? a.replies.length : 0;
				var bc = (!!b.replies) ? b.replies.length : 0;

				return bc - ac;
			});

			break;
		}

		// Sort threads by the sum of replies to its comments
		case 'by-discussion': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And sort them by the sum of each comment's replies
			sortArray = tmpArray.sort (function (a, b) {
				var replyCountA = replyPropertySum (a, replyCounter);
				var replyCountB = replyPropertySum (b, replyCounter);

				return replyCountB - replyCountA;
			});

			break;
		}

		// Sort threads by the sum of likes to it's comments
		case 'by-popularity': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And sort them by the sum of each comment's net likes
			sortArray = tmpArray.sort (function (a, b) {
				var likeCountA = replyPropertySum (a, netLikes);
				var likeCountB = replyPropertySum (b, netLikes);

				return likeCountB - likeCountA;
			});

			break;
		}

		// Sort all comment by the commenter names
		case 'by-name': {
			// Get all comments
			var tmpArray = this.getAllComments (this.instance.comments.primary);

			// And sort them alphabetically by the commenter names
			sortArray = tmpArray.sort (function (a, b) {
				var nameA = (a.name || defaultName).toLowerCase ();
				var nameB = (b.name || defaultName).toLowerCase ();

				nameA = (nameA.charAt (0) === '@') ? nameA.slice (1) : nameA;
				nameB = (nameB.charAt (0) === '@') ? nameB.slice (1) : nameB;

				if (nameA > nameB) {
					return 1;
				}

				if (nameA < nameB) {
					return -1;
				}

				return 0;
			});

			break;
		}

		// Sort threads in reverse order
		case 'threaded-descending': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And reverse the comments
			sortArray = tmpArray.reverse ();

			break;
		}

		// Sort threads by date
		case 'threaded-by-date': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And sort them by date
			sortArray = tmpArray.sort (function (a, b) {
				if (a['sort-date'] === b['sort-date']) {
					return 1;
				}

				return b['sort-date'] - a['sort-date'];
			});

			break;
		}

		// Sort threads by net likes
		case 'threaded-by-likes': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And sort them by the net number of likes
			sortArray = tmpArray.sort (function (a, b) {
				a.likes = a.likes || 0;
				b.likes = b.likes || 0;
				a.dislikes = a.dislikes || 0;
				b.dislikes = b.dislikes || 0;

				return (b.likes - b.dislikes) - (a.likes - a.dislikes);
			});

			break;
		}

		// Sort threads by commenter names
		case 'threaded-by-name': {
			// Clone the primary comments
			var tmpArray = this.cloneObject (this.instance.comments.primary);

			// And sort them alphabetically by the commenter names
			sortArray = tmpArray.sort (function (a, b) {
				var nameA = (a.name || defaultName).toLowerCase ();
				var nameB = (b.name || defaultName).toLowerCase ();

				nameA = (nameA.charAt (0) === '@') ? nameA.slice (1) : nameA;
				nameB = (nameB.charAt (0) === '@') ? nameB.slice (1) : nameB;

				if (nameA > nameB) {
					return 1;
				}

				if (nameA < nameB) {
					return -1;
				}

				return 0;
			});

			break;
		}

		// By default simply use the primary comments as-is
		default: {
			sortArray = this.instance.comments.primary;
			break;
		}
	}

	// Parse the sorted comments
	this.parseAll (sortArray, this.instance['sort-section'], false, false, true, method);
};