added generic index comments + reworked how .savecomments to use it...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-06-14 00:04:53 +03:00
parent a250c248ae
commit 09202c09f6
2 changed files with 166 additions and 62 deletions

View File

@ -769,6 +769,130 @@ module.FileSystemLoaderUI = core.ImageGridFeatures.Feature({
//---------------------------------------------------------------------
// FS Comments...
// XXX split this to loader and writer???
var FileSystemCommentsActions = actions.Actions({
// Format:
// {
// <keywork>: <data>,
// ...
// }
__comments: null,
get comments(){
var comments = this.__comments = this.__comments || {}
return comments
},
set comments(value){
this.__comments = value
},
/* XXX we do not actually need this...
// XXX this will not save comments for merged indexes...
saveComments: ['- File/',
function(path, date, logger){
if(this.location.method != 'loadIndex'
|| this.location.loaded.length > 1){
return
}
logger = logger || this.logger
var path = this.location.path
var comments_dir = this.config['index-dir'] +'/comments'
var data = JSON.parse(JSON.stringify(this.comments))
// XXX
return file.writeIndex(
data,
path +'/'+ comments_dir,
date || Date.timeStamp(),
this.config['index-filename-template'],
logger)
}],
//*/
// XXX at this point this makes no attempt to merge comments of
// nested indexes...
// ...until there is a generic merge protocol defined, clients
// should take care of merging for themselves...
loadComments: ['- File/',
function(path, date, logger){
if(this.location.method != 'loadIndex'){
return
}
logger = logger || this.logger
var that = this
var path = this.location.path
var comments_dir = this.config['index-dir'] +'/comments'
return file.loadIndex(path, this.config['index-dir'] +'/comments', date, logger)
// XXX should this merge the comments???
.then(function(res){
that.comments = res[path]
return res
})
}],
})
var FileSystemComments =
module.FileSystemComments = core.ImageGridFeatures.Feature({
title: '',
doc: '',
tag: 'fs-comments',
depends: [
'fs-loader'
],
suggested: [
],
actions: FileSystemCommentsActions,
handlers: [
// save/resore .comments
['json',
function(res){
if(this.comments != null){
res.comments = JSON.parse(JSON.stringify(this.comments))
}
}],
['load',
function(_, data){
if(data.comments != null){
this.comments = data.comments
}
}],
['prepareIndexForWrite',
function(res){
var changed = this.changes == null
|| this.changes.comments
if(changed){
var comments = res.raw.comments
Object.keys(comments).forEach(function(k){
res.index['comments/' + k] = comments[k]
})
}
}],
['loadIndex',
function(res){
var that = this
res.then(function(){
that.loadComments()
})
}],
],
})
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Save History... // Save History...
@ -776,18 +900,17 @@ var FileSystemSaveHistoryActions = actions.Actions({
// Save comments... // Save comments...
// //
// Format: // Format:
// { // .comments.save = {
// // comment staged for next .saveIndex(..)... // // comment staged for next .saveIndex(..)...
// 'current': <comment>, // 'current': <comment>,
// //
// <timestamp>: <comment>, // <timestamp>: <comment>,
// ... // ...
// } // }
savecomments: null,
getSaveComment: ['- File/', getSaveComment: ['- File/',
function(save){ function(save){
return this.savecomments && this.savecomments[save || 'current'] || '' }], return (this.comments.save && this.comments.save[save || 'current']) || '' }],
// Comment a save... // Comment a save...
// //
// Comment current save... // Comment current save...
@ -811,7 +934,7 @@ var FileSystemSaveHistoryActions = actions.Actions({
// ...normally it is Date.timeStamp() compatible string. // ...normally it is Date.timeStamp() compatible string.
setSaveComment: ['- File/Comment a save', setSaveComment: ['- File/Comment a save',
function(save, comment){ function(save, comment){
var comments = this.savecomments = this.savecomments || {} var comments = this.comments.save = this.comments.save || {}
// no explicit save given -- stage a comment for next save... // no explicit save given -- stage a comment for next save...
if(comment === undefined){ if(comment === undefined){
@ -829,7 +952,7 @@ var FileSystemSaveHistoryActions = actions.Actions({
comments[save] = comment comments[save] = comment
} }
this.markChanged('savecomments') this.markChanged('comments')
}], }],
loadSaveHistoryList: ['- File/', loadSaveHistoryList: ['- File/',
@ -848,7 +971,8 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
tag: 'fs-save-history', tag: 'fs-save-history',
depends: [ depends: [
'fs-loader' 'fs-loader',
'fs-comments',
], ],
suggested: [ suggested: [
'ui-fs-save-history', 'ui-fs-save-history',
@ -857,39 +981,6 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
actions: FileSystemSaveHistoryActions, actions: FileSystemSaveHistoryActions,
handlers: [ handlers: [
// XXX save/load comments to:
// <index-dir>/comments/<timestamp>-save.json
// XXX might be a good idea to make this a more generic comment
// framework...
// .comments.<type> - specific comment set...
['saveIndex',
function(){
// XXX if .savecomments changed, save it...
}],
['loadIndex',
function(){
// XXX load save comments...
}],
// XXX legacy comment handling....
// ...remove this as soon as the new scheme is done...
// save/resore .savecomments
//
['json',
function(res){
if(this.savecomments != null){
res.savecomments = JSON.parse(JSON.stringify(this.savecomments))
}
}],
['load',
function(_, data){
if(data.savecomments != null){
this.savecomments = data.savecomments
}
}],
// Prepare comments for writing... // Prepare comments for writing...
// //
// NOTE: defining this here enables us to actually post-bind to // NOTE: defining this here enables us to actually post-bind to
@ -897,29 +988,24 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
// available. // available.
['prepareIndexForWrite', ['prepareIndexForWrite',
function(res){ function(res){
var changed = this.changes == null var changed = this.changes == null || this.changes.comments
|| this.changes.savecomments
if(changed){ if(changed){
var comments = res.raw.savecomments || {} var comments = res.raw.comments && res.raw.comments.save || {}
// set the 'current' comment to the correct date... // set the 'current' comment to the correct date...
if(comments.current){ if(comments.current){
comments[res.date] = comments.current comments[res.date] = comments.current
delete comments.current delete comments.current
} }
res.prepared.savecomments = comments
} }
}], }],
// replace .savecomments['current'] with .location.from... // replace .comments.save['current'] with .location.from...
// // drop unsaved changes...
// NOTE: this will also drop any unsaved changes from browsing
// history...
['saveIndex', ['saveIndex',
function(res){ function(res){
var that = this var that = this
var comments = this.savecomments var comments = this.comments.save
if(comments && comments.current){ if(comments && comments.current){
res res
@ -935,7 +1021,6 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
}) })
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Save History UI... // Save History UI...
@ -977,7 +1062,7 @@ var FileSystemSaveHistoryUIActions = actions.Actions({
date = date || 'current' date = date || 'current'
a = a || that a = a || that
var comment = a.savecomments && a.savecomments[date] var comment = a.comments.save && a.comments.save[date]
//title.push(comment || '') //title.push(comment || '')
comment && title.push(comment) comment && title.push(comment)
@ -1078,7 +1163,7 @@ var FileSystemSaveHistoryUIActions = actions.Actions({
// NOTE: the original 'current' // NOTE: the original 'current'
// comment is saved to // comment is saved to
// .unsaved_index // .unsaved_index
delete that.savecomments.current delete that.comments.save.current
}) })
}) })
// mark the current loaded position... // mark the current loaded position...
@ -1377,9 +1462,11 @@ var FileSystemWriterActions = actions.Actions({
// // an argument or the one returned by .json('base') // // an argument or the one returned by .json('base')
// raw: <original-json>, // raw: <original-json>,
// //
// // this is the prepared object, the one that is going to be // // this is the prepared index object, the one that is going to be
// // saved. // // saved.
// prepared: <prepared-json>, // index: <index-json>,
//
// ...
// } // }
// //
// //
@ -1389,7 +1476,7 @@ var FileSystemWriterActions = actions.Actions({
// ... // ...
// } // }
// //
// The <prepared-json> is written out to a fs index in the following // The <index-json> is written out to a fs index in the following
// way: // way:
// <index-dir>/<timestamp>-<keyword>.json // <index-dir>/<timestamp>-<keyword>.json
// //
@ -1407,7 +1494,7 @@ var FileSystemWriterActions = actions.Actions({
return { return {
date: Date.timeStamp(), date: Date.timeStamp(),
raw: json, raw: json,
prepared: file.prepareIndex(json, changes), index: file.prepareIndex(json, changes),
} }
}], }],
@ -1462,7 +1549,7 @@ var FileSystemWriterActions = actions.Actions({
var full_path = path +'/'+ this.config['index-dir'] var full_path = path +'/'+ this.config['index-dir']
return file.writeIndex( return file.writeIndex(
index.prepared, index.index,
// XXX should we check if index dir is present in path??? // XXX should we check if index dir is present in path???
//path, //path,
full_path, full_path,
@ -1481,7 +1568,8 @@ var FileSystemWriterActions = actions.Actions({
location.method = 'loadIndex' location.method = 'loadIndex'
location.from = index.date location.from = index.date
return location //return location
return index
}) })
}], }],
@ -1628,7 +1716,7 @@ var FileSystemWriterActions = actions.Actions({
// NOTE: if we are to use .saveIndex(..) here, do not forget // NOTE: if we are to use .saveIndex(..) here, do not forget
// to reset .changes // to reset .changes
queue.push(file.writeIndex( queue.push(file.writeIndex(
index.prepared, index.index,
index_path, index_path,
index.date, index.date,
this.config['index-filename-template'], this.config['index-filename-template'],

View File

@ -367,6 +367,7 @@ function(path, index_dir, logger){
.then(function(paths){ .then(function(paths){
// start loading... // start loading...
return Promise.all(paths.map(function(p){ return Promise.all(paths.map(function(p){
p = util.normalizePath(p)
//var path = pathlib.normalize(p +'/'+ index_dir) //var path = pathlib.normalize(p +'/'+ index_dir)
var path = util.normalizePath(p +'/'+ index_dir) var path = util.normalizePath(p +'/'+ index_dir)
return loadSaveHistoryList(path, index_dir) return loadSaveHistoryList(path, index_dir)
@ -485,9 +486,14 @@ function(path, index_dir, from_date, logger){
// NOTE: one way to do this is use the logger, it will get // NOTE: one way to do this is use the logger, it will get
// each index data on an index event // each index data on an index event
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
// prepare the index_dir and path....
// NOTE: index_dir can be more than a single directory...
var i = util.normalizePath(index_dir).split(/[\\\/]/g)
var p = util.normalizePath(path).split(/[\\\/]/g).slice(-i.length)
// we've got an index... // we've got an index...
// XXX do we need to check if if it's a dir??? // XXX do we need to check if if it's a dir???
if(pathlib.basename(path) == index_dir){ if(i.filter(function(e, j){ return e == p[j] }).length == i.length){
logger && logger.emit('path', path) logger && logger.emit('path', path)
@ -592,6 +598,7 @@ function(path, index_dir, from_date, logger){
.then(function(paths){ .then(function(paths){
// start loading... // start loading...
Promise.all(paths.map(function(p){ Promise.all(paths.map(function(p){
p = util.normalizePath(p)
//var path = pathlib.normalize(p +'/'+ index_dir) //var path = pathlib.normalize(p +'/'+ index_dir)
var path = util.normalizePath(p +'/'+ index_dir) var path = util.normalizePath(p +'/'+ index_dir)
return loadIndex(path, index_dir, from_date, logger) return loadIndex(path, index_dir, from_date, logger)
@ -910,6 +917,9 @@ function(json, changes){
var FILENAME = '${DATE}-${KEYWORD}.${EXT}' var FILENAME = '${DATE}-${KEYWORD}.${EXT}'
// NOTE: keyword in index can take the form <path>/<keyword>, in which
// case the keyword will be saved to the <path>/<file-format>
// NOTE: loadIndex(..) at this point will ignore sub-paths...
var writeIndex = var writeIndex =
module.writeIndex = module.writeIndex =
function(json, path, date, filename_tpl, logger){ function(json, path, date, filename_tpl, logger){
@ -935,7 +945,14 @@ function(json, path, date, filename_tpl, logger){
// be too many files... // be too many files...
return Promise return Promise
.all(Object.keys(json).map(function(keyword){ .all(Object.keys(json).map(function(keyword){
var file = path +'/'+ (filename_tpl var data = JSON.stringify(json[keyword])
// get the sub-path and keyword...
var sub_path = keyword.split(/[\\\/]/g)
keyword = sub_path.pop()
sub_path = sub_path.join('/')
var file = path +'/'+ sub_path +'/'+ (filename_tpl
.replace('${DATE}', date) .replace('${DATE}', date)
.replace('${KEYWORD}', keyword) .replace('${KEYWORD}', keyword)
.replace('${EXT}', 'json')) .replace('${EXT}', 'json'))
@ -943,7 +960,6 @@ function(json, path, date, filename_tpl, logger){
return ensureDir(pathlib.dirname(file)) return ensureDir(pathlib.dirname(file))
.then(function(){ .then(function(){
files.push(file) files.push(file)
var data = JSON.stringify(json[keyword])
logger && logger.emit('queued', file) logger && logger.emit('queued', file)