reworked .render2(..) to support sections...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-09-03 13:42:27 +03:00
parent f5a1ccce77
commit a1d0943d64

View File

@ -3117,20 +3117,16 @@ var BaseBrowserPrototype = {
__renderer__: { __renderer__: {
// placeholders... // placeholders...
root: null, root: null,
options: null,
// renderers... // renderers...
elem: function(elem, index, path){ elem: function(elem, index, path, options){
//return elem.id || elem }, return path.join('/') },
return index }, inline: function(lst, index, path, options){
inline: function(lst, index, path){
return lst }, return lst },
nest: function(header, lst, index, path){ nest: function(header, lst, index, path, options){
header = this.elem(header, index, path)
return [ return [
header, this.elem(header, index, path),
...(lst || []).map(function(e){ ...lst ] },
return header +'/'+ e }) ] },
// render life-cycle... // render life-cycle...
start: function(root, options){ start: function(root, options){
@ -3138,13 +3134,15 @@ var BaseBrowserPrototype = {
Object.create(this), Object.create(this),
{ {
root, root,
options,
}) }, }) },
finalize: function(lst){ finalize: function(sections, options){
return lst.join('\n') }, return Object.entries(sections)
.reduce(function(res, [section, lst]){
return res.concat(lst.join('\n')) }, [])
.join('\n===\n') },
}, },
// XXX need: // XXX need:
// - section rendering... // - section rendering... (DONE)
// - from/to/around/count support... // - from/to/around/count support...
// - ability to render separate items/sub-trees or lists of items... // - ability to render separate items/sub-trees or lists of items...
render2: function(options, renderer){ render2: function(options, renderer){
@ -3161,69 +3159,101 @@ var BaseBrowserPrototype = {
args.pop() args.pop()
: 0 : 0
options = Object.assign(
Object.create(this.options || {}),
{
iterateNonIterable: true,
includeInlinedBlocks: true,
},
options || {})
var render = renderer || this.__renderer__ var render = renderer || this.__renderer__
render = render.root == null ? render = render.root == null ?
render.start( render.start(this, options)
this,
Object.assign(
Object.create(this.options || {}),
{
iterateNonIterable: true,
includeInlinedBlocks: true,
},
options || {}))
: render : render
options = render.options
var section = options.section || '*'
section = section == '*' ?
options.sections
: section
section = (section instanceof Array && section.length == 1) ?
section[0]
: section
// XXX from/to/around/count... // XXX from/to/around/count...
// XXX // XXX
var l var l
return this.walk( return ((render.root === this && section instanceof Array) ?
function(e, i, p, children){ // render list of sections...
// NOTE: since we let the nested browsers render sections section
// of the list, we also need to compensate for the .reduce(function(res, name){
// number of elements they render... res[name] = that.render2(
base_index += (l || []).length Object.assign({},
l = [] options,
i += base_index {
p = base_path.concat(p) section: name,
nonFinalized: true,
}),
render)
return res }, {})
// render single section...
: this.walk(
function(e, i, p, children){
// NOTE: since we let the nested browsers render sections
// of the list, we also need to compensate for the
// number of elements they render...
base_index += (l || []).length
l = []
i += base_index
p = base_path.concat(p)
// do not go down child browsers -- use their mechanics for rendering... // do not go down child browsers -- use their .render2(..)
;(e instanceof BaseBrowser || e.children instanceof BaseBrowser) ;(e instanceof BaseBrowser
&& children(false) || e.children instanceof BaseBrowser)
&& children(false)
return ( return (
// skip... // skip...
// XXX // XXX
false ? false ?
[] []
// inlined... // inlined...
: e instanceof BaseBrowser ? : e instanceof BaseBrowser ?
render.inline(l = e.render2(options, render, i+1, p), i, p) render.inline(
: e instanceof Array ? l = e.render2(options, render, i+1, p),
render.inline(children(true), i, p) i, p, options)
: e instanceof Array ?
render.inline(
children(true),
i, p, options)
// nested... // nested...
: e.children instanceof BaseBrowser ? : e.children instanceof BaseBrowser ?
render.nest(e, render.nest(e,
// NOTE: we handle .collapsed here as the nested // NOTE: we handle .collapsed here as the nested
// browser is one level down and knows nothing // browser is one level down and knows nothing
// of it... // of it...
(options.iterateCollapsed || !e.collapsed) (options.iterateCollapsed || !e.collapsed)
&& (l = e.children.render2(options, render, i+1, p)), && (l = e.children.render2(options, render, i+1, p)),
i, p) i, p, options)
: e.children instanceof Array ? : e.children instanceof Array ?
render.nest(e, children(true), i, p) render.nest(e,
children(true),
i, p, options)
// normal item... // normal item...
: render.elem(e, i, p) ) : render.elem(e, i, p, options) )
}, options) }, options))
// finalize render...
.run(function(){ .run(function(){
return render.root === that ? return (!options.nonFinalized && render.root === that) ?
// finalize render... render.finalize(this instanceof Array ?
render.finalize(this) {[section]: this}
: this, options)
: this }) }, : this }) },