-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
308 lines (231 loc) · 7.58 KB
/
main.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Prevent multiple load events when scrolling
var canLoadMore = false;
// Load batches of pages of this size
var numberOfPagesToLoad = 10;
// How many pixels should there be space around the FAB
var fabSpace = 30;
// How many milliseconds should an error message be visible
var errorTime = 2000;
$(document).ready(function(){
// Check for localStorage
if (typeof(Storage) !== "undefined") {
// Storage support present, login
login();
}
// Window scroll for loading more pages
window.onscroll = function(){
// Trigger loading of more pages when there are still cards to scroll
var triggerElement = $(".article:nth-last-child("+numberOfPagesToLoad+")");
if(isScrolledIntoView(triggerElement)){
if(canLoadMore){
canLoadMore = false;
loadPages();
}
}
}
// Window resize for positioning elements
window.onresize = resize;
resize();
});
// Position FAB and error popups when resizing
function resize(){
var fab = $("#fab");
var screenWidth = $("#wrapper").width();
var fabLeft = ($(window).width() - screenWidth)/2 + screenWidth - fab.width() - fabSpace;
fab.css("bottom", fabSpace);
fab.css("left", fabLeft);
$(".error").css("right", $("#wrapper").offset().left)
}
// http://stackoverflow.com/a/488073/1456971
function isScrolledIntoView(elem) {
if(!elem.is(":visible")) return false;
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
// Shows an error message
function error(msg){
// Prettify msg, replace all _ with blanks and set to lowercase
msg = msg.toLowerCase().replace(new RegExp("_", "g"), " ");
// Create popup
var popup = $("#error").clone();
popup.removeAttr("id");
$("body").append(popup);
// Set positions
resize();
// Set message and animate popup
popup.find("span").text(msg);
popup.addClass("active");
setTimeout(function(){
popup.removeClass("active");
// Destroy element
setTimeout(function(){
popup.remove();
}, errorTime);
}, errorTime);
}
function showScreen(name){
// Hide all other screens
$("[id^=screen-]").hide();
// Show desired screen
$("#screen-"+name).show();
// Show/hide hero for certain screen names
var heroVisible = name == "login" || name == "nosupport";
if(heroVisible){
$("#hero").show();
}
else{
$("#hero").hide();
}
}
// Check for existing access token and login or show login screen
function login(){
if(localStorage.token){
main();
}
else{
// Show login screen
showScreen("login");
}
}
function createAccount(){
// Attributes
var author_name = $("input[name='author_name']").val();
var short_name = $("input[name='short_name']").val();
var author_url = $("input[name='author_url']").val();
// Request token
$.getJSON("https://api.telegra.ph/createAccount", {
"author_name": author_name,
"short_name": short_name,
"author_url": author_url
}, function(data){
// Check for valid data
if(!data.ok){
error(data.error);
return;
}
// Store token
localStorage.token = data.result.access_token;
// handle login link
$("#screen-login-telegraph a").attr("href", data.result.auth_url);
showScreen("login-telegraph");
});
}
// Login with provided token, this requires generating a new one
function tokenLogin(){
var token = $("input[name='token']").val();
$.getJSON("https://api.telegra.ph/revokeAccessToken", {
"access_token": token
}, function(data){
// Check for valid data
if(!data.ok){
error(data.error);
return;
}
// Store new token
localStorage.token = data.result.access_token;
// handle login link
$("#screen-login-telegraph a").attr("href", data.result.auth_url);
showScreen("login-telegraph");
});
}
// User has clicked the telegraph authorization link
function doneAuthorized(){
main();
}
// Shows the main screen
function main(){
showScreen("main");
// Load user info
$.getJSON("https://api.telegra.ph/getAccountInfo", {
"access_token": localStorage.token
}, function(data){
// Show login screen for errors
if(!data.ok){
showScreen("login");
}
// Username might be empty
var username = data.result.author_name;
if(username == "") username = "<unkown>";
$("#user-name").text(username);
$("#user-short").text(data.result.short_name);
// Hide user url if it is not provided
$("#user-url").toggle(data.result.author_url != "");
$("#user-url").text(data.result.author_url);
$("#user-url").attr("href", data.result.author_url);
});
// Clear old pages
$("#page-list").empty();
// Load pages
loadPages();
}
// Loads more pages
function loadPages(){
var pageList = $("#page-list");
$.getJSON("https://api.telegra.ph/getPageList", {
"access_token": localStorage.token,
"offset" : pageList.children().length,
"limit" : numberOfPagesToLoad
}, function(data){
// Set page text
var s = data.result.total_count == 1 ? "" : "s";
$("#user-page-count").text(data.result.total_count + " page" + s);
// Check for valid data
if(!data.ok){
error(data.error);
return;
}
// Append new pages
$.each(data.result.pages, function(){
// Should the text read 'view' or 'views'
var s = this.views == 1 ? "" : "s";
pageList.append("<a class='article content' href='"+this.url+"' target='_blank'><h1>"+this.title+"</h1><p>"+this.views+" view"+s+"</p><p>"+this.description+"</p></a>");
});
// If we got many pages, we can load more
if(data.result.pages.length == numberOfPagesToLoad){
// We can now receive another load event
canLoadMore = true;
}
});
}
// Show edit screen for user profile
function edit(){
// Populate edit fields
$("#logout-token").text(localStorage.token);
$.getJSON("https://api.telegra.ph/getAccountInfo", {
"access_token": localStorage.token
}, function(data){
// Check for valid data
if(!data.ok){
error(data.error);
return;
}
$("input[name='author_name_edit']").val(data.result.author_name);
$("input[name='short_name_edit']").val(data.result.short_name);
$("input[name='author_url_edit']").val(data.result.author_url);
showScreen("edit");
});
}
// Save edited profile
function saveEdit(){
$.getJSON("https://api.telegra.ph/editAccountInfo", {
"access_token": localStorage.token,
"author_name": $("input[name='author_name_edit']").val(),
"short_name": $("input[name='short_name_edit']").val(),
"author_url": $("input[name='author_url_edit']").val(),
}, function(data){
// Check for valid data
if(!data.ok){
error(data.error);
return;
}
main();
});
}
// logout
function logout(){
localStorage.removeItem("token");
window.location.reload(true);
}