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: move view logic to videos-container element

Christian Fei3150f19620cb
diff --git a/client/components/search-videos.js b/client/components/search-videos.js
index 485c004..90422fa 100644
--- a/client/components/search-videos.js
+++ b/client/components/search-videos.js
@@ -1,7 +1,5 @@
/* global HTMLElement, customElements, document */
import { createVideoElement, addToast } from '/lib/utils.js' /* eslint-disable-line */
-import Store from '/lib/store.js' /* eslint-disable-line */
-const store = new Store()
const searchEventSupported = 'search' in document.createElement('input')
console.log({ searchEventSupported })
@@ -122,13 +120,11 @@ class SearchVideos extends HTMLElement {
fetch(`/api/videos${query}`)
.then(res => res.json())
.then((videos) => {
- const $videosContainer = document.querySelector(this.dataset.videosContainer || '.main-videos-container')
+ const $videosContainer = document.querySelector('videos-container')
if (!$videosContainer) return
- $videosContainer.innerHTML = ''
- const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)
- videos.forEach(video =>
- $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail))
- )
+ $videosContainer.dataset.videos = JSON.stringify(videos)
+ console.log('updated videos attribute')
+
document.title = `(${videos.length}) my-yt`
if (event) {
if (videos.length > 0) {
diff --git a/client/components/videos-container.js b/client/components/videos-container.js
new file mode 100644
index 0000000..a41a783
--- /dev/null
+++ b/client/components/videos-container.js
@@ -0,0 +1,37 @@
+/* global HTMLElement, customElements */
+import Store from '/lib/store.js' /* eslint-disable-line */
+const store = new Store()
+
+class VideosContainer extends HTMLElement {
+ connectedCallback () {
+ this.render()
+ }
+
+ static get observedAttributes () {
+ return ['data-videos']
+ }
+
+ attributeChangedCallback (name, _, newValue) {
+ console.log('attributeChangedCallback', name)
+ this.render()
+ }
+
+ render () {
+ const videos = JSON.parse(this.dataset.videos || '[]')
+ console.log('videos', videos)
+
+ this.innerHTML = ''
+
+ for (const video of videos) {
+ const $video = document.createElement('video-element')
+ $video.dataset.data = JSON.stringify(Object.assign(video, store.get(store.showOriginalThumbnailKey)
+ ? {
+ thumbnail: video.thumbnail.replace('mq2.jpg', 'mqdefault.jpg')
+ }
+ : {}))
+ $video.dataset.videoId = video.id
+ this.appendChild($video)
+ }
+ }
+}
+customElements.define('videos-container', VideosContainer)
diff --git a/client/index.html b/client/index.html
index 2c53af1..6de78f0 100644
--- a/client/index.html
+++ b/client/index.html
@@ -17,7 +17,7 @@
<div class="flex space-between align-start p-1">
<a id="home-link" class="hide p-v-2" href="/">go back home</a></h1>
<div class="flex-1 header-spacer"></div>
- <search-videos data-videos-container=".main-videos-container"></search-videos>
+ <search-videos data-videos-container="videos-container"></search-videos>
<div class="flex-1 tar p-v-2">
<a href="/settings">Settings</a>
<a tabindex="-1" href="https://github.com/christian-fei/my-yt" target="_blank">📖</a>
@@ -42,7 +42,7 @@
<template id="main-template">
<channels-list></channels-list>
- <div class="main-videos-container"></div>
+ <videos-container></videos-container>
<empty-state style="display:none"></empty-state>
</template>
@@ -131,6 +131,7 @@
<script type="module" src="/components/channels-list.js"></script>
<script type="module" src="/components/video-element.js"></script>
<script type="module" src="/components/sse-connection.js"></script>
+ <script type="module" src="/components/videos-container.js"></script>
<script type="module" src="/components/forms/add-channel-form.js"></script>
<script type="module" src="/components/forms/manage-channels-form.js"></script>
<script type="module" src="/components/forms/video-quality-form.js"></script>
diff --git a/client/lib/router.js b/client/lib/router.js
index dcb2be7..6bd936d 100644
--- a/client/lib/router.js
+++ b/client/lib/router.js
@@ -22,7 +22,7 @@ const routes = {
new MutationObserver((mutationList, observer) => {
handleEmptyState()
- }).observe(document.querySelector('.main-videos-container'), { attributes: false, childList: true, subtree: true })
+ }).observe(document.querySelector('videos-container'), { attributes: false, childList: true, subtree: true })
applyShowThumbnails(store.get(store.showThumbnailsKey))
applyShowBigPlayer(store.get(store.showBigPlayerKey))
diff --git a/client/main.css b/client/main.css
index f09c057..df9eb7c 100644
--- a/client/main.css
+++ b/client/main.css
@@ -262,7 +262,7 @@ button:disabled, select:disabled, input[type="checkbox"]:disabled, input[type="r
color: var(--active-channel);
}
-.videos-container, .main-videos-container {
+.videos-container, videos-container {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
diff --git a/client/main.js b/client/main.js
index f554d94..ba11e6c 100644
--- a/client/main.js
+++ b/client/main.js
@@ -1,7 +1,4 @@
/* global EventSource, MutationObserver */
-import Store from '/lib/store.js' /* eslint-disable-line */
-import { createVideoElement } from '/lib/utils.js' /* eslint-disable-line */
-const store = new Store()
window.state = {
downloading: {},
summarizing: {},
@@ -56,15 +53,11 @@ window.eventSource.onmessage = (message) => {
return
}
if (data.type === 'new-videos' && data.videos) {
- const $videosContainer = document.querySelector('.main-videos-container')
+ const $videosContainer = document.querySelector('videos-container')
if (!$videosContainer) return
- const showOriginalThumbnail = store.get(store.showOriginalThumbnailKey)
-
- data.videos.forEach(video => {
- const $videoElement = $videosContainer.querySelector('video-element')
- if (!$videoElement) return $videosContainer.appendChild(createVideoElement(video, showOriginalThumbnail))
- $videoElement.parentNode.insertBefore(createVideoElement(video, showOriginalThumbnail), $videoElement)
- })
+ let videos = JSON.parse($videosContainer.dataset.videos || '[]')
+ videos = data.videos.concat(videos)
+ $videosContainer.dataset.videos = JSON.stringify(videos)
return
}
if (data.type === 'summary-error' && data.videoId) {
diff --git a/server/router/app.js b/server/router/app.js
index ba6a9d9..4952bbd 100644
--- a/server/router/app.js
+++ b/server/router/app.js
@@ -17,6 +17,7 @@ export default function apiHandler (req, res) {
if (url.pathname === '/components/forms/manage-disk-space-form.js') { return fileHandler('client/components/forms/manage-disk-space-form.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/forms/transcode-videos-form.js') { return fileHandler('client/components/forms/transcode-videos-form.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/forms/excluded-terms-form.js') { return fileHandler('client/components/forms/excluded-terms-form.js', 'application/javascript')(req, res) }
+ if (url.pathname === '/components/videos-container.js') { return fileHandler('client/components/videos-container.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/sse-connection.js') { return fileHandler('client/components/sse-connection.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/search-videos.js') { return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/channels-list.js') { return fileHandler('client/components/channels-list.js', 'application/javascript')(req, res) }