client: implement a buggy inifinite scroll on folder view

This commit is contained in:
geoffrey45
2021-12-18 12:09:24 +03:00
parent 5ebe7b0dff
commit 457180ecaf
5 changed files with 86 additions and 35 deletions
+11 -8
View File
@@ -46,18 +46,20 @@ def search_by_title():
else: else:
query = request.args.get('q') query = request.args.get('q')
all_songs = []
songs = all_songs_instance.find_song_by_title(query) songs = all_songs_instance.find_song_by_title(query)
all_songs = convert_to_json(songs) all_songs.append(convert_to_json(songs))
albums = all_songs_instance.find_songs_by_album(query) songs_by_albums = all_songs_instance.find_songs_by_album(query)
all_songs.append(convert_to_json(albums)) all_songs.append(convert_to_json(songs_by_albums))
artists = all_songs_instance.find_songs_by_artist(query) songs_by_artists = all_songs_instance.find_songs_by_artist(query)
all_songs.append(convert_to_json(artists)) all_songs.append(convert_to_json(songs_by_artists))
songs = remove_duplicates(all_songs) # songs = remove_duplicates(all_songs)
return {'songs': songs} return {'songs': all_songs}
@bp.route('/populate') @bp.route('/populate')
@@ -272,6 +274,7 @@ def getFolderTree():
start = time.time() start = time.time()
req_dir = request.args.get('f') req_dir = request.args.get('f')
last_id = request.args.get('last_id')
if req_dir is not None: if req_dir is not None:
requested_dir = home_dir + req_dir requested_dir = home_dir + req_dir
@@ -298,7 +301,7 @@ def getFolderTree():
if entry.is_file(): if entry.is_file():
if isValidFile(entry.name) == True: if isValidFile(entry.name) == True:
songs_array = all_songs_instance.find_songs_by_folder(req_dir) songs_array = all_songs_instance.find_songs_by_folder(req_dir, last_id)
songs = convert_to_json(songs_array) songs = convert_to_json(songs_array)
for song in songs: for song in songs:
song['artists'] = song['artists'].split(', ') song['artists'] = song['artists'].split(', ')
+10 -5
View File
@@ -45,12 +45,13 @@ class AllSongs(Mongo):
# def drop_db(self): # def drop_db(self):
# self.collection.drop() # self.collection.drop()
def get_song_by_id(self, file_id): def get_song_by_id(self, file_id):
return self.collection.find_one({'_id': ObjectId(file_id)}) return self.collection.find_one({'_id': ObjectId(file_id)})
def insert_song(self, song_obj): def insert_song(self, song_obj):
self.collection.update({'filepath': song_obj['filepath']}, song_obj, upsert=True) self.collection.update(
{'filepath': song_obj['filepath']}, song_obj, upsert=True)
def find_song_by_title(self, query): def find_song_by_title(self, query):
self.collection.create_index([('title', pymongo.TEXT)]) self.collection.create_index([('title', pymongo.TEXT)])
@@ -62,8 +63,12 @@ class AllSongs(Mongo):
def get_all_songs(self): def get_all_songs(self):
return self.collection.find() return self.collection.find()
def find_songs_by_folder(self, query): def find_songs_by_folder(self, query, last_id=None):
return self.collection.find({'folder': query}) limit = 18
if last_id is None:
return self.collection.find({'folder': query}).limit(limit)
else:
return self.collection.find({'folder': query, '_id': {'$gt': ObjectId(last_id)}}).limit(limit)
def find_songs_by_artist(self, query): def find_songs_by_artist(self, query):
return self.collection.find({'artists': {'$regex': query, '$options': 'i'}}) return self.collection.find({'artists': {'$regex': query, '$options': 'i'}})
@@ -79,4 +84,4 @@ class AllSongs(Mongo):
self.collection.remove({'filepath': filepath}) self.collection.remove({'filepath': filepath})
return True return True
except: except:
return False return False
+3 -4
View File
@@ -1,7 +1,7 @@
<template> <template>
<div class="folder"> <div class="folder">
<div class="table rounded" ref="songtitle" v-if="songs.length"> <div class="table rounded" ref="songtitle" v-if="songs.length">
<table class="rounded"> <table>
<tr> <tr>
<th>Track</th> <th>Track</th>
<th>Artist</th> <th>Artist</th>
@@ -30,7 +30,7 @@
> >
</div> </div>
</td> </td>
<td :style="{ width: songTitleWidth + 'px' }">{{ song.album }}</td> <td :style="{ width: songTitleWidth + 'px' }"> <div class="ellip">{{ song.album }}</div></td>
<td <td
:style="{ width: songTitleWidth + 'px' }" :style="{ width: songTitleWidth + 'px' }"
v-if="songTitleWidth > minWidth" v-if="songTitleWidth > minWidth"
@@ -52,7 +52,6 @@ export default {
props: ["songs"], props: ["songs"],
setup() { setup() {
const songtitle = ref(null); const songtitle = ref(null);
console.log(songtitle);
const songTitleWidth = ref(null); const songTitleWidth = ref(null);
const image_path = "http://127.0.0.1:8900/images/thumbnails/"; const image_path = "http://127.0.0.1:8900/images/thumbnails/";
@@ -86,7 +85,7 @@ export default {
<style lang="scss"> <style lang="scss">
.table { .table {
width: 100%; width: 100%;
height: calc(100%); height: 100%;
background-color: rgba(56, 56, 56, 0.363); background-color: rgba(56, 56, 56, 0.363);
overflow-y: auto; overflow-y: auto;
+11 -3
View File
@@ -1,12 +1,19 @@
import { ref } from "@vue/reactivity"; import { ref } from "@vue/reactivity";
let home_url = "http://127.0.0.1:9876"; let folders_uri = "http://127.0.0.1:9876";
const getData = async (path) => { const getData = async (path, last_id) => {
let url;
const songs = ref(null); const songs = ref(null);
const folders = ref(null); const folders = ref(null);
const res = await fetch(`${home_url}/?f=${path}`); if (last_id) {
url = `${folders_uri}/?f=${path}&last_id=${last_id}`;
} else {
url = url = `${folders_uri}/?f=${path}`;
}
const res = await fetch(url);
if (!res.ok) { if (!res.ok) {
const message = `An erro has occured: ${res.status}`; const message = `An erro has occured: ${res.status}`;
@@ -14,6 +21,7 @@ const getData = async (path) => {
} }
const data = await res.json(); const data = await res.json();
songs.value = data.files; songs.value = data.files;
folders.value = data.folders; folders.value = data.folders;
+51 -15
View File
@@ -1,9 +1,9 @@
<template> <template>
<div id="f-view-parent" class="rounded"> <div id="f-view-parent" class="rounded">
<div class="fixed"> <div class="fixed">
<SearchBox :path="path"/> <SearchBox :path="path" />
</div> </div>
<div id="scrollable"> <div id="scrollable" ref="scrollable">
<FolderList :folders="folders" /> <FolderList :folders="folders" />
<SongList :songs="songs" /> <SongList :songs="songs" />
</div> </div>
@@ -19,7 +19,7 @@ import FolderList from "@/components/FolderView/FolderList.vue";
import SearchBox from "@/components/FolderView/SearchBox.vue"; import SearchBox from "@/components/FolderView/SearchBox.vue";
import getData from "../composables/getFiles.js"; import getData from "../composables/getFiles.js";
import { watch } from "@vue/runtime-core"; import { onMounted, watch } from "@vue/runtime-core";
export default { export default {
components: { components: {
@@ -34,25 +34,58 @@ export default {
const songs = ref([]); const songs = ref([]);
const folders = ref([]); const folders = ref([]);
const getPathFolders = (path) => { const scrollable = ref(null);
getData(path).then((data) => {
document.getElementById('scrollable').scrollTop = 0; const last_song_id = ref(null);
songs.value = data.songs.value; const last_page = ref([]);
folders.value = data.folders.value;
onMounted(() => {
const getPathFolders = (path, last_id) => {
getData(path, last_id).then((data) => {
scrollable.value.scrollTop = 0;
songs.value = data.songs.value;
last_page.value = songs.value;
if (songs.value.length) {
last_song_id.value = songs.value.slice(-1)[0]._id.$oid;
}
folders.value = data.folders.value;
});
};
getPathFolders(path.value);
watch(route, (new_route) => {
path.value = new_route.params.path;
getPathFolders(encodeURI(path.value));
}); });
};
getPathFolders(path.value); scrollable.value.onscroll = () => {
let dom = scrollable.value;
watch(route, (new_route) => { let scrollY = dom.scrollHeight - dom.scrollTop;
path.value = new_route.params.path; let height = dom.offsetHeight;
getPathFolders(encodeURI(path.value)); let offset = height - scrollY;
if (offset == 0 || offset == 1) {
getData(path.value, last_song_id.value).then((data) => {
songs.value = songs.value.concat(data.songs.value);
if (songs.value.length) {
last_song_id.value = songs.value.slice(-1)[0]._id.$oid;
}
});
}
};
}); });
return { return {
songs, songs,
folders, folders,
path path,
scrollable,
}; };
}, },
}; };
@@ -78,6 +111,9 @@ export default {
#scrollable { #scrollable {
overflow-y: scroll; overflow-y: scroll;
height: 100%; height: 100%;
padding: $small 1rem 0 0;
&::-webkit-scrollbar {
display: none;
}
} }
</style> </style>