sort caching almost done...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-02-13 06:56:08 +03:00
parent a3a633096c
commit d98b13f004

View File

@ -79,40 +79,53 @@ module.SortActions = actions.Actions({
// //
// Format: // Format:
// { // {
// <method-name>: function(a, b){ ... }, // <method-name>: function(){
// ...
// return function(a, b){ ... }
// },
// ... // ...
// } // }
// //
// The methods are cmp constructors rather than direct cmp functions
// to enable index construction and other more complicated sort
// approaches...
//
// NOTE: the cmp function is called in the actions context. // NOTE: the cmp function is called in the actions context.
// //
// XXX add sequence number with overflow... // XXX add sequence number with overflow...
__sort_methods__: { __sort_methods__: {
'name-leading-sequence': function(a, b){ 'name-leading-sequence': function(){
a = this.images.getImageNameLeadingSeq(a) return function(a, b){
a = typeof(a) == typeof('str') ? 0 : a a = this.images.getImageNameLeadingSeq(a)
b = this.images.getImageNameLeadingSeq(b) a = typeof(a) == typeof('str') ? 0 : a
b = typeof(b) == typeof('str') ? 0 : b b = this.images.getImageNameLeadingSeq(b)
b = typeof(b) == typeof('str') ? 0 : b
return a - b return a - b
}
}, },
'name-sequence': function(a, b){ 'name-sequence': function(){
a = this.images.getImageNameSeq(a) return function(a, b){
a = typeof(a) == typeof('str') ? 0 : a a = this.images.getImageNameSeq(a)
b = this.images.getImageNameSeq(b) a = typeof(a) == typeof('str') ? 0 : a
b = typeof(b) == typeof('str') ? 0 : b b = this.images.getImageNameSeq(b)
b = typeof(b) == typeof('str') ? 0 : b
return a - b return a - b
}
}, },
// This is specifically designed to terminate sort methods to prevent // This is specifically designed to terminate sort methods to prevent
// images that are not relevant to the previous order to stay in place // images that are not relevant to the previous order to stay in place
// //
// XXX need to test how will this affect a set of images where part // XXX need to test how will this affect a set of images where part
// of the set is sortable an part is not... // of the set is sortable an part is not...
'keep-position': function(a, b){ 'keep-position': function(){
a = this.data.order.indexOf(a) return function(a, b){
b = this.data.order.indexOf(b) a = this.data.order.indexOf(a)
b = this.data.order.indexOf(b)
return a - b return a - b
}
}, },
}, },
// Sort images... // Sort images...
@ -222,10 +235,10 @@ module.SortActions = actions.Actions({
// remove duplicate methods... // remove duplicate methods...
.unique() .unique()
.map(function(m){ .map(function(m){
return SortActions.__sort_methods__[m] return (SortActions.__sort_methods__[m]
|| (that.__sort_methods__ && that.__sort_methods__[m]) || (that.__sort_methods__ && that.__sort_methods__[m])
// sort by attr path... // sort by attr path...
|| (function(){ || function(){
var p = m.split(/\./g) var p = m.split(/\./g)
var _get = function(obj){ var _get = function(obj){
if(obj == null){ if(obj == null){
@ -254,7 +267,7 @@ module.SortActions = actions.Actions({
} else { } else {
return +1 return +1
} }
}})() }}).call(that)
}) })
// prepare the cmp function... // prepare the cmp function...
@ -327,20 +340,46 @@ module.SortActions = actions.Actions({
// save manual order... // save manual order...
if(this.data.sort_method == 'Manual'){ if(this.data.sort_method == 'Manual'){
cache['Manual'] = this.data.order.slice() this.cacheOrder()
} }
// special case: manual order... var sort = `"${mode}"`+ (reverse ? ' reverse' : '')
if(mode == 'Manual'){
this.data.order = cache['Manual'].slice() // cached order...
this.sortImages('update' + (reverse ? ' reverse' : '')) // XXX use load cache action...
this.data.sort_method = mode if(mode in cache
|| sort in cache){
var order = (cache[mode] || cache[sort]).slice()
// invalid cache...
if(order.length != this.data.order.length){
// XXX should we drop the cache here???
// XXX
this.sortImages(sort)
// load cache...
} else {
this.data.order = order
this.sortImages('update' + (reverse ? ' reverse' : ''))
this.data.sort_method = mode
}
} else { } else {
this.sortImages('"'+mode+'"' + (reverse ? ' reverse' : '')) this.sortImages(sort)
} }
})], })],
// XXX add drop/load cache actions...
cacheOrder: ['- Sort/',
function(){
var method = this.data.sort_method
if(method){
var cache = this.data.sort_cache = this.data.sort_cache || {}
cache[method] = this.data.order.slice()
}
}],
// Store/load sort data: // Store/load sort data:
// .data.sort_method - current sort mode (optional) // .data.sort_method - current sort mode (optional)
// .data.sort_cache - manual sort order (optional) // .data.sort_cache - manual sort order (optional)