/
g2tt.js
1360 lines (1152 loc) · 44.7 KB
/
g2tt.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pref_IsCat = false;
if (typeof ($.cookie('g2tt_feed')) !== 'undefined') {
pref_Feed = $.cookie('g2tt_feed');
}
if (typeof ($.cookie('g2tt_isCat')) !== 'undefined') {
pref_IsCat = $.cookie('g2tt_isCat');
}
if (typeof ($.cookie('g2tt_viewMode')) !== 'undefined') {
pref_ViewMode = $.cookie('g2tt_viewMode');
}
/* Not used
if (typeof ($.cookie('g2tt_textType')) !== 'undefined') {
pref_textType = $.cookie('g2tt_textType');
}
*/
if (typeof ($.cookie('g2tt_orderBy')) !== 'undefined') {
pref_OrderBy = $.cookie('g2tt_orderBy');
}
if (typeof ($.cookie('g2tt_feedSort')) !== 'undefined') {
pref_FeedSort = $.cookie('g2tt_feedSort');
}
global_backCat = []; // Feed view always starts with all items
global_ids = []; // List of all article IDs currently displayed
global_parentId = '-4';
$(document).ready(function () {
$('html').unbind('click').click(function () {
$('#header-menu').removeClass('m-button-pressed');
$('#menuDown').removeClass('hidden');
$('#menuUp').addClass('hidden');
$('.g2tt-menu').hide();
});
$("#login").submit(function (event) {
if (request) {
request.abort();
}
var $loginForm = $(this);
var $inputs = $loginForm.find("input");
var values = {};
$inputs.each(function () {
values[this.name] = $(this).val();
});
var data = {
'op': 'login',
'user': values['Username'],
'password': values['Passwd'],
};
$inputs.prop("disabled", true);
var request = apiCall(data);
request.done(function (response, textStatus, jqXHR) {
//console.log(response['content']);
if (response['content'].error =='LOGIN_ERROR'){
window.alert("Username and/or Password were incorrect!");
}
if (response['content'].error =='API_DISABLED'){
window.alert("The API Setting is disabled. Login on the desktop version and enable API in the Preferences.");
}
else {
$.cookie('g2tt_sid', response['content'].session_id, {
expires: 7
});
$('.login').addClass('hidden');
$('#main').removeClass('hidden');
load();
}
});
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown) {
// log the error to the console
console.error(
"The following error occured: " +
textStatus, errorThrown);
});
// callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// reenable the inputs
$inputs.prop("disabled", false);
});
// prevent default posting of form
event.preventDefault();
});
//end of #login function
// Show more items
$('#load-more-items').unbind('click').click(function () {
if(pref_OrderBy == "date_reverse") {
var last = $('.entry-row').last().attr('id');
} else {
var last = $('.entry-row').length;
}
getHeadlines(last);
});
// Menu button
$('#header-menu').unbind('click').click(function (event) {
$(this).toggleClass('m-button-pressed');
$('#menuDown').toggleClass('hidden');
$('#menuUp').toggleClass('hidden');
//Adjust the placement of the menu based on the height of the Nav bar
//(for when category title is long)
$('.g2tt-menu').css({top: parseInt($('.nav-bar-container').height()) - 8 + "px"});
$('.g2tt-menu').toggle();
event.stopPropagation();
});
// Refresh button
$('#header-refresh').unbind('click').click(function () {
$(this).addClass('m-button-pressed');
if ($('#subscriptions').is(':hidden')) {
location.reload(true);
}
else {
refreshCats();
}
});
// View mode menu selection
$('#' + pref_ViewMode).addClass('g2tt-option-selected');
$('.showItem').unbind('click').click(function () {
pref_ViewMode = $(this).attr('id');
$.cookie('g2tt_viewMode', pref_ViewMode);
$('.showItem').removeClass('g2tt-option-selected');
$(this).addClass('g2tt-option-selected');
$('.feedsItem').removeClass('g2tt-option-selected');
$('#feeds-' + pref_ViewMode).addClass('g2tt-option-selected');
$('#entries').empty();
$('#subscriptions').attr('class', 'hidden show-' + pref_ViewMode);
getHeadlines();
});
// Order by menu selection
$('#' + pref_OrderBy).addClass('g2tt-option-selected');
$('.sortItem').unbind('click').click(function () {
pref_OrderBy = $(this).attr('id');
$.cookie('g2tt_orderBy', pref_OrderBy);
$('.sortItem').removeClass('g2tt-option-selected');
$(this).addClass('g2tt-option-selected');
$('#entries').empty();
getHeadlines();
});
// Back to Feeds
$('.back-to-feeds').unbind('click').click(function () {
refreshCats();
showFeeds();
});
// ADDED - Subscribe to new Feeds
$('#add-new-subscription').unbind('click').click(function () {
$( "#dialog-form" ).dialog( "open" );
getCategoriesForNewSubscribe();
});
// View mode feeds menu selection
$('#feeds-' + pref_ViewMode).addClass('g2tt-option-selected');
$('#subscriptions').addClass('show-' + pref_ViewMode);
$('.feedsItem').unbind('click').click(function () {
pref_ViewMode = $(this).attr('id').substring(6);
$.cookie('g2tt_viewMode', pref_ViewMode);
$('.feedsItem').removeClass('g2tt-option-selected');
$(this).addClass('g2tt-option-selected');
$('.showItem').removeClass('g2tt-option-selected');
$('#' + pref_ViewMode).addClass('g2tt-option-selected');
$('#subscriptions').attr('class', 'show-' + $(this).attr('id').substring(6));
});
// Sort feeds A-Z
if(pref_FeedSort == '1') {
$('.feedsSort').addClass('g2tt-option-selected');
}
$('.feedsSort').unbind('click').click(function () {
if(pref_FeedSort == '1') {
pref_FeedSort = '0';
} else {
pref_FeedSort = '1';
}
$.cookie('g2tt_feedSort', pref_FeedSort);
$(this).toggle('g2tt-option-selected');
});
// Back to Feeds from sub category
$('#sub-list-back').unbind('click').click(function () {
refreshCats();
getFeeds(global_backCat.pop());
$('#add-new-subscription').removeClass('hidden');
});
// Mark all as read
$('#show-more-row, #menu-mark-read').unbind('click').click(function () {
$('body').removeClass('loaded').addClass('loading');
$('.load-more-message').html('Marking as read...');
//remove those that need to be kept unread
keepUnread.removeFromArray(global_ids);
var data = new Object();
data.op = "updateArticle";
data.article_ids = global_ids.join(',');
data.mode = 0;
data.field = 2;
var request = apiCall(data);
request.done(function (response) {
$('#entries').empty();
getHeadlines();
});
});
// Logout
$('#menu-logout').unbind('click').click(function () {
var data = new Object();
data.op = "logout";
var request = apiCall(data);
request.done(function (response) {
$.removeCookie('g2tt_feed');
$.removeCookie('g2tt_isCat');
$.removeCookie('g2tt_viewMode');
// Not used $.removeCookie('g2tt_textType');
$.removeCookie('g2tt_orderBy');
$.removeCookie('g2tt_sid');
location.reload(true);
});
});
// Search
// Show search
$('#menu-search').unbind('click').click(function () {
$('.search-box').removeClass('hidden');
$('#search-input').focus();
});
// Clear and hide search
$('#search-cancel').unbind('click').click(function () {
$('#search-input').val('');
$('.search-box').addClass('hidden');
});
// Enter in search field searches
$('#search-input').keypress(function(e) {
if(e.which == 13) {
jQuery(this).blur();
jQuery('#search-submit').focus().click();
return false;
}
});
// Remove currently displayed headlines and search
$('#search-submit').unbind('click').click(function () {
$('#entries').empty();
getHeadlines();
return false;
});
load();
//Added for Subscribe to New Feeds
$('.ui-loader').remove();
var feedURL = $( "#feedURL" ),
//password = $( "#password" ),
allFields = $( [] ).add( feedURL ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" ).removeClass("hidden");
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
function checkLength( o, n, min, max ) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass( "ui-state-error" );
updateTips( "Length of " + n + " must be between " +
min + " and " + max + "." );
return false;
} else {
return true;
}
}
function firstToUpperCase( str ) {
return str.substr(0, 5).toLowerCase() + str.substr(5);
}
function checkRegexp( o, regexp, n ) {
var makeOvalidHttp = o.val().trim();
console.log(firstToUpperCase(makeOvalidHttp) );
if ( !( regexp.test( firstToUpperCase(makeOvalidHttp) ) ) ) {
o.addClass( "ui-state-error" );
updateTips( n );
return false;
} else {
return true;
}
}
$( "#dialog-form" ).dialog({
autoOpen: false,
//height: 300,
dialogClass: "dialog-nav-bar",
draggable: false,
resizable: false,
//position: { my: "left top", at: "left top" } ,
position: [ 5,10] ,
width: 300,
modal: true,
buttons: {
"Subscribe": function() {
var bValid = true;
allFields.removeClass( "ui-state-error" );
tips.addClass( "hidden");
bValid = bValid && checkLength( feedURL, "URL", 5, 1000 );
bValid = bValid && checkRegexp( feedURL, /^(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/,
"URL must be a valid URL. Make sure the URL is correct and re-submit" );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
if ( bValid ) {
var catIDnum = $( "#catItems option:selected" ).val();
var feedURLTrimmed = firstToUpperCase(feedURL.val().trim());
var multipleFeedSelected = $( "#feedsAvail option:selected" ).val();
//console.log('When subscribe is chosen again ' + multipleFeedSelected);
if (multipleFeedSelected == null) {
$('#feedURL').val(feedURLTrimmed);
var subscriberesult = subscribe( feedURLTrimmed, catIDnum);
// console.log(subscriberesult);
} else {
$('#feedURL').val(multipleFeedSelected);
var subscriberesult = subscribe( multipleFeedSelected, catIDnum);
//console.log(subscriberesult);
}
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
//Added for Subscribe to New Feeds
// Hotkeys
$(document).bind('keydown', 'j', expandNextEntry);
$(document).bind('keydown', 'k', expandPreviousEntry);
$(document).bind('keydown', 'n', jumpNextEntry);
$(document).bind('keydown', 'p', jumpPreviousEntry);
$(document).bind('keydown', 'o', toggleCurrentEntryAsExpanded);
$(document).bind('keydown', 'm', toggleCurrentEntryAsRead);
});
function refreshCats() {
var data = new Object();
data.op = "getCounters";
data.output_mode = "fc";
var request = apiCall(data);
request.done(function (response) {
var counters = response['content'];
var cats = [];
var feeds = [];
for (var i = 0; i < counters.length; i++) {
if (counters[i]['kind'] == 'cat') {
cats[counters[i]['id']] = (counters[i]);
} else {
feeds[counters[i]['id']] = (counters[i]);
}
}
$('.sub-row').each(function (i,j) {
var id = $(this).attr('id').substring(10);
var is_cat = ($(this).hasClass('open-sub-folder') || $(this).hasClass('closed-sub-folder'));
if (id == "-4" || id == "-1") {
$(this).find('.item-count-value').html(feeds['global-unread']['counter']);
if (feeds['global-unread']['counter'] == '0') {
$(this).addClass('no-unread-sub-row').removeClass('unread-sub');
$('#subscriptions').removeClass('show-unread').addClass('show-all');
} else {
$(this).removeClass('no-unread-sub-row').addClass('unread-sub');
if (pref_ViewMode == 'unread' && $('#subscriptions').hasClass('show-all')) {
$('#subscriptions').removeClass('show-all').addClass('show-unread');
}
}
} else if (is_cat) {
$(this).find('.item-count-value').html(cats[id]['counter']);
if (cats[id]['counter'] == '0') {
$(this).addClass('no-unread-sub-row').removeClass('unread-sub');
} else {
$(this).removeClass('no-unread-sub-row').addClass('unread-sub');
}
} else {
$(this).find('.item-count-value').html(feeds[id]['counter']);
if (feeds[id]['counter'] == '0') {
$(this).addClass('no-unread-sub-row').removeClass('unread-sub');
} else {
$(this).removeClass('no-unread-sub-row').addClass('unread-sub');
}
}
});
showEmpty();
$('#header-refresh').removeClass('m-button-pressed');
});
}
function showEmpty() {
var visible = $('#sub-' + global_parentId).children(':visible');
if (visible.length == 0) {
$('#subscriptions').removeClass('show-unread').addClass('show-all');
}
}
function showFeeds() {
$('#feed').addClass('hidden');
$('#subscriptions').removeClass('hidden');
$('.back-to-feeds').addClass('hidden');
$('.articlesMenu').addClass('hidden');
$('.feedsMenu').removeClass('hidden');
//added to show + for adding new subscriptions
$('#add-new-subscription').removeClass('hidden');
if (global_parentId != '-4') {
$('#sub-list-back').removeClass('hidden');
//added to show + for hiding new subscriptions
$('#add-new-subscription').addClass('hidden');
}
$('#nav-title').html('');
}
function showArticles() {
$('#feed').removeClass('hidden');
$('#subscriptions').addClass('hidden');
//added to hide + for add new subscriptions
$('#add-new-subscription').addClass('hidden');
$('.back-to-feeds').removeClass('hidden');
$('.articlesMenu').removeClass('hidden');
$('.feedsMenu').addClass('hidden');
$('#sub-list-back').addClass('hidden');
}
function apiCall(data, asynch) {
if (typeof (asynch) === 'undefined') asynch = true;
data.sid = $.cookie('g2tt_sid');
data = JSON.stringify(data);
var request = $.ajax({
url: global_ttrssUrl + "/api/",
type: "post",
dataType: "json",
data: data,
asynch: asynch,
});
return request;
}
function getHeadlines(since) {
$('body').addClass('loading');
$('.load-more-message').html('Loading...');
$('.entries-count').html('');
if (typeof (since) === 'undefined') since = 0;
//Anytime we get headlines, check if there is a search filter
var search = $('#search-input').val();
var data = new Object();
data.op = "getHeadlines";
data.feed_id = pref_Feed;
//data.limit = 25;
//uses config.js to get feed limit
data.limit = pref_Feed_limit;
data.show_excerpt = 1;
data.show_content = 1;
data.include_attachments = 0;
data.view_mode = pref_ViewMode;
data.is_cat = pref_IsCat;
data.include_nested = true;
data.order_by = pref_OrderBy;
if (pref_OrderBy == "date_reverse") {
data.since_id = since;
} else {
data.skip = since;
}
data.search = search;
var headlines = apiCall(data);
headlines.done(function (response, textStatus, jqXHR) {
if (response['status'] != 0) {
$.removeCookie('g2tt_sid');
getData();
return;
}
headlines = response['content'];
if (headlines.length != data.limit) {
$('#load-more-items').hide();
} else {
$('#load-more-items').show();
}
$.each(headlines, function (index, headline) {
global_ids.push(headline.id);
var email_subject = headline.title;
var email_body = '<br><h4>Sent to you via tt-rss</h4><h2><a href="' + headline.link + '">' + headline.title + '</a></h2>' + headline.content;
var $content = $(headline.content);
var alt = null;
if($content.length == 1 && $content.is("img") && (alt = ($content.attr("title") || $content.attr("alt")))){
$content = $("<div>" + $content[0].outerHTML + "<div>" + alt + "</div></div>");
} else {
var $container = $("<div></div>");
$container.append($content);
$content = $container;
}
var date = new Date(headline.updated * 1000);
var entry = "<div id='" + headline.id + "' class='entry-row whisper" + ((!headline.unread) ? " read" : "") + "'> \
<div class='entry-container'> \
<div class='entry-top-bar'> \
<span class='link entry-next'> \
<span class='entry-next-fa-icon'><i class='fa fa-arrow-down'></i></span> \
<span class='entry-next-text'>Next item</span> \
</span> \
<span class='link entry-collapse'> \
<span class='entry-collapse-fa-icon'><i class='fa fa-bars'></i></span> \
<span class='entry-collapse-text'>Collapse</span> \
</span> \
</div> \
<div class='entry-header'> \
<div class='entry-icons'> \
<i class='favStarDiv fa fa-star-o fa-2x starBorder'> </i> \
<i class='favStar fa fa-star fa-2x "+ ((headline.marked) ? "starActive" : "starNotActive") + "'></i> \
</div> \
<div class='entry-header-body'> \
<div class='text'> \
<span class='item-title-collapsed'>" + headline.title + "</span> \
<a href='" + headline.link + "' \
class='item-title item-title-link' target='_blank'>" + headline.title + "</a> \
<span class='item-source-title'> - " + headline.feed_title + "</span> \
<div class='item-snippet'>" + ((headline.excerpt && headline.excerpt != '…')? headline.excerpt : $(headline.content).text().substr(0, 100) + '…') + "</div> \
</div> \
<div class='entry-sub-header'>by " + headline.author + " on " + date.toLocaleString() + "</div> \
</div> \
</div> \
<div class='entry'> \
<div id='entry-contents' class='entry whisper'> \
<div class='entry-annotations'></div> \
<div class='entry-contents-inner'>" + $content[0].outerHTML + "</div> \
</div> \
<div class='entry-footer'> \
<div class='entry-actions'> \
<div class='entry-actions-primary'> \
<i class='fa fa-book read-state link unselectable' title='Toggle read'> Mark unread</i> \
<span class='link unselectable' title='Sent by mail'> \
<i class='fa fa-envelope-o' style='vertical-align:top;'></i> \
<a class='link unselectable' href='mailto:?subject=" + encodeURIComponent(email_subject) + "&body=" + encodeURIComponent(email_body) + "'>E-Mail</a> \
</span> \
<wbr /> \
</div> \
</div> \
</div> \
<div class='action-area-container'></div> \
</div> \
</div> \
</div>";
$('#entries').append(entry);
});
// Expand an entry
$('.entry-header-body').unbind('click').click(function () {
expandEntry($(this).closest('.entry-row'));
});
// Collapse an entry
$('.entry-top-bar').unbind('click').click(function () {
collapseEntry($(this).closest('.entry-row'));
});
// Next entry
$('.entry-next').unbind('click').click(function (event) {
expandEntry($(this).closest('.entry-row').next());
event.stopPropagation();
});
// Toggle read
$('.read-state').unbind('click').click(function () {
toggleEntryAsRead($(this).closest('.entry-row'));
});
/*
// Mark (star) entry
$('.star').unbind('click').click(function () {
var data = new Object();
data.op = "updateArticle";
data.article_ids = $(this).closest('.entry-row').attr('id');
data.mode = 2;
data.field = 0;
var response = apiCall(data);
$(this).toggleClass('item-star').toggleClass('item-star-active');
});
*/
// Mark NewFont (star) entry
$('.favStarDiv').unbind('click').click(function () {
var data = new Object();
data.op = "updateArticle";
data.article_ids = $(this).closest('.entry-row').attr('id');
data.mode = 2;
data.field = 0;
var response = apiCall(data);
$(this).next().toggleClass('starNotActive').toggleClass('starActive');
//console.log(newstar);
});
// Done loading
$('body').removeClass('loading').addClass('loaded');
$('.load-more-message').html('Mark these items as read');
$('.entries-count').html('Showing ' + $('.entry-row').length + ' items');
keepUnread.clean(global_ids);
});
}
function getTopCategories() {
$('#nav-title').html('');
$('#sub-list-back').addClass('hidden');
if ($('#sub--4').length != 0) {
$('#subscriptions-list').children().addClass('hidden');
$('#sub--4').removeClass('hidden');
$('.closed-sub-folder').unbind('click').click(function () {
global_backCat.push("-4");
$('#subscriptions-list').children().addClass('hidden');
getFeeds($(this).attr('id').substring(10), $(this).find('.sub-item').html(), $(this).find('.item-count-value').html());
});
} else {
$('body').addClass('loading').addClass('sub-tree');
$('#loading-area-container').removeClass('hidden');
$('#subscriptions-list').append("<div id='sub--4'></div>");
var data = new Object();
data.op = "getUnread";
var request = apiCall(data);
request.done(function (response, textStatus, jqXHR) {
unread = response['content'].unread;
var entry = "<div class='row whisper sub-row open-sub-folder" + ((unread > 0) ? " unread-sub" : " no-unread-sub-row") + "' id='tree-item--4'> \
<div class='icon-cell'> \
<i class='fa fa-folder-open fa-lg'></i> </div> \
<div class='text sub-item'>All articles</div> \
<div class='item-count larger whisper'> \
<span class='item-count-value' id='tree-item--4-unread-count'>" + unread + "</span> \
</div> \
</div>";
$('#sub--4').prepend(entry);
$('#tree-item--4').unbind('click').click(function () {
$.cookie('g2tt_feed', $(this).attr('id').substring(10));
$.cookie('g2tt_isCat', false);
pref_Feed = $.cookie('g2tt_feed');
pref_IsCat = $.cookie('g2tt_isCat');
getData();
});
});
var data = new Object();
data.op = "getCategories";
data.enable_nested = true;
var cats = apiCall(data);
cats.done(function (response, textStatus, jqXHR) {
cats = response['content'];
cats.sort(function (a, b) {
var db_order = ((a.order_id < b.order_id) ? -1 : ((a.order_id > b.order_id) ? 1 : 0));
var alpha_order = ((a.title < b.title) ? -1 : ((a.title > b.title) ? 1 : 0));
if (pref_FeedSort == '1') {
return alpha_order;
} else {
return db_order;
}
});
$.each(cats, function (index, cat) {
var entry = "<div class='row whisper sub-row closed-sub-folder" + ((cat.unread > 0) ? " unread-sub" : " no-unread-sub-row") + " nested-sub' id='tree-item-" + cat.id + "'> \
<div class='icon-cell'> \
<i class='fa fa-folder fa-lg'></i></div> \
<div class='text sub-item'>" + cat.title + "</div> \
<div class='item-count larger whisper'> \
<span class='item-count-value' id='tree-item-" + cat.id + "-unread-count'>" + cat.unread + "</span> \
</div> \
</div>";
$('#sub--4').append(entry);
});
$('.closed-sub-folder').unbind('click').click(function () {
global_backCat.push("-4");
$('#subscriptions-list').children().addClass('hidden');
getFeeds($(this).attr('id').substring(10), $(this).find('.sub-item').html(), $(this).find('.item-count-value').html());
});
// Done loading
$('body').removeClass('loading').addClass('loaded');
$('#loading-area-container').addClass('hidden');
});
}
}
function getFeeds(parent_id, parent_title, parent_unread) {
global_parentId = parent_id;
if (parent_id === '-4') {
getTopCategories();
return;
}
$('#nav-title').html('');
$('#sub-list-back').removeClass('hidden');
//added to show + for adding new subscriptions
$('#add-new-subscription').addClass('hidden');
if ($('#sub-' + parent_id).length != 0) {
$('#subscriptions-list').children().addClass('hidden');
$('#sub-' + parent_id).removeClass('hidden');
$('.closed-sub-folder').unbind('click').click(function () {
global_backCat.push(parent_id);
$('#subscriptions-list').children().addClass('hidden');
getFeeds($(this).attr('id').substring(10), $(this).find('.sub-item').html(), $(this).find('.item-count-value').html());
});
} else {
$('body').addClass('loading').addClass('sub-tree');
$('#loading-area-container').removeClass('hidden');
var data = new Object();
data.op = "getFeeds";
data.cat_id = parent_id;
data.include_nested = true;
var feeds = apiCall(data);
feeds.done(function (response, textStatus, jqXHR) {
feeds = response['content'];
feeds.sort(function (a, b) {
var alpha_order = ((a.title < b.title) ? -1 : ((a.title > b.title) ? 1 : 0));
if (pref_FeedSort == '1') {
return alpha_order;
} else {
return ((a.cat_id < b.cat_id) ? -1 : ((a.cat_id > b.cat_id) ? 1 : 0));
}
});
$('#subscriptions-list').append("<div id='sub-" + parent_id + "'></div>");
var entry = "<div class='row whisper sub-row open-sub-folder" + ((parent_unread > 0) ? " unread-sub" : " no-unread-sub-row") + "' id='tree-item-" + parent_id + "'> \
<div class='icon-cell'> \
<i class='fa fa-folder-open fa-lg'></i> </div> \
<div class='text sub-item'>" + parent_title + "</div> \
<div class='item-count larger whisper'> \
<span class='item-count-value' id='tree-item-" + parent_id + "-unread-count'>" + parent_unread + "</span> \
</div> \
</div>";
$('#sub-' + parent_id).prepend(entry);
$.each(feeds, function (index, feed) {
var entry = "<div class='row whisper sub-row" + ((feed.unread > 0) ? " unread-sub" : " no-unread-sub-row") + "" + ((feed.is_cat) ? " closed-sub-folder" : " sub") + " nested-sub' id='tree-item-" + feed.id + "'> \
<div class='icon-cell'> \
<i class='fa fa-rss-square'></i> </div> \
<div class='text sub-item'>" + feed.title + "</div> \
<div class='item-count larger whisper'> \
<span class='item-count-value' id='tree-item-" + feed.id + "-unread-count'>" + feed.unread + "</span> \
</div> \
</div>";
$('#sub-' + parent_id).append(entry);
});
$('.closed-sub-folder').unbind('click').click(function () {
global_backCat.push(parent_id);
$('#subscriptions-list').children().addClass('hidden');
getFeeds($(this).attr('id').substring(10), $(this).find('.sub-item').html(), $(this).find('.item-count-value').html());
});
$('.open-sub-folder[id!="tree-item--4"]').unbind('click').click(function () {
$.cookie('g2tt_feed', $(this).attr('id').substring(10));
$.cookie('g2tt_isCat', true);
pref_Feed = $.cookie('g2tt_feed');
pref_IsCat = $.cookie('g2tt_isCat');
getData();
});
$('.sub').unbind('click').click(function () {
$.cookie('g2tt_feed', $(this).attr('id').substring(10));
$.cookie('g2tt_isCat', false);
pref_Feed = $.cookie('g2tt_feed');
pref_IsCat = $.cookie('g2tt_isCat');
getData();
});
// Done loading
$('body').removeClass('loading').addClass('loaded');
$('#loading-area-container').addClass('hidden');
});
}
}
function getTitle() {
var data = new Object();
if (pref_IsCat == "true") {
data.op = "getCategories";
} else {
data.op = "getFeeds";
data.cat_id = "-4";
}
var request = apiCall(data);
request.done(function (response, textStatus, jqXHR) {
if (response['status'] != 0) {
$.removeCookie('g2tt_sid');
getData();
return;
}
items = response['content'];
$.each(items, function (index, item) {
if (item.id == pref_Feed) {
$('#nav-title').html(item.title);
return;
}
});
});
}
function load() {
if (typeof ($.cookie('g2tt_sid')) === 'undefined') {
$('#main').addClass('hidden');
$('.login').removeClass('hidden');
} else if (pref_StartInCat == '1') {
showFeeds();
getTopCategories();
} else {
getTitle();
getHeadlines();
getTopCategories();
}
}
function getData() {
showArticles();
$('body').removeClass('loaded').addClass('loading');
$('.load-more-message').html('Marking as read...');
$('#entries').empty();
global_ids = [];
getTitle();
getHeadlines();
}
var keepUnread = new function() {
var COOKIE_NAME = 'g2tt_keepUnread_ids';
this.keepUnreadIdMap = undefined;
this.hasId = function (ids, articleId) {
return true == getIdMap[articleId];
};
this.removeId = function (articleId) {
delete getIdMap()[articleId];
};
this.addId = function (articleId) {
getIdMap()[articleId] = true;
this.save();
};
this.clean = function (ids) {
//check that global_keepUnread_ids does not contain items which are no longer in global_ids
var keepUnreadIds = getIdMap();
if (ids.length > 0) {
for (var id in keepUnreadIds) {
id = id|0;//id must be numeric
if ($.inArray(id, ids) < 0) {
this.removeId(id);
}
}
}
this.save();
};
var getIdMap = function () {
if (undefined == this.keepUnreadIdMap) {
//attempt to load from cookie
this.keepUnreadIdMap = [];
var savedKeepUnread_ids;
if (typeof ($.cookie(COOKIE_NAME)) !== 'undefined') {
savedKeepUnread_ids = $.cookie(COOKIE_NAME);
}
if (savedKeepUnread_ids && savedKeepUnread_ids.length > 0) {
var idList = savedKeepUnread_ids.split(',');
for (var i=0; i < idList.length; i++) {
this.keepUnreadIdMap[idList[i]] = true;
}
}
}
return this.keepUnreadIdMap;
};
/*given array of ids, remove all that need to be kept unread*/
this.removeFromArray = function (ids) {
var keepUnreadIds = getIdMap();
for (var id in keepUnreadIds) {
id = id|0;//id must be numeric
var index = $.inArray(id, ids);
if ( index >= 0) {
ids.splice(index, 1);
}
}
};
this.save = function () {
var strVal = '';
var keepIdMap = getIdMap();
for (articleId in keepIdMap) {
if (strVal.length > 0) {
strVal += ',';
}
strVal += articleId;
}
$.cookie(COOKIE_NAME, strVal);
};
}
//ADDED for subscribing to new feeds
function subscribe( feedurl, categoryID) {
var subscribeResult = "";
var data = new Object();
data.op = "subscribeToFeed";
data.feed_url = feedurl;
data.category_id = categoryID;
//data.sid = session_id;
$('#indicator').removeClass('hidden');
var request = apiCall(data);
// return request;
//var request = apiCall(data);
request.done(function (response, textStatus, jqXHR) {
var content = response['content'];
var message = response['content'].status.message;
var status = response['content'].status;
var statusCode = response['content'].status.code;
//var feeds = [];
var feeds = response['content'].status.feeds;
var feedUrls = [];
var feedUrlsTitles = [];
for (var key in feeds) {
if (feeds.hasOwnProperty(key)) {
feedUrls.push(key);
feedUrlsTitles.push(feeds[key]);
}
}
//console.log(statusCode);
switch(statusCode) {