-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
372 lines (286 loc) · 9.42 KB
/
index.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
const addActivityButton = () => {
const h1 = document.querySelector('h1')
const button = document.createElement('button')
const addCss = addCssAdder(button)
addCss('float', 'right')
addCss('fontSize', '16px')
addCss('margin', '5px 10px 0 0')
addCss('borderRadius', '5px')
button.textContent = 'Manage activities'
button.addEventListener('click', () => {
const loadMore = prompt('Do you want to load more of your activity?\nIf so, how many times do you want we press to "Show More" bottom button?\n(Cancel or leace blank to open the modal withouts load more activity)')
const number = getNumber(loadMore)
loadAndOpenModals(number)
})
h1.append(button)
}
const getNumber = (number) => {
const value = Number(number)
return Number.isNaN(value) ? 0 : value
}
const clickShowMore = () => {
const button = getShowMoreButton()
button && button.click()
}
const showMore = (times = 10, callback) => {
const counter = times
const loaderModal = createModal()
const updateLoaderModal = updateModalText(loaderModal)
const update = () => {
const finished = times <= 0
if (finished || noMoreActivitiesToLoad()) {
loaderModal.remove()
callback && callback()
} else {
if (isNotLoading()) {
clickShowMore()
times--
updateLoaderModal(`Loading more activities: ${counter - times}/${counter}`)
}
setTimeout(update, 50)
}
}
update()
}
const updateModalText = (modal) => (text) => {
const modalContent = modal.querySelector('[data-content]')
modalContent.textContent = text
}
const createModalToRemoveHistory = (showSession = true) => {
const modal = createModal()
const modalContent = modal.querySelector('[data-content]')
const createTableWithData = tableCreator(modalContent)
addFilters(modalContent, createTableWithData)
createTableWithData(getTitlesData(showSession))
}
const tableCreator = (modalContent) => (data) => {
const table = createTable(data)
const currentTable = modalContent.querySelector('table')
currentTable && currentTable.remove()
modalContent.append(table)
}
const addFilters = (modalContent, createTableWithData) => {
const input = createSearchInput()
const checkbox = createCheckboxSession()
const updateTable = tableUpdater(createTableWithData)
input.addEventListener('input', ({ target }) => {
updateTable({ searchText: target.value.trim() })
})
checkbox.addEventListener('change', ({ target }) => {
updateTable({ groupBySession: target.checked })
})
modalContent.append(input)
modalContent.append(checkbox)
}
const tableUpdater = (createTableWithData) => {
let lastOpts = { groupBySession: true }
return (opts) => {
lastOpts = { ...lastOpts, ...opts }
executeOnBackground(() => createTableWithData(createFilteredData(lastOpts)))
}
}
const createFilteredData = (opts) => {
const { searchText, groupBySession } = opts
const data = getTitlesData(groupBySession)
if (searchText) {
const rgx = new RegExp(`(${searchText})`, 'i')
return Object.keys(data)
.filter(str => rgx.test(str))
.reduce((acc, value) => {
acc[addBoldTagInText(value, rgx)] = data[value]
return acc
}, {})
}
return data
}
const createSearchInput = () => {
const input = document.createElement('input')
const addCss = addCssAdder(input)
addCss('width', '500px')
input.placeholder = 'Search for serie, shows or movies...'
return input
}
const createCheckboxSession = () => {
const label = document.createElement('label')
const checkbox = document.createElement('input')
const addCss = addCssAdder(label)
const addCssCheckbox = addCssAdder(checkbox)
addCss('cursor', 'pointer')
addCss('display', 'block')
addCss('margin', '5px 0')
addCssCheckbox('marginRight', '5px')
addCssCheckbox('height', '15px')
checkbox.type = 'checkbox'
checkbox.checked = true
label.textContent = 'Group shows and series by session'
label.prepend(checkbox)
return label
}
const getTitlesData = (showSession) =>
getLis().reduce((acc, li) => addLiReference(acc, getTitleFromLi(li, showSession), li), {})
const addLiReference = (obj, title, li) => {
if (obj[title]) {
obj[title].push(li)
} else {
obj[title] = [li]
}
return obj
}
const getUl = () => document.querySelector('.structural.retable.stdHeight')
const getLis = () => [...getUl().querySelectorAll('.retableRow:not(.retableRemoved)')]
const getTitleFromLi = (li, showSession) => getMainName(li.querySelector('a').textContent, showSession)
const sortByStrings = (a, b) => a.localeCompare(b)
const isNotLoading = () => getUl().nextElementSibling.childElementCount === 0
const isEscapeKey = ({ key }) => key === 'Escape'
const clickRemoveButton = (li) => li.querySelector('.deleteBtn').click()
const getLiLink = (li) => li.querySelector('.title > a').href
const getShowMoreButton = () => document.querySelector('.btn.btn-blue.btn-small')
const noMoreActivitiesToLoad = () => getShowMoreButton().disabled === true
const executeOnBackground = (fn) => setTimeout(fn, 0)
const addBoldTagInText = (str, rgx) => str.replace(rgx, '<b>$1</b>')
const loadAndOpenModals = (times) => showMore(times, createModalToRemoveHistory)
const getMainName = (str, showSession) => {
const [title, session] = str.split(':')
return showSession && session ? [title, session].join(':') : title
}
const createModal = () => {
const modal = insertModal()
setupModalLayout(modal)
setupModalBind(modal)
return modal
}
const insertModal = () => {
const body = document.querySelector('body')
body.insertAdjacentHTML('beforeend', modalHTML())
return document.querySelector('#superModal')
}
const setupModalLayout = (modal) => {
const addCss = addCssAdder(modal)
const addCssContent = addCssAdder(modal.querySelector('.modal-content'))
const addCssClose = addCssAdder(modal.querySelector('.close'))
addCss('position', 'fixed')
addCss('zIndex', '1')
addCss('left', '0')
addCss('top', '0')
addCss('width', '100%')
addCss('height', '100%')
addCss('overflow', 'auto')
addCss('backgroundColor', 'rgb(0, 0, 0)')
addCss('backgroundColor', 'rgba(0, 0, 0, 0.4)')
addCssContent('backgroundColor', '#fefefe')
addCssContent('margin', '15% auto')
addCssContent('padding', '20px')
addCssContent('border', '1px solid #888')
addCssContent('width', '60%')
addCssClose('color', '#aaa')
addCssClose('float', 'right')
addCssClose('fontSize', '28px')
addCssClose('fontWeight', 'bold')
addCssClose('cursor', 'pointer')
}
const setupModalBind = (modal) => {
const closeBtn = modal.querySelector('.close')
const keyUpAction = (event) => isEscapeKey(event) && removeModal()
const removeModal = () => {
document.removeEventListener('keyup', keyUpAction)
closeBtn.removeEventListener('click', removeModal)
modal.remove()
}
closeBtn.addEventListener('click', removeModal)
document.addEventListener('keyup', keyUpAction)
}
const addCssAdder = (el) => (prop, value) => {
el.style[prop] = value
}
const modalHTML = () =>
`<div id="superModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div data-content></div>
</div>
</div>`
const createTable = (data) => {
const table = document.createElement('table')
const titles = Object.keys(data).sort(sortByStrings)
table.append(createTableHeader(titles))
titles.map((title, index) => {
const backgroundColor = index % 2 ? 'transparent' : 'lightgray'
table.append(
createTableTr(data, title, backgroundColor)
)
})
addTableStyle(table)
return table
}
const createTableHeader = (titles) => {
const tr = document.createElement('tr')
tr.append(
addTdStyle(createElementWithHTML('th', `${titles.length} titles`))
)
tr.append(
addTdStyle(createElementWithHTML('th'))
)
addTrStyle(tr)
return tr
}
const createTableTr = (data, title, backgroundColor) => {
const lis = data[title]
const tr = document.createElement('tr')
tr.append(
addTdStyle(createTdTitleWithLink(title, lis))
)
tr.append(
addTdStyle(createRemoveButtonTd(title, lis))
)
addTrStyle(tr, backgroundColor)
return tr
}
const createTdTitleWithLink = (title, lis) => {
const link = getLiLink(lis[0])
const td = createElementWithHTML('td')
const a = createElementWithHTML('a', title)
a.href = link
a.target = '_blank'
td.append(a)
return td
}
const createElementWithHTML = (tag, html) => {
const element = document.createElement(tag)
html && (element.innerHTML = html)
return element
}
const createRemoveButtonTd = (title, lis) => {
const td = document.createElement('td')
const a = document.createElement('a')
const addCss = addCssAdder(a)
addCss('cursor', 'pointer')
a.textContent = `Remove (${lis.length})`
a.title = `Remove all activities of: ${title}`
td.append(a)
bindRemoveButtonTd(a, td, lis)
return td
}
const bindRemoveButtonTd = (a, td, lis) => {
const remover = () => {
lis.map(clickRemoveButton)
a.removeEventListener('click', remover)
a.remove()
td.textContent = `${lis.length} activities removed`
}
a.addEventListener('click', remover)
}
const addTableStyle = (table) => {
const addCss = addCssAdder(table)
addCss('width', '100%')
}
const addTrStyle = (tr, backgroundColor) => {
const addCss = addCssAdder(tr)
addCss('border', '1px solid black')
backgroundColor && addCss('backgroundColor', backgroundColor)
}
const addTdStyle = (td) => {
const addCss = addCssAdder(td)
addCss('padding', '5px')
return td
}
addActivityButton()