From 9ca1712b5d5c16708e8bd719f4260ef0831f4815 Mon Sep 17 00:00:00 2001 From: redxef Date: Sun, 19 Jan 2025 22:39:24 +0100 Subject: [PATCH] fix: loading of the config pages. --- .../Pages/smartCollections.js | 43 +++++++++------- .../Pages/smartPlaylists.js | 49 ++++++++++--------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/Jellyfin.Plugin.SmartPlaylist/Pages/smartCollections.js b/Jellyfin.Plugin.SmartPlaylist/Pages/smartCollections.js index 8b9aadb..db0ec34 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Pages/smartCollections.js +++ b/Jellyfin.Plugin.SmartPlaylist/Pages/smartCollections.js @@ -3,7 +3,7 @@ var COLLECTIONS = [ { Id: 'My New Smartcollection', Name: 'My New Smartcollection', - Program: '(is-favourite)', + Program: 'nil', SortProgram: '(begin *items*)', CollectionId: '00000000000000000000000000000000', Enabled: true, @@ -73,17 +73,29 @@ ApiClient.setSmartCollection = function (c) { data: JSON.stringify(c), }); } - +function initial_load(selectedId) { + Dashboard.showLoadingMsg(); + ApiClient.getSmartCollections().then(function (collections) { + COLLECTIONS = [COLLECTIONS[0]].concat(collections); + const selection = document.querySelector('#SmartcollectionSelection'); + fillCollectionSelect(COLLECTIONS); + if (selectedId === null) { + selection.selectedIndex = 0; + } else { + selection.selectedIndex = COLLECTIONS.map(x => x.Id).indexOf(selectedId); + } + fillForm(COLLECTIONS[selection.selectedIndex]); + Dashboard.hideLoadingMsg(); + }); +} document.querySelector('#SmartCollectionConfigPage') .addEventListener('viewshow', function() { - Dashboard.showLoadingMsg(); - ApiClient.getSmartCollections().then(function (collections) { - COLLECTIONS = [COLLECTIONS[0]].concat(collections); - const selection = document.querySelector('#SmartcollectionSelection'); - fillCollectionSelect(COLLECTIONS); - fillForm(COLLECTIONS[selection.selectedIndex]); - Dashboard.hideLoadingMsg(); - }); + initial_load(null); + }); + +document.querySelector('#SmartCollectionConfigPage') + .addEventListener('pageshow', function() { + initial_load(null); }); document.querySelector('#SmartcollectionSelection') @@ -96,16 +108,9 @@ document.querySelector('#SmartCollectionConfigForm') .addEventListener('submit', function (e) { Dashboard.showLoadingMsg(); const selection = document.querySelector('#SmartcollectionSelection'); - const selectedid = COLLECTIONS[selection.selectedIndex].Id; + const selectedId = COLLECTIONS[selection.selectedIndex].Id; ApiClient.setSmartCollection(jsonFromForm(COLLECTIONS[selection.selectedIndex].CollectionId)).then(function () { - ApiClient.getSmartCollections().then(function (collections) { - COLLECTIONS = [COLLECTIONS[0]].concat(collections); - fillCollectionSelect(COLLECTIONS); - const idx = COLLECTIONS.map(x => x.Id).indexOf(selectedid); - selection.selectedIndex = idx; - fillForm(COLLECTIONS[selection.selectedIndex]); - Dashboard.hideLoadingMsg(); - }); + initial_load(selectedId); }); e.preventDefault(); }); diff --git a/Jellyfin.Plugin.SmartPlaylist/Pages/smartPlaylists.js b/Jellyfin.Plugin.SmartPlaylist/Pages/smartPlaylists.js index 7a0d677..0eda8ed 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Pages/smartPlaylists.js +++ b/Jellyfin.Plugin.SmartPlaylist/Pages/smartPlaylists.js @@ -97,19 +97,32 @@ ApiClient.setSmartPlaylist = function (p) { }); } +function initial_load(selectedId) { + Dashboard.showLoadingMsg(); + ApiClient.getSmartPlaylists().then(function (playlists) { + PLAYLISTS = [PLAYLISTS[0]].concat(playlists); + ApiClient.getUsers().then(function (users) { + USERS = users; + const selection = document.querySelector('#SmartplaylistSelection'); + fillPlaylistSelect(PLAYLISTS); + if (selectedId === null) { + selection.selectedIndex = 0; + } else { + selection.selectedIndex = PLAYLISTS.map(x => x.Id).indexOf(selectedId); + } + fillForm(PLAYLISTS[selection.selectedIndex], USERS); + Dashboard.hideLoadingMsg(); + }); + }); +} + document.querySelector('#SmartPlaylistConfigPage') .addEventListener('viewshow', function() { - Dashboard.showLoadingMsg(); - ApiClient.getSmartPlaylists().then(function (playlists) { - PLAYLISTS = [PLAYLISTS[0]].concat(playlists); - ApiClient.getUsers().then(function (users) { - USERS = users; - const selection = document.querySelector('#SmartplaylistSelection'); - fillPlaylistSelect(PLAYLISTS); - fillForm(PLAYLISTS[selection.selectedIndex], USERS); - Dashboard.hideLoadingMsg(); - }); - }); + initial_load(null); + }); +document.querySelector('#SmartPlaylistConfigPage') + .addEventListener('pageshow', function() { + initial_load(null); }); document.querySelector('#SmartplaylistSelection') @@ -122,19 +135,9 @@ document.querySelector('#SmartPlaylistConfigForm') .addEventListener('submit', function (e) { Dashboard.showLoadingMsg(); const selection = document.querySelector('#SmartplaylistSelection'); - const selectedid = PLAYLISTS[selection.selectedIndex].Id; + const selectedId = PLAYLISTS[selection.selectedIndex].Id; ApiClient.setSmartPlaylist(jsonFromForm()).then(function () { - ApiClient.getSmartPlaylists().then(function (playlists) { - PLAYLISTS = [PLAYLISTS[0]].concat(playlists); - ApiClient.getUsers().then(function (users) { - USERS = users; - fillPlaylistSelect(PLAYLISTS); - const idx = PLAYLISTS.map(x => x.Id).indexOf(selectedid); - selection.selectedIndex = idx; - fillForm(PLAYLISTS[selection.selectedIndex], USERS); - Dashboard.hideLoadingMsg(); - }); - }); + initial_load(selectedId); }); e.preventDefault(); });