some minor tweaks to browser.select()...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2015-05-25 20:02:47 +03:00
parent bca322950b
commit eb23b632f7
2 changed files with 47 additions and 8 deletions

View File

@ -194,6 +194,7 @@ requirejs(['../lib/keyboard', '../object'], function(k, o){
b = Browser($('.container').last(), { b = Browser($('.container').last(), {
list: function(path){ list: function(path){
console.log('>>>', path)
var cur = TREE var cur = TREE
path.forEach(function(p){ path.forEach(function(p){
cur = cur[p] cur = cur[p]

View File

@ -229,21 +229,34 @@ var BrowserPrototype = {
// -> elem // -> elem
// //
// Select element by sequence number // Select element by sequence number
// NOTE: negative numbers count from the tail.
// NOTE: overflowing selects the first/last element.
// .select(<number>) // .select(<number>)
// -> elem // -> elem
// //
// Select element by its text... // Select element by its text...
// NOTE: if text matches one of the reserved commands above use
// quotes to escape it...
// .select('<text>')
// .select("'<text>'")
// .select('"<text>"') // .select('"<text>"')
// -> elem // -> elem
// //
// Select element via a regular expression...
// .select(<elem>)
// -> elem
//
// .select(<elem>) // .select(<elem>)
// -> elem // -> elem
// //
// This will return a jQuery object. // This will return a jQuery object.
// //
// NOTE: if multiple matches occur this will select the first.
//
// //
// XXX revise return values... // XXX revise return values...
// XXX Q: should this trigger a "select" event??? // XXX Q: should this trigger a "select" event???
// XXX on string/regexp mismatch this will select the first, is this correct???
select: function(elem){ select: function(elem){
var browser = this.dom var browser = this.dom
var elems = browser.find('.list div') var elems = browser.find('.list div')
@ -252,6 +265,7 @@ var BrowserPrototype = {
return $() return $()
} }
elem = elem == 0 ? 'first' : elem
elem = elem || this.select('!') elem = elem || this.select('!')
// if none selected get the first... // if none selected get the first...
elem = elem.length == 0 ? 'first' : elem elem = elem.length == 0 ? 'first' : elem
@ -281,23 +295,41 @@ var BrowserPrototype = {
return elems.filter('.selected') return elems.filter('.selected')
// number... // number...
// NOTE: on overflow this will get the first/last element...
} else if(typeof(elem) == typeof(123)){ } else if(typeof(elem) == typeof(123)){
return this.select($(elems[elem])) return this.select($(elems.slice(elem)[0] || elems.slice(-1)[0] ))
// string... // string...
} else if(typeof(elem) == typeof('str') // XXX on mismatch this will select the first, is this correct???
&& /^'.*'$|^".*"$/.test(elem.trim())){ } else if(typeof(elem) == typeof('str')){
elem = elem.trim().slice(1, -1) if(/^'.*'$|^".*"$/.test(elem.trim())){
elem = elem.trim().slice(1, -1)
}
return this.select(browser.find('.list div') return this.select(browser.find('.list div')
.filter(function(i, e){ .filter(function(i, e){
return $(e).text() == elem return $(e).text() == elem
})) }))
// regexp...
// XXX on mismatch this will select the first, is this correct???
} else if(elem.constructor === RegExp){
return this.select(browser.find('.list div')
.filter(function(i, e){
return elem.test($(e).text())
}))
// element... // element...
} else { } else {
this.select('none') elem = $(elem).first()
browser.find('.path .dir.cur').text(elem.text())
return elem.addClass('selected') if(elem.length == 0){
this.select()
} else {
this.select('none')
browser.find('.path .dir.cur').text(elem.text())
return elem.addClass('selected')
}
} }
}, },
@ -374,7 +406,9 @@ var BrowserPrototype = {
return this return this
} }
var path = this.path.push(elem.text()) var path = this.path
path.push(elem.text())
var res = this.open(path) var res = this.open(path)
@ -382,11 +416,15 @@ var BrowserPrototype = {
}, },
// extension methods... // extension methods...
// XXX this is wrong...
// ...need to actually open something...
open: function(path){ open: function(path){
path = path || this.path
var m = this.options.list var m = this.options.list
return m ? m.call(this, path) : path return m ? m.call(this, path) : path
}, },
list: function(path){ list: function(path){
path = path || this.path
var m = this.options.list var m = this.options.list
return m ? m.call(this, path) : [] return m ? m.call(this, path) : []
}, },