From 7f0dc84b290aa5d4cb10f264ad2695780539e1cb Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 5 Feb 2013 22:28:56 -0800 Subject: [PATCH] Added auto complete to AddSeries RootDir --- NzbDrone.Api/Directories/DirectoryModule.cs | 60 +++++++++++++++++++ NzbDrone.Api/NzbDrone.Api.csproj | 1 + .../NzbDrone.Services.Api.csproj | 2 +- NzbDrone.Web/NzbDrone.Web.csproj | 1 + .../AddSeries/RootFolders/RootDirView.js | 3 + NzbDrone.Web/_backboneApp/Content/base.css | 4 ++ .../_backboneApp/Shared/AutoComplete.js | 16 +++++ 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 NzbDrone.Api/Directories/DirectoryModule.cs create mode 100644 NzbDrone.Web/_backboneApp/Shared/AutoComplete.js diff --git a/NzbDrone.Api/Directories/DirectoryModule.cs b/NzbDrone.Api/Directories/DirectoryModule.cs new file mode 100644 index 000000000..674171e8e --- /dev/null +++ b/NzbDrone.Api/Directories/DirectoryModule.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Nancy; +using NzbDrone.Api.Extentions; +using NzbDrone.Common; +using NzbDrone.Core.RootFolders; + +namespace NzbDrone.Api.Directories +{ + public class DirectoryModule : NzbDroneApiModule + { + private readonly DiskProvider _diskProvider; + + public DirectoryModule(DiskProvider diskProvider) + : base("/directories") + { + _diskProvider = diskProvider; + Post["/"] = x => GetDirectories(); + } + + private Response GetDirectories() + { + if (!Request.Form.query.HasValue) + return new List().AsResponse(); + + string query = Request.Form.query.Value; + + IEnumerable dirs = null; + try + { + //Windows (Including UNC) + var windowsSep = query.LastIndexOf('\\'); + + if (windowsSep > -1) + { + dirs = _diskProvider.GetDirectories(query.Substring(0, windowsSep + 1)); + } + + //Unix + var index = query.LastIndexOf('/'); + + if (index > -1) + { + dirs = _diskProvider.GetDirectories(query.Substring(0, index + 1)); + } + } + catch (Exception) + { + //Swallow the exceptions so proper JSON is returned to the client (Empty results) + return new List().AsResponse(); + } + + if (dirs == null) + throw new Exception("A valid path was not provided"); + + return dirs.AsResponse(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 63e4b4977..96d560fbe 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -92,6 +92,7 @@ + diff --git a/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj b/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj index 6c492fbe6..f347d90b7 100644 --- a/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj +++ b/NzbDrone.Services.Api/NzbDrone.Services.Api.csproj @@ -148,7 +148,7 @@ - True + False True 1306 / diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index bc97a5604..b84df8ad8 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -410,6 +410,7 @@ + diff --git a/NzbDrone.Web/_backboneApp/AddSeries/RootFolders/RootDirView.js b/NzbDrone.Web/_backboneApp/AddSeries/RootFolders/RootDirView.js index b8c65410f..37d074cd7 100644 --- a/NzbDrone.Web/_backboneApp/AddSeries/RootFolders/RootDirView.js +++ b/NzbDrone.Web/_backboneApp/AddSeries/RootFolders/RootDirView.js @@ -2,6 +2,7 @@ /// /// /// +/// NzbDrone.AddSeries.RootDirItemView = Backbone.Marionette.ItemView.extend({ @@ -53,6 +54,8 @@ NzbDrone.AddSeries.RootDirView = Backbone.Marionette.Layout.extend({ this.currentDirs.show(new NzbDrone.AddSeries.RootDirListView({ collection: this.collection })); this.collection.fetch(); + + this.ui.pathInput.folderAutoComplete(); }, diff --git a/NzbDrone.Web/_backboneApp/Content/base.css b/NzbDrone.Web/_backboneApp/Content/base.css index fdbb5dab8..717563d21 100644 --- a/NzbDrone.Web/_backboneApp/Content/base.css +++ b/NzbDrone.Web/_backboneApp/Content/base.css @@ -41,6 +41,10 @@ body { text-align: center; } +.nz-center .typeahead { + text-align: left; +} + #footer-region { font-size: 16px; text-decoration: none; diff --git a/NzbDrone.Web/_backboneApp/Shared/AutoComplete.js b/NzbDrone.Web/_backboneApp/Shared/AutoComplete.js new file mode 100644 index 000000000..fc86e278b --- /dev/null +++ b/NzbDrone.Web/_backboneApp/Shared/AutoComplete.js @@ -0,0 +1,16 @@ +$.fn.folderAutoComplete = function () { + $(this).typeahead({ + source: function (query, process) { + $.ajax({ + url: '/api/directories', + dataType: "json", + type: "POST", + data: { query: query }, + success: function (data) { + process(data); + } + }); + }, + minLength: 3 + }); +} \ No newline at end of file