From 09202c09f6a33fd42f830bde7a20b0b52088bd96 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 14 Jun 2016 00:04:53 +0300 Subject: [PATCH] added generic index comments + reworked how .savecomments to use it... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/filesystem.js | 206 ++++++++++++++++++++++--------- ui (gen4)/imagegrid/file.js | 22 +++- 2 files changed, 166 insertions(+), 62 deletions(-) diff --git a/ui (gen4)/features/filesystem.js b/ui (gen4)/features/filesystem.js index 08fbdca1..168aa0b0 100755 --- a/ui (gen4)/features/filesystem.js +++ b/ui (gen4)/features/filesystem.js @@ -769,6 +769,130 @@ module.FileSystemLoaderUI = core.ImageGridFeatures.Feature({ +//--------------------------------------------------------------------- +// FS Comments... + +// XXX split this to loader and writer??? +var FileSystemCommentsActions = actions.Actions({ + // Format: + // { + // : , + // ... + // } + __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... @@ -776,18 +900,17 @@ var FileSystemSaveHistoryActions = actions.Actions({ // Save comments... // // Format: - // { + // .comments.save = { // // comment staged for next .saveIndex(..)... // 'current': , // // : , // ... // } - savecomments: null, getSaveComment: ['- File/', function(save){ - return this.savecomments && this.savecomments[save || 'current'] || '' }], + return (this.comments.save && this.comments.save[save || 'current']) || '' }], // Comment a save... // // Comment current save... @@ -811,7 +934,7 @@ var FileSystemSaveHistoryActions = actions.Actions({ // ...normally it is Date.timeStamp() compatible string. setSaveComment: ['- File/Comment a save', 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... if(comment === undefined){ @@ -829,7 +952,7 @@ var FileSystemSaveHistoryActions = actions.Actions({ comments[save] = comment } - this.markChanged('savecomments') + this.markChanged('comments') }], loadSaveHistoryList: ['- File/', @@ -848,7 +971,8 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({ tag: 'fs-save-history', depends: [ - 'fs-loader' + 'fs-loader', + 'fs-comments', ], suggested: [ 'ui-fs-save-history', @@ -857,39 +981,6 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({ actions: FileSystemSaveHistoryActions, handlers: [ - // XXX save/load comments to: - // /comments/-save.json - // XXX might be a good idea to make this a more generic comment - // framework... - // .comments. - 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... // // NOTE: defining this here enables us to actually post-bind to @@ -897,29 +988,24 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({ // available. ['prepareIndexForWrite', function(res){ - var changed = this.changes == null - || this.changes.savecomments + var changed = this.changes == null || this.changes.comments if(changed){ - var comments = res.raw.savecomments || {} + var comments = res.raw.comments && res.raw.comments.save || {} // set the 'current' comment to the correct date... if(comments.current){ comments[res.date] = comments.current delete comments.current } - - res.prepared.savecomments = comments } }], - // replace .savecomments['current'] with .location.from... - // - // NOTE: this will also drop any unsaved changes from browsing - // history... + // replace .comments.save['current'] with .location.from... + // drop unsaved changes... ['saveIndex', function(res){ var that = this - var comments = this.savecomments + var comments = this.comments.save if(comments && comments.current){ res @@ -935,7 +1021,6 @@ module.FileSystemSaveHistory = core.ImageGridFeatures.Feature({ }) - //--------------------------------------------------------------------- // Save History UI... @@ -977,7 +1062,7 @@ var FileSystemSaveHistoryUIActions = actions.Actions({ date = date || 'current' a = a || that - var comment = a.savecomments && a.savecomments[date] + var comment = a.comments.save && a.comments.save[date] //title.push(comment || '') comment && title.push(comment) @@ -1078,7 +1163,7 @@ var FileSystemSaveHistoryUIActions = actions.Actions({ // NOTE: the original 'current' // comment is saved to // .unsaved_index - delete that.savecomments.current + delete that.comments.save.current }) }) // mark the current loaded position... @@ -1377,9 +1462,11 @@ var FileSystemWriterActions = actions.Actions({ // // an argument or the one returned by .json('base') // raw: , // - // // 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. - // prepared: , + // index: , + // + // ... // } // // @@ -1389,7 +1476,7 @@ var FileSystemWriterActions = actions.Actions({ // ... // } // - // The is written out to a fs index in the following + // The is written out to a fs index in the following // way: // /-.json // @@ -1407,7 +1494,7 @@ var FileSystemWriterActions = actions.Actions({ return { date: Date.timeStamp(), 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'] return file.writeIndex( - index.prepared, + index.index, // XXX should we check if index dir is present in path??? //path, full_path, @@ -1481,7 +1568,8 @@ var FileSystemWriterActions = actions.Actions({ location.method = 'loadIndex' 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 // to reset .changes queue.push(file.writeIndex( - index.prepared, + index.index, index_path, index.date, this.config['index-filename-template'], diff --git a/ui (gen4)/imagegrid/file.js b/ui (gen4)/imagegrid/file.js index c2360bf6..18cce9bd 100755 --- a/ui (gen4)/imagegrid/file.js +++ b/ui (gen4)/imagegrid/file.js @@ -367,6 +367,7 @@ function(path, index_dir, logger){ .then(function(paths){ // start loading... return Promise.all(paths.map(function(p){ + p = util.normalizePath(p) //var path = pathlib.normalize(p +'/'+ index_dir) var path = util.normalizePath(p +'/'+ 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 // each index data on an index event 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... // 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) @@ -592,6 +598,7 @@ function(path, index_dir, from_date, logger){ .then(function(paths){ // start loading... Promise.all(paths.map(function(p){ + p = util.normalizePath(p) //var path = pathlib.normalize(p +'/'+ index_dir) var path = util.normalizePath(p +'/'+ index_dir) return loadIndex(path, index_dir, from_date, logger) @@ -910,6 +917,9 @@ function(json, changes){ var FILENAME = '${DATE}-${KEYWORD}.${EXT}' +// NOTE: keyword in index can take the form /, in which +// case the keyword will be saved to the / +// NOTE: loadIndex(..) at this point will ignore sub-paths... var writeIndex = module.writeIndex = function(json, path, date, filename_tpl, logger){ @@ -935,7 +945,14 @@ function(json, path, date, filename_tpl, logger){ // be too many files... return Promise .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('${KEYWORD}', keyword) .replace('${EXT}', 'json')) @@ -943,7 +960,6 @@ function(json, path, date, filename_tpl, logger){ return ensureDir(pathlib.dirname(file)) .then(function(){ files.push(file) - var data = JSON.stringify(json[keyword]) logger && logger.emit('queued', file)