@hydroperx/tiles
Version:
Metro tile layout
101 lines (100 loc) • 2.58 kB
JavaScript
/**
* The state of a `Tiles` component, containing positions and labels.
*/
export class State {
constructor() {
Object.defineProperty(this, "groups", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
Object.defineProperty(this, "tiles", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
}
/**
* Constructs `State` from JSON. The `object` argument
* may be a JSON serialized string or a plain object.
*/
static fromJSON(object) {
object = typeof object === "string" ? JSON.parse(object) : object;
const r = new State();
for (const id in object.groups) {
const o1 = object.groups[id];
r.groups.set(id, {
index: Number(o1.index),
label: String(o1.label),
});
}
for (const id in object.tiles) {
const o1 = object.tiles[id];
r.tiles.set(id, {
size: String(o1.size),
x: Number(o1.x),
y: Number(o1.y),
group: String(o1.group),
});
}
return r;
}
/**
* Returns a plain object (**not** a string).
*/
toJSON() {
const groups = {};
for (const [id, g] of this.groups) {
groups[id] = {
index: g.index,
label: g.label,
};
}
const tiles = {};
for (const [id, t] of this.tiles) {
tiles[id] = {
size: t.size,
x: t.x,
y: t.y,
group: t.group,
};
}
return {
groups,
tiles,
};
}
clear() {
this.groups.clear();
this.tiles.clear();
}
set(state) {
for (const [id, group] of state.groups) {
this.groups.set(id, {
index: group.index,
label: group.label,
});
}
for (const [id, tile] of state.tiles) {
this.tiles.set(id, {
size: tile.size,
x: tile.x,
y: tile.y,
group: tile.group,
});
}
}
clone() {
const r = new State();
r.set(this);
return r;
}
groupExists(id) {
return this.groups.has(id);
}
tileExists(id) {
return this.tiles.has(id);
}
}