-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.js
123 lines (90 loc) · 3.13 KB
/
items.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
// Track items with array
exports.toreadItems = JSON.parse(localStorage.getItem('toreadItems')) || []
// Save items to localstorage
exports.saveItems = () => {
localStorage.setItem('toreadItems', JSON.stringify(this.toreadItems))
}
// Toggle item as selected
exports.selectItem = (e) => {
$('.read-item').removeClass('is-active')
$(e.currentTarget).addClass('is-active')
}
// Select next/prev item
exports.changeItem = (direction) => {
// Get current active item
let activeItem = $('.read-item.is-active')
// Check direction and get next or previous read-item
let newItem = (direction === 'down') ? activeItem.next('.read-item') : activeItem.prev('.read-item')
// Only if item exists, make selection change
if(newItem.length) {
activeItem.removeClass('is-active')
newItem.addClass('is-active')
}
}
// Window function
// Delete item by index
window.deleteItem = (i = false) => {
// Set i to active item if not passed as argument
if (i === false) i = ($('.read-item.is-active').index() - 1)
// Remove item from DOM
$('.read-item').eq(i).remove()
// Remove from toreadItems array
this.toreadItems = this.toreadItems.filter((item, index) => {
return index !== i
})
// Update storage
this.saveItems()
//Select prev item or none if list empty
if (this.toreadItems.length) {
// If first item was deleted, sleect new first item in list, else previous item
let newIndex = (i === 0) ? 0 : i - 1
// Assign active class to new index
$('.read-item').eq(newIndex).addClass('is-active')
//Else show no items message
} else {
$('#no-items').show()
}
}
// Open item in default browswer
window.openInBrowser = () => {
// Only if item exists
if ( !this.toreadItems.length ) return
// Get selected item
let targetItem = $('.read-item.is-active')
// Open in browser
require('electron').shell.openExternal(targetItem.data('url'))
}
// Open item for reading
window.openItem = () => {
// Only if items have been added
if( !this.toreadItems.length ) return
// Get Selected item
let targetItem = $('.read-item.is-active')
// Get item's content url
let contentURL = encodeURIComponent(targetItem.data('url'))
// Get item index to pass to proxy window
let itemIndex = targetItem.index() - 1
// Reader window URL
let readerWinURL = `file://${__dirname}/reader.html?url=${contentURL}&itemIndex=${itemIndex}`
// Open item in a new proxy BroswerWindow
let readerWin = window.open(readerWinURL, targetItem.data('title'))
}
// Add new item
exports.addItem = (item) => {
// Hide 'no items' message
$('#no-items').hide()
// New item html
let itemHTML = `<a class="panel-block read-item" data-url="${item.url}" data-title="${item.title}">
<figure class= "image has-shadow is-64x64 thumb">
<img src="${item.screenshot}">
</figure>
<h2 class="title is-4 column">${item.title}</h2>
</a>`
// Append to read-list
$('#read-list').append(itemHTML)
// Attach select event handler
$('.read-item')
.off('click, dblclick')
.on('click', this.selectItem)
.on('dblclick', window.openItem)
}