Browse Source

support drag and drop for playlist items

Luke Pulverenti 9 years ago
parent
commit
6c3355b26f

+ 25 - 1
MediaBrowser.Api/PlaylistService.cs

@@ -40,10 +40,27 @@ namespace MediaBrowser.Api
         /// Gets or sets the user id.
         /// </summary>
         /// <value>The user id.</value>
-        [ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
+        [ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
         public string UserId { get; set; }
     }
 
+    [Route("/Playlists/{Id}/Items/{ItemId}/Move/{NewIndex}", "POST", Summary = "Moves a playlist item")]
+    public class MoveItem : IReturnVoid
+    {
+        [ApiMember(Name = "ItemId", Description = "ItemId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public string ItemId { get; set; }
+
+        [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
+        public string Id { get; set; }
+
+        /// <summary>
+        /// Gets or sets the user id.
+        /// </summary>
+        /// <value>The user id.</value>
+        [ApiMember(Name = "NewIndex", Description = "NewIndex", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public int NewIndex { get; set; }
+    }
+
     [Route("/Playlists/{Id}/Items", "DELETE", Summary = "Removes items from a playlist")]
     public class RemoveFromPlaylist : IReturnVoid
     {
@@ -105,6 +122,13 @@ namespace MediaBrowser.Api
             _libraryManager = libraryManager;
         }
 
+        public void Post(MoveItem request)
+        {
+            var task = _playlistManager.MoveItem(request.Id, request.ItemId, request.NewIndex);
+
+            Task.WaitAll(task);
+        }
+
         public async Task<object> Post(CreatePlaylist request)
         {
             var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest

+ 8 - 0
MediaBrowser.Controller/Playlists/IPlaylistManager.cs

@@ -45,5 +45,13 @@ namespace MediaBrowser.Controller.Playlists
         /// <returns>Folder.</returns>
         Folder GetPlaylistsFolder(string userId);
 
+        /// <summary>
+        /// Moves the item.
+        /// </summary>
+        /// <param name="playlistId">The playlist identifier.</param>
+        /// <param name="entryId">The entry identifier.</param>
+        /// <param name="newIndex">The new index.</param>
+        /// <returns>Task.</returns>
+        Task MoveItem(string playlistId, string entryId, int newIndex);
     }
 }

+ 21 - 0
MediaBrowser.Server.Implementations/Playlists/PlaylistManager.cs

@@ -230,6 +230,27 @@ namespace MediaBrowser.Server.Implementations.Playlists
             });
         }
 
+        public async Task MoveItem(string playlistId, string entryId, int newIndex)
+        {
+            var playlist = _libraryManager.GetItemById(playlistId) as Playlist;
+
+            if (playlist == null)
+            {
+                throw new ArgumentException("No Playlist exists with the supplied Id");
+            }
+
+            var children = playlist.GetManageableItems().ToList();
+
+            var oldIndex = children.FindIndex(i => string.Equals(entryId, i.Item1.Id, StringComparison.OrdinalIgnoreCase));
+
+            var item = playlist.LinkedChildren[oldIndex];
+
+            playlist.LinkedChildren.Remove(item);
+            playlist.LinkedChildren.Insert(newIndex, item);
+
+            await playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);
+        }
+
         public Folder GetPlaylistsFolder(string userId)
         {
             return _libraryManager.RootFolder.Children.OfType<PlaylistsFolder>()

+ 6 - 0
MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj

@@ -110,6 +110,12 @@
     <Content Include="dashboard-ui\apiclient\sync\serversync.js">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
+    <Content Include="dashboard-ui\bower_components\dragula.js\dist\dragula.min.css">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="dashboard-ui\bower_components\dragula.js\dist\dragula.min.js">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="dashboard-ui\bower_components\fastclick\lib\fastclick.js">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>