more refactoring of data.js...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-11-22 18:25:40 +03:00
parent 8e6e7de822
commit b6e15f7385

View File

@ -2017,11 +2017,14 @@ var DataPrototype = {
res.current = this.current res.current = this.current
res.order = this.order.slice() res.order = this.order.slice()
res.ribbon_order = this.ribbon_order.slice() res.ribbon_order = this.ribbon_order.slice()
res.ribbons = {}
// make ribbons sparse... this.eachImageList(function(lst, k, s){
for(var k in this.ribbons){ if(res[s] == null){
res.ribbons[k] = this.ribbons[k].slice() res[s] = {}
} }
res[s][k] = this[s][k].slice()
})
return res return res
}, },
@ -2061,11 +2064,20 @@ var DataPrototype = {
this.current = data.current this.current = data.current
this.order = data.order.slice() this.order = data.order.slice()
this.ribbon_order = data.ribbon_order.slice() this.ribbon_order = data.ribbon_order.slice()
this.ribbons = {}
// make ribbons sparse... // make sparse lists...
for(var k in data.ribbons){ var that = this
this.ribbons[k] = this.makeSparseImages(data.ribbons[k]) this.__gid_lists.forEach(function(s){
} if(data[s] == null){
return
}
if(that[s] == null){
that[s] = {}
}
for(var k in data[s]){
that[s][k] = that.makeSparseImages(data[s][k])
}
})
return this return this
}, },
@ -2082,10 +2094,13 @@ var DataPrototype = {
ribbon_order: this.ribbon_order.slice(), ribbon_order: this.ribbon_order.slice(),
ribbons: {}, ribbons: {},
} }
// compact ribbons... // compact sets...
for(var k in this.ribbons){ this.eachImageList(function(lst, k, s){
res.ribbons[k] = this.ribbons[k].compact() if(res[s] == null){
} res[s] = {}
}
res[s][k] = lst.compact()
})
if(mode == 'string' || mode == 'str'){ if(mode == 'string' || mode == 'str'){
res = JSON.stringify(res) res = JSON.stringify(res)
} }
@ -2103,42 +2118,7 @@ var DataWithTagsPrototype = {
// ....is there a way not to say DataPrototype here??? // ....is there a way not to say DataPrototype here???
__gid_lists: DataPrototype.__gid_lists.concat(['tags']), __gid_lists: DataPrototype.__gid_lists.concat(['tags']),
// NOTE: this is here only to make the tags mutable...
// XXX need a unified init/dump/load scheme...
loadJSON: function(json){
json = typeof(json) == typeof('str') ? JSON.parse(json) : json
// XXX hate manual super calls...
DataWithTagsPrototype.__proto__.loadJSON.call(this, json)
if(json.tags != null){
// XXX should we make copy here???
this.tags = json.tags
this.sortTags()
}
return this
},
dumpJSON: function(){
// XXX hate manual super calls...
var res = DataWithTagsPrototype.__proto__.dumpJSON.call(this)
if(this.tags != null){
var tags = this.tags
var restags = {}
Object.keys(tags).forEach(function(tag){
var lst = tags[tag].compact()
if(lst.len > 0){
restags[tag] = lst
}
})
if(Object.keys(restags).length > 0){
res.tags = restags
}
}
return res
},
crop: function(){ crop: function(){
var crop = DataWithTagsPrototype.__proto__.crop.apply(this, arguments) var crop = DataWithTagsPrototype.__proto__.crop.apply(this, arguments)
@ -2149,29 +2129,7 @@ var DataWithTagsPrototype = {
return crop return crop
}, },
clone: function(){
var clone = DataWithTagsPrototype.__proto__.clone.apply(this, arguments)
if(this.tags != null){
clone.tags = {}
for(var k in this.tags){
clone.tags[k] = this.tags[k].slice()
}
}
return clone
},
group: function(){
var res = DataWithTagsPrototype.__proto__.group.apply(this, arguments)
this.sortTags()
return res
},
ungroup: function(){
var res = DataWithTagsPrototype.__proto__.ungroup.apply(this, arguments)
this.sortTags()
return res
},
sortTags: function(){ sortTags: function(){
var that = this var that = this