Constellations/js/Grid/IO.js

50 lines
1 KiB
JavaScript

import * as File from WTK.File;
import * as Async from UnitJS.Async;
import Grid;
import Grid.Color;
import iter from Grid.Util;
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) {
iter(data, 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");
}