fixed a couple of bugs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-05-27 19:16:51 +03:00
parent f53afb410c
commit 952cfe7031

View File

@ -340,6 +340,7 @@ var makeItemEventMethod = function(event, handler, default_item, filter, options
filter = args[0] instanceof Function filter = args[0] instanceof Function
&& args.shift() && args.shift()
var filterItems = function(items){ var filterItems = function(items){
items = items instanceof Array ? items : [items]
return filter ? return filter ?
items.filter(filter) items.filter(filter)
: items } : items }
@ -350,6 +351,7 @@ var makeItemEventMethod = function(event, handler, default_item, filter, options
// distinguish one from the other... // distinguish one from the other...
{ noQueryCheck: true }, { noQueryCheck: true },
options || {}) options || {})
var getter = options.getMode || 'search'
// base event method... // base event method...
// NOTE: this is not returned directly as we need to query the items // NOTE: this is not returned directly as we need to query the items
// and pass those on to the handlers rather than the arguments // and pass those on to the handlers rather than the arguments
@ -382,7 +384,7 @@ var makeItemEventMethod = function(event, handler, default_item, filter, options
.unique()) .unique())
// explicit item or query... // explicit item or query...
: item != null ? : item != null ?
filterItems(this.search(item, options)) filterItems(this[getter](item, options))
// item is null or undefined -- get default... // item is null or undefined -- get default...
: default_item instanceof Function ? : default_item instanceof Function ?
[default_item.call(that) || []].flat() [default_item.call(that) || []].flat()
@ -1522,8 +1524,6 @@ var BaseBrowserPrototype = {
// first result only. // first result only.
// //
// XXX should we be able to get offset values relative to any match? // XXX should we be able to get offset values relative to any match?
// XXX should we use .wald2(..) here???
// XXX revise return value...
get: function(pattern, options){ get: function(pattern, options){
var args = [...arguments] var args = [...arguments]
pattern = args.shift() pattern = args.shift()
@ -1532,7 +1532,7 @@ var BaseBrowserPrototype = {
: pattern : pattern
var offset = (pattern == 'next' || pattern == 'prev') var offset = (pattern == 'next' || pattern == 'prev')
&& typeof(args[0]) == typeof(123) ? && typeof(args[0]) == typeof(123) ?
args.shift() args.shift() + 1
: 1 : 1
var func = args[0] instanceof Function ? var func = args[0] instanceof Function ?
args.shift() args.shift()
@ -1546,36 +1546,36 @@ var BaseBrowserPrototype = {
// NOTE: we do not care about return values here as we'll return // NOTE: we do not care about return values here as we'll return
// via stop(..)... // via stop(..)...
var res = [] var b = pattern == 'prev' ? [] : null
return [ return [
// next + offset... // next + offset...
pattern == 'next' ? pattern == 'next' ?
this.search(true, this.search(true,
function(elem, i, path, stop){ function(elem, i, path, stop){
if(elem.focused == true){ if(elem.focused == true){
res = offset + 1 b = offset
// get the offset item... // get the offset item...
} else if(res <= 0){ } else if(b != null && b <= 0){
stop([func(elem, i, path)]) stop([func(elem, i, path)])
} }
// countdown to offset... // countdown to offset...
res = typeof(res) == typeof(123) ? b = typeof(b) == typeof(123) ?
res - 1 b - 1
: res }, : b },
options) options)
// prev + offset... // prev + offset...
: pattern == 'prev' ? : pattern == 'prev' ?
this.search(true, this.search(true,
function(elem, i, path, stop){ function(elem, i, path, stop){
elem.focused == true elem.focused == true
&& stop([func(res.length >= offset ? && stop([func(...(b.length >= offset ?
res[0] b[0]
: undefined)]) : [undefined]))])
// buffer the previous offset items... // buffer the previous offset items...
res.push((elem, i, path)) b.push([elem, i, path])
res.length > offset b.length > offset
&& res.shift() }, && b.shift() },
options) options)
// base case -> get first match... // base case -> get first match...
: this.search(pattern, : this.search(pattern,
@ -1592,9 +1592,6 @@ var BaseBrowserPrototype = {
sublists: function(func, options){ sublists: function(func, options){
return this.search({children: true}, func, options) }, return this.search({children: true}, func, options) },
next: function(){},
prev: function(){},
// XXX should there return an array or a .constructor(..) instance?? // XXX should there return an array or a .constructor(..) instance??
// XXX should these call respective methods (.forEach(..), .filter(..), // XXX should these call respective methods (.forEach(..), .filter(..),
// .reduce(..)) on the nested browsers??? // .reduce(..)) on the nested browsers???
@ -2189,7 +2186,8 @@ var BaseBrowserPrototype = {
&& (item.focused = true) && (item.focused = true)
}, },
// default... // default...
function(){ return this.get(0) }), function(){ return this.get(0) },
{ getMode: 'get' }),
blur: makeItemEventMethod('blur', function(evt, items){ blur: makeItemEventMethod('blur', function(evt, items){
items.forEach(function(item){ items.forEach(function(item){
delete item.focused }) }), delete item.focused }) }),
@ -2198,6 +2196,8 @@ var BaseBrowserPrototype = {
'focus', 'blur', 'focus', 'blur',
function(){ return this.focused || 0 }, function(){ return this.focused || 0 },
false), false),
next: function(){ return this.focus('next') },
prev: function(){ return this.focus('prev') },
select: makeItemEventMethod('select', select: makeItemEventMethod('select',
function(evt, items){ function(evt, items){
@ -2734,8 +2734,8 @@ var BrowserPrototype = {
left: function(){}, left: function(){},
right: function(){}, right: function(){},
next: function(){}, //next: function(){},
prev: function(){}, //prev: function(){},
//collapse: function(){}, //collapse: function(){},
// XXX scroll... // XXX scroll...