Browse Source

Loading of a project json file now works!

dev
Bryan Miller 5 years ago
parent
commit
8922f21cf5
5 changed files with 59 additions and 109 deletions
  1. +8
    -13
      app/js/common/JSONSchema.js
  2. +19
    -32
      app/js/ctrls/CTRLBanksStore.js
  3. +11
    -11
      app/js/ctrls/CTRLIO.js
  4. +12
    -40
      app/js/ctrls/CTRLPalettesStore.js
  5. +9
    -13
      app/js/models/NESPalette.js

+ 8
- 13
app/js/common/JSONSchema.js View File

@@ -1,9 +1,7 @@



var SCHEMA_LIST = [];
var DIRTY = false;
var CUR_AJV = null;
var CUR_AJV = new Ajv();

export default Object.freeze({
add:function(s){
@@ -14,7 +12,7 @@ export default Object.freeze({
throw new Error("Schema already exists with $id '" + s["$id"] + "'.");
}
SCHEMA_LIST.push(s);
DIRTY = true;
CUR_AJV.addSchema(s);
},

remove:function(id){
@@ -23,7 +21,7 @@ export default Object.freeze({
});
if (idx >= 0){
SCHEMA_LIST.splice(idx, 1);
DIRTY = true;
CUR_AJV.removeSchema(id);
}
},

@@ -34,15 +32,12 @@ export default Object.freeze({
},

getValidator:function(id){
if (DIRTY){
DIRTY = false;
if (SCHEMA_LIST.length <= 0){
CUR_AJV = null;
} else {
CUR_AJV = new Ajv({schema:SCHEMA_LIST});
}
}
return (CUR_AJV !== null) ? CUR_AJV.getSchema(id) : null;
},

getLastErrors:function(){
if (CUR_AJV === null || CUR_AJV.errors === null){return null;}
return CUR_AJV.errors;
}
});


+ 19
- 32
app/js/ctrls/CTRLBanksStore.js View File

@@ -17,9 +17,10 @@ var Banks = {};
var CurrentBank = "";


const SCHEMA_ID="http://nespaint/BanksStoreSchema.json";
JSONSchema.add({
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "BanksStoreSchema.json",
"$id": SCHEMA_ID,
"type": "array",
"items":{
"type": "object",
@@ -146,23 +147,19 @@ class CTRLBanksStore{
}

set obj(d){
try {
this.json = JSON.stringify(d);
} catch (e) {
throw e;
}
/*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.");
}
var validator = JSONSchema.getValidator(SCHEMA_ID);
if (validator !== null && validator(d)){
this.clear();
d.forEach((item) => {
this.createBank(item.name, item.data);
});
} else {
var errs = JSONSchema.getLastErrors();
if (errs !== null){
console.log(errs);
}
});*/
throw new Error("Object failed to validate against BanksStoreSchema.");
}
}

get json(){
@@ -170,22 +167,11 @@ class CTRLBanksStore{
}

set json(j){
var validator = null;
try {
validator = JSONSchema.getValidator("BanksStoreSchema.json");
this.obj = JSON.parse(j);
} catch (e) {
throw e;
}

if (validator(j)){
this.clear();
var o = JSON.parse(j);
o.forEach((item) => {
this.createBank(item.name, item.data);
});
} else {
throw new Error("JSON Object validation failed.");
}
}

initialize(){
@@ -262,12 +248,13 @@ class CTRLBanksStore{

clear(){
Object.keys(Banks).forEach((item) => {
item.el.parentNode.removeChild(item.el);
Banks[item].el.parentNode.removeChild(Banks[item].el);
});
Banks = {};
if (CurrentBank !== "")
if (CurrentBank !== ""){
CurrentBank = "";
GlobalEvents.emit("change_surface", null);
CurrentBank = "";
}
}
}


+ 11
- 11
app/js/ctrls/CTRLIO.js View File

@@ -12,9 +12,10 @@ const SUPPORTED_PROJECT_VERSIONS=[
"0.1"
];

const SCHEMA_ID = "http://nespaint/Project.json";
JSONSchema.add({
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "NESPainterProject.json",
"$id": SCHEMA_ID,
"type":"object",
"properties":{
"id":{
@@ -25,8 +26,8 @@ JSONSchema.add({
"type":"string",
"pattern":"^[0-9]{1,}\.[0-9]{1,}$"
},
"paletteStore":{"$ref":"PalettesStoreSchema.json"},
"bankStore":{"$ref":"BanksStoreSchema.json"}
"paletteStore":{"$ref":"http://nespaint/PalettesStoreSchema.json"},
"bankStore":{"$ref":"http://nespaint/BanksStoreSchema.json"}
},
"required":["id","version","paletteStore","bankStore"]
});
@@ -104,18 +105,17 @@ function HANDLE_LoadProject(e){
if (this.files && this.files.length > 0){
var reader = new FileReader();
reader.onload = (function(e) {
var validator = null;
try{
validator = JSONSchema.getValidator("NESPainterProject.json");
var o = null;
var validator = JSONSchema.getValidator(SCHEMA_ID);
try {
o = JSON.parse(e.target.result);
} catch (e) {
console.log("Failed to validate project file. " + e.toString());
return;
console.log("Failed to parse JSON string. " + e.toString());
}
if (validator(e.target.result)){
var o = JSON.parse(e.target.result);
if (validator !== null && validator(o)){
// TODO: Validate 'id' and 'version' properties.
CTRLPalettesStore.obj = o.paletteStore;
CTRLBanksStore.obj = o.banksStore;
CTRLBanksStore.obj = o.bankStore;
}
if (this.parentNode.nodeName.toLowerCase() === "form"){
this.parentNode.reset();

+ 12
- 40
app/js/ctrls/CTRLPalettesStore.js View File

@@ -20,9 +20,10 @@ var CurrentPaletteIndex = 0;
var BlockEmits = false;


const SCHEMA_ID="http://nespaint/PalettesStoreSchema.json";
JSONSchema.add({
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "PalettesStoreSchema.json",
"$id": SCHEMA_ID,
"type": "array",
"items": {
"type": "object",
@@ -32,7 +33,7 @@ JSONSchema.add({
"minLength":1
},
"palette":{
"$ref":"NESPaletteSchema.json"
"$ref":"http://nespaint/NESPaletteSchema.json"
}
},
"required":["name","palette"]
@@ -184,34 +185,15 @@ class CTRLPalettesStore{


set obj(d){
try {
this.json = JSON.stringify(d);
} catch (e) {
throw e;
}
/*if (d.hasOwnProperty("cpi") && d.hasOwnProperty("pals")){
if (Utils.isInt(d.cpi) && d.pals instanceof Array){
var newPalettes = []
for (let i=0; i < d.pals.length; i++){
if (d.pals[i] instanceof Array){
if (this.getPalette(d.pals[i][0]) === null){
this.createPalette(d.pals[i][0], d.pals[i][1]);
}
}
}
CurrentPaletteIndex = 0
if (Palettes.length > 0){
if (d.cpi >= 0 && d.cpi < Palettes.length){
CurrentPaletteIndex = d.cpi;
}
GlobalEvents.emit("set_app_palette", Palettes[CurrentPaletteIndex][1]);
}
} else {
throw new TypeError("Object Property Value types invalid.");
var validator = JSONSchema.getValidator(SCHEMA_ID);
if (validator !== null && validator(d)){
this.clear();
for (let i=0; i < d.length; i++){
this.createPalette(d[i].name, JSON.stringify(d[i].palette));
}
} else {
throw new TypeError("Object missing expected properties.");
}*/
throw new Error("Object failed to validate against PalettesStoreSchema.");
}
}

get json(){
@@ -219,22 +201,11 @@ class CTRLPalettesStore{
}

set json(j){
var validator = null;
try {
validator = JSONSchema.getValidator("PalettesStoreSchema.json");
this.obj = JSON.parse(j);
} catch (e) {
throw e;
}

if (validator(j)){
this.clear();
var o = JSON.parse(j);
for (let i=0; i < o.length; i++){
this.createPalette(o[i].name, JSON.stringify(o[i].palette));
}
} else {
throw new Error("JSON Object failed verification.");
}
}

initialize(){
@@ -336,6 +307,7 @@ class CTRLPalettesStore{
for (let i=0; i < Palettes.length; i++){
Palettes[i][2].parentNode.removeChild(Palettes[i][2]);
}
Palettes = [];
CurrentPaletteIndex = 0;
}
}

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

@@ -2,9 +2,10 @@ import {EventCaller} from "/app/js/common/EventCaller.js";
import JSONSchema from "/app/js/common/JSONSchema.js";


const SCHEMA_ID="http://nespaint/NESPaletteSchema.json";
JSONSchema.add({
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "NESPaletteSchema.json",
"$id": SCHEMA_ID,
"type":"array",
"minItems":25,
"maxItems":25,
@@ -42,10 +43,11 @@ export default class NESPalette extends EventCaller{
}

set obj(d){
try {
this.json = JSON.stringify(d);
} catch (e) {
throw e;
var validator = JSONSchema.getValidator(SCHEMA_ID);
if (validator !== null && validator(d)){
this.set_palette(d);
} else {
throw new Error("Object failed to validate against NESPaletteSchema");
}
}

@@ -54,17 +56,11 @@ export default class NESPalette extends EventCaller{
}

set json(j){
try{
var validator = JSONSchema.getValidator("NESPaletteSchema.json");
try {
this.obj = JSON.parse(j);
} catch (e) {
throw e;
}

if (validator(j)){
this.set_palette(JSON.parse(j));
} else {
throw new Error("JSON Object failed to pass validation.");
}
}

/**

Loading…
Cancel
Save