refactored comments, now almost happy with them...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-06-14 17:39:26 +03:00
parent 1559a6fc42
commit b357732259

View File

@ -770,24 +770,90 @@ module.FileSystemLoaderUI = core.ImageGridFeatures.Feature({
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// FS Comments... // Comments...
// XXX these are quite generic, might be a good idea to move them out of fs...
// XXX split this to loader and writer??? var CommentsActions = actions.Actions({
var FileSystemCommentsActions = actions.Actions({
// Format: // Format:
// { // {
// // raw loaded comments...
// raw: {
// <path>: <comments>,
// ...
// },
//
// <keywork>: <data>, // <keywork>: <data>,
// ... // ...
// } // }
__comments: null, __comments: null,
get comments(){ get comments(){
var comments = this.__comments = this.__comments || {} return this.__comments
return comments
}, },
set comments(value){ set comments(value){
this.__comments = value this.__comments = value
}, },
})
var Comments =
module.Comments = core.ImageGridFeatures.Feature({
title: '',
doc: '',
tag: 'comments',
actions: CommentsActions,
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
}
}],
// prepare comments for saving to "comments/<keyword>"...
//
// NOTE: this will skip the 'raw' comment field...
['prepareIndexForWrite',
function(res){
var changed = this.changes == null
|| this.changes.comments
if(changed && res.raw.comments){
var comments = res.raw.comments
Object.keys(comments)
// skip the raw field...
.filter(function(k){ return k != 'raw' })
.forEach(function(k){
res.index['comments/' + k] = comments[k]
})
}
}],
],
})
//---------------------------------------------------------------------
// FS Comments...
// XXX split this to loader and writer???
// XXX might be good to split this to a generic API and a loader...
var FileSystemCommentsActions = actions.Actions({
config: {
// This helps prevent the comment loading process from delaying
// showing the user the images...
'comments-delay-load': 300,
},
/* XXX we do not actually need this... /* XXX we do not actually need this...
// XXX this will not save comments for merged indexes... // XXX this will not save comments for merged indexes...
@ -813,10 +879,6 @@ var FileSystemCommentsActions = actions.Actions({
logger) 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/', loadComments: ['- File/',
function(path, date, logger){ function(path, date, logger){
if(this.location.method != 'loadIndex'){ if(this.location.method != 'loadIndex'){
@ -826,16 +888,34 @@ var FileSystemCommentsActions = actions.Actions({
logger = logger || this.logger logger = logger || this.logger
var that = this var that = this
var path = this.location.path var loaded = this.location.loaded
var comments_dir = this.config['index-dir'] +'/comments'
return file.loadIndex(path, this.config['index-dir'] +'/comments', date, logger) // prepare empty comments...
// XXX should this merge the comments??? // XXX should we reset or just merge???
this.comments = {
raw: {}
}
return Promise.all(loaded.map(function(path){
var comments_dir = that.config['index-dir'] +'/comments'
return file.loadIndex(path, that.config['index-dir'] +'/comments', date, logger)
.then(function(res){ .then(function(res){
that.comments = res[path]
// if we have no sub-indexes just load the
// comments as-is...
if(loaded.length == 1){
that.comments = JSON.parse(JSON.stringify(res[path]))
that.comments.raw = {path: res[path]}
// sub-indexes -> let the client merge their stuff...
} else {
that.comments.raw[path] = res[path]
}
return res return res
}) })
}))
}], }],
}) })
@ -847,48 +927,21 @@ module.FileSystemComments = core.ImageGridFeatures.Feature({
tag: 'fs-comments', tag: 'fs-comments',
depends: [ depends: [
'fs-loader' 'comments',
], 'fs-loader',
suggested: [
], ],
actions: FileSystemCommentsActions, actions: FileSystemCommentsActions,
handlers: [ 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
}
}],
// prepare comments for saving to "comments/<keyword>"...
['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', ['loadIndex',
function(res){ function(res){
var that = this var that = this
res.then(function(){ res.then(function(){
setTimeout(function(){
that.loadComments() that.loadComments()
}) }, that.config['comments-delay-load'] || 0) })
}], }],
], ],
}) })
@ -904,15 +957,23 @@ var FileSystemSaveHistoryActions = actions.Actions({
// Format: // Format:
// .comments.save = { // .comments.save = {
// // comment staged for next .saveIndex(..)... // // comment staged for next .saveIndex(..)...
// //
// // NOTE: 'current' will get replaced with save timestamp...
// 'current': <comment>, // 'current': <comment>,
// //
// <timestamp>: <comment>, // <timestamp>: <comment>,
// ... // ...
// } // }
// NOTE: if comments are not loaded yet this will break...
getSaveComment: ['- File/', getSaveComment: ['- File/',
function(save){ function(save){
this.comments == null
&& console.error('Comments do not appear to be loaded yet...')
return (this.comments.save && this.comments.save[save || 'current']) || '' }], return (this.comments.save && this.comments.save[save || 'current']) || '' }],
// Comment a save... // Comment a save...
// //
// Comment current save... // Comment current save...
@ -934,8 +995,12 @@ var FileSystemSaveHistoryActions = actions.Actions({
// NOTE: "save" is the save format as returned by file.groupByDate(..), // NOTE: "save" is the save format as returned by file.groupByDate(..),
// or .loadSaveHistoryList(..) // or .loadSaveHistoryList(..)
// ...normally it is Date.timeStamp() compatible string. // ...normally it is Date.timeStamp() compatible string.
// NOTE: if comments are not loaded yet this will break...
setSaveComment: ['- File/Comment a save', setSaveComment: ['- File/Comment a save',
function(save, comment){ function(save, comment){
this.comments == null
&& console.error('Comments do not appear to be loaded yet...')
var comments = this.comments.save = this.comments.save || {} 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...
@ -985,9 +1050,12 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
handlers: [ handlers: [
// Prepare comments for writing... // Prepare comments for writing...
// //
// These will replace .comments.save['current'] with .location.from...
//
// NOTE: defining this here enables us to actually post-bind to // NOTE: defining this here enables us to actually post-bind to
// an action that is defined later or may not even be // an action that is defined later or may not even be
// available. // available.
// NOTE: 'loadIndex' will also drop any unsaved changes...
['prepareIndexForWrite', ['prepareIndexForWrite',
function(res){ function(res){
var changed = this.changes == null || this.changes.comments var changed = this.changes == null || this.changes.comments
@ -1002,8 +1070,6 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
} }
} }
}], }],
// replace .comments.save['current'] with .location.from...
// drop unsaved changes...
['saveIndex', ['saveIndex',
function(res){ function(res){
var that = this var that = this
@ -1019,14 +1085,32 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({
}) })
} }
// drop unsaved changes...
delete this.unsaved_index delete this.unsaved_index
}], }],
// XXX merge comments... // merge save comments...
// ...not yet sure how... ['loadComments',
['loadIndex',
function(res){ function(res){
// XXX var that = this
res.then(function(){
// NOTE: if we loaded just one index the comments
// are already loaded and we do not need to do
// anything...
if(that.location.loaded.length > 1){
var comments = that.comments
var raw = comments.raw
comments.save = {}
Object.keys(raw).forEach(function(path){
raw[path] && Object.keys(raw[path].save || {}).forEach(function(date){
comments.save[date] = raw[path].save[date]
})
})
}
})
}], }],
] ]
}) })