Browse Source

CTRLBanksStore, CTRLPalettesStore, and NESPalette now have an .obj property which returns a java object of the class's underlying data in a form that can be turned into a JSON. Each class's .json property has been modified to use the new .obj property.

dev
Bryan Miller 5 years ago
parent
commit
efbeb123db
3 changed files with 54 additions and 22 deletions
  1. +21
    -2
      app/js/ctrls/CTRLBanksStore.js
  2. +18
    -11
      app/js/ctrls/CTRLPalettesStore.js
  3. +15
    -9
      app/js/models/NESPalette.js

+ 21
- 2
app/js/ctrls/CTRLBanksStore.js View File

@@ -111,14 +111,33 @@ class CTRLBanksStore{
return Object.keys(Banks).length;
}

get json(){
get obj(){
var data = [];
Object.keys(Banks).forEach((key) => {
if (Banks.hasOwnProperty(key)){
data.push({name:key, data:Banks[key].bank.base64});
}
});
return JSON.stringify(data);
return data;
}

set obj(d){
if (!(d instanceof Array))
throw new TypeError("Expected Array object.");
this.clear();
d.forEach((item) => {
if (typeof(item) === typeof({})){
if ((name in item) && (data in item)){
this.createBank(item.name, item.data);
} else {
console.log("WARNING: Bank object missing required properties. Skipped.");
}
}
});
}

get json(){
return JSON.stringify(this.obj);
}

initialize(){

+ 18
- 11
app/js/ctrls/CTRLPalettesStore.js View File

@@ -153,24 +153,19 @@ class CTRLPalettesStore{
}).bind(this));
}

get json(){
get obj(){
var d = {
cpi: CurrentPaletteIndex,
pals: []
};
for (let i=0; i < Palettes.length; i++){
d.pals.push([Palettes[i][0], Palettes[i][1].json]);
d.pals.push([Palettes[i][0].value, Palettes[i][1].json]);
}
return JSON.stringify(d);
return d;
}

set json(j){
try {
var d = JSON.parse(j);
} catch (e) {
throw e;
}

set obj(d){
if (d.hasOwnProperty("cpi") && d.hasOwnProperty("pals")){
if (Utils.isInt(d.cpi) && d.pals instanceof Array){
var newPalettes = []
@@ -189,10 +184,22 @@ class CTRLPalettesStore{
GlobalEvents.emit("set_app_palette", Palettes[CurrentPaletteIndex][1]);
}
} else {
throw new TypeError("JSON Property Value types invalid.");
throw new TypeError("Object Property Value types invalid.");
}
} else {
throw new TypeError("JSON missing expected properties.");
throw new TypeError("Object missing expected properties.");
}
}

get json(){
return JSON.stringify(this.obj);
}

set json(j){
try {
this.obj = JSON.parse(j);
} catch (e) {
throw e;
}
}


+ 15
- 9
app/js/models/NESPalette.js View File

@@ -22,21 +22,27 @@ export default class NESPalette extends EventCaller{
];
}

get json(){
return JSON.stringify(([this.__BGColor]).concat(this.__palette));
get obj(){
return ([this.__BGColor]).concat(this.__palette);
}

set json(j){
try{
var d = JSON.parse(j);
set obj(d){
if (!(d instanceof Array) || d.length !== 25)
throw new TypeError("Invalid Object or value range.");
try {
this.set_palette(d);
} catch (e) {
throw e;
}
}

if (!(d instanceof Array) || d.length !== 25)
throw new TypeError("Invalid JSON or value range.");
try {
this.set_palette(d);
get json(){
return JSON.stringify(this.obj);
}

set json(j){
try{
this.obj = JSON.parse(j);
} catch (e) {
throw e;
}

Loading…
Cancel
Save