« 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:
-rw-r--r--playlist-utils.js23
-rw-r--r--ui.js6
2 files changed, 26 insertions, 3 deletions
diff --git a/playlist-utils.js b/playlist-utils.js
index 92e62b1..f20ec98 100644
--- a/playlist-utils.js
+++ b/playlist-utils.js
@@ -104,6 +104,28 @@ function updateTrackFormat(track) {
   return Object.assign(defaultTrack, trackObj)
 }
 
+function cloneGrouplike(grouplike) {
+  const newGrouplike = {
+    name: grouplike.name,
+    items: grouplike.items.map(item => {
+      if (isGroup(item)) {
+        return cloneGrouplike(item)
+      } else {
+        return {
+          name: item.name,
+          downloaderArg: item.downloaderArg
+        }
+      }
+    })
+  }
+
+  for (const item of newGrouplike.items) {
+    item[parentSymbol] = newGrouplike
+  }
+
+  return newGrouplike
+}
+
 function filterTracks(grouplike, handleTrack) {
   // Recursively filters every track in the passed grouplike. The track-handler
   // function passed should either return true (to keep a track) or false (to
@@ -468,6 +490,7 @@ function isTrack(obj) {
 module.exports = {
   parentSymbol,
   updatePlaylistFormat, updateGroupFormat, updateTrackFormat,
+  cloneGrouplike,
   filterTracks,
   flattenGrouplike,
   partiallyFlattenGrouplike, collapseGrouplike,
diff --git a/ui.js b/ui.js
index e876904..28977fa 100644
--- a/ui.js
+++ b/ui.js
@@ -1,7 +1,7 @@
 const { getAllCrawlersForArg } = require('./crawlers')
 const { getDownloaderFor } = require('./downloaders')
 const { getPlayer } = require('./players')
-const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, updateGroupFormat } = require('./playlist-utils')
+const { parentSymbol, isGroup, isTrack, getItemPath, getItemPathString, flattenGrouplike, cloneGrouplike } = require('./playlist-utils')
 const { shuffleArray } = require('./general-util')
 const processSmartPlaylist = require('./smart-playlist')
 const UndoManager = require('./undo-manager')
@@ -182,8 +182,8 @@ class AppElement extends FocusElement {
               // TODO: More "proper" way of cloning a grouplike. (The purpose of updateGroupFormat
               // here is to make the parentSymbols be properly set, as well as to create a set of
               // totally new objects, so none of the pasted groups already appear somewhere else;
-              // or rather, not as the same objects.)
-              parent.items.splice(index, 0, ...updateGroupFormat(this.markGrouplike).items.map(
+              // or rather, not as the same objects.) (..Done!..)
+              parent.items.splice(index, 0, ...cloneGrouplike(this.markGrouplike).items.map(
                 item => Object.assign({}, item, {[parentSymbol]: parent})
               ))
               updateListingsFor(parent)