done refactoring output format construction (need to revise)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-02-15 23:20:06 +03:00
parent dec295fd05
commit 6358945168
4 changed files with 160 additions and 239 deletions

View File

@ -923,17 +923,69 @@ module.Base = core.ImageGridFeatures.Feature({
['prepareIndexForWrite',
function(res){
// we save .current unconditionally...
res.index.current = res.raw.data.current
// XXX .data
// XXX .images
var changes = res.changes
// data...
if(changes === true || changes.data){
res.index.data = res.raw.data
}
// images (full)...
if(changes === true || changes.images === true){
res.index.images = res.raw.images
// images-diff...
} else if(changes && changes.images){
var diff = res.index['images-diff'] = {}
changes.images.forEach(function(gid){
diff[gid] = res.raw.images[gid]
})
}
}],
['prepareJSONForLoad',
function(res, json){
res.data.current = json.current || res.data.current
function(res, json, base_path){
console.log('>>>>', res, json, base_path)
// XXX .data
// XXX .images
// build data and images...
// XXX do we actually need to build stuff here, shouldn't
// .load(..) take care of this???
var d = data.Data.fromJSON(json.data)
d.current = json.current || d.current
var img = images.Images(json.images)
if(base_path){
d.base_path = base_path
// XXX STUB remove ASAP...
// ...need a real way to handle base dir, possible
// approaches:
// 1) .base_path attr in image, set on load and
// do not save (or ignore on load)...
// if exists prepend to all paths...
// - more to do in view-time
// + more flexible
// 2) add/remove on load/save (approach below)
// + less to do in real time
// - more processing on load/save
img.forEach(function(_, img){ img.base_path = base_path })
}
// extra stuff...
// ...this will restore stuff stored in the data but not
// explicitly restored above...
// XXX do we need this???
Object.keys(json.data).forEach(function(k){
if(d[k] === undefined){
d[k] = json.data[k]
}
})
res.data = d
res.images = img
}],
],
})
@ -1130,19 +1182,19 @@ module.Tags = core.ImageGridFeatures.Feature({
//
// XXX see if this can be automated...
['prepareIndexForWrite',
function(res, _, full){
var changes = this.changes
function(res){
var changes = res.changes
if((full || changes == null || changes.tags) && res.raw.data.tags){
if((changes === true || changes.tags) && res.raw.data.tags){
res.index.tags = res.raw.data.tags
}
if((full || changes == null || changes.selected)
if((changes === true || changes.selected)
&& res.raw.data.tags
&& res.raw.data.tags.selected){
res.index.marked = res.raw.data.tags.selected
}
if((full || changes == null || changes.bookmarked)
if((changes === true || changes.bookmarked)
&& res.raw.data.tags
&& res.raw.data.tags.bookmark){
res.index.bookmarked = [

View File

@ -82,6 +82,92 @@ module.IndexFormat = core.ImageGridFeatures.Feature({
/*********************************************************************/
var ExportFormatActions = actions.Actions({
// Convert json index to a format compatible with file.writeIndex(..)
//
// This is here so as other features can participate in index
// preparation...
// There are several stages features can control the output format:
// 1) .json() action
// - use this for global high level serialization format
// - the output of this is .load(..) compatible
// 2) .prepareIndexForWrite(..) action
// - use this for file system write preparation
// - this directly affects the index structure
//
// This will get the base index, ignoring the cropped state.
//
// Returns:
// {
// // Timestamp...
// // NOTE: this is the timestamp used to write the index.
// date: <timestamp>,
//
// // normalized changes...
// // - true - everything changed
// // - false - nothing changes/disabled
// / - <object> - specific changes
// changes: <changes>,
//
// // This is the original json object, either the one passed as
// // an argument or the one returned by .json('base')
// raw: <original-json>,
//
// // this is the prepared index object, the one that is going to be
// // saved.
// index: <index-json>,
//
// ...
// }
//
//
// The format for the <prapared-json> is as follows:
// {
// <keyword>: <data>,
// ...
// }
//
// The <index-json> is written out to a fs index in the following
// way:
// <index-dir>/<timestamp>-<keyword>.json
//
// <index-dir> - taken from .config['index-dir'] (default: '.ImageGrid')
// <timestamp> - as returned by Date.timeStamp() (see: jli)
//
prepareIndexForWrite: ['- File/Prepare index for writing',
function(json, full){
json = json || this.json('base')
var changes = full ? null
: this.hasOwnProperty('changes') ? this.changes
: null
changes = changes === null ? true : changes
return {
date: Date.timeStamp(),
changes: changes,
raw: json,
index: {},
}
}],
// XXX should this return {} or json???
prepareJSONForLoad: ['- File/Prepare JSON for loading',
function(json, base_path){ return {} }],
})
var ExportFormat =
module.ExportFormat = core.ImageGridFeatures.Feature({
title: '',
doc: '',
tag: 'export-format',
actions: ExportFormatActions,
})
/*********************************************************************/
var FileSystemInfoActions = actions.Actions({
@ -130,16 +216,6 @@ var FileSystemLoaderActions = actions.Actions({
'default-load-method': 'loadIndex',
},
// NOTE: this is the reverse of .prepareIndexForWrite(..)
//
// XXX do we need both this and file.buildIndex(..), we essentially create
// a Data object and then create it again in .load()...
prepareJSONForLoad: ['- File/Prepare JSON for loading',
function(json, base_path){
// XXX move the code up here from file.js...
return file.buildIndex(json, base_path) }],
// XXX is this a hack???
// XXX need a more generic form...
checkPath: ['- File/',
@ -629,6 +705,7 @@ module.FileSystemLoader = core.ImageGridFeatures.Feature({
tag: 'fs-loader',
depends: [
'export-format',
'location',
'recover',
'fs-info',
@ -943,10 +1020,10 @@ module.Comments = core.ImageGridFeatures.Feature({
// doing anything on .prepareJSONForLoad(..)
['prepareIndexForWrite',
function(res, _, full){
var changed = this.changes == null
|| this.changes.comments
var changed = res.changes === true
|| res.changes.comments
if((full || changed) && res.raw.comments){
if(changed && res.raw.comments){
var comments = res.raw.comments
Object.keys(comments)
@ -1183,10 +1260,10 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
// available.
// NOTE: 'loadIndex' will also drop any unsaved changes...
['prepareIndexForWrite',
function(res, _, full){
var changed = this.changes == null || this.changes.comments
function(res){
var changed = res.changes === true || res.changes.comments
if(full || changed){
if(changed){
var comments = res.raw.comments && res.raw.comments.save || {}
// set the 'current' comment to the correct date...
@ -1552,67 +1629,6 @@ var FileSystemWriterActions = actions.Actions({
},
// Convert json index to a format compatible with file.writeIndex(..)
//
// This is here so as other features can participate in index
// preparation...
// There are several stages features can control the output format:
// 1) .json() action
// - use this for global high level serialization format
// - the output of this is .load(..) compatible
// 2) .prepareIndexForWrite(..) action
// - use this for file system write preparation
// - this directly affects the index structure
//
// This will get the base index, ignoring the cropped state.
//
// Returns:
// {
// // Timestamp...
// // NOTE: this is the timestamp used to write the index.
// date: <timestamp>,
//
// // This is the original json object, either the one passed as
// // an argument or the one returned by .json('base')
// raw: <original-json>,
//
// // this is the prepared index object, the one that is going to be
// // saved.
// index: <index-json>,
//
// ...
// }
//
//
// The format for the <prapared-json> is as follows:
// {
// <keyword>: <data>,
// ...
// }
//
// The <index-json> is written out to a fs index in the following
// way:
// <index-dir>/<timestamp>-<keyword>.json
//
// <index-dir> - taken from .config['index-dir'] (default: '.ImageGrid')
// <timestamp> - as returned by Date.timeStamp() (see: jli)
//
// For more info see file.writeIndex(..) and file.loadIndex(..).
//
// NOTE: this is the reverse of .prepareJSONForLoad(..)
prepareIndexForWrite: ['- File/Prepare index for writing',
function(json, full){
json = json || this.json('base')
var changes = full ? null
: this.hasOwnProperty('changes') ? this.changes
: null
return {
date: Date.timeStamp(),
raw: json,
index: file.prepareIndex(json, changes),
}
}],
// Save index...
//
// Returns:

View File

@ -467,14 +467,14 @@ module.Sort = core.ImageGridFeatures.Feature({
// maintain .sort_order and .sort_cache separately from .data in
// the store...
['prepareIndexForWrite',
function(res, _, full){
var c = this.changes
function(res){
var c = res.changes
;['sort_order', 'sort_cache']
.forEach(function(attr){
if((full || c == null || c[attr]) && res.raw.data[attr]){
if((c === true || c[attr]) && res.raw.data[attr]){
// full save...
if(full || c == null){
if(c === true){
res.index[attr] = res.raw.data[attr]
// build diff...

View File

@ -742,96 +742,6 @@ function(){
}
// Build a data and images objects from the json returned by loadIndex(..)
//
// Contrary to loadIndex(..) this expects a specific format of data:
// .data
// .images
// .bookmarked
// .marked
// .tags
// .current
//
//
// XXX need a clear format upgrade path/strategy...
// ...this can be:
// - full upgrade -- full update all data to new format
// - format continuation -- store in new format stating with a
// new snapshot keeping the older data as-is...
// XXX will need a "cut-off strategy", i.e. when a keyword
// stops being used we need some way to tell the
// loader/builder to ignore it...
// currently I'm for the second option...
//
// XXX move this to a better spot...
var buildIndex =
module.buildIndex = function(index, base_path){
var res = {}
// we'll handle these in a special way...
var special = [
'data',
'tags',
'bookmarked',
'marked',
'current',
'images',
]
// copy the rest as-is...
for(var k in index){
if(special.indexOf(k) > -1){
continue
}
res[k] = index[k]
}
// now do the special stuff...
var d = data.Data.fromJSON(index.data)
// buildup the data object...
// NOTE: this is mostly to attach stuff that is stored in separate files...
// images...
// XXX there seems to be a problem with updated images...
// - in the test set not all rotated manually images are loaded rotated...
var img = images.Images(index.images)
if(base_path){
d.base_path = base_path
// XXX STUB remove ASAP...
// ...need a real way to handle base dir, possible
// approaches:
// 1) .base_path attr in image, set on load and
// do not save (or ignore on load)...
// if exists prepend to all paths...
// - more to do in view-time
// + more flexible
// 2) add/remove on load/save (approach below)
// + less to do in real time
// - more processing on load/save
img.forEach(function(_, img){ img.base_path = base_path })
}
// extra stuff...
// ...this will restore stuff stored in the data but not explicitly
// restored above...
Object.keys(index.data).forEach(function(k){
if(d[k] === undefined){
d[k] = index.data[k]
}
})
res.data = d
res.images = img
return res
}
/*********************************************************************/
// Builder...
@ -852,63 +762,6 @@ module.buildIndex = function(index, base_path){
//
// NOTE: for now we'll stick to the current format...
// this will take the output of .json()
//
// .data
// .images
// .bookmarked
// .marked
// .tags
// .current
//
//
// changes can be:
// true | null - write all
// false - write only .current
// <detailed-format>
// - see below...
//
// changes detailed format:
// {
// data: <bool>,
//
// images: <bool> | [ <gid>, ... ]
//
// tags: <bool>,
// bookmarked: <bool>,
// selected: <bool>,
// }
//
// NOTE: this will prepare for version 2.0 file structure...
var prepareIndex =
module.prepareIndex =
function(json, changes){
changes = changes === false ? false
// nothing set then save all...
: changes == null ? true
: changes
var res = {}
// data...
if(changes === true || changes && changes.data){
res.data = json.data
}
if(changes === true || changes && changes.images === true){
res.images = json.images
} else if(changes && changes.images){
var diff = res['images-diff'] = {}
changes.images.forEach(function(gid){
diff[gid] = json.images[gid]
})
}
return res
}
var FILENAME = '${DATE}-${KEYWORD}.${EXT}'
// NOTE: keyword in index can take the form <path>/<keyword>, in which