-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
195 lines (175 loc) · 6.6 KB
/
options.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
document.onclick = (event) => {
if(event) {
if(event.target.matches('#keywords-list .list-item .remove')) {
let container = event.target.parentNode.parentNode;
let keyword = container.querySelector('.list-value').textContent;
removeKeyword(keyword, container)
console.log(options);
}
if(event.target.matches('#channels-list .list-item .remove')) {
let container = event.target.parentNode.parentNode;
let channel = container.querySelector('.list-value').textContent;
removeChannel(channel, container)
console.log(options);
}
if(event.target.matches('#keywords-list .pin')) {
let container = event.target.parentNode;
let keyword = container.querySelector('.list-value').textContent;
togglePin(keyword, container)
console.log(options);
}
if(event.target.parentNode.matches('#keywords-list .pin')) {
let container = event.target.parentNode.parentNode;
let keyword = container.querySelector('.list-value').textContent;
togglePin(keyword, container)
console.log(options);
}
}
}
document.getElementById('new-keyword-form').onsubmit = (event) => {
event.preventDefault();
let keywordField = document.getElementById('new-keyword-field');
let newKeyword = keywordField.value;
keywordField.value = "";
options.filteredKeywords.push(newKeyword);
chrome.storage.sync.set({options});
addKeywordElement(newKeyword);
console.log(options);
}
// document.getElementById('new-channel-form').onsubmit = (event) => {
// event.preventDefault();
// let channelField = document.getElementById('new-channel-field');
// let newChannel = channelField.value;
// channelField.value = "";
// options.filteredChannels.push(newChannel);
// chrome.storage.sync.set({options});
// addChannelElement(newChannel);
// console.log(options);
// }
const options = {};
console.log('Options:');
console.log(options);
chrome.storage.sync.get('options', (data) => {
Object.assign(options, data.options);
if(options.filteredKeywords.length > 0) {
document.getElementById('no-keywords-list-content').classList.add('hide')
options.filteredKeywords.forEach(keyword => {
addKeywordElement(keyword);
});
}
let listItems = document.querySelectorAll('#keywords-list .list-item-container');
options.pinnedKeywords.forEach(keyword => {
listItems.forEach(item => {
let value = item.querySelector('.list-item .list-value').textContent;
if(value === keyword) {
pinItem(item);
}
});
});
if(options.filteredChannels.length > 0) {
document.getElementById('no-channels-list-content').classList.add('hide')
options.filteredChannels.forEach(channel => {
document.getElementById('channels-list').append(newListItem(channel, true));
});
}
})
/**
* remove keyword from user's filter options
* @param keyword keyword to remove
* @param container html list item container to be removed
*/
function removeKeyword(keyword, container) {
options.filteredKeywords = options.filteredKeywords.filter(e => e !== keyword);
options.pinnedKeywords = options.pinnedKeywords.filter(e => e !== keyword);
options.disabledPinnedKeywords = options.disabledPinnedKeywords.filter(e => e !== keyword);
chrome.storage.sync.set({options});
container.remove();
}
/**
* remove channel from user's filter options
* @param channel channel to remove
* @param container html list item container to be removed
*/
function removeChannel(channel, container) {
options.filteredChannels = options.filteredChannels.filter(e => e !== channel);
chrome.storage.sync.set({options});
container.remove();
}
/**
* pin/unpin a keyword from being pinned to the popup menu
* @param keyword keyword to toggle
* @param container html element to modify
*/
function togglePin(keyword, container) {
if(container.classList.contains('pinned')) {
options.pinnedKeywords = options.pinnedKeywords.filter(e => e !== keyword);
options.disabledPinnedKeywords = options.disabledPinnedKeywords.filter(e => e !== keyword);
chrome.storage.sync.set({options});
unpinItem(container);
} else {
options.pinnedKeywords.push(keyword);
chrome.storage.sync.set({options});
pinItem(container);
}
}
/**
* modify list items html element to show that the item has been pinned
* @param item html element
*/
function pinItem(item) {
item.classList.add('pinned');
item.querySelector('.pin img').setAttribute('src', 'rsc/images/pinned.svg');
}
/**
* modify list items html element to show that the item has been unpinned
* @param item html element
*/
function unpinItem(item) {
item.classList.remove('pinned');
item.querySelector('.pin img').setAttribute('src', 'rsc/images/pin.svg');
}
/**
* add keyword element to the keywords list
* @param keyword keyword to add
*/
function addKeywordElement(keyword) {
document.getElementById('keywords-list').append(newListItem(keyword, false));
}
/**
* add channel element to the channels list
* @param keyword channel to add
*/
function addChannelElement(channel) {
document.getElementById('channels-list').append(newListItem(channel, true));
}
/**
* create new list item container element to be added to the page
* @param name name of the keyword or channel
* @param isChannel whether or not the new element is for a new channel or new keyword
* @return new list item container element
*/
function newListItem(name, isChannel) {
let listItemContainer = document.createElement('div');
listItemContainer.classList.add('list-item-container');
let listItem = document.createElement('div');
listItem.classList.add('list-item');
let listValue = document.createElement('div');
listValue.classList.add('list-value');
listValue.innerHTML = name;
let remove = document.createElement('div');
remove.classList.add('remove');
remove.innerHTML = 'remove';
listItem.appendChild(listValue);
listItem.appendChild(remove);
listItemContainer.appendChild(listItem);
if(!isChannel) {
let pin = document.createElement('div');
pin.classList.add('pin');
let pinImg = document.createElement('img');
pinImg.setAttribute('src', 'rsc/images/pin.svg');
pinImg.setAttribute('height', '20px');
pin.appendChild(pinImg);
listItemContainer.appendChild(pin);
}
return listItemContainer;
}