Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

refactor: extract utils

Christian Fei619cca9ac950
diff --git a/client/components/channels-list.js b/client/components/channels-list.js
index 4d4d360..22af758 100644
--- a/client/components/channels-list.js
+++ b/client/components/channels-list.js
@@ -1,13 +1,3 @@
-function handleClickAddListener ($el, handler) {
- if (!$el) return
- $el.addEventListener('click', handler)
- $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))
-}
-function handleClickRemoveListener ($el, handler) {
- if (!$el) return
- $el.removeEventListener('click', handler)
- $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))
-}
class ChannelsList extends HTMLElement {
constructor () {
super()
@@ -54,11 +44,11 @@ class ChannelsList extends HTMLElement {
}
registerEvents() {
const channels = this.querySelectorAll('.channel')
- channels.forEach(channel => handleClickAddListener(channel, this.channelClick.bind(this)))
+ channels.forEach(channel => window.utils.addClickListener(channel, this.channelClick.bind(this)))
}
unregisterEvents() {
const channels = this.querySelectorAll('.channel')
- channels.forEach(channel => handleClickRemoveListener(channel, this.channelClick.bind(this)))
+ channels.forEach(channel => window.utils.removeClickListener(channel, this.channelClick.bind(this)))
}
channelClick(event) {
if (!this.$searchInput) return console.log('missing search field, skipping channel event register')
diff --git a/client/components/empty-state.js b/client/components/empty-state.js
index 8cb95ee..ab89f09 100644
--- a/client/components/empty-state.js
+++ b/client/components/empty-state.js
@@ -1,18 +1,9 @@
-
class EmptyState extends HTMLElement {
constructor () {
super()
}
connectedCallback () {
this.render()
- this.registerEvents()
- }
- disconnectedCallback () {
- this.deregisterEvents()
- }
- registerEvents () {
- }
- deregisterEvents () {
}
render () {
/* html for empty app state */
diff --git a/client/components/video-element.js b/client/components/video-element.js
index 11fbf17..de42f38 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -1,13 +1,3 @@
-function handleClickAddListener ($el, handler) {
- if (!$el) return
- $el.addEventListener('click', handler)
- $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))
-}
-function handleClickRemoveListener ($el, handler) {
- if (!$el) return
- $el.removeEventListener('click', handler)
- $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))
-}
function tryFormatDate(date) {
try {
return new Date(date).toISOString().substring(0, 10)
@@ -78,24 +68,24 @@ class VideoElement extends HTMLElement {
this.registerEvents()
}
registerEvents () {
- handleClickAddListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))
- handleClickAddListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))
- handleClickAddListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))
- handleClickAddListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))
- handleClickAddListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))
- handleClickAddListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))
- handleClickAddListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))
- handleClickAddListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))
+ window.utils.addClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))
}
unregisterEvents () {
- handleClickRemoveListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))
- handleClickRemoveListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))
- handleClickRemoveListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))
- handleClickRemoveListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))
- handleClickRemoveListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))
- handleClickRemoveListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))
- handleClickRemoveListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))
- handleClickRemoveListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.action.download'), this.downloadVideoHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('img.download'), this.downloadVideoHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.action.delete'), this.deleteVideoHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.action.summarize'), this.summarizeVideoHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.action.show-summary'), this.showSummaryHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.action.ignore'), this.toggleIgnoreVideoHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.channel-name'), this.filterByChannelHandler.bind(this))
+ window.utils.removeClickListener(this.querySelector('.play.video-placeholder'), this.watchVideoHandler.bind(this))
this.querySelector('video') && this.unregisterVideoEvents(this.querySelector('video'))
}
watchVideoHandler (event) {
diff --git a/client/index.html b/client/index.html
index 9905ce2..0eb25de 100644
--- a/client/index.html
+++ b/client/index.html
@@ -93,6 +93,7 @@
<h1>404 Not Found</h1>
</template>
+ <script type="module" src="/lib/utils.js"></script>
<script type="module" src="/components/empty-state.js"></script>
<script type="module" src="/components/video-filters.js"></script>
<script type="module" src="/components/search-videos.js"></script>
diff --git a/client/lib/router.js b/client/lib/router.js
index 992bf89..7fa0991 100644
--- a/client/lib/router.js
+++ b/client/lib/router.js
@@ -1,15 +1,3 @@
-
-function handleRoute(route = window.location.pathname) {
- console.log('handling route', route)
- if (routes[route]) {
- document.querySelector('main').replaceChildren(routes[route].template.content.cloneNode(true))
- routes[route].initialize && routes[route].initialize()
- } else {
- document.querySelector('main').replaceChildren(routes['/404'].template.content.cloneNode(true))
- routes['/404'].initialize && routes['/404'].initialize()
- }
-}
-
const routes = {
'/': { template: document.getElementById('main-template'), async initialize() {
console.log('initialize /')
@@ -48,8 +36,8 @@ const routes = {
}, []).join(',')
document.querySelector('channels-list').dataset['list'] = channelsList
- applyShowThumbnails(store.get(store.showThumbnailsKey))
- applyShowBigPlayer(store.get(store.showBigPlayerKey))
+ window.utils.applyShowThumbnails(store.get(store.showThumbnailsKey))
+ window.utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))
} },
'/settings': { template: document.getElementById('settings-template'), async initialize () {
console.log('initalize /settings')
@@ -64,7 +52,7 @@ const routes = {
$showThumbnails.addEventListener('click', (event) => {
store.toggle(store.showThumbnailsKey)
- applyShowThumbnails(store.get(store.showThumbnailsKey))
+ window.utils.applyShowThumbnails(store.get(store.showThumbnailsKey))
})
const $showBigPlayer = document.getElementById('show-big-player')
@@ -72,7 +60,7 @@ const routes = {
$showBigPlayer.addEventListener('click', (event) => {
store.toggle(store.showBigPlayerKey)
- applyShowBigPlayer(store.get(store.showBigPlayerKey))
+ window.utils.applyShowBigPlayer(store.get(store.showBigPlayerKey))
})
const $showOriginalThumbnail = document.getElementById('show-original-thumbnail')
@@ -130,22 +118,13 @@ document.querySelectorAll('[href="/"],[href="/settings"]').forEach(($el) => {
})
-
-function applyShowThumbnails(showThumbnails) {
- if (showThumbnails) {
- document.body.classList.remove('hide-thumbnails')
+function handleRoute(route = window.location.pathname) {
+ console.log('handling route', route)
+ if (routes[route]) {
+ document.querySelector('main').replaceChildren(routes[route].template.content.cloneNode(true))
+ routes[route].initialize && routes[route].initialize()
} else {
- document.body.classList.add('hide-thumbnails')
+ document.querySelector('main').replaceChildren(routes['/404'].template.content.cloneNode(true))
+ routes['/404'].initialize && routes['/404'].initialize()
}
- const $showThumbnailsCheckbox = document.getElementById('show-thumbnails')
- if ($showThumbnailsCheckbox) $showThumbnailsCheckbox.checked = showThumbnails
}
-function applyShowBigPlayer(showBigPlayer) {
- if (showBigPlayer) {
- document.body.classList.add('show-big-player')
- } else {
- document.body.classList.remove('show-big-player')
- }
- const $showBigPlayerCheckbox = document.getElementById('show-big-player')
- if ($showBigPlayerCheckbox) $showBigPlayerCheckbox.checked = showBigPlayer
-}
\ No newline at end of file
diff --git a/client/lib/utils.js b/client/lib/utils.js
new file mode 100644
index 0000000..22b0aa5
--- /dev/null
+++ b/client/lib/utils.js
@@ -0,0 +1,34 @@
+window.utils = {
+ applyShowThumbnails(showThumbnails) {
+ if (showThumbnails) {
+ document.body.classList.remove('hide-thumbnails')
+ } else {
+ document.body.classList.add('hide-thumbnails')
+ }
+ const $showThumbnailsCheckbox = document.getElementById('show-thumbnails')
+ if ($showThumbnailsCheckbox) $showThumbnailsCheckbox.checked = showThumbnails
+ },
+
+ applyShowBigPlayer(showBigPlayer) {
+ if (showBigPlayer) {
+ document.body.classList.add('show-big-player')
+ } else {
+ document.body.classList.remove('show-big-player')
+ }
+ const $showBigPlayerCheckbox = document.getElementById('show-big-player')
+ if ($showBigPlayerCheckbox) $showBigPlayerCheckbox.checked = showBigPlayer
+ },
+
+ addClickListener ($el, handler) {
+ if (!$el) return
+ $el.addEventListener('click', handler)
+ $el.addEventListener('keydown', (event) => event.key === 'Enter' && handler(event))
+ },
+
+ removeClickListener ($el, handler) {
+ if (!$el) return
+ $el.removeEventListener('click', handler)
+ $el.removeEventListener('keydown', (event) => event.key === 'Enter' && handler(event))
+ }
+}
+
diff --git a/lib/server.js b/lib/server.js
index 540d90d..0fa37ca 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -68,6 +68,8 @@ export function createServer ({repo, connections = []}) {
return fileHandler('client/lib/store.js', 'application/javascript')(req, res)
if (url.pathname === '/lib/router.js')
return fileHandler('client/lib/router.js', 'application/javascript')(req, res)
+ if (url.pathname === '/lib/utils.js')
+ return fileHandler('client/lib/utils.js', 'application/javascript')(req, res)
if (url.pathname === '/components/video-element.js')
return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)
if (url.pathname === '/components/add-channel-form.js')