fixed bug that made sorting seem chaotic...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-12-09 03:53:33 +03:00
parent d2a5e3c15e
commit dafeedbaaf

View File

@ -161,8 +161,7 @@ module.SortActions = actions.Actions({
return actions.ASIS( return actions.ASIS(
get(methods) get(methods)
|| that.getSortMethods(splitMethods(methods)) || that.getSortMethods(splitMethods(methods))
|| null) || null) }
}
// return multiple methods... // return multiple methods...
var res = {} var res = {}
@ -171,8 +170,7 @@ module.SortActions = actions.Actions({
res[m] = m == 'reverse' ? res[m] = m == 'reverse' ?
(res[m] || []).concat([m]) (res[m] || []).concat([m])
: get(m) }) : get(m) })
return res return res }],
}],
// XXX should this count 'reverese' arity??? // XXX should this count 'reverese' arity???
expandSortMethod: ['- Sort/', expandSortMethod: ['- Sort/',
core.doc`Build list of basic sort methods... core.doc`Build list of basic sort methods...
@ -255,9 +253,7 @@ module.SortActions = actions.Actions({
// 'image-modify-date', // 'image-modify-date',
'image-create-date': 'image-create-date':
'metadata.createDate file-date name-sequence', 'metadata.date/timeOriginal file-date name-sequence',
// XXX
//'image-modify-date':
// 'metadata.createDate birthtime ctime name-sequence', // 'metadata.createDate birthtime ctime name-sequence',
'file-create-date': 'file-create-date':
@ -388,20 +384,9 @@ module.SortActions = actions.Actions({
// XXX legacy: this is added to every sort automatically... // XXX legacy: this is added to every sort automatically...
// ...do we still need this here??? // ...do we still need this here???
'keep-position': function(){ 'keep-position': function(){
var order = this.data.order_index
var logger = this.logger
&& this.logger.push('Sort indexing')
logger && logger.emit('queued', 'position')
var order = new Map(
this.data.order
.map(function(e, i){
return [e, i] }))
logger && logger.emit('done', 'position')
return function(a, b){ return function(a, b){
return order.get(a) - order.get(b) } }, return order[a] - order[b] } },
}, },
// XXX would be nice to be able to sort a list of gids or a section // XXX would be nice to be able to sort a list of gids or a section
// of images... // of images...
@ -460,8 +445,7 @@ module.SortActions = actions.Actions({
if(method == 'reverse'){ if(method == 'reverse'){
method = 'update' method = 'update'
reverse = true reverse = true }
}
reverse = reverse == null ? false reverse = reverse == null ? false
: reverse == 'reverse' : reverse == 'reverse'
@ -495,7 +479,6 @@ module.SortActions = actions.Actions({
// build the compare routine... // build the compare routine...
method = method method = method
// remove duplicate methods... // remove duplicate methods...
// XXX should we keep the last occurrence or the first occurrence???
.unique() .unique()
.concat(['keep-position']) .concat(['keep-position'])
.tailUnique() .tailUnique()
@ -505,33 +488,29 @@ module.SortActions = actions.Actions({
// sort by attr path... // sort by attr path...
|| function(){ || function(){
var p = m.split(/\./g) var p = m.split(/\./g)
// get attr...
var _get = function(obj){ var _get = function(obj){
if(obj == null){ if(obj == null){
return null return null }
}
for(var i=0; i<p.length; i++){ for(var i=0; i<p.length; i++){
obj = obj[p[i]] obj = obj[p[i]]
if(obj === undefined){ if(obj === undefined){
return null return null } }
} return obj }
}
return obj
}
return function(a, b){ return function(a, b){
a = _get(this.images[a]) a = _get(this.images[a])
b = _get(this.images[b]) b = _get(this.images[b])
if(a == b
// not enough data to compare items, test next... // not enough data to compare items, test next...
if(a == null || b == null){ || (a == null && b == null)){
return 0 return 0
} else if(a < b
} else if(a == b){ // keep stuff without value at the end...
return 0 || a == null){
} else if(a < b){
return -1 return -1
} else { } else {
return +1 return +1 }
}
}}).call(that) }) }}).call(that) })
// prepare the cmp function... // prepare the cmp function...
@ -548,14 +527,14 @@ module.SortActions = actions.Actions({
// do the sort (in place)... // do the sort (in place)...
if(method && method.length > 0 && this.images){ if(method && method.length > 0 && this.images){
this.data.order = reverse ? this.data.order =
reverse ?
this.data.order.slice().sort(cmp.bind(this)).reverse() this.data.order.slice().sort(cmp.bind(this)).reverse()
: this.data.order.slice().sort(cmp.bind(this)) : this.data.order.slice().sort(cmp.bind(this))
// just reverse... // just reverse...
} else if(method.length <= 0 && reverse) { } else if(method.length <= 0 && reverse) {
this.data.order.reverse() this.data.order.reverse() }
}
this.data.updateImagePositions() }], this.data.updateImagePositions() }],
@ -611,37 +590,29 @@ module.SortActions = actions.Actions({
;(this.data.sort_order ;(this.data.sort_order
&& mode in this.data.sort_order) ? && mode in this.data.sort_order) ?
this.loadOrder(mode, reverse == 'reverse') this.loadOrder(mode, reverse == 'reverse')
: this.sortImages(sort) : this.sortImages(sort) })],
})],
// XXX add drop/load actions... // XXX add drop/load actions...
saveOrder: ['- Sort/', saveOrder: ['- Sort/',
function(title){ function(title){
title = title || 'Manual' title = title || 'Manual'
var cache = this.data.sort_order = this.data.sort_order || {} var cache = this.data.sort_order = this.data.sort_order || {}
cache[title] = this.data.order.slice() cache[title] = this.data.order.slice() }],
}],
loadOrder: ['- Sort/', loadOrder: ['- Sort/',
function(title, reverse){ function(title, reverse){
var order = (this.data.sort_order || {})[title] var order = (this.data.sort_order || {})[title]
if(order){ if(order){
this.data.order = order.slice() this.data.order = order.slice()
this.sortImages('update' + (reverse ? ' reverse' : '')) this.sortImages('update' + (reverse ? ' reverse' : ''))
this.data.sort_method = title this.data.sort_method = title } }],
}
}],
// XXX add drop/load actions... // XXX add drop/load actions...
cacheOrder: ['- Sort/', cacheOrder: ['- Sort/',
function(){ function(){
var method = this.data.sort_method var method = this.data.sort_method
if(method){ if(method){
var cache = this.data.sort_cache = this.data.sort_cache || {} var cache = this.data.sort_cache = this.data.sort_cache || {}
cache[method] = this.data.order.slice() } }],
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)
@ -650,34 +621,22 @@ module.SortActions = actions.Actions({
load: [function(data){ load: [function(data){
return function(){ return function(){
var that = this var that = this
data.data data.data
&& ['sort_method', 'sort_order', 'sort_cache'] && ['sort_method', 'sort_order', 'sort_cache']
.forEach(function(attr){ .forEach(function(attr){
if(data.data[attr]){ if(data.data[attr]){
that.data[attr] = data.data[attr] that.data[attr] = data.data[attr] } }) } }],
}
})
}
}],
json: [function(){ json: [function(){
return function(res){ return function(res){
var that = this var that = this
;['sort_method', 'sort_order', 'sort_cache'] ;['sort_method', 'sort_order', 'sort_cache']
.forEach(function(attr){ .forEach(function(attr){
if(that.data[attr]){ if(that.data[attr]){
res.data[attr] = that.data[attr] res.data[attr] = that.data[attr] } })
}
})
// special case: unsaved manual order... // special case: unsaved manual order...
if(this.toggleImageSort('?') == 'Manual'){ if(this.toggleImageSort('?') == 'Manual'){
res.data.sort_order = res.sort_order || {} res.data.sort_order = res.sort_order || {}
res.data.sort_order['Manual'] = this.data.order.slice() res.data.sort_order['Manual'] = this.data.order.slice() } } }],
}
}
}],
}) })
var Sort = var Sort =
@ -699,8 +658,7 @@ module.Sort = core.ImageGridFeatures.Feature({
handlers: [ handlers: [
['shiftImageRight shiftImageLeft', ['shiftImageRight shiftImageLeft',
function(){ function(){
this.data.sort_method = 'Manual' this.data.sort_method = 'Manual' }],
}],
// maintain .sort_order and .sort_cache separately from .data in // maintain .sort_order and .sort_cache separately from .data in
// the store... // the store...
@ -709,14 +667,12 @@ module.Sort = core.ImageGridFeatures.Feature({
var c = res.changes var c = res.changes
if(!c){ if(!c){
return return }
}
;['sort_order', 'sort_cache'] ;['sort_order', 'sort_cache']
.forEach(function(attr){ .forEach(function(attr){
if(!res.raw.data){ if(!res.raw.data){
return return }
}
if((c === true || c[attr]) && res.raw.data[attr]){ if((c === true || c[attr]) && res.raw.data[attr]){
// full save... // full save...
if(c === true){ if(c === true){
@ -727,23 +683,16 @@ module.Sort = core.ImageGridFeatures.Feature({
var diff = {} var diff = {}
c[attr].forEach(function(k){ c[attr].forEach(function(k){
diff[k] = res.raw.data[attr][k] }) diff[k] = res.raw.data[attr][k] })
res.index[attr +'-diff'] = diff res.index[attr +'-diff'] = diff }
}
// cleanup... // cleanup...
delete res.index.data[attr] delete res.index.data[attr] } }) }],
}
})
}],
['prepareIndexForLoad', ['prepareIndexForLoad',
function(res){ function(res){
['sort_order', 'sort_cache'] ['sort_order', 'sort_cache']
.forEach(function(attr){ .forEach(function(attr){
if(res[attr]){ if(res[attr]){
res.data[attr] = res[attr] res.data[attr] = res[attr] } }) }],
}
})
}],
// manage changes... // manage changes...
['sortImages', ['sortImages',
@ -935,8 +884,7 @@ var SortUIActions = actions.Actions({
// sort action... // sort action...
} else if(method in that){ } else if(method in that){
that.showDoc(method) } } that.showDoc(method) } }
this.keyboard.handler('General', '?', 'showDoc') this.keyboard.handler('General', '?', 'showDoc') })
})
return o })], return o })],
}) })