aboutsummaryrefslogtreecommitdiff
path: root/static/fluoride.js
blob: 25ab4a328b56d5ef36e7715c7f58fc941cd790f1 (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
var actionIcons = {
	"like": "/static/icons/star-o.png",
	"unlike": "/static/icons/liked.png",
	"retweet": "/static/icons/retweet.png",
	"unretweet": "/static/icons/retweeted.png"
};

var reverseActions = {
	"like": "unlike",
	"unlike": "like",
	"retweet": "unretweet",
	"unretweet": "retweet"
};

function http(method, url, success, error) {
	var req = new XMLHttpRequest();
	req.onload = function() {
		if (this.status === 200 && typeof success === "function") {
			success(this.responseText, this.responseType);
		} else if (typeof error === "function") {
			error(this.responseText);
		}
	};
	req.onerror = function() {
		if (typeof error === "function") {
			error(this.responseText);
		}
	};
	req.open(method, url);
	req.send();
}

function updateActionForm(id, f, action) {
	f.children[1].src = actionIcons[action];
	f.action = "/" + action + "/" + id;
	f.dataset.action = action;
}

function handleLikeForm(id, f) {
	f.onsubmit = function(event) {
		event.preventDefault();

		var action = f.dataset.action;
		var forms = document.querySelectorAll(".status-"+id+" .status-like");
		forms.forEach(function(f) {
			updateActionForm(id, f, reverseActions[action]);
		});

		http("POST", "/fluoride/" + action + "/" + id, function(res, type) {
			var data = JSON.parse(res);
			var count = data.data;
			if (count === 0) {
				count = "";
			}
			var counts = document.querySelectorAll(".status-"+id+" .status-like-count");
			counts.forEach(function(c) {
				c.innerHTML = count;
			});
		}, function(err) {
			forms.forEach(function(f) {
				updateActionForm(id, f, action);
			});
		});
	}
}

function handleRetweetForm(id, f) {
	f.onsubmit = function(event) {
		event.preventDefault();

		var action = f.dataset.action;
		var forms = document.querySelectorAll(".status-"+id+" .status-retweet");
		forms.forEach(function(f) {
			updateActionForm(id, f, reverseActions[action]);
		});

		http("POST", "/fluoride/" + action + "/" + id, function(res, type) {
			var data = JSON.parse(res);
			var count = data.data;
			if (count === 0) {
				count = "";
			}
			var counts = document.querySelectorAll(".status-"+id+" .status-retweet-count");
			counts.forEach(function(c) {
				c.innerHTML = count;
			});
		}, function(err) {
			forms.forEach(function(f) {
				updateActionForm(id, f, action);
			});
		});
	}
}

function handleReplyToLink(link) {
	if (!link) {
		return;
	}
	var id = link.firstElementChild.getAttribute('href');
	if (!id || id[0] != '#') {
		return;
	}
	link.onmouseenter = function(event) {
		var id = event.target.firstElementChild.getAttribute('href');
		var status = document.querySelector(id);
		if (!status) {
			return;
		}
		var copy = status.cloneNode(true);
		copy.id = "reply-to-popup";
		link.appendChild(copy);
	}
	link.onmouseleave = function(event) {
		var popup = document.getElementById("reply-to-popup");
		if (popup) {
			event.target.removeChild(popup);    
		}
	}
}

function handleReplyLink(link) {
	link.onmouseenter = function(event) {
		var id = event.target.firstElementChild.getAttribute('href');
		var status = document.querySelector(id);
		if (!status) {
			return;
		}
		var copy = status.cloneNode(true);
		copy.id = "reply-popup";
		link.appendChild(copy);
	}
	link.onmouseleave = function(event) {
		var popup = document.getElementById("reply-popup");
		if (popup) {
			event.target.removeChild(popup);    
		}
	}
}

document.addEventListener("DOMContentLoaded", function() { 
	var statuses = document.querySelectorAll(".status-container");
	statuses.forEach(function(s) {
		var id = s.dataset.id;

		var likeForm = s.querySelector(".status-like");
		handleLikeForm(id, likeForm);

		var retweetForm = s.querySelector(".status-retweet");
		handleRetweetForm(id, retweetForm);

		var replyToLink = s.querySelector(".status-reply-to");
		handleReplyToLink(replyToLink);

		var replyLinks = s.querySelectorAll(".status-reply");
		replyLinks.forEach(handleReplyLink);
	});
});