reworked .map(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-03-23 16:30:31 +03:00
parent 12efd8870c
commit 71e2806917

View File

@ -1147,79 +1147,75 @@ var BaseBrowserPrototype = {
path = path instanceof Array ? path : [path] path = path instanceof Array ? path : [path]
var options = args.pop() || {} var options = args.pop() || {}
// options...
options = Object.assign(Object.create(this.options || {}), options || {}) options = Object.assign(Object.create(this.options || {}), options || {})
var iterateNonIterable = options.iterateAll || options.iterateNonIterable var iterateNonIterable = options.iterateAll || options.iterateNonIterable
var iterateCollapsed = options.iterateAll || options.iterateCollapsed var iterateCollapsed = options.iterateAll || options.iterateCollapsed
var skipNested = !options.iterateAll && options.skipNested var skipNested = !options.iterateAll && options.skipNested
// XXX this does not handle nesting in a correct manner
var reverse = !!options.reverseIteration var reverse = !!options.reverseIteration
var doElem = function(elem){ // NOTE: func is in closure as it will not change within one run
// on any level of nesting...
var doElem = function(elem, path){
return [func ? return [func ?
func.call(that, elem, path.concat(elem.id), that) func.call(that, elem, path.concat(elem.id), that)
: elem] } : elem] }
var doLevel = function(elem, nested, path){
var _render return [
return this.items doElem(elem, path),
// check if we need to go from the end... (!iterateCollapsed && elem.collapsed) ?
[]
: nested ]
// reverse level order...
.run(function(){
reverse
&& this.reverse() })
.flat() }
// NOTE: we need to reverse two things: // NOTE: we need to reverse two things:
// - level order (done here) // - level order (done here)
// - linearization order (done below) // - linearization order (done below)
var walk = function(path, list){
return list
// reverse the items...
.run(function(){ .run(function(){
return reverse ? return reverse ?
// NOTE: we .slice() as we do not want to affect
// the actual list...
this.slice().reverse() this.slice().reverse()
: this }) : this })
// XXX need path threaded into this -- not sure how to do it yet... .map(function(elem){
.map(_render = function(elem){
return ( return (
// item not iterable -- skip... // item not iterable -> skip...
(!iterateNonIterable && elem.noniterable) ? (!iterateNonIterable && elem.noniterable) ?
[] []
// value is Browser (inline) -- list browser items... // value is Browser (inline)...
: elem.value instanceof Browser ? : elem.value instanceof Browser ?
elem.value.map(func, elem.value.map(func,
options.inlinedPaths ? options.inlinedPaths ?
path.concat(elem.id) path.concat(elem.id)
: path.slice(), : path.slice(),
options) options)
// .sublist is Browser (nested) -- list header then browser items... // .sublist is Browser (nested)...
: (!skipNested : (!skipNested
&& elem.sublist instanceof Browser) ? && elem.sublist instanceof Browser) ?
[doElem(elem), doLevel(
(!iterateCollapsed && elem.collapsed) ? elem,
[] elem.sublist
: elem.sublist.map(func, path.concat(elem.id), options)] .map(func, path.concat(elem.id), options),
// handle reverse... path)
.run(function(){ // .sublist is Array (nested)...
reverse
&& this.reverse() })
.flat()
// .sublist is Array (nested) -- list header then array content...
// XXX this skips nested browser .sublists...
: (!skipNested : (!skipNested
&& elem.sublist instanceof Array) ? && elem.sublist instanceof Array) ?
doElem(elem) doLevel(
.concat((!iterateCollapsed && elem.collapsed) ? elem,
[] walk(path.concat(elem.id), elem.sublist),
/*/ XXX need to path.concat(..) in the context for this... path)
: elem.sublist.map(_render)).flat() // normal item...
/*/ : doElem(elem, path) ) })
: (func ? .flat() }
elem.sublist
.map(function(e){ return walk(path, this.items)
return func.call(that, e, path.concat(elem.id, e.id), that) }) },
: elem.sublist.slice()))
//*/
// handle reverse...
// XXX if we support nested browsers in lists
// this will mess things up...
.run(function(){
reverse
&& this.reverse() })
// normal item -- list...
: doElem(elem) ) })
.flat() },
// Sublist map functions... // Sublist map functions...
// NOTE: there are different from .map(..) in that instead of paths // NOTE: there are different from .map(..) in that instead of paths