added .reveal(..) + tweaking and refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-03-17 23:30:27 +03:00
parent 566f833ae6
commit 46931af4da

View File

@ -279,7 +279,7 @@ var BaseBrowserPrototype = {
// //
// NOTE: this can't be a map/dict as we need both order manipulation // NOTE: this can't be a map/dict as we need both order manipulation
// and nested structures which would overcomplicate things, as // and nested structures which would overcomplicate things, as
// a compromise we use .item_index below for item identification. // a compromise we use .item_key_index below for item identification.
__items: null, __items: null,
get items(){ get items(){
this.__items this.__items
@ -305,15 +305,15 @@ var BaseBrowserPrototype = {
// XXX can we make the format here simpler with less level // XXX can we make the format here simpler with less level
// of indirection?? // of indirection??
// ...currently to go down a path we need to: // ...currently to go down a path we need to:
// this.item_index.A.sublist.item_index.B.sublist... // this.item_key_index.A.sublist.item_key_index.B.sublist...
// would be nice to be closer to: // would be nice to be closer to:
// this.A.B... // this.A.B...
__item_index: null, __item_index: null,
get item_index(){ get item_key_index(){
this.__item_index this.__item_index
|| this.make() || this.make()
return this.__item_index }, return this.__item_index },
set item_index(value){ set item_key_index(value){
this.__item_index = value }, this.__item_index = value },
@ -398,7 +398,7 @@ var BaseBrowserPrototype = {
// //
// The resulting item is stored in: // The resulting item is stored in:
// .items // .items
// .item_index (keyed via .id or JSONified .value) // .item_key_index (keyed via .id or JSONified .value)
// //
// Each of the above structures is reset on each call to .make(..) // Each of the above structures is reset on each call to .make(..)
// //
@ -479,7 +479,7 @@ var BaseBrowserPrototype = {
// Make .items and .item_index... // Make .items and .item_key_index...
// //
// .make() // .make()
// .make(options) // .make(options)
@ -491,7 +491,7 @@ var BaseBrowserPrototype = {
// For more doc on item construction see: .__init__(..) // For more doc on item construction see: .__init__(..)
// //
// //
// NOTE: each call to this will reset both .items and .item_index // NOTE: each call to this will reset both .items and .item_key_index
// NOTE: for items with repeating values there is no way to correctly // NOTE: for items with repeating values there is no way to correctly
// identify an item thus no state is maintained between .make(..) // identify an item thus no state is maintained between .make(..)
// calls for such items... // calls for such items...
@ -742,13 +742,6 @@ var BaseBrowserPrototype = {
// XXX item API... // XXX item API...
// //
// .get()
// .get(id)
// .get(index)
// .get(path)
// -> item
// -> undefined
//
// XXX add path support... // XXX add path support...
// XXX add literal item support (???) // XXX add literal item support (???)
// XXX do not get .subtree elements of a .collapsed item... // XXX do not get .subtree elements of a .collapsed item...
@ -822,12 +815,12 @@ var BaseBrowserPrototype = {
var k = this.__value2key__(key) var k = this.__value2key__(key)
// direct match... // direct match...
if(k in this.item_index){ if(k in this.item_key_index){
return this.item_index[k] return this.item_key_index[k]
} }
// query nested... // query nested...
var nested = Object.values(this.item_index) var nested = Object.values(this.item_key_index)
.filter(function(e){ .filter(function(e){
return e.sublist instanceof Browser }) return e.sublist instanceof Browser })
while(nested.length > 0){ while(nested.length > 0){
@ -842,6 +835,17 @@ var BaseBrowserPrototype = {
return undefined return undefined
}, },
// Get item...
//
// .get()
// .get(id)
// .get(index)
// .get(path)
// -> item
// -> undefined
//
// options format: the same as for .map(..).
//
// XXX this is not too fast for indexing very long lists... // XXX this is not too fast for indexing very long lists...
get: function(key, options){ get: function(key, options){
key = key == null ? 0 : key key = key == null ? 0 : key
@ -853,29 +857,34 @@ var BaseBrowserPrototype = {
[key] [key]
: key : key
options = options || {} options = options || {}
var iterateCollapsed = options.iterateAll || options.iterateCollapsed
// get path... // get path...
if(key instanceof Array){ if(key instanceof Array){
var res = this.item_index[key.shift()] var res = this.item_key_index[key.shift()]
return key.length == 0 ? return key.length == 0 ?
res res
// nested... // nested...
: options.iterateCollapsed || !res.collapsed ? : iterateCollapsed || !res.collapsed ?
res.sublist.get(key, options) res.sublist.get(key, options)
: undefined } : undefined }
// search... // get index...
// XXX getting an element by index is o(n) and not o(1)...
// ...unless we cache .sublists() not sure if this can be
// made better in the general case...
var i = 0 var i = 0
var res var res
var Stop = new Error() var Stop = new Error('.get(..): Result found exception.')
try { try {
this.map(function(e){ this.map(function(e){
res = key == i ? res = key == i ?
e e
: res : res
if(res){ if(res){
throw Stop throw Stop }
} i++
}, options) }, options)
} catch(e){ } catch(e){
if(e === Stop){ if(e === Stop){
@ -886,6 +895,27 @@ var BaseBrowserPrototype = {
return res return res
}, },
// Like .get(.., {iterateCollapsed: true}) but will expand all the
// path items to reveal the target...
reveal: function(key, options){
// get the item...
var res = this.get(key, Object.assign({iterateCollapsed: true}, options))
// expand the path up...
var cur = res.parent
while(cur && cur.parent instanceof Browser){
delete (cur.parent.item_key_index[cur.id]
|| cur.parent.items
.filter(function(e){
return e.sublist === cur })
.shift()).collapsed
cur = cur.parent }
// re-render...
this.render()
return res
},
// Extended map... // Extended map...