Improved folder select

This commit is contained in:
Ruud
2012-09-25 14:22:48 +02:00
parent d40ad1ddf2
commit 08ae51dbe6
3 changed files with 68 additions and 16 deletions
+24 -1
View File
@@ -27,6 +27,8 @@ class FileBrowser(Plugin):
}, },
'return': {'type': 'object', 'example': """{ 'return': {'type': 'object', 'example': """{
'is_root': bool, //is top most folder 'is_root': bool, //is top most folder
'parent': string, //parent folder of requested path
'home': string, //user home folder
'empty': bool, //directory is empty 'empty': bool, //directory is empty
'dirs': array, //directory names 'dirs': array, //directory names
}"""} }"""}
@@ -64,14 +66,35 @@ class FileBrowser(Plugin):
path = getParam('path', '/') 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: try:
dirs = self.getDirectories(path = path, show_hidden = getParam('show_hidden', True)) dirs = self.getDirectories(path = path, show_hidden = getParam('show_hidden', True))
except: except:
dirs = [] 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({ return jsonified({
'is_root': path == '/' or not path, 'is_root': path == '/',
'empty': len(dirs) == 0, 'empty': len(dirs) == 0,
'parent': parent,
'home': home + os.path.sep,
'platform': os.name,
'dirs': dirs, 'dirs': dirs,
}) })
+31 -15
View File
@@ -635,7 +635,7 @@ Option.Directory = new Class({
), ),
self.dir_list = new Element('ul', { self.dir_list = new Element('ul', {
'events': { 'events': {
'click:relay(li)': function(e, el){ 'click:relay(li:not(.empty))': function(e, el){
(e).preventDefault(); (e).preventDefault();
self.selectDirectory(el.get('data-value')) self.selectDirectory(el.get('data-value'))
}, },
@@ -701,12 +701,24 @@ Option.Directory = new Class({
fillBrowser: function(json){ fillBrowser: function(json){
var self = this; var self = this;
var v = self.input.get('text'); self.data = json;
var v = self.getValue();
var previous_dir = self.getParentDir(); var previous_dir = self.getParentDir();
if(v == '')
self.input.set('text', json.home);
if(previous_dir != v && previous_dir.length >= 1 && !json.is_root){ 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('data-value', previous_dir)
self.back_button.set('html', '« '+self.getCurrentDirname(previous_dir)) self.back_button.set('html', '« '+prev_dirname)
self.back_button.show() self.back_button.show()
} }
else { else {
@@ -719,23 +731,24 @@ Option.Directory = new Class({
else else
self.cached[v] = json; self.cached[v] = json;
setTimeout(function(){ self.dir_list.empty();
self.dir_list.empty(); if(json.dirs.length > 0)
json.dirs.each(function(dir){ json.dirs.each(function(dir){
if(dir.indexOf(v) != -1){ new Element('li', {
new Element('li', { 'data-value': dir,
'data-value': dir, 'text': self.getCurrentDirname(dir)
'text': self.getCurrentDirname(dir) }).inject(self.dir_list)
}).inject(self.dir_list)
}
}); });
}, 50); else
new Element('li.empty', {
'text': 'Selected folder is empty'
}).inject(self.dir_list)
}, },
getDirs: function(){ getDirs: function(){
var self = this; var self = this;
var c = self.input.get('text'); var c = self.getValue();
if(self.cached[c] && self.use_cache){ if(self.cached[c] && self.use_cache){
self.fillBrowser() self.fillBrowser()
@@ -754,7 +767,10 @@ Option.Directory = new Class({
getParentDir: function(dir){ getParentDir: function(dir){
var self = this; 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 sep = Api.getOption('path_sep');
var dirs = v.split(sep); var dirs = v.split(sep);
if(dirs.pop() == '') if(dirs.pop() == '')
@@ -941,7 +957,7 @@ Option.Choice = new Class({
mtches.append([value == matchsplit ? match : matchsplit]); mtches.append([value == matchsplit ? match : matchsplit]);
}); });
}); });
if(mtches.length == 0 && value != '') if(mtches.length == 0 && value != '')
mtches.include(value); mtches.include(value);
@@ -235,6 +235,19 @@
.page .directory_list li:hover { .page .directory_list li:hover {
background-color: #515c68; 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 { .page .directory_list .actions {
clear: both; clear: both;