From 08ae51dbe615e7d847adbfd9f770d4b6db87a749 Mon Sep 17 00:00:00 2001 From: Ruud Date: Tue, 25 Sep 2012 14:22:48 +0200 Subject: [PATCH] Improved folder select --- couchpotato/core/plugins/browser/main.py | 25 ++++++++++- couchpotato/static/scripts/page/settings.js | 46 ++++++++++++++------- couchpotato/static/style/page/settings.css | 13 ++++++ 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/couchpotato/core/plugins/browser/main.py b/couchpotato/core/plugins/browser/main.py index aa11b220..b84284b1 100644 --- a/couchpotato/core/plugins/browser/main.py +++ b/couchpotato/core/plugins/browser/main.py @@ -27,6 +27,8 @@ class FileBrowser(Plugin): }, 'return': {'type': 'object', 'example': """{ 'is_root': bool, //is top most folder + 'parent': string, //parent folder of requested path + 'home': string, //user home folder 'empty': bool, //directory is empty 'dirs': array, //directory names }"""} @@ -64,14 +66,35 @@ class FileBrowser(Plugin): path = getParam('path', '/') + # Set proper home dir for some systems + try: + import pwd + os.environ['HOME'] = pwd.getpwuid(os.geteuid()).pw_dir + except: + pass + + home = os.path.expanduser('~') + + if not path: + path = home + try: dirs = self.getDirectories(path = path, show_hidden = getParam('show_hidden', True)) except: dirs = [] + parent = os.path.dirname(path.rstrip(os.path.sep)) + if parent == path.rstrip(os.path.sep): + parent = '/' + elif parent != '/' and parent[-2:] != ':\\': + parent += os.path.sep + return jsonified({ - 'is_root': path == '/' or not path, + 'is_root': path == '/', 'empty': len(dirs) == 0, + 'parent': parent, + 'home': home + os.path.sep, + 'platform': os.name, 'dirs': dirs, }) diff --git a/couchpotato/static/scripts/page/settings.js b/couchpotato/static/scripts/page/settings.js index 4352fb2c..161da685 100644 --- a/couchpotato/static/scripts/page/settings.js +++ b/couchpotato/static/scripts/page/settings.js @@ -635,7 +635,7 @@ Option.Directory = new Class({ ), self.dir_list = new Element('ul', { 'events': { - 'click:relay(li)': function(e, el){ + 'click:relay(li:not(.empty))': function(e, el){ (e).preventDefault(); self.selectDirectory(el.get('data-value')) }, @@ -701,12 +701,24 @@ Option.Directory = new Class({ fillBrowser: function(json){ var self = this; - var v = self.input.get('text'); + self.data = json; + + var v = self.getValue(); var previous_dir = self.getParentDir(); + if(v == '') + self.input.set('text', json.home); + if(previous_dir != v && previous_dir.length >= 1 && !json.is_root){ + + var prev_dirname = self.getCurrentDirname(previous_dir); + if(previous_dir == json.home) + prev_dirname = 'Home'; + else if (previous_dir == '/' && json.platform == 'nt') + prev_dirname = 'Computer'; + self.back_button.set('data-value', previous_dir) - self.back_button.set('html', '« '+self.getCurrentDirname(previous_dir)) + self.back_button.set('html', '« '+prev_dirname) self.back_button.show() } else { @@ -719,23 +731,24 @@ Option.Directory = new Class({ else self.cached[v] = json; - setTimeout(function(){ - self.dir_list.empty(); + self.dir_list.empty(); + if(json.dirs.length > 0) json.dirs.each(function(dir){ - if(dir.indexOf(v) != -1){ - new Element('li', { - 'data-value': dir, - 'text': self.getCurrentDirname(dir) - }).inject(self.dir_list) - } + new Element('li', { + 'data-value': dir, + 'text': self.getCurrentDirname(dir) + }).inject(self.dir_list) }); - }, 50); + else + new Element('li.empty', { + 'text': 'Selected folder is empty' + }).inject(self.dir_list) }, getDirs: function(){ var self = this; - var c = self.input.get('text'); + var c = self.getValue(); if(self.cached[c] && self.use_cache){ self.fillBrowser() @@ -754,7 +767,10 @@ Option.Directory = new Class({ getParentDir: function(dir){ var self = this; - var v = dir || self.input.get('text'); + if(!dir && self.data && self.data.parent) + return self.data.parent; + + var v = dir || self.getValue(); var sep = Api.getOption('path_sep'); var dirs = v.split(sep); if(dirs.pop() == '') @@ -941,7 +957,7 @@ Option.Choice = new Class({ mtches.append([value == matchsplit ? match : matchsplit]); }); }); - + if(mtches.length == 0 && value != '') mtches.include(value); diff --git a/couchpotato/static/style/page/settings.css b/couchpotato/static/style/page/settings.css index 475e61e6..89f81e74 100644 --- a/couchpotato/static/style/page/settings.css +++ b/couchpotato/static/style/page/settings.css @@ -235,6 +235,19 @@ .page .directory_list li:hover { background-color: #515c68; } + + .page .directory_list li.empty { + background: none; + height: 100px; + text-align: center; + font-style: italic; + border: none; + line-height: 100px; + cursor: default; + color: #BBB; + text-shadow: none; + font-size: 12px; + } .page .directory_list .actions { clear: both;