« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xindex.js2
-rw-r--r--ui.js72
2 files changed, 39 insertions, 35 deletions
diff --git a/index.js b/index.js
index c0e2932..7ecccc7 100755
--- a/index.js
+++ b/index.js
@@ -67,7 +67,7 @@ async function main() {
 
   grouplike = await processSmartPlaylist(grouplike)
 
-  appElement.grouplikeListingElement.loadGrouplike(grouplike)
+  appElement.tabber.currentElement.loadGrouplike(grouplike)
 
   root.select(appElement)
 
diff --git a/ui.js b/ui.js
index 01e5b1a..43039e1 100644
--- a/ui.js
+++ b/ui.js
@@ -47,36 +47,7 @@ class AppElement extends FocusElement {
     this.paneLeft.addChild(this.tabber)
     this.form.addInput(this.tabber, false)
 
-    this.grouplikeListingElement = new GrouplikeListingElement(this.recordStore)
-    this.tabber.addTab(this.grouplikeListingElement)
-
-    const handleSelectFromMain = item => {
-      if (isGroup(item)) {
-        this.grouplikeListingElement.loadGrouplike(item)
-      } else {
-        this.playGrouplikeItem(item)
-      }
-    }
-
-    this.grouplikeListingElement.on('download', item => this.downloadGrouplikeItem(item))
-    this.grouplikeListingElement.on('select (enter)', item => handleSelectFromMain(item))
-    this.grouplikeListingElement.on('select (space)', item => this.handleSpacePressed(
-      () => handleSelectFromMain(item)))
-    this.grouplikeListingElement.on('queue', item => this.queueGrouplikeItem(item))
-    this.grouplikeListingElement.on('queue (shuffled)', item => this.shuffleQueueGrouplikeItem(item))
-
-    const handleSelectFromPathElement = item => {
-      this.form.selectInput(this.grouplikeListingElement)
-      if (isGroup(item)) {
-        this.grouplikeListingElement.loadGrouplike(item)
-      } else if (item[parentSymbol]) {
-        this.grouplikeListingElement.loadGrouplike(item[parentSymbol])
-        this.grouplikeListingElement.selectAndShow(item)
-      }
-    }
-
-    this.form.addInput(this.grouplikeListingElement.pathElement, false)
-    this.grouplikeListingElement.pathElement.on('select', item => handleSelectFromPathElement(item))
+    this.newGrouplikeListing()
 
     this.queueListingElement = new QueueListingElement(this.recordStore)
     this.queueListingElement.loadGrouplike(this.queueGrouplike)
@@ -90,7 +61,7 @@ class AppElement extends FocusElement {
     this.queueListingElement.on('shuffle', () => this.shuffleQueue())
     this.queueListingElement.on('clear', () => this.clearQueue())
     this.queueListingElement.on('select main listing',
-      () => this.form.selectInput(this.grouplikeListingElement))
+      () => this.form.selectInput(this.tabber))
 
     this.form.addInput(this.queueListingElement.pathElement, false)
     this.queueListingElement.pathElement.on('select', item => handleSelectFromPathElement(item))
@@ -116,6 +87,39 @@ class AppElement extends FocusElement {
     this.root.select(this.form)
   }
 
+  newGrouplikeListing() {
+    const grouplikeListing = new GrouplikeListingElement(this.recordStore)
+    this.tabber.addTab(grouplikeListing)
+
+    const handleSelectFromMain = item => {
+      if (isGroup(item)) {
+        grouplikeListing.loadGrouplike(item)
+      } else {
+        this.playGrouplikeItem(item)
+      }
+    }
+
+    grouplikeListing.on('download', item => this.downloadGrouplikeItem(item))
+    grouplikeListing.on('select (enter)', item => handleSelectFromMain(item))
+    grouplikeListing.on('select (space)', item => this.handleSpacePressed(
+      () => handleSelectFromMain(item)))
+    grouplikeListing.on('queue', item => this.queueGrouplikeItem(item))
+    grouplikeListing.on('queue (shuffled)', item => this.shuffleQueueGrouplikeItem(item))
+
+    const handleSelectFromPathElement = item => {
+      this.form.selectInput(grouplikeListing)
+      if (isGroup(item)) {
+        grouplikeListing.loadGrouplike(item)
+      } else if (item[parentSymbol]) {
+        grouplikeListing.loadGrouplike(item[parentSymbol])
+        grouplikeListing.selectAndShow(item)
+      }
+    }
+
+    // this.form.addInput(grouplikeListing.pathElement, false)
+    grouplikeListing.pathElement.on('select', item => handleSelectFromPathElement(item))
+  }
+
   async handlePlaylistSource(source) {
     this.openPlaylistDialog.close()
     this.alertDialog.showMessage('Opening playlist...', false)
@@ -139,7 +143,7 @@ class AppElement extends FocusElement {
     this.root.select(this.form)
 
     grouplike = await processSmartPlaylist(grouplike)
-    this.grouplikeListingElement.loadGrouplike(grouplike)
+    this.tabber.currentElement.loadGrouplike(grouplike)
   }
 
   openPlaylist(arg) {
@@ -220,8 +224,8 @@ class AppElement extends FocusElement {
       this.playPreviousTrack(this.playingTrack)
     } else if (telc.isShiftDown(keyBuf) || telc.isCaselessLetter(keyBuf, 'n')) {
       this.playNextTrack(this.playingTrack)
-    } else if (telc.isCharacter(keyBuf, '1') && this.grouplikeListingElement.selectable) {
-      this.form.curIndex = this.form.inputs.indexOf(this.grouplikeListingElement)
+    } else if (telc.isCharacter(keyBuf, '1') && this.tabber.selectable) {
+      this.form.curIndex = this.form.inputs.indexOf(this.tabber)
       this.form.updateSelectedElement()
     } else if (telc.isCharacter(keyBuf, '2') && this.queueListingElement.selectable) {
       this.form.curIndex = this.form.inputs.indexOf(this.queueListingElement)