Constellations/js/Grid/IO.js
2022-07-31 16:35:26 +02:00

49 lines
1,014 B
JavaScript

import * as File from WTK.File;
import * as Async from UnitJS.Async;
import Grid;
import Grid.Color;
import Mode;
return {
init: init
}
function init() {
document.getElementById('load').addEventListener('click', load);
document.getElementById('save').addEventListener('click', save);
}
function load() {
Async.run(
Async.bind(
File.pick({accept: 'text/json,.json'}),
function(input) {
Grid.clear();
Mode.setEnabled(false, ['play', 'solve']);
return File.load(input.files[0]);
},
function(data) {
return Async.wrap(setGridData(JSON.parse(data)));
}
)
);
}
function setGridData(data) {
if(data != undefined) {
Grid.iter(function(row, column) {
if(data[row][column] != undefined) {
Grid.Color.ize(row, column, data[row][column]);
}
});
if(Grid.get().missing.isEmpty()) {
Mode.setEnabled(true, ['play', 'solve']);
} else {
Mode.set('edit');
}
}
}
function save() {
File.save('data:text/json,' + JSON.stringify(Grid.get().data), "grid.json");
}