added .item_index, thinking of how to maintain a list...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-02-16 04:25:16 +03:00
parent 86b1793572
commit 32c983f28c

View File

@ -185,6 +185,7 @@ var BaseBrowserPrototype = {
// XXX should we mix item/list options or separate them into sub-objects??? // XXX should we mix item/list options or separate them into sub-objects???
options: null, options: null,
//
// Format: // Format:
// [ // [
// <item> | <browser>, // <item> | <browser>,
@ -198,9 +199,9 @@ var BaseBrowserPrototype = {
// ... // ...
// } // }
// //
// XXX should this be a list or a Map/Object???? // NOTE: this can't be a map/dict as we need both order manipulation
// ...we do not need ultra fast traversal but we do need a way // and nested structures which would overcomplicate things, as
// to identify and select items in a unique way... // a compromise we use .item_index blow for item identification.
__items: null, __items: null,
get items(){ get items(){
this.__items this.__items
@ -209,28 +210,66 @@ var BaseBrowserPrototype = {
set items(value){ set items(value){
this.__items = value }, this.__items = value },
//
// Format:
// {
// <key>: <item>,
// ...
// }
//
// XXX need to maintain this over item add/remove/change...
// XXX Q: should we be able to add/remove/change items outside of .__list__(..)???
// ...only some item updates (how .collapsed is handled) make
// since at this time -- need to think about this more
// carefully + strictly document the result...
item_index: null,
// Item list constructor...
// //
// .__list__(make, options) // .__list__(make, options)
// -> undefined // -> undefined
// -> list // -> list
// //
// //
// make(value, options) // Item constructor:
// -> make // make(value)
// make(value, options)
// -> make
// //
// //
// There are two modes of operation: // There are two modes of operation:
// 1) call make(..) to create items // 1) call make(..) to create items
// 2) return a list of items // 2) return a list of items
// //
// The if make is called at least once the return value is ignored.
// //
// The if make(..) is called at least once the return value is
// ignored (mode #1), otherwise, the returned list is used as the
// .items structure.
//
//
// When calling make(..) (mode #1) the item is built by combining
// the following in order:
// - original item (.items[key]) if present,
// - options passed to .make(<options>) method calling .__list__(..),
// - options passed to make(.., <options>) constructing the item,
// - {value: <value>} where <value> passed to make(<value>, ..)
//
// Each of the above will override values of the previous sections.
//
// The resulting item is stored in:
// .items
// .item_index (keyed via .id or JSONified .value)
//
// Each of the above structures is reset on each call to .make(..)
// //
// Example: // Example:
// XXX // XXX
// //
// //
// In mode #2 XXX
//
//
// NOTE: this is not designed to be called directly... // NOTE: this is not designed to be called directly...
__list__: function(make, options){ __list__: function(make, options){
throw new Error('.__list__(..): Not implemented.') }, throw new Error('.__list__(..): Not implemented.') },
@ -239,37 +278,49 @@ var BaseBrowserPrototype = {
// Make .items... // Make .items...
// //
// .make() // .make()
// .make(options)
// -> this // -> this
// //
// The items are constructed by passing a make function to .__list__(..)
// which in turn will call this make(..) per item created.
//
// For more doc on item construction see: .__init__(..)
//
//
// NOTE: each call to this will reset both .items and .item_index
//
// XXX revise options handling for .__list__(..) // XXX revise options handling for .__list__(..)
// XXX add persistent items...
// ...take values from corresponding .items[key]
// to do this we need a simple way to access an item...
// .....might be a good idea to simply load the .items[key] and
// assign/update it with the new values...
// XXX make .items a "dict"... (???)
// - Item.group(..) would need to be reworked...
// - nesting/ordering would need to be rethought...
// ...is it worth it???
// can item access via index be done via a separate index???
make: function(options){ make: function(options){
var items = this.items = [] var items = this.items = []
var old_index = this.item_index || {}
var new_index = this.item_index = {}
// item constructor... // item constructor...
// //
// make(..) // make(value)
// make(value, options)
// -> make // -> make
// //
var make_called = false var make_called = false
var make = function(value, opts){ var make = function(value, opts){
make_called = true make_called = true
items.push(Object.assign( opts = opts || {}
{},
// XXX get default values from corresponding .item[..]... // XXX revise id generation...
// XXX var key = opts.id || JSON.stringify(value)
// build the item...
var item = Object.assign({},
// get the old item values...
old_index[key] || {},
options || {}, options || {},
opts || {}, opts,
{value: value})) {value: value})
// store the item...
items.push(item)
new_index[key] = item
return make return make
}.bind(this) }.bind(this)
make.__proto__ = Items make.__proto__ = Items