added cloning and some cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2015-12-08 19:05:13 +03:00
parent 227e732cfa
commit 54c37a769a
4 changed files with 121 additions and 19 deletions

View File

@ -750,11 +750,27 @@ module.MetaActions = {
// NOTE: this will not copy/clone any data.
//
// XXX is this correct???
clone: function(){
// XXX should this be an action???
clone: function(full){
var o = Object.create(this)
o.config = Object.create(this.config)
if(this.config){
if(full){
o.config = JSON.parse(JSON.stringify(this.config))
} else {
o.config = Object.create(this.config)
}
}
return o
},
// This will create a .config in instances...
// NOTE: this makes Actions compatible with lib/object.js...
__init__: function(){
if(this.__proto__.config && !Object.hasOwnProperty(this, 'config')){
this.config = Object.create(this.__proto__.config)
}
}
}

View File

@ -330,14 +330,6 @@ module.QueueActions = actions.Actions({
delete this.__failed
delete this.__done
}],
// This will create a .config in instances...
__init__: function(){
if(this.__proto__.config && !Object.hasOwnProperty(this, 'config')){
this.config = Object.create(this.__proto__.config)
}
}
})

View File

@ -122,6 +122,19 @@ var RibbonsPrototype = {
this.images = images
},
// XXX
clone: function(){
var o = new this.constructor()
if(this.viewer){
// XXX does this completely detach from the orriginal???
// XXX do we need to reattach something???
o.viewer = this.viewer.clone()
}
if(this.images){
o.images = this.images.clone()
}
return o
},
// Constructors...
createViewer: RibbonsClassPrototype.createViewer,

View File

@ -260,6 +260,21 @@ actions.Actions({
delete this.images
}],
// NOTE: for complete isolation it is best to completely copy the
// .config...
clone: [function(full){
var res = actions.MetaActions.clone.call(this, full)
if(this.data){
res.data = this.data.clone()
}
if(this.images){
res.images = this.images.clone()
}
return res
}],
// XXX should this be here???
loadURLs: ['File/Load a URL list',
function(lst, base){
@ -1000,6 +1015,18 @@ actions.Actions({
clear: [
function(){ this.ribbons.clear() }],
clone: [function(full){
return function(res){
if(this.ribbons){
// NOTE: this is a bit wasteful as .ribbons will clone
// their ref to .images that we will throw away...
res.ribbons = this.ribbons.clone()
res.ribbons.images = res.images
}
}
}],
loadURLs: [
function(){
return function(){
@ -1449,19 +1476,39 @@ module.Journal = ImageGridFeatures.Feature({
depends: ['base'],
actions: actions.Actions({
journal: null,
clone: [function(full){
return function(res){
res.journal = null
if(full && Object.hasOwnProperty(this, 'journal') && this.journal){
res.journal = JSON.parse(JSON.stringify(this.journal))
}
}
}],
// XXX might be good to add some kind of metadata to journal...
journalPush: ['Journal/Add an item to journal',
function(){
if(this.journal == null){
this.journal = []
}
this.journal = (Object.hasOwnProperty(this, 'journal')
|| this.journal) ?
this.journal
: []
//console.log('ACTION:', action, args2array(arguments))
this.journal.push(args2array(arguments))
}],
clearJournal: ['Journal/Clear the action journal',
function(){
if(this.journal){
delete this.journal
// NOTE: overwriting here is better as it will keep
// shadowing the parent's .journal in case we
// are cloned.
// NOTE: either way this will have no effect as we
// only use the local .journal but the user may
// get confused...
//delete this.journal
this.journal = null
}
}],
runJournal: ['Journal/Run journal',
@ -3402,6 +3449,17 @@ var FileSystemLoaderActions = actions.Actions({
'index-dir': '.ImageGrid',
},
clone: [function(full){
return function(res){
if(this.base_path){
res.base_path = this.base_path
}
if(this.loaded_paths){
res.loaded_paths = JSON.parse(JSON.stringify(this.loaded_paths))
}
}
}],
// NOTE: these will remove the trailing '/' (or '\') unless the path
// is root...
// ...this is mainly to facilitate better browse support, i.e.
@ -3816,6 +3874,14 @@ var FileSystemWriterActions = actions.Actions({
// NOTE: .current is written always.
chages: null,
clone: [function(full){
return function(res){
res.changes = null
if(full && Object.hasOwnProperty(this, 'changes') && this.changes){
res.changes = JSON.parse(JSON.stringify(this.changes))
}
}
}],
// Convert json index to a format compatible with file.writeIndex(..)
//
@ -3861,7 +3927,9 @@ var FileSystemWriterActions = actions.Actions({
prepareIndexForWrite: ['File/Prepare index for writing',
function(json, full){
json = json || this.json('base')
var changes = full ? null : this.changes
var changes = full ? null
: Object.hasOwnProperty(this, 'changes') ? this.changes
: null
return {
raw: json,
prepared: file.prepareIndex(json, changes),
@ -4017,7 +4085,11 @@ module.FileSystemWriter = ImageGridFeatures.Feature({
'loadURLs',
].join(' '),
function(_, target){
delete this.changes
// NOTE: this is better than delete as it will shadow
// the parent's changes in case we got cloned from
// a live instance...
//delete this.changes
this.changes = null
}],
// data...
@ -4045,7 +4117,10 @@ module.FileSystemWriter = ImageGridFeatures.Feature({
'collapseGroup',
].join(' '),
function(_, target){
var changes = this.changes = this.changes || {}
var changes = this.changes =
Object.hasOwnProperty(this, 'changes') ?
this.changes
: {}
changes.data = true
}],
@ -4058,7 +4133,10 @@ module.FileSystemWriter = ImageGridFeatures.Feature({
'flipVertical',
].join(' '),
function(_, target){
var changes = this.changes = this.changes || {}
var changes = this.changes =
Object.hasOwnProperty(this, 'changes') ?
this.changes
: {}
var images = changes.images = changes.images || []
target = this.data.getImage(target)
@ -4069,7 +4147,10 @@ module.FileSystemWriter = ImageGridFeatures.Feature({
// NOTE: tags are also stored in images...
['tag untag',
function(_, tags, gids){
var changes = this.changes = this.changes || {}
var changes = this.changes =
Object.hasOwnProperty(this, 'changes') ?
this.changes
: {}
var images = changes.images = changes.images || []
gids = gids || [this.data.getImage()]