This repository was archived by the owner on May 10, 2026. It is now read-only.
forked from martinkadlec0/Smart-RSS
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy patharticleList.js
More file actions
669 lines (586 loc) · 24.6 KB
/
Copy patharticleList.js
File metadata and controls
669 lines (586 loc) · 24.6 KB
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
/**
* @module App
* @submodule views/articleList
*/
define([
'backbone', 'collections/Groups', 'models/Group', 'views/GroupView',
'views/ItemView', 'mixins/selectable', 'modules/Locale'
],
function (BB, Groups, Group, GroupView, ItemView, selectable, Locale) {
const groups = new Groups();
/**
* List of articles
* @class ArticleListView
* @constructor
* @extends Backbone.View
*/
let ArticleListView = BB.View.extend({
/**
* Tag name of article list element
* @property tagName
* @default 'div'
* @type String
*/
tagName: 'div',
/**
* ID of article list
* @property id
* @default 'article-list'
* @type String
*/
id: 'article-list',
/**
* Class of article views
* @property itemClass
* @default 'item'
* @type string
*/
itemClass: 'articles-list-item',
/**
* Unordered list of all article views
* @property views
* @default []
* @type Array
*/
views: [],
/**
* Data received from feedList about current selection (feed ids, name of special, filter, unreadOnly)
* @property currentData
* @default { feeds: [], name: 'all-feeds', filter: { trashed: false}, unreadOnly: false }
* @type Object
*/
currentData: {
feeds: [],
name: 'all-feeds',
filter: {trashed: false},
unreadOnly: false
},
/**
* Flag to prevent focusing more items in one tick
* @property noFocus
* @default false
* @type Boolean
*/
noFocus: false,
currentRenderId: 0,
events: {
// 'dragstart .articles-list-item': 'handleDragStart',
'mousedown .articles-list-item': 'handleMouseDown',
'click .articles-list-item': 'handleClick',
'mouseup .articles-list-item': 'handleMouseUp',
'dblclick .articles-list-item': 'handleItemDblClick',
'mousedown .item-pin,.item-pinned': 'handleClickPin'
},
/**
* Opens articles url in new tab
* @method handleItemDblClick
* @triggered on double click on article
*/
handleItemDblClick: function () {
app.actions.execute('articles:oneFullArticle');
},
handleMouseDown(event) {
if (event.button === 1) {
const linkElement = event.target.closest('a');
if (typeof browser !== 'undefined') {
this.prefetcher.href = linkElement.href;
}
}
},
/**
* Selects article
* @method handleClick
* @triggered on click on article
* @param event {MouseEvent}
*/
handleClick: function (event) {
this.handleSelectableMouseDown(event);
},
/**
* Changes pin state
* @method handleClickPin
* @triggered on click on pin button
* @param event {MouseEvent}
*/
handleClickPin: function (event) {
event.currentTarget.parentNode.view.handleClickPin(event);
},
/**
* Calls necessary select methods
* @method handleMouseUp
* @triggered on mouse up on article
* @param event {MouseEvent}
*/
handleMouseUp: function (event) {
event.currentTarget.view.handleMouseUp(event);
this.handleSelectableMouseUp(event);
},
/**
* Called when new instance is created
* @method initialize
*/
initialize: function () {
if (typeof browser !== 'undefined') {
this.prefetcher = document.createElement('link');
this.prefetcher.rel = 'preload';
this.prefetcher.setAttribute('as', 'fetch');
this.prefetcher.setAttribute('crossorigin', 'crossorigin');
document.head.appendChild(this.prefetcher);
}
bg.items.on('reset', this.addItems, this);
bg.items.on('add', this.addItem, this);
bg.items.on('sort', this.handleSort, this);
bg.items.on('search', this.handleSearch, this);
bg.sources.on('destroy', this.handleSourcesDestroy, this);
bg.sources.on('clear-events', this.handleClearEvents, this);
bg.settings.on('change', this.onSettingsChange, this);
groups.on('add', this.addGroup, this);
this.on('attach', this.handleAttached, this);
this.on('pick', this.handlePick, this);
},
onSettingsChange: function(){
this.unreadOnly = bg.getBoolean('defaultToUnreadOnly');
this.handleNewSelected(this.currentData);
},
/**
* Sends msg to show selected article
* @method handlePick
* @triggered when one article is selected
* @param view {views/ItemView}
*/
handlePick: function (view) {
if (!view.model.collection) {
// This shouldn't usually happen
// It might happen when source is deleted and created in the same tick
return;
}
app.trigger('select:' + this.el.id, {action: 'new-select', value: view.model.id});
if (view.model.get('unread') && bg.getBoolean('readOnVisit')) {
view.model.save({
visited: true,
unread: false
});
} else if (!view.model.get('visited')) {
view.model.save('visited', true);
}
},
/**
* Sets comm event listeners
* @method handleAttached
* @triggered when article list is attached to DOM
*/
handleAttached: function () {
app.on('select:feed-list', function (data) {
this.el.scrollTop = 0;
this.unreadOnly = bg.getBoolean('defaultToUnreadOnly');
if (data.action === 'new-select') {
this.handleNewSelected(data);
}
}, this);
app.on('give-me-next', function () {
if (this.selectedItems[0] && this.selectedItems[0].model.get('unread') === true) {
this.selectedItems[0].model.save({unread: false});
}
this.selectNextSelectable({selectUnread: true});
app.actions.execute('content:focus');
}, this);
if (bg.sourceToFocus) {
setTimeout(function () {
app.trigger('focus-feed', bg.sourceToFocus);
bg.sourceToFocus = null;
}, 0);
return;
}
if (bg.getBoolean('selectAllFeeds') && bg.getBoolean('showAllFeeds')) {
this.loadAllFeeds();
}
},
/**
* Loads all untrashed feeds
* @method loadAllFeeds
* @chainable
*/
loadAllFeeds: function () {
setTimeout(() => {
const unread = bg.items.where({trashed: false, unread: true});
if (unread.length) {
this.addItems(unread);
} else {
this.addItems(bg.items.where({trashed: false}));
}
const event = new MouseEvent('mousedown', {
view: window,
bubbles: true,
cancelable: true
});
const cb = document.querySelector('.special');
cb.dispatchEvent(event);
}, 0);
return this;
},
/**
* Renders unrendered articles in view by calling handleScroll
* @method handleSearch
* @triggered when new items arr added or when source is destroyed
*/
handleSearch: function () {
if (document.querySelector('input[type="search"]').value.trim() !== '') {
app.actions.execute('articles:search');
}
},
/**
* Unbinds all listeners to bg process
* @method handleClearEvents
* @triggered when tab is closed/refreshed
* @param id {Number} id of the closed tab
*/
handleClearEvents: function (id) {
if (window === null || id === tabID) {
bg.items.off('reset', this.addItems, this);
bg.items.off('add', this.addItem, this);
bg.items.off('sort', this.handleSort, this);
bg.items.off('search', this.handleSearch, this);
bg.sources.off('destroy', this.handleSourcesDestroy, this);
bg.sources.off('clear-events', this.handleClearEvents, this);
}
},
/**
* Clears searchbox and sorts the list
* @method handleSort
* @triggered when sort setting is changed
*/
handleSort: function () {
document.querySelector('input[type="search"]').value = '';
this.handleNewSelected(this.currentData);
},
/**
* Selects new item when the last selected is deleted
* @method selectAfterDelete
* @param view {views/ItemView}
*/
selectAfterDelete: function (view) {
const children = Array.from(this.el.children);
const length = children.length;
if (children[length - 1].view === view) {
this.selectPrev({currentIsRemoved: true});
} else {
this.selectNextSelectable({currentIsRemoved: true});
}
},
/**
* Tests whether newly fetched item should be added to current list.
* (If the item's feed is selected)
* @method inCurrentData
* @return Boolean
* @param item {Item} bg.Item
*/
inCurrentData: function (item) {
const feeds = this.currentData.feeds;
if (!feeds.length) {
if (!this.currentData.filter) {
return true;
} else if (item.query(this.currentData.filter)) {
return true;
}
} else if (feeds.indexOf(item.get('sourceID')) >= 0) {
return true;
}
return false;
},
/**
* Adds new article item to the list
* @method addItem
* @param item {Item} bg.Item
*/
addItem: function (item) {
//Don't add newly fetched items to middle column, when they shouldn't be
if (!this.inCurrentData(item)) {
return false;
}
let after = null;
[
...document
.querySelectorAll('#article-list .articles-list-item, #article-list .date-group')
]
.some((itemEl) => {
if (bg.items.comparator(itemEl.view.model, item) === 1) {
after = itemEl;
return true;
}
});
const view = new ItemView({model: item}, this);
if (!after) {
view.render();
this.views.push(view);
this.el.insertAdjacentElement('beforeend', view.el);
if (this.selectedItems.length === 0 && bg.getBoolean('selectFirstArticle')) {
this.select(view);
}
} else {
// is this block even executed?
after.insertAdjacentElement('afterend', view.render().el);
const indexElement = after.view instanceof ItemView ? after : after.nextElementSibling;
const index = indexElement ? this.views.indexOf(indexElement.view) : -1;
this.views.splice(index, 0, view);
}
if (!bg.getBoolean('disableDateGroups') && bg.settings.get('sortBy') === 'date') {
const group = Group.getGroup(item.get('date'));
if (!groups.findWhere({title: group.title})) {
groups.add(new Group(group), {before: view.el});
}
}
},
/**
* Adds new date group to the list
* @method addGroup
* @param model {models/Group} group create by groups.create
* @param col {collections/Groups}
* @param opt {Object} options { before: insertBeforeItem }
*/
addGroup: function (model, col, opt) {
const before = opt.before;
const view = new GroupView({model: model}, groups);
before.insertAdjacentElement('beforebegin', view.render().el);
},
/**
* Removes everything from lists and adds new collection of articles
* @method setItemHeight
* @param items {Backbone.Collection} bg.Items
* @param multiple
*/
addItems: function (items, multiple = false) {
groups.reset();
/**
* Select removal
*/
this.selectedItems = [];
while (this.el.firstChild) {
this.el.removeChild(this.el.firstChild);
}
this.selectPivot = null;
const length = items.length;
if (length === 0) {
return;
}
const that = this;
const renderBlock = (renderId, startingPoint = 0) => {
if (that.currentRenderId !== renderId) {
return;
}
let internalCounter = 0;
while (internalCounter !== 100 && startingPoint + internalCounter !== length) {
const item = items[startingPoint + internalCounter];
if (!item) {
break;
}
item.multiple = multiple;
that.addItem(item, true);
internalCounter++;
}
if (startingPoint + internalCounter === length) {
if (document.querySelector('input[type="search"]').value !== '') {
app.actions.execute('articles:search');
}
return;
}
window.requestIdleCallback(renderBlock.bind(this, renderId, startingPoint + internalCounter));
};
this.currentRenderId = Date.now();
window.requestIdleCallback(renderBlock.bind(this, this.currentRenderId, 0));
},
/**
* Called every time when new feed is selected and before it is rendered
* @method clearOnSelect
*/
clearOnSelect: function () {
// if prev selected was trash, hide undelete buttons
if (this.currentData.name === 'trash') {
app.articles.toolbar.showItems('articles:update');
app.articles.toolbar.hideItems('articles:undelete');
document.querySelector('#context-undelete').hidden = true;
// document.querySelector('#context-undelete').classList.add('hidden');
}
this.currentData = {
feeds: [],
name: 'all-feeds',
filter: {trashed: false},
unreadOnly: false
};
},
/**
* Called every time when new feed is selected. Gets the right data from store.
* @method handleNewSelected
* @param data {Object} data object received from feed list
*/
handleNewSelected: function (data) {
this.clearOnSelect();
this.currentData = data;
const searchIn = data.filter ? bg.items.where(data.filter) : bg.items.where({trashed: false});
// if newly selected is trash
if (this.currentData.name === 'trash') {
app.articles.toolbar.hideItems('articles:update').showItems('articles:undelete');
document.querySelector('#context-undelete').hidden = false;
}
const items = searchIn.filter((item) => {
if (!item.get('unread') && this.unreadOnly) {
return false;
}
return data.name || data.feeds.includes(item.get('sourceID'));
}, this);
this.addItems(items, data.multiple);
},
/**
* If current feed is removed, select all feeds
* @triggered when any source is destroyed
* @method handleSourcesDestroy
* @param source {Source} Destroyed source
*/
handleSourcesDestroy: function (source) {
const data = this.currentData;
const index = data.feeds.indexOf(source.id);
if (index >= 0) {
data.feeds.splice(index, 1);
}
if (!data.feeds.length && !data.filter) {
this.clearOnSelect();
if (document.querySelector('.articles-list-item')) {
this.once('items-destroyed', () => {
this.loadAllFeeds();
}, this);
} else {
this.loadAllFeeds();
}
}
},
/**
* Moves item from trash back to its original source
* @method undeleteItem
* @param view {views/ItemView} Undeleted article view
*/
undeleteItem: function (view) {
view.model.save({
'trashed': false
});
this.destroyItem(view);
},
/**
* Moves item to trash
* @method removeItem
* @param view {views/ItemView} Removed article view
*/
removeItem: function (view) {
const askRmPinned = bg.settings.get('askRmPinned');
if (view.model.get('pinned') && askRmPinned === 'all') {
const confirmation = confirm(Locale.PIN_QUESTION_A + view.model.escape('title') + Locale.PIN_QUESTION_B);
if (!confirmation) {
return;
}
}
view.model.trash();
this.destroyItem(view);
this.trigger('items-destroyed');
},
/**
* Removes item from both source and trash leaving only info it has been already fetched and deleted
* @method removeItemCompletely
* @param view {views/ItemView} Removed article view
*/
removeItemCompletely: function (view) {
const askRmPinned = bg.settings.get('askRmPinned');
if (view.model.get('pinned') && askRmPinned && askRmPinned !== 'none') {
const confirmation = confirm(Locale.PIN_QUESTION_A + view.model.escape('title') + Locale.PIN_QUESTION_B);
if (!confirmation) {
return;
}
}
view.model.markAsDeleted();
},
/**
* Calls undeleteItem/removeItem/removeItemCompletely in a batch for several items
* @method destroyBatch
* @param arr {Array} List of views
* @param fn {Function} Function to be called on each view
*/
destroyBatch: function (arr, fn) {
for (let i = 0, j = arr.length; i < j; i++) {
fn.call(this, arr[i]);
}
},
/**
* List of views to be closed when nextFrame animation frame is called
* @property nextFrame
* @default null
* @type Object
*/
nextFrameStore: [],
/**
* RequestAnimationFrame return value for next destroy item call.
* @property nextFrame
* @default null
* @type Object
*/
nextFrame: null,
/**
* Removes article view (clearing events and all)
* @method destroyItem
* @param view {views/ItemView} Destroyed article view
*/
destroyItem: function (view) {
this.nextFrameStore.push(view);
if (!this.nextFrame) {
this.nextFrame = requestAnimationFrame(() => {
const lastView = this.nextFrameStore[this.nextFrameStore.length - 1];
this.selectAfterDelete(lastView);
for (let i = 0, j = this.nextFrameStore.length - 1; i < j; i++) {
this.destroyItemFrame(this.nextFrameStore[i]);
}
this.destroyItemFrame(lastView);
this.nextFrame = null;
this.nextFrameStore = [];
});
}
},
/**
* Called asynchronously from destroyItem. It does the real removing job.
* @method destroyItemFrame
* @param view {views/ItemView} Destroyed article view
*/
destroyItemFrame: function (view) {
// START: REMOVE DATE GROUP
const prev = view.el.previousElementSibling;
const next = view.el.nextElementSibling;
if (prev && prev.classList.contains('date-group')) {
if (!next || next.classList.contains('date-group')) {
groups.remove(prev.view.model);
}
}
// END: REMOVE DATE GROUP
view.clearEvents();
view.remove();
const selectedItemIndex = this.selectedItems.indexOf(view);
if (selectedItemIndex >= 0) {
this.selectedItems.splice(selectedItemIndex, 1);
}
const viewIndex = this.views.indexOf(view);
if (viewIndex >= 0) {
this.views.splice(viewIndex, 1);
}
},
/**
* Toggles unread state of selected items (with onlyToRead option)
* @method changeUnreadState
* @param options {Object} Options { onlyToRead: bool }
*/
changeUnreadState: function (options) {
options = options || {};
const unread = this.selectedItems.length && !options.onlyToRead ? !this.selectedItems[0].model.get('unread') : false;
this.selectedItems.forEach(function (item) {
if (!options.onlyToRead || item.model.get('unread') === true) {
item.model.save({unread: unread, visited: true});
}
}, this);
}
});
ArticleListView = ArticleListView.extend(selectable);
return new ArticleListView();
});