updated data format to 2.1, some refactoring and minor optimizations...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-12-20 06:10:45 +04:00
parent 5a5e0366ba
commit 9b15699525
6 changed files with 70 additions and 29 deletions

View File

@ -36,7 +36,7 @@ function getAllData(){
// XXX should this set the .current to anything but null or the first elem???
function makeCroppedData(gids, keep_ribbons, keep_unloaded_gids){
var res = {
varsion: '2.0',
varsion: DATA_VERSION,
current: null,
ribbons: [],
order: DATA.order.slice(),

View File

@ -6,6 +6,24 @@
*
**********************************************************************/
// Format version...
//
// version format:
// <major>.<minor>
//
// Major version change mean a significant incompatibility.
//
// Minor version changes mean some detail changed and can be handled
// by it's specific handler seamlessly.
//
// Changes:
// 2.0 - completely and incompatible new format
// - use convertDataGen1(..) to update old data
// 2.1 - MARKED now maintained sorted
// - will auto-update on loading marks to 2.1 of 2.1 data, will
// need a re-save
var DATA_VERSION = '2.1'
var CONFIG = {
// Application name...
@ -107,8 +125,8 @@ var UI_STATE = {
// Data format...
var DATA = {
// Format version...
version: '2.0',
// see DATA_VERSION for description...
version: DATA_VERSION,
// Current position, GID...
current: null,
@ -1295,7 +1313,7 @@ function dataFromImages(images){
var gids = Object.keys(images).sort()
return {
version: '2.0',
version: DATA_VERSION,
current: gids[0],
ribbons: [
gids
@ -1390,7 +1408,7 @@ function mergeData(a, b){
})
return {
version: '2.0',
version: DATA_VERSION,
current: null,
ribbons: concatZip.apply(null, ribbon_sets),
order: order,
@ -1437,7 +1455,7 @@ function splitData(data, gid1){
gids.push(arguments[i])
res.push({
version: '2.0',
version: DATA_VERSION,
current: null,
ribbons: [],
order: data.order.slice(prev, cur),
@ -1446,7 +1464,7 @@ function splitData(data, gid1){
}
// tail section...
res.push({
version: '2.0',
version: DATA_VERSION,
current: null,
ribbons: [],
order: data.order.slice(cur),

View File

@ -202,7 +202,7 @@ function loadLatestFile(path, dfl, pattern, diff_pattern, default_data){
}
function makeFileLoader(title, file_dfl, file_pattern, data_set, skip_reg){
function makeFileLoader(title, file_dfl, file_pattern, set_data, evt_name, skip_reg){
var _loader = function(path){
var res = $.Deferred()
// default locations...
@ -232,7 +232,11 @@ function makeFileLoader(title, file_dfl, file_pattern, data_set, skip_reg){
bubbleProgress(title, loader, res)
res.done(data_set)
res.done(set_data)
if(evt_name != null){
res.done(function(){ $('.viewer').trigger(evt_name) })
}
return res
}
@ -241,13 +245,13 @@ function makeFileLoader(title, file_dfl, file_pattern, data_set, skip_reg){
}
// XXX make this check for updates -- no need to re-save if nothing
// changed...
function makeFileSaver(file_dfl, data_get, skip_reg){
function makeFileSaver(file_dfl, get_data, skip_reg){
var _saver = function(name){
name = name == null
? normalizePath(CONFIG.cache_dir_var +'/'+ Date.timeStamp())
: name
dumpJSON(name + '-' + file_dfl, data_get())
dumpJSON(name + '-' + file_dfl, get_data())
}
!skip_reg && FILE_SAVERS.push(_saver)
return _saver
@ -443,8 +447,8 @@ function loadFileState(path, prefix){
reloadViewer()
res.resolve()
// version 2.0
} else if(json.version == '2.0') {
// version 2.*
} else if(/2\.[0-9*]/.test(json.version)) {
DATA = json
$.when(
// XXX load config...
@ -457,6 +461,7 @@ function loadFileState(path, prefix){
// run registered loaders...
runFileLoaders(prefix, res))
.done(function(){
$('.viewer').trigger('fileStateLoaded')
reloadViewer()
res.resolve()
})

View File

@ -9,11 +9,11 @@
/*********************************************************************/
// Convert legacy Gen1 data format to current Gen3 (v2.0)
// Convert legacy Gen1 data format to current Gen3 (v2.0+)
function convertDataGen1(data, cmp){
var res = {
data: {
version: '2.0',
version: DATA_VERSION,
current: null,
ribbons: [],
order: [],

View File

@ -162,9 +162,8 @@ var updateSelectedImageMark = makeMarkUpdater(
// NOTE: MARKED may contain both gids that are not loaded and that do
// not exist, as there is no way to distinguish between the two
// situations the cleanup is optional...
function cropMarkedImages(cmp, keep_ribbons, keep_unloaded_gids){
cmp = cmp == null ? imageOrderCmp : cmp
var marked = MARKED.slice().sort(cmp)
function cropMarkedImages(keep_ribbons, keep_unloaded_gids){
var marked = MARKED.slice()//.sort(imageOrderCmp)
cropDataTo(marked, keep_ribbons, keep_unloaded_gids)
@ -186,7 +185,7 @@ var toggleMarkedOnlyView = makeCropModeToggler(
var toggleMarkedOnlyWithRibbonsView = makeCropModeToggler(
'marked-only-view',
function(){
cropMarkedImages(null, true)
cropMarkedImages(true)
})
@ -550,8 +549,23 @@ var loadFileMarks = makeFileLoader(
MARKED_FILE_DEFAULT,
MARKED_FILE_PATTERN,
function(data){
// for version below 2.1, sort MARKED and update to 2.1...
if(DATA.version == '2.0'){
setTimeout(function(){
var t0 = Date.now()
data.sort(imageOrderCmp)
var t1 = Date.now()
// XXX is this the correct way to do this???
DATA.version = DATA_VERSION
console.warn('Marks: sort: done ('+( t1 - t0 )+'ms) -- resave the data.')
}, 0)
}
// set the MARKED...
MARKED = data
})
},
'marksLoaded')
// Save image marks to file

View File

@ -48,7 +48,8 @@ function buildTagsFromImages(tagset, images){
}
// XXX
// XXX think I need to do something a-la fickr-style normalization here...
// XXX also need to remember the original notation...
function normalizeTag(tag){
return tag.trim()
}
@ -176,7 +177,9 @@ function updateTags(tags, gid, tagset, images){
// this implements the AND selector...
//
// NOTE: do not like this algorithm as it can get O(n^2)-ish
function selectByTags(tags, tagset){
// NOTE: unless no_sort is set, this will sort the resulted gids in the
// same order as DATA.order...
function selectByTags(tags, no_sort, tagset){
tags = typeof(tags) == typeof('str') ? [ tags ] : tags
tagset = tagset == null ? TAGS : tagset
@ -190,6 +193,7 @@ function selectByTags(tags, tagset){
}
subtagset.push(tagset[tag])
})
// sort by length...
subtagset.sort(function(a, b){
return b.length - a.length
})
@ -206,7 +210,7 @@ function selectByTags(tags, tagset){
}
}
if(gid != null){
res.push(gid)
no_sort ? res.push(gid) : insertGIDToPosition(gid, res)
}
})
@ -238,8 +242,9 @@ function untagList(list, tags){
}
// same as tagList(..), but will also remove the tags form gids no in
// list...
function tagOnlyList(list, tags){
selectByTags(tags).forEach(function(gid){
function tagOnlyList(list, tags, no_sort){
no_sort = no_sort == null ? true : false
selectByTags(tags, no_sort).forEach(function(gid){
if(list.indexOf(gid) < 0){
removeTag(tags, gid)
}
@ -275,7 +280,7 @@ function markTagged(tags){
return MARKED
}
function unmarkTagged(tags){
var set = selectByTags(tags)
var set = selectByTags(tags, false)
set.forEach(function(gid){
var i = MARKED.indexOf(gid)
if(i > -1){
@ -291,9 +296,8 @@ function unmarkTagged(tags){
// cropping of tagged images...
function cropTagged(tags, cmp, keep_ribbons, keep_unloaded_gids){
cmp = cmp == null ? imageOrderCmp : cmp
var set = selectByTags(tags).sort(cmp)
function cropTagged(tags, keep_ribbons, keep_unloaded_gids){
var set = selectByTags(tags)
cropDataTo(set, keep_ribbons, keep_unloaded_gids)