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 video-filters component

Christian Feiefc4976480a8
diff --git a/client/components/video-filters.js b/client/components/video-filters.js
new file mode 100644
index 0000000..caef93a
--- /dev/null
+++ b/client/components/video-filters.js
@@ -0,0 +1,50 @@
+class VideoFilters extends HTMLElement {
+ constructor () {
+ super()
+ }
+ connectedCallback () {
+ this.render()
+ this.registerEvents()
+ }
+ disconnectedCallback () {
+ this.deregisterEvents()
+ }
+ registerEvents () {
+ const $showDownloadedVideos = this.querySelector('#show-downloaded-videos')
+ const $showSummarizedVideos = this.querySelector('#show-summarized-videos')
+ const $showIgnoredVideos = this.querySelector('#show-ignored-videos')
+ $showDownloadedVideos.addEventListener('click', this.handleDownloadedVideos.bind(this))
+ $showSummarizedVideos.addEventListener('click', this.handleSummarizedVideos.bind(this))
+ $showIgnoredVideos.addEventListener('click', this.handleIgnoredVideos.bind(this))
+ }
+ deregisterEvents () {
+ const $showDownloadedVideos = this.querySelector('#show-downloaded-videos')
+ const $showSummarizedVideos = this.querySelector('#show-summarized-videos')
+ const $showIgnoredVideos = this.querySelector('#show-ignored-videos')
+ $showDownloadedVideos.removeEventListener('click', this.handleDownloadedVideos.bind(this))
+ $showSummarizedVideos.removeEventListener('click', this.handleSummarizedVideos.bind(this))
+ $showIgnoredVideos.removeEventListener('click', this.handleIgnoredVideos.bind(this))
+ }
+ render () {
+ this.innerHTML = /*html*/`
+<div id="filters-container" class="flex space-around p-1">
+ <button id="show-downloaded-videos">Downloaded videos</button>
+ <button id="show-summarized-videos">Summarized videos</button>
+ <button id="show-ignored-videos">Ignored videos</button>
+</div>
+ `
+ }
+ handleDownloadedVideos(event) {
+ event.target.classList.toggle('active')
+ document.body.classList.toggle('show-downloaded-videos')
+ }
+ handleSummarizedVideos(event) {
+ event.target.classList.toggle('active')
+ document.body.classList.toggle('show-summarized-videos')
+ }
+ handleIgnoredVideos(event) {
+ event.target.classList.toggle('active')
+ document.body.classList.toggle('show-ignored-videos')
+ }
+}
+customElements.define('video-filters', VideoFilters)
diff --git a/client/index.html b/client/index.html
index a0fc53a..0f83b97 100644
--- a/client/index.html
+++ b/client/index.html
@@ -29,11 +29,7 @@
<template id="main-template">
<channels-list></channels-list>
<div id="filter-results-status"></div>
- <div id="filters-container" class="flex space-around p-1">
- <button id="show-downloaded-videos">Downloaded videos</button>
- <button id="show-summarized-videos">Summarized videos</button>
- <button id="show-ignored-videos">Ignored videos</button>
- </div>
+ <video-filters></video-filters>
<div class="main-videos-container"></div>
<empty-state style="display:none"></empty-state>
</template>
@@ -85,9 +81,10 @@
<script type="module" src="/main.js"></script>
<script type="module" src="/components/empty-state.js"></script>
- <script type="module" src="/components/channels-list.js"></script>
+ <script type="module" src="/components/video-filters.js"></script>
<script type="module" src="/components/search-videos.js"></script>
- <script type="module" src="/components/add-channel-form.js"></script>
+ <script type="module" src="/components/channels-list.js"></script>
<script type="module" src="/components/video-element.js"></script>
+ <script type="module" src="/components/add-channel-form.js"></script>
</body>
</html>
diff --git a/client/main.js b/client/main.js
index dae6d20..07e2030 100644
--- a/client/main.js
+++ b/client/main.js
@@ -26,33 +26,15 @@ const routes = {
if (videos.length === 0) {
document.querySelector('empty-state').style.display = ''
document.querySelector('channels-list').style.display = 'none'
- document.querySelector('#filters-container').style.display = 'none'
+ document.querySelector('video-filters').style.display = 'none'
} else {
document.querySelector('empty-state').style.display = 'none'
document.querySelector('channels-list').style.display = ''
- document.querySelector('#filters-container').style.display = ''
+ document.querySelector('video-filters').style.display = ''
}
- const $showDownloadedVideos = document.getElementById('show-downloaded-videos')
- handleClick($showDownloadedVideos, (event) => {
- event.target.classList.toggle('active')
- document.body.classList.toggle('show-downloaded-videos')
- })
- const $showSummarizedVideos = document.getElementById('show-summarized-videos')
- handleClick($showSummarizedVideos, (event) => {
- event.target.classList.toggle('active')
- document.body.classList.toggle('show-summarized-videos')
- })
- const $showIgnoredVideos = document.getElementById('show-ignored-videos')
- handleClick($showIgnoredVideos, (event) => {
- event.target.classList.toggle('active')
- document.body.classList.toggle('show-ignored-videos')
- })
-
document.getElementById('search').removeAttribute('disabled')
-
-
$videosContainer.innerHTML = ''
const ignoredTerms = store.get(store.ignoreTermsKey)
const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)
diff --git a/lib/server.js b/lib/server.js
index 85bd9aa..ca9df78 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -68,6 +68,8 @@ export function createServer ({repo, connections = []}) {
return fileHandler('client/components/channels-list.js', 'application/javascript')(req, res)
if (url.pathname === '/components/empty-state.js')
return fileHandler('client/components/empty-state.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/video-filters.js')
+ return fileHandler('client/components/video-filters.js', 'application/javascript')(req, res)
if (url.pathname === '/lib/store.js')
return fileHandler('client/lib/store.js', 'application/javascript')(req, res)