summaryrefslogtreecommitdiffstats
path: root/server-handler/admin.html
blob: 26b3bd053ba4a275546682cbbcf6ce4567fc46bf (plain) (blame)
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
<html>
<head>
<title>AnyClip Moment Selector Admin Panel</title>
<style>
body { font-family: "Trebuchet MS", "Verdana", sans-serif; }
</style>
<script type="text/javascript">
function changeName()
{
	var changeNameRequest = new XMLHttpRequest();
	changeNameRequest.oldLabel = this.innerText;
	this.innerText = "Please wait...";
	this.disabled = true;
	changeNameRequest.me = this;
	changeNameRequest.open("GET", "/adminrequest?action=changeName&id=" + this.momentId + "&name=" + escape(this.momentName.value));
	changeNameRequest.onreadystatechange = function() {
		if (changeNameRequest.readyState == 4 && changeNameRequest.status == 200) {
			if (changeNameRequest.responseText == "success") {
				changeNameRequest.me.innerText = "Success!";
				setTimeout(function() {
					changeNameRequest.me.innerText = changeNameRequest.oldLabel;
					changeNameRequest.me.disabled = false;
				}, 2500);
			} else {
				changeNameRequest.me.innerText = "Error";
			}
		}
	};
	changeNameRequest.send(null);
}
function deleteMoment()
{
	var deleteMomentRequest = new XMLHttpRequest();
	this.innerText = "Please wait...";
	this.disabled = true;
	deleteMomentRequest.me = this;
	deleteMomentRequest.open("GET", "/adminrequest?action=deleteMoment&id=" + this.momentId);
	deleteMomentRequest.onreadystatechange = function() {
		if (deleteMomentRequest.readyState == 4 && deleteMomentRequest.status == 200) {
			if (deleteMomentRequest.responseText == "success") {
				deleteMomentRequest.me.parentNode.parentNode.removeChild(deleteMomentRequest.me.parentNode);
				var totalCount = document.getElementById("totalCount");
				totalCount.innerText = (--totalCount.total).toString() + " moments in database";
				var titleLink = document.getElementById(deleteMomentRequest.me.titleId);
				if (titleLink != null) {
					if (!--titleLink.count)
						titleLink.parentNode.removeChild(titleLink);
					else
						titleLink.innerText = titleLink.title + " (" +  titleLink.count.toString() + ")";
				}
			}
			else
				deleteMomentRequest.me.innerText = "Error";
		}
	};
	deleteMomentRequest.send(null);
}
function loadMomentList()
{
	document.body.scrollTop = 0;
	
	var bullets = document.getElementById("titles").childNodes;
	for (var i = 0; i < bullets.length; i++) {
		if (bullets[i] == undefined || bullets[i].style == undefined)
			continue;
		if (bullets[i] != this)
			bullets[i].style.color = "#36C";
		else
			bullets[i].style.color = "#000000";
	}
	
	var momentsDiv = document.getElementById("moments");
	while (momentsDiv.childNodes.length > 0)
		momentsDiv.removeChild(momentsDiv.firstChild);
	var loading = document.createElement("h4");
	loading.align = "center";
	loading.innerText = "Loading moments. Please wait...";
	momentsDiv.appendChild(loading);
	
	var momentRequest = new XMLHttpRequest();
	momentRequest.me = this;
	momentRequest.open("GET", "/adminrequest?action=moments&id=" + this.titleId);
	momentRequest.onreadystatechange = function() {
		if (momentRequest.readyState == 4 && momentRequest.status == 200 && momentRequest.responseText.length > 0) {
			while (momentsDiv.childNodes.length > 0)
				momentsDiv.removeChild(momentsDiv.firstChild);
			var response = JSON.parse(momentRequest.responseText);
			for (var i = 0; i < response.length; i++) {
				var momentDiv = document.createElement("div");
				
				var momentName;
				if (response[i].submittedToAPI) {
					momentName = document.createElement("span");
					momentName.innerText = response[i].name;
				} else {
					momentName = document.createElement("input");
					momentName.value = response[i].name;
					momentName.style.width = "300px";
					momentName.style.background = "inherit";
					momentName.style.border = "1px solid #36C";
					momentName.style.font = "inherit";
				}
				momentName.style.fontSize = "14px";
				momentName.style.fontWeight = "bold";
				momentDiv.appendChild(momentName);
				
				if (!response[i].submittedToAPI) {
					var changeNameButton = document.createElement("button");
					changeNameButton.innerText = "Change Moment Name";
					changeNameButton.momentId = response[i].id;
					changeNameButton.momentName = momentName;
					changeNameButton.onclick = changeName;
					momentDiv.appendChild(changeNameButton);
					
					var deleteMomentButton = document.createElement("button");
					deleteMomentButton.innerText = "Delete Moment";
					deleteMomentButton.momentId = response[i].id;
					deleteMomentButton.titleId = momentRequest.me.titleId;
					deleteMomentButton.onclick = deleteMoment;
					momentDiv.appendChild(deleteMomentButton);
				}
				
				var quoteArea = document.createElement("div");
				quoteArea.innerText = response[i].quote;
				quoteArea.style.fontFamily = "Georgia, Times New Roman, Times, Serif";
				quoteArea.style.padding = "10px";
				momentDiv.appendChild(quoteArea);
				
				momentDiv.style.borderTop = momentDiv.style.borderBottom = "1px dotted #36C";
				momentDiv.style.marginTop = momentDiv.style.marginBottom = "6px";
				momentDiv.style.padding = "6px";
				momentDiv.style.background = "#E5ECF9";
				momentsDiv.appendChild(momentDiv);
			}
		}
	};
	momentRequest.send(null);
}
function loadTitles()
{
	var titleRequest = new XMLHttpRequest();
	titleRequest.open("GET", "/adminrequest?action=titles");
	titleRequest.onreadystatechange = function() {
		if (titleRequest.readyState == 4 && titleRequest.status == 200 && titleRequest.responseText.length > 0) {
			var response = JSON.parse(titleRequest.responseText);
			var titlesDiv = document.getElementById("titles");
			titlesDiv.style.visibility = "visible";
			document.getElementById("totalCount").innerText = response.total.toString() + " moments in database";
			document.getElementById("totalCount").total = response.total;
			for (var i = 0; i < response.titles.length; i++) {
				var entry = document.createElement("li");
				entry.innerText = response.titles[i].title + " (" + response.titles[i].count.toString() + ")";
				entry.style.cursor = "pointer";
				entry.style.color = "#36C";
				entry.titleId = response.titles[i].id;
				entry.id = entry.titleId;
				entry.title = response.titles[i].title;
				entry.count = response.titles[i].count;
				entry.onclick = loadMomentList;
				titlesDiv.appendChild(entry);
			}
		}
	};
	titleRequest.send(null);
}
window.onload = function() {
	if (navigator.userAgent.toLowerCase().indexOf('chrome') == -1)
		document.getElementById("totalCount").innerHTML = 'You are not running Google Chrome. Download it <a href="http://www.google.com/chrome">here</a> to use the admin panel.';
	else
		loadTitles();
};
</script>
</head>
<body>
<h1>AnyClip Moment Selector Admin Panel</h1>
<h3 id="totalCount">Loading list. Please wait...</h3>
<div style="visibility: hidden; float:left; background: #E5ECF9; border-right: 1px dotted #36C; padding: 6px; width: 300px;" id="titles">
</div>
<div style="margin-left: 320px;" id="moments">
</div>
</body>
</html>