-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
537 lines (479 loc) · 17.4 KB
/
app.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// require('pebblejs');
var UI = require('ui');
var ajax = require('ajax');
var Settings = require('settings');
var Feature = require('platform/feature');
var blackStatusBar = {
color: "white",
backgroundColor: "black",
separator: 'none'
};
var defaultFeeds = [{
title: "Vox",
url: "https://www.vox.com/rss/full.xml"
},{
title: "The Atlantic",
url: "https://www.theatlantic.com/feed/all"
},{
title: "Vice News",
url: "http://news.vice.com/rss"
},{
title: "Time Magazine",
url: "http://feeds2.feedburner.com/time/topstories"
},{
title: "ProPublica",
url: "http://feeds.propublica.org/propublica/main"
},{
title: "The Verge",
url: "https://www.theverge.com/rss/full.xml"
},{
title: "Techcrunch",
url: "http://feeds.feedburner.com/TechCrunch/"
},{
title: "SBNation",
url: "http://feeds.feedburner.com/sportsblogs/sbnation.xml"
},{
title: "Curbed",
url: "https://www.curbed.com/rss/full.xml"
}];
Settings.config({url: 'https://wowfunhappy.github.io/Pebble-RSS-Reader/', hash: true}, function(){
if (typeof Settings.option().feeds === 'undefined' || Settings.option().feeds.length < 1) {
Settings.option("feeds", defaultFeeds);
}
}, function(){
feedSelectMenu.hide(); // Will likely close app
});
/*-----------------------------------------------------------------------------*/
var articleSelectMenuExists = false;
var feedSelectMenu;
var loadingCardVisible;
var lastSeenArticleCard = {};
var articleSelectMenu = {};
selectFeed();
if (typeof Settings.data('welcomeScreenCompleted') === 'undefined') {
welcomeScreen();
}
else {
if (allSavedInfoExists()) {
displayArticlePage(Settings.data('savedArticleList'), Settings.data('savedArticleNum'), Settings.data('savedPageNum'));
}
}
function removeSavedInfo() {
Settings.data('savedArticleList', undefined);
Settings.data('savedArticleNum', undefined);
Settings.data('savedPageNum', undefined);
//savedFeed is NOT removed!
}
function allSavedInfoExists() {
if (typeof Settings.data('savedFeed') === 'undefined' || typeof Settings.data('savedArticleList') === 'undefined' || typeof Settings.data('savedArticleNum') === 'undefined' || typeof Settings.data('savedPageNum') === 'undefined') {
return false;
}
else {
return true;
}
}
function saveCurrPage(articleCard, articleList, articleNum, pageNum) {
Settings.data('savedArticleList', articleList);
Settings.data('savedArticleNum', articleNum);
Settings.data('savedPageNum', pageNum);
lastSeenArticleCard = articleCard;
}
function selectFeed() {
/*Overwrite default feeds with what user input in Settings.*/
if (typeof Settings.option().feeds !== 'undefined' && Settings.option().feeds.length > 0) {
feeds = Settings.option().feeds;
}
else {
feeds = defaultFeeds;
}
feedSelectMenu = new UI.Menu({
status: false,
sections: [{
title: "Select Feed:",
items: feeds
}]
});
feedSelectMenu.show();
feedSelectMenu.on('select', function(e) {
getArticles(feeds[e.itemIndex]);
});
feedSelectMenu.on('longSelect', function() {
welcomeScreen();
});
lastSeenArticleCard = {};
feedSelectMenu.on('show', function(){
lastSeenArticleCard = {};
})
}
function getArticles(feed) {
loadingCard = new UI.Card({status: blackStatusBar});
loadingCard.title(" ");
loadingCard.subtitle(" ");
if (Feature.round()) {
loadingCard.body("Loading..."); //This will get centered automatically on the Round.
}
else {
loadingCard.body(" Loading...");
}
loadingCard.show();
loadingCardVisible = true;
Settings.data('savedFeed', feed);
articleList = [];
jsonUrl = "https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent(feed.url);
var rss2jsonApiKey = "";
if (typeof Settings.option().rss2jsonApiKey !== 'undefined') {
rss2jsonApiKey = Settings.option().rss2jsonApiKey;
jsonUrl = jsonUrl + "&api_key=" + rss2jsonApiKey + "&count=100";
}
ajax({ url: jsonUrl }, function(json) {
json = JSON.parse(json);
if (json.status === "ok") {
items = json.items;
items.forEach(function(item) {
article = {
title: formatText(item.title),
author: formatText(item.author),
pages: makePages(item.content)
};
articleList.push(article);
if (articleList.length === json.items.length) {
if (loadingCardVisible) {
selectArticle(articleList, feed.title);
}
loadingCard.hide();
return;
}
});
}
else {
if (rss2jsonApiKey === "") {
problemCard.body("Cannot retrieve articles. Try generating an api key at rss2json.com, and adding it in Settings.");
}
else {
problemCard.body("Cannot retrieve articles. The rss2json api isn't working.");
}
problemCard.show();
}
});
/* Handle case where user returned to menu while article was loading. */
loadingCard.on('hide', function() {
loadingCardVisible = false;
});
}
function makePages(content) {
paragraphs = content.split(/<\/p>|<br>|[\r\n\t\f\v]+/gim);
for (paragraphNum = 0; paragraphNum < paragraphs.length; paragraphNum++) {
paragraphs[paragraphNum] = formatText(paragraphs[paragraphNum]);
}
paragraphs = paragraphs.filter(Boolean); //remove empty strings from array
pages = [];
for (paragraphNum = 0; paragraphNum < paragraphs.length; paragraphNum++) {
if (paragraphNum === 0) {
// The first page is allowed be much longer. However, PebbleJS exhibits odd behavior once page length nears ~2,000 characters.
pages = splitTextCleanly(pages, 1800, 0, true, paragraphs[paragraphNum]);
}
else {
if (! Feature.round()) {
// Whatever number you choose for maxCharsPerPage will never be ideal for all pages, due to line wrapping and variable width characters.
pages = splitTextCleanly(pages, 80, 45, true, paragraphs[paragraphNum]);
}
else {
// Time round cannot display as many characters.
pages = splitTextCleanly(pages, 72, 0, true, paragraphs[paragraphNum]);
}
}
}
return pages;
}
function splitTextCleanly(pageArr, maxCharsPerPage, maxCharsExtension, includeContinuedIndicator, content) {
firstPart = content.substring(0, maxCharsPerPage);
laterPart = content.substring(maxCharsPerPage);
if (laterPart.length > maxCharsExtension) { //maxCharsExtension prevents short widow pages at the end of paragraphs, at cost of more font shrinkage.
//Prevent page split from occurring mid-word.
firstPart = firstPart.split(' ');
if (firstPart.length > 1) { //If firstPart contains a space.
afterLastSpace = firstPart[ firstPart.length - 1 ];
laterPart = afterLastSpace + laterPart;
firstPart.pop(); //remove afterLastSpace from firstPart
firstPart = firstPart.join(' ');
if (includeContinuedIndicator) {
firstPart = firstPart + String.fromCharCode(160) + "›"; // CharCode 160 is a nonbreaking space.
laterPart = "‹" + String.fromCharCode(160) + laterPart;
}
}
else {
//There are no spaces.
if (includeContinuedIndicator) {
firstPart = firstPart[0] + "…";
laterPart = "…" + laterPart;
}
else {
firstPart = firstPart[0];
}
}
pageArr.push(firstPart);
pageArr.concat(splitTextCleanly(pageArr, maxCharsPerPage, maxCharsExtension, includeContinuedIndicator, laterPart));
}
else {
pageArr.push(content);
}
return pageArr;
}
function formatText(text) {
if(!text) {
return "";
}
else {
text = text.replace(/<caption.*?>.*?<\/caption>/gi, "");
text = text.replace(/<figcaption.*?>.*?<\/figcaption>/gi, "");
text = text.replace(/<small.*?>.*?<\/small>/gi, "");
text = text.replace(/<aside.*?>.*?<\/aside>/gi, "");
text = text.replace(/<cite.*?>.*?<\/cite>/gi, "");
text = text.replace(/<.*? .*?class=".*?caption.*?".*?>.*?<\/.*?>/gi, "");
text = text.replace(/<.*? .*?class=".*?credit.*?".*?>.*?<\/.*?>/gi, "");
text = text.replace(/<.*? .*?data-id="injected-recirculation-link".*?>.*?<\/.*?>/gi, "");
text = text.replace(/<[^>]*>/gi, ''); //Remove html tags.
text = text.replace(/[ ]{2,}/gi, ' '); //Remove multiple spaces
text = text.replace(/ /gi, " ");
text = text.replace(/"/gi, '"');
text = text.replace(/&[lr]dquo;/gi, '"');
text = text.replace(/&[lr]squo;/gi, "'");
text = text.replace(/–/gi, "–");
text = text.replace(/—/gi, "—");
text = text.replace(/—/gi, String.fromCharCode(8203) + "—" + String.fromCharCode(8203)); //Insert zero width spaces before and after mdash's, to allow line breaks.
text = text.replace(/…/gi, "…");
text = text.replace(/>/gi, '>');
text = text.replace(/</gi, '<');
text = text.replace(/&/gi, "&");
text = text.replace(/^[<>[\]]*$/g,'');
text = text.replace(/^Advertisement$/g,'');
text = text.replace(/^Let's block ads! \(Why\?\)$/g,'');
text = text.replace(/^Read More[:]{0,1}[ ]{0,1}$/g,'');
text = text.trim();
return text;
}
}
function selectArticle(articleList, heading) {
/*articleDisplayTitles = [];
if (! Feature.round()) {
for (i = 0; i < articleList.length; i++) {
currTitle = [];
splitTextCleanly(currTitle, 17, 0, false, articleList[i].title);
articleDisplayTitles.push({
title: currTitle[0],
subtitle: currTitle.slice(1, currTitle.length).join(' ')
});
}
}
else {
articleDisplayTitles = articleList;
}*/
articleSelectMenu = new UI.Menu({
status: false,
sections: [{
title: heading,
items: articleList
}]
});
articleSelectMenu.show();
articleSelectMenuExists = true;
removeSavedInfo();
articleSelectMenu.on("select", function(e) {
displayArticlePage(articleList, e.itemIndex, 0);
});
articleSelectMenu.on("longSelect", function() {
if (Object.keys(lastSeenArticleCard).length !== 0) {
lastSeenArticleCard.show();
}
})
articleSelectMenu.on('show', function() {
setTimeout(function(){
//PebbleJS has a tendency to crash here, with certain articles. (The Atlantic is particularly troublesome.)
//I can't fix the crash because it's not my code, but I can wait a bit before removing savedInfo to make it less painful.
removeSavedInfo();
}, 950);
});
}
function displayArticlePage(articleList, articleNum, pageNum) {
article = articleList[articleNum];
articleCard = new UI.Card({status: blackStatusBar});
if (pageNum === 0) {
articleCard.scrollable(true);
articleCard.title(article.title);
if (article.author && article.pages[0].indexOf(article.author) === -1) { //If the article has an author which isn't repeated in the first page of the body
if (article.author.lastIndexOf("by ") === 0) { //If the article's author string starts with "by "
articleCard.subtitle(article.author);
}
else {
articleCard.subtitle("by " + article.author);
}
}
articleCard.body(article.pages[pageNum]);
if (pageNum < article.pages.length - 1) {
articleCard.action({
up: 'images/action_up.png',
select: "images/action_next.png",
down: 'images/action_down.png'
});
}
else {
articleCard.action({
up: 'images/action_up.png',
select: "images/action_check.png",
down: 'images/action_down.png'
});
}
}
else {
articleCard.scrollable(false);
articleCard.body(article.pages[pageNum]);
articleCard.action(false);
}
articleCard.show();
saveCurrPage(articleCard, articleList, articleNum, pageNum);
articleCard.on('show', function() {
saveCurrPage(articleCard, articleList, articleNum, pageNum);
});
articleCard.on('click', function(e) {
if (e.button === 'select' || e.button === 'down') {
if (pageNum < article.pages.length - 1) {
displayArticlePage(articleList, articleNum, pageNum + 1);
}
else {
if (articleSelectMenuExists) {
articleSelectMenu.show();
articleSelectMenu.selection(0, articleNum + 1); //Note: will leave wrong story selected if feed was updated since article was saved.
}
else {
getArticles(Settings.data('savedFeed'));
}
}
}
if (e.button === 'up' || e.button === 'back') {
if (pageNum > 0) {
displayArticlePage(articleList, articleNum, pageNum - 1);
}
else {
if (articleSelectMenuExists) {
articleSelectMenu.show();
articleSelectMenu.selection(0, articleNum); //Note: will leave wrong story selected if feed was updated since article was saved.
}
else {
getArticles(Settings.data('savedFeed'));
}
}
}
this.hide();
});
articleCard.on('longClick', 'select', function() {
if (articleSelectMenuExists) {
articleSelectMenu.show();
articleSelectMenu.selection(0, articleNum); //Note: will leave wrong story selected if feed was updated since article was saved.
}
else {
getArticles(Settings.data('savedFeed'));
}
this.hide();
});
//Hack to override back button so PebbleJS lets us handle it above.
articleCard.on('click', 'back', function(){});
}
/*-----------------------------------------------------------------------------*/
/*There are many cleverer ways I could have written this welcome sequence. However, this way was the easiest.*/
function welcomeScreen() {
switch(Settings.data('lastSeenWelcomeScreenNum')) {
default:
welcomeScreen1();
break;
case 2:
welcomeScreen2();
break;
case 3:
welcomeScreen3();
break;
case 4:
welcomeScreen4();
break;
case 5:
welcomeScreen5();
break;
case 6:
welcomeScreen6();
break;
case 7:
welcomeScreen7();
break;
}
}
function welcomeScreen1() {
Settings.data('lastSeenWelcomeScreenNum', 1);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.scrollable(true);
welcomeCard.action({
up: 'images/action_up.png',
select: "images/action_next.png",
down: 'images/action_down.png'
});
welcomeCard.body("Welcome to the Pebble RSS Reader!\n\nBefore you start reading articles, there's a few things you should know!");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('click', 'select', function() {
welcomeScreen2();
this.hide();
});
welcomeCard.on('longClick', function(e) {if (e.button === 'select') {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();}});
}
function welcomeScreen2() {
Settings.data('lastSeenWelcomeScreenNum', 2);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.body("Press the BACK or UP buttons to return to the previous page.\n\nPress the SELECT or DOWN buttons to go to the next page.");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('longClick', function(e) {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();this.hide();});
welcomeCard.on('click', function(e) {if (e.button === 'up' || e.button === 'back') {welcomeScreen1();this.hide();}if (e.button === 'select' || e.button === 'down') {welcomeScreen3();this.hide();}});
}
function welcomeScreen3(){
Settings.data('lastSeenWelcomeScreenNum', 3);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.body("Some paragraphs are too long to fit on a single page. If you see a symbol at the end of a page, it ›");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('longClick', function(e) {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();this.hide();});
welcomeCard.on('click', function(e) {if (e.button === 'up' || e.button === 'back') {welcomeScreen2();this.hide();}if (e.button === 'select' || e.button === 'down') {welcomeScreen4();this.hide();}});
}
function welcomeScreen4(){
Settings.data('lastSeenWelcomeScreenNum', 4);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.body("‹ means that the current paragraph will be continued on the next page.");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('longClick', function(e) {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();this.hide();});
welcomeCard.on('click', function(e) {if (e.button === 'up' || e.button === 'back') {welcomeScreen3();this.hide();}if (e.button === 'select' || e.button === 'down') {welcomeScreen5();this.hide();}});
}
function welcomeScreen5(){
Settings.data('lastSeenWelcomeScreenNum', 5);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.body("Hold down the BACK button to exit the app. We'll remember where you left off.");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('longClick', function(e) {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();this.hide();});
welcomeCard.on('click', function(e) {if (e.button === 'up' || e.button === 'back') {welcomeScreen4();this.hide();}if (e.button === 'select' || e.button === 'down') {welcomeScreen6();this.hide();}});
}
function welcomeScreen6(){
Settings.data('lastSeenWelcomeScreenNum', 6);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.body("Hold down the SELECT button to open the menu. Do it again to return to what you were reading.");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('longClick', function(e) {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();this.hide();});
welcomeCard.on('click', function(e) {if (e.button === 'up' || e.button === 'back') {welcomeScreen5();this.hide();}if (e.button === 'select' || e.button === 'down') {welcomeScreen7();this.hide();}});
}
function welcomeScreen7(){
Settings.data('lastSeenWelcomeScreenNum', 7);
welcomeCard = new UI.Card({status: blackStatusBar});
welcomeCard.body("That's all for now! Open the menu to choose a feed.");
welcomeCard.show();
welcomeCard.on('click', 'back', function(){}); //Disable back button.
welcomeCard.on('click', function(e) {if (e.button === 'up' || e.button === 'back') {welcomeScreen6();this.hide();}});
welcomeCard.on('longClick', function(e) {Settings.data('welcomeScreenCompleted', true);feedSelectMenu.show();this.hide();});
}