mirror of
https://github.com/flynx/ImageGrid.git
synced 2025-10-29 18:30:09 +00:00
cleanup...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
98412ab28c
commit
08850cfd5c
@ -1029,97 +1029,6 @@ var BaseBrowserPrototype = {
|
|||||||
renderGroup: function(items, context){
|
renderGroup: function(items, context){
|
||||||
return items },
|
return items },
|
||||||
|
|
||||||
// Render state...
|
|
||||||
//
|
|
||||||
// .render()
|
|
||||||
// .render(options)
|
|
||||||
// .render(context)
|
|
||||||
// -> state
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// context format:
|
|
||||||
// {
|
|
||||||
// root: <root-browser>,
|
|
||||||
// options: <options>,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// NOTE: currently options and context are distinguished only via
|
|
||||||
// the .options attribute...
|
|
||||||
//
|
|
||||||
// XXX should this use .map(..) internally???
|
|
||||||
// ...there are two ways to go:
|
|
||||||
// - local recursion (as-is now or via .map(.., { .. }))
|
|
||||||
// - outer recursion (.map(..))
|
|
||||||
/* XXX LEGACY...
|
|
||||||
render: function(options){
|
|
||||||
var that = this
|
|
||||||
// XXX Q: should options and context be distinguished only via
|
|
||||||
// the .options attr as is the case now???
|
|
||||||
// ...see no reason why not, though it does not feel right...
|
|
||||||
var context = (options == null || options.options == null) ?
|
|
||||||
{
|
|
||||||
root: this,
|
|
||||||
// NOTE: we are not combining this with .options as nested
|
|
||||||
// lists can have their own unique sets of options
|
|
||||||
// independently of the root list...
|
|
||||||
//options: options || this.options || {},
|
|
||||||
options: Object.assign(Object.create(this.options || {}), options || {}),
|
|
||||||
}
|
|
||||||
: options
|
|
||||||
options = context.options
|
|
||||||
|
|
||||||
// render the items...
|
|
||||||
var _render
|
|
||||||
// XXX should we control render parameters (range, start, end, ...)
|
|
||||||
// from outside render and pass this info down to nested lists???
|
|
||||||
// ...if yes how??
|
|
||||||
// - options
|
|
||||||
// - arg threading
|
|
||||||
var items = this.items
|
|
||||||
.map(_render = function(item, i){
|
|
||||||
return (
|
|
||||||
// group...
|
|
||||||
item instanceof Array ?
|
|
||||||
that.renderGroup(
|
|
||||||
item.map(_render), context)
|
|
||||||
// renderable item...
|
|
||||||
: item.render instanceof Function ?
|
|
||||||
item.render(context)
|
|
||||||
// renderable value -- embedded list...
|
|
||||||
: (item.value || {}).render instanceof Function ?
|
|
||||||
item.value.render(context)
|
|
||||||
// .sublist -- nested list...
|
|
||||||
: item.sublist ?
|
|
||||||
that.renderNested(
|
|
||||||
that.renderNestedHeader(item, i, context),
|
|
||||||
// collapsed...
|
|
||||||
(item.collapsed ?
|
|
||||||
null
|
|
||||||
// renderable...
|
|
||||||
: item.sublist.render instanceof Function ?
|
|
||||||
item.sublist.render(context)
|
|
||||||
// list of items...
|
|
||||||
: item.sublist.map(_render)),
|
|
||||||
item,
|
|
||||||
context)
|
|
||||||
// basic item...
|
|
||||||
: that.renderItem(item, i, context)) })
|
|
||||||
.filter(function(e){
|
|
||||||
return e != null })
|
|
||||||
|
|
||||||
// determine the render mode...
|
|
||||||
return context.root === this ?
|
|
||||||
// root context -> render list and return this...
|
|
||||||
this.renderList(items, context)
|
|
||||||
// non-root context -> return items as-is...
|
|
||||||
// XXX should this be a list of the return value of a
|
|
||||||
// renderer like .renderNested(..) ???
|
|
||||||
: items
|
|
||||||
},
|
|
||||||
//*/
|
|
||||||
|
|
||||||
|
|
||||||
// Render state...
|
// Render state...
|
||||||
//
|
//
|
||||||
// .render()
|
// .render()
|
||||||
@ -1218,107 +1127,6 @@ var BaseBrowserPrototype = {
|
|||||||
.render(options) },
|
.render(options) },
|
||||||
|
|
||||||
|
|
||||||
// XXX item API...
|
|
||||||
//
|
|
||||||
/* XXX this is a more complicated version of .get(..) that should be
|
|
||||||
// a bit faster on very large lists -- o(1) on direct non-nested
|
|
||||||
// indexing vs o(n) for the same case in the new .get(..)
|
|
||||||
// implementation...
|
|
||||||
// XXX add path support...
|
|
||||||
// XXX add literal item support (???)
|
|
||||||
// XXX do not get .subtree elements of a .collapsed item...
|
|
||||||
// XXX skip .noniterable items...
|
|
||||||
get: function(key, options){
|
|
||||||
key = key == null ? 0 : key
|
|
||||||
options = Object.assign(Object.create(this.options || {}), options || {})
|
|
||||||
|
|
||||||
// index...
|
|
||||||
if(typeof(key) == typeof(123)){
|
|
||||||
|
|
||||||
var items = this.items
|
|
||||||
// XXX cache this (prop?)...
|
|
||||||
var sublists = this.sublists()
|
|
||||||
.map(function(e, i){
|
|
||||||
return [e, i] })
|
|
||||||
.compact()
|
|
||||||
|
|
||||||
var i = 0
|
|
||||||
var nested = 0
|
|
||||||
var offset = 0
|
|
||||||
|
|
||||||
do {
|
|
||||||
var x = key - offset + nested
|
|
||||||
// direct match...
|
|
||||||
// XXX skip .noniterable...
|
|
||||||
if(sublists.length == 0 || x < sublists[0][1]){
|
|
||||||
return items[x]
|
|
||||||
}
|
|
||||||
|
|
||||||
// query the sublist...
|
|
||||||
var [sublist, i] = sublists.shift()
|
|
||||||
nested += 1
|
|
||||||
|
|
||||||
// inlined...
|
|
||||||
if(sublist.value instanceof Browser){
|
|
||||||
var res = sublist.value.get(x - i, options)
|
|
||||||
|
|
||||||
// nested...
|
|
||||||
// XXX support .collapsed...
|
|
||||||
} else {
|
|
||||||
var res = x - i == 0 ?
|
|
||||||
sublist
|
|
||||||
: sublist.sublist instanceof Browser ?
|
|
||||||
// NOTE: we are decrementing here to compensate
|
|
||||||
// for the header...
|
|
||||||
sublist.sublist.get(x - i - 1, options)
|
|
||||||
: sublist.sublist[x - i - 1]
|
|
||||||
// account for the header...
|
|
||||||
offset += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if(res !== undefined){
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: we need to get the full length here rather than
|
|
||||||
// the number of iterable elements -> prefer .lengthAll...
|
|
||||||
offset += (sublist.sublist || sublist.value).lengthAll
|
|
||||||
|| (sublist.sublist || sublist.value).length
|
|
||||||
|
|
||||||
// NOTE: we do not need an explicit exit here as the first
|
|
||||||
// test will bail us out as soon as sublists are
|
|
||||||
// depleted...
|
|
||||||
} while(true)
|
|
||||||
|
|
||||||
|
|
||||||
// key...
|
|
||||||
// XXX account for paths...
|
|
||||||
} else {
|
|
||||||
// XXX
|
|
||||||
var k = this.__value2key__(key)
|
|
||||||
|
|
||||||
// direct match...
|
|
||||||
if(k in this.item_key_index){
|
|
||||||
return this.item_key_index[k]
|
|
||||||
}
|
|
||||||
|
|
||||||
// query nested...
|
|
||||||
var nested = Object.values(this.item_key_index)
|
|
||||||
.filter(function(e){
|
|
||||||
return e.sublist instanceof Browser })
|
|
||||||
while(nested.length > 0){
|
|
||||||
var n = nested.shift().sublist
|
|
||||||
var res = n.get(key)
|
|
||||||
if(res !== undefined){
|
|
||||||
// return first match...
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined
|
|
||||||
},
|
|
||||||
//*/
|
|
||||||
|
|
||||||
// Get item...
|
// Get item...
|
||||||
//
|
//
|
||||||
// .get()
|
// .get()
|
||||||
@ -1557,158 +1365,6 @@ var BaseBrowserPrototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
/* XXX LEGACY...
|
|
||||||
// Extended map...
|
|
||||||
//
|
|
||||||
// Generic map...
|
|
||||||
// .map([options])
|
|
||||||
// .map(func[, options])
|
|
||||||
// -> items
|
|
||||||
//
|
|
||||||
// options format:
|
|
||||||
// {
|
|
||||||
// // Iterate ALL items...
|
|
||||||
// //
|
|
||||||
// // NOTE: this if true overrides all other iteration coverage
|
|
||||||
// // options...
|
|
||||||
// iterateAll: <bool>,
|
|
||||||
//
|
|
||||||
// // If true do not skip items with .noniterable set to true...
|
|
||||||
// iterateNonIterable: <bool>,
|
|
||||||
// // If true do not skip item.sublist of items with .collapsed
|
|
||||||
// // set to true...
|
|
||||||
// iterateCollapsed: <bool>,
|
|
||||||
// // If true skip iterating nested items...
|
|
||||||
// skipNested: <bool>,
|
|
||||||
//
|
|
||||||
// // If true include inlined parent id in path...
|
|
||||||
// inlinedPaths: <bool>,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// By default this will not iterate items that are:
|
|
||||||
// - non-iterable (item.noniterable is true)
|
|
||||||
// - collapsed sub-items (item.collapsed is true)
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// This extends the Array .map(..) by adding:
|
|
||||||
// - ability to run without arguments
|
|
||||||
// - support for options
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// NOTE: a semi-documented signature is also used internally to
|
|
||||||
// generate paths:
|
|
||||||
// .map(func, path, options)
|
|
||||||
//
|
|
||||||
// XXX this essentially repeats what .render(..) does but in a more
|
|
||||||
// complex way -- can we reuse one or the other and simplify things???
|
|
||||||
// XXX make item access by index lazy...
|
|
||||||
// - index nested stuff and lengths... (.sublist_length)
|
|
||||||
// - stop when target reached... (control callback???)
|
|
||||||
// XXX Q: should we have an option to treat groups as elements???
|
|
||||||
map: function(func, options){
|
|
||||||
var that = this
|
|
||||||
|
|
||||||
// parse args...
|
|
||||||
var args = [...arguments]
|
|
||||||
func = args[0] instanceof Function ?
|
|
||||||
args.shift()
|
|
||||||
: undefined
|
|
||||||
var path = (args[0] instanceof Array
|
|
||||||
|| typeof(args[0]) == typeof('str')) ?
|
|
||||||
args.shift()
|
|
||||||
: []
|
|
||||||
path = path instanceof Array ? path : [path]
|
|
||||||
var options = args.pop() || {}
|
|
||||||
|
|
||||||
// options...
|
|
||||||
options = Object.assign(Object.create(this.options || {}), options || {})
|
|
||||||
var iterateNonIterable = options.iterateAll || options.iterateNonIterable
|
|
||||||
var iterateCollapsed = options.iterateAll || options.iterateCollapsed
|
|
||||||
var skipNested = !options.iterateAll && options.skipNested
|
|
||||||
var reverse = !!options.reverseIteration
|
|
||||||
|
|
||||||
// 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 ?
|
|
||||||
func.call(that, elem, path.concat(elem.id), that)
|
|
||||||
: elem] }
|
|
||||||
var doLevel = function(elem, nested, path){
|
|
||||||
return [
|
|
||||||
doElem(elem, path),
|
|
||||||
(!iterateCollapsed && elem.collapsed) ?
|
|
||||||
[]
|
|
||||||
: nested ]
|
|
||||||
// reverse level order...
|
|
||||||
.run(function(){
|
|
||||||
reverse
|
|
||||||
&& this.reverse() })
|
|
||||||
.flat() }
|
|
||||||
// NOTE: we need to reverse two things:
|
|
||||||
// - level order (done here)
|
|
||||||
// - linearization order (done below)
|
|
||||||
// XXX can we make this even simpler???
|
|
||||||
var walk = function(path, list){
|
|
||||||
return list
|
|
||||||
// reverse the items...
|
|
||||||
.run(function(){
|
|
||||||
return reverse ?
|
|
||||||
// NOTE: we .slice() as we do not want to affect
|
|
||||||
// the actual list...
|
|
||||||
this.slice().reverse()
|
|
||||||
: this })
|
|
||||||
.map(function(elem){
|
|
||||||
return (
|
|
||||||
// group...
|
|
||||||
(elem instanceof Array ?
|
|
||||||
walk(path, elem)
|
|
||||||
// item not iterable -> skip...
|
|
||||||
: !iterateNonIterable && elem.noniterable) ?
|
|
||||||
[]
|
|
||||||
// elem is Browser (inline)...
|
|
||||||
: elem instanceof Browser ?
|
|
||||||
elem.map(func,
|
|
||||||
options.inlinedPaths ?
|
|
||||||
path.concat(elem.id)
|
|
||||||
: path.slice(),
|
|
||||||
options)
|
|
||||||
// value is Browser (inline)...
|
|
||||||
// XXX legacy...
|
|
||||||
//: elem.value instanceof Browser ?
|
|
||||||
// elem.value.map(func,
|
|
||||||
// options.inlinedPaths ?
|
|
||||||
// path.concat(elem.id)
|
|
||||||
// : path.slice(),
|
|
||||||
// options)
|
|
||||||
// .sublist is Browser (nested)...
|
|
||||||
: (!skipNested
|
|
||||||
&& elem.sublist instanceof Browser) ?
|
|
||||||
doLevel(
|
|
||||||
elem,
|
|
||||||
elem.sublist
|
|
||||||
.map(func, path.concat(elem.id), options),
|
|
||||||
path)
|
|
||||||
// .sublist is Array (nested)...
|
|
||||||
: (!skipNested
|
|
||||||
&& elem.sublist instanceof Array) ?
|
|
||||||
doLevel(
|
|
||||||
elem,
|
|
||||||
walk(path.concat(elem.id), elem.sublist),
|
|
||||||
path)
|
|
||||||
// normal item...
|
|
||||||
: doElem(elem, path) ) })
|
|
||||||
.flat() }
|
|
||||||
|
|
||||||
return walk(path, this.items)
|
|
||||||
},
|
|
||||||
//*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Events...
|
// Events...
|
||||||
//
|
//
|
||||||
// Format:
|
// Format:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user