sort save/cache refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-02-13 20:54:15 +03:00
parent d98b13f004
commit c48529693b

View File

@ -322,11 +322,11 @@ module.SortActions = actions.Actions({
|| 'none' }, || 'none' },
function(){ function(){
return Object.keys(this.config['sort-methods']) return Object.keys(this.config['sort-methods'])
.concat((this.data // manual...
&& ((this.data.sort_cache || {})['Manual'] .concat(this.data.sort_method == 'Manual' ? ['Manual'] : [])
|| this.data.sort_method == 'Manual')) ? // list saved sorts...
['Manual'] .concat(Object.keys(this.data.sort_order || {}))
: [])}, .unique()},
// prevent setting 'none' as mode... // prevent setting 'none' as mode...
function(mode){ function(mode){
return !!this.images return !!this.images
@ -337,9 +337,13 @@ module.SortActions = actions.Actions({
function(mode, _, reverse){ function(mode, _, reverse){
reverse = reverse == 'reverse' || reverse reverse = reverse == 'reverse' || reverse
var cache = this.data.sort_cache = this.data.sort_cache || {} var cache = this.data.sort_cache = this.data.sort_cache || {}
var method = this.data.sort_method
// save manual order... // cache sort order...
if(this.data.sort_method == 'Manual'){ if(method == 'Manual'){
this.saveOrder(method)
} else if(method && !(method in cache)){
this.cacheOrder() this.cacheOrder()
} }
@ -350,10 +354,10 @@ module.SortActions = actions.Actions({
if(mode in cache if(mode in cache
|| sort in cache){ || sort in cache){
var order = (cache[mode] || cache[sort]).slice() var order = (cache[mode] || cache[sort]).slice()
// invalid cache... // invalid cache -> sort...
if(order.length != this.data.order.length){ if(order.length != this.data.order.length){
// XXX should we drop the cache here??? // drop the cached order...
// XXX delete cache[ mode in cache ? mode : sort ]
this.sortImages(sort) this.sortImages(sort)
// load cache... // load cache...
@ -363,12 +367,31 @@ module.SortActions = actions.Actions({
this.data.sort_method = mode this.data.sort_method = mode
} }
// saved sort order...
} else if(this.data.sort_order
&& mode in this.data.sort_order){
this.data.order = this.data.sort_order[mode].slice()
this.sortImages('update' + (reverse ? ' reverse' : ''))
this.data.sort_method = mode
} else { } else {
this.sortImages(sort) this.sortImages(sort)
} }
})], })],
// XXX add drop/load cache actions... // XXX add drop/load actions...
saveOrder: ['- Sort/',
function(method){
method = method || this.data.sort_method
if(method){
var cache = this.data.sort_order = this.data.sort_order || {}
cache[method] = this.data.order.slice()
}
}],
// XXX add drop/load actions...
cacheOrder: ['- Sort/', cacheOrder: ['- Sort/',
function(){ function(){
var method = this.data.sort_method var method = this.data.sort_method
@ -389,6 +412,9 @@ module.SortActions = actions.Actions({
this.data.sort_method = data.data.sort_method this.data.sort_method = data.data.sort_method
} }
if(data.data && data.sort_order){
this.data.sort_order = data.sort_order
}
if(data.data && data.sort_cache){ if(data.data && data.sort_cache){
this.data.sort_cache = data.sort_cache this.data.sort_cache = data.sort_cache
} }
@ -401,13 +427,16 @@ module.SortActions = actions.Actions({
res.data.sort_method = this.data.sort_method res.data.sort_method = this.data.sort_method
} }
if(this.data.sort_order){
res.sort_order = this.data.sort_order
}
if(this.data.sort_cache){ if(this.data.sort_cache){
res.sort_cache = this.data.sort_cache res.sort_cache = this.data.sort_cache
} }
if(this.toggleImageSort('?') == 'Manual'){ if(this.toggleImageSort('?') == 'Manual'){
res.sort_cache = res.sort_cache || {} res.sort_order = res.sort_order || {}
res.sort_cache['Manual'] = this.data.order.slice() res.sort_order['Manual'] = this.data.order.slice()
} }
} }
}], }],
@ -439,6 +468,9 @@ module.Sort = core.ImageGridFeatures.Feature({
var changed = this.changes == null var changed = this.changes == null
|| this.changes.sort_cache || this.changes.sort_cache
if(changed && res.raw.sort_order){
res.index['sort_order'] = res.raw.sort_order
}
if(changed && res.raw.sort_cache){ if(changed && res.raw.sort_cache){
res.index['sort_cache'] = res.raw.sort_cache res.index['sort_cache'] = res.raw.sort_cache
} }