« get me outta code hell

rename handlePlaylistSource & let accept grouplike - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/ui.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-07-09 14:40:25 -0300
committerFlorrie <towerofnix@gmail.com>2020-07-09 16:19:17 -0300
commit8baf386abdf611acd855e61578f06cf7ac8f014f (patch)
tree357d36d07df7ce57a78394c942c7baa013ffc4f2 /ui.js
parent0e0cb91ffc1e7a28a03428528d437e074145a72e (diff)
rename handlePlaylistSource & let accept grouplike
It's called loadPlaylistOrSource now, and can take a grouplike (which it
will process with processSmartPlaylist as usual) instead of a URL to
pass to a crawler.

This is so that all functionality for loading a playlist can be
collected in and accessed through one interface, so that modifications
to the way playlists are loaded will be reflected across everywhere that
loads a playlist.
Diffstat (limited to 'ui.js')
-rw-r--r--ui.js34
1 files changed, 19 insertions, 15 deletions
diff --git a/ui.js b/ui.js
index 04c65c0..41ab4cc 100644
--- a/ui.js
+++ b/ui.js
@@ -295,8 +295,8 @@ class AppElement extends FocusElement {
     this.openPlaylistDialog = new OpenPlaylistDialog()
     this.setupDialog(this.openPlaylistDialog)
 
-    this.openPlaylistDialog.on('source selected', source => this.handlePlaylistSource(source))
-    this.openPlaylistDialog.on('source selected (new tab)', source => this.handlePlaylistSource(source, true))
+    this.openPlaylistDialog.on('source selected', source => this.loadPlaylistOrSource(source))
+    this.openPlaylistDialog.on('source selected (new tab)', source => this.loadPlaylistOrSource(source, true))
 
     this.alertDialog = new AlertDialog()
     this.setupDialog(this.alertDialog)
@@ -944,7 +944,7 @@ class AppElement extends FocusElement {
 
   openSpecialOrThroughSystem(item) {
     if (item.url.endsWith('.json')) {
-      return this.handlePlaylistSource(item.url, true)
+      return this.loadPlaylistOrSource(item.url, true)
       /*
     } else if (item.url.endsWith('.txt')) {
       if (this.textInfoPane.visible) {
@@ -1094,7 +1094,7 @@ class AppElement extends FocusElement {
     })
   }
 
-  async handlePlaylistSource(source, newTab = false) {
+  async loadPlaylistOrSource(sourceOrPlaylist, newTab = false) {
     if (this.openPlaylistDialog.visible) {
       this.openPlaylistDialog.close()
     }
@@ -1102,18 +1102,22 @@ class AppElement extends FocusElement {
     this.alertDialog.showMessage('Opening playlist...', false)
 
     let grouplike
-    try {
-      grouplike = await this.openPlaylist(source)
-    } catch (error) {
-      if (error === 'unknown argument') {
-        this.alertDialog.showMessage('Could not figure out how to load a playlist from: ' + source)
-      } else if (typeof error === 'string') {
-        this.alertDialog.showMessage(error)
-      } else {
-        throw error
-      }
+    if (typeof sourceOrPlaylist === 'object' && isGroup(sourceOrPlaylist) || sourceOrPlaylist.source) {
+      grouplike = sourceOrPlaylist
+    } else {
+      try {
+        grouplike = await this.openPlaylist(sourceOrPlaylist)
+      } catch (error) {
+        if (error === 'unknown argument') {
+          this.alertDialog.showMessage('Could not figure out how to load a playlist from: ' + sourceOrPlaylist)
+        } else if (typeof error === 'string') {
+          this.alertDialog.showMessage(error)
+        } else {
+          throw error
+        }
 
-      return
+        return
+      }
     }
 
     this.alertDialog.close()