| var SCHEMA_LIST = []; | var SCHEMA_LIST = []; | ||||
| var DIRTY = false; | |||||
| var CUR_AJV = null; | |||||
| var CUR_AJV = new Ajv(); | |||||
| export default Object.freeze({ | export default Object.freeze({ | ||||
| add:function(s){ | add:function(s){ | ||||
| throw new Error("Schema already exists with $id '" + s["$id"] + "'."); | throw new Error("Schema already exists with $id '" + s["$id"] + "'."); | ||||
| } | } | ||||
| SCHEMA_LIST.push(s); | SCHEMA_LIST.push(s); | ||||
| DIRTY = true; | |||||
| CUR_AJV.addSchema(s); | |||||
| }, | }, | ||||
| remove:function(id){ | remove:function(id){ | ||||
| }); | }); | ||||
| if (idx >= 0){ | if (idx >= 0){ | ||||
| SCHEMA_LIST.splice(idx, 1); | SCHEMA_LIST.splice(idx, 1); | ||||
| DIRTY = true; | |||||
| CUR_AJV.removeSchema(id); | |||||
| } | } | ||||
| }, | }, | ||||
| }, | }, | ||||
| getValidator:function(id){ | 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; | 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; | |||||
| } | } | ||||
| }); | }); | ||||
| var CurrentBank = ""; | var CurrentBank = ""; | ||||
| const SCHEMA_ID="http://nespaint/BanksStoreSchema.json"; | |||||
| JSONSchema.add({ | JSONSchema.add({ | ||||
| "$schema": "http://json-schema.org/draft-07/schema#", | "$schema": "http://json-schema.org/draft-07/schema#", | ||||
| "$id": "BanksStoreSchema.json", | |||||
| "$id": SCHEMA_ID, | |||||
| "type": "array", | "type": "array", | ||||
| "items":{ | "items":{ | ||||
| "type": "object", | "type": "object", | ||||
| } | } | ||||
| set obj(d){ | 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(){ | get json(){ | ||||
| } | } | ||||
| set json(j){ | set json(j){ | ||||
| var validator = null; | |||||
| try { | try { | ||||
| validator = JSONSchema.getValidator("BanksStoreSchema.json"); | |||||
| this.obj = JSON.parse(j); | |||||
| } catch (e) { | } catch (e) { | ||||
| throw 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(){ | initialize(){ | ||||
| clear(){ | clear(){ | ||||
| Object.keys(Banks).forEach((item) => { | Object.keys(Banks).forEach((item) => { | ||||
| item.el.parentNode.removeChild(item.el); | |||||
| Banks[item].el.parentNode.removeChild(Banks[item].el); | |||||
| }); | }); | ||||
| Banks = {}; | Banks = {}; | ||||
| if (CurrentBank !== "") | |||||
| if (CurrentBank !== ""){ | |||||
| CurrentBank = ""; | |||||
| GlobalEvents.emit("change_surface", null); | GlobalEvents.emit("change_surface", null); | ||||
| CurrentBank = ""; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| "0.1" | "0.1" | ||||
| ]; | ]; | ||||
| const SCHEMA_ID = "http://nespaint/Project.json"; | |||||
| JSONSchema.add({ | JSONSchema.add({ | ||||
| "$schema": "http://json-schema.org/draft-07/schema#", | "$schema": "http://json-schema.org/draft-07/schema#", | ||||
| "$id": "NESPainterProject.json", | |||||
| "$id": SCHEMA_ID, | |||||
| "type":"object", | "type":"object", | ||||
| "properties":{ | "properties":{ | ||||
| "id":{ | "id":{ | ||||
| "type":"string", | "type":"string", | ||||
| "pattern":"^[0-9]{1,}\.[0-9]{1,}$" | "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"] | "required":["id","version","paletteStore","bankStore"] | ||||
| }); | }); | ||||
| if (this.files && this.files.length > 0){ | if (this.files && this.files.length > 0){ | ||||
| var reader = new FileReader(); | var reader = new FileReader(); | ||||
| reader.onload = (function(e) { | 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) { | } 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. | // TODO: Validate 'id' and 'version' properties. | ||||
| CTRLPalettesStore.obj = o.paletteStore; | CTRLPalettesStore.obj = o.paletteStore; | ||||
| CTRLBanksStore.obj = o.banksStore; | |||||
| CTRLBanksStore.obj = o.bankStore; | |||||
| } | } | ||||
| if (this.parentNode.nodeName.toLowerCase() === "form"){ | if (this.parentNode.nodeName.toLowerCase() === "form"){ | ||||
| this.parentNode.reset(); | this.parentNode.reset(); |
| var BlockEmits = false; | var BlockEmits = false; | ||||
| const SCHEMA_ID="http://nespaint/PalettesStoreSchema.json"; | |||||
| JSONSchema.add({ | JSONSchema.add({ | ||||
| "$schema": "http://json-schema.org/draft-07/schema#", | "$schema": "http://json-schema.org/draft-07/schema#", | ||||
| "$id": "PalettesStoreSchema.json", | |||||
| "$id": SCHEMA_ID, | |||||
| "type": "array", | "type": "array", | ||||
| "items": { | "items": { | ||||
| "type": "object", | "type": "object", | ||||
| "minLength":1 | "minLength":1 | ||||
| }, | }, | ||||
| "palette":{ | "palette":{ | ||||
| "$ref":"NESPaletteSchema.json" | |||||
| "$ref":"http://nespaint/NESPaletteSchema.json" | |||||
| } | } | ||||
| }, | }, | ||||
| "required":["name","palette"] | "required":["name","palette"] | ||||
| set obj(d){ | 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 { | } else { | ||||
| throw new TypeError("Object missing expected properties."); | |||||
| }*/ | |||||
| throw new Error("Object failed to validate against PalettesStoreSchema."); | |||||
| } | |||||
| } | } | ||||
| get json(){ | get json(){ | ||||
| } | } | ||||
| set json(j){ | set json(j){ | ||||
| var validator = null; | |||||
| try { | try { | ||||
| validator = JSONSchema.getValidator("PalettesStoreSchema.json"); | |||||
| this.obj = JSON.parse(j); | |||||
| } catch (e) { | } catch (e) { | ||||
| throw 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(){ | initialize(){ | ||||
| for (let i=0; i < Palettes.length; i++){ | for (let i=0; i < Palettes.length; i++){ | ||||
| Palettes[i][2].parentNode.removeChild(Palettes[i][2]); | Palettes[i][2].parentNode.removeChild(Palettes[i][2]); | ||||
| } | } | ||||
| Palettes = []; | |||||
| CurrentPaletteIndex = 0; | CurrentPaletteIndex = 0; | ||||
| } | } | ||||
| } | } |
| import JSONSchema from "/app/js/common/JSONSchema.js"; | import JSONSchema from "/app/js/common/JSONSchema.js"; | ||||
| const SCHEMA_ID="http://nespaint/NESPaletteSchema.json"; | |||||
| JSONSchema.add({ | JSONSchema.add({ | ||||
| "$schema": "http://json-schema.org/draft-07/schema#", | "$schema": "http://json-schema.org/draft-07/schema#", | ||||
| "$id": "NESPaletteSchema.json", | |||||
| "$id": SCHEMA_ID, | |||||
| "type":"array", | "type":"array", | ||||
| "minItems":25, | "minItems":25, | ||||
| "maxItems":25, | "maxItems":25, | ||||
| } | } | ||||
| set obj(d){ | 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"); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| set json(j){ | set json(j){ | ||||
| try{ | |||||
| var validator = JSONSchema.getValidator("NESPaletteSchema.json"); | |||||
| try { | |||||
| this.obj = JSON.parse(j); | |||||
| } catch (e) { | } catch (e) { | ||||
| throw e; | throw e; | ||||
| } | } | ||||
| if (validator(j)){ | |||||
| this.set_palette(JSON.parse(j)); | |||||
| } else { | |||||
| throw new Error("JSON Object failed to pass validation."); | |||||
| } | |||||
| } | } | ||||
| /** | /** |