aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/frontend/likecomment.js
blob: 15a0d25cdf7fbe1545d698e6000bab7139f037dd (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
// For liking comments (likecomment.js)
HashOver.prototype.likeComment = function (action, permalink)
{
	// Reference to this object
	var hashover = this;

	var file = this.permalinks.getFile (permalink);
	var actionLink = this.elements.get (action + '-' + permalink, true);
	var likesElement = this.elements.get (action + 's-' + permalink, true);
	var likePath = '/likecomment';

	// Set request queries
	var queries = [
		'url=' + encodeURIComponent (this.instance['page-url']),
		'thread=' + this.instance['thread-name'],
		'comment=' + file,
		'action=' + action
	];

	// When loaded update like count
	this.ajax ('POST', likePath, queries, function (likeResponse) {
		// If a message is returned display it to the user
		if (likeResponse.message !== undefined) {
			alert (likeResponse.message);
			return;
		}

		// If an error is returned display a standard error to the user
		if (likeResponse.error !== undefined) {
			alert ('Error! Something went wrong!');
			return;
		}

		// Get number of likes
		var likesKey = (action !== 'dislike') ? 'likes' : 'dislikes';
		var likes = likeResponse[likesKey] || 0;

		// Change "Like" button title and class
		if (hashover.classes.contains (actionLink, 'hashover-' + action) === true) {
			// Change class to indicate the comment has been liked/disliked
			hashover.classes.add (actionLink, 'hashover-' + action + 'd');
			hashover.classes.remove (actionLink, 'hashover-' + action);
			actionLink.title = (action === 'like') ? hashover.locale['liked-comment'] : hashover.locale['disliked-comment'];
			actionLink.textContent = (action === 'like') ? hashover.locale['liked'] : hashover.locale['disliked'];

			// Add listener to change link text to "Unlike" on mouse over
			if (action === 'like') {
				hashover.mouseOverChanger (actionLink, 'unlike', 'liked');
			}
		} else {
			// Change class to indicate the comment is unliked
			hashover.classes.add (actionLink, 'hashover-' + action);
			hashover.classes.remove (actionLink, 'hashover-' + action + 'd');
			actionLink.title = (action === 'like') ? hashover.locale['like-comment'] : hashover.locale['dislike-comment'];
			actionLink.textContent = (action === 'like') ? hashover.locale['like'][0] : hashover.locale['dislike'][0];

			// Add listener to change link text to "Unlike" on mouse over
			if (action === 'like') {
				hashover.mouseOverChanger (actionLink, null, null);
			}
		}

		if (likes > 0) {
			// Decide if locale is pluralized
			var plural = (likes !== 1) ? 1 : 0;
			var likeLocale = (action !== 'like') ? 'dislike' : 'like';
			var likeCount = likes + ' ' + hashover.locale[likeLocale][plural];

			// Change number of likes; set font weight bold
			likesElement.textContent = likeCount;
			likesElement.style.fontWeight = 'bold';
		} else {
			// Remove like count; set font weight normal
			likesElement.textContent = '';
			likesElement.style.fontWeight = '';
		}
	}, true);
};