2019-07-03 01:42:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
class Architype {
|
|
|
|
|
constructor(container) {
|
|
|
|
|
this.container_ = container;
|
|
|
|
|
|
|
|
|
|
this.container_.classList.add('architype');
|
|
|
|
|
// TODO: make theme selectable
|
|
|
|
|
this.container_.classList.add('dark');
|
|
|
|
|
|
2019-07-10 20:21:46 +00:00
|
|
|
document.addEventListener('keydown',
|
|
|
|
|
(e) => { this.onKeyDown(e); },
|
|
|
|
|
{ capture: true });
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-10 20:21:46 +00:00
|
|
|
this.editorElem_ = document.createElement('ul');
|
|
|
|
|
this.container_.appendChild(this.editorElem_);
|
|
|
|
|
this.editor_ = new Editor(this.editorElem_);
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-10 22:43:29 +00:00
|
|
|
this.gridElem_ = document.createElement('div');
|
|
|
|
|
this.container_.appendChild(this.gridElem_);
|
|
|
|
|
this.grid_ = new Grid(this.gridElem_);
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-03 22:10:36 +00:00
|
|
|
this.generation_ = 0;
|
2019-07-03 22:23:41 +00:00
|
|
|
this.renderGeneration_ = -1;
|
2019-07-03 22:10:36 +00:00
|
|
|
this.drawGeneration_ = -1;
|
|
|
|
|
|
2019-07-03 22:23:41 +00:00
|
|
|
this.render_ = [];
|
|
|
|
|
for (let i = 0; i < navigator.hardwareConcurrency; ++i) {
|
|
|
|
|
let render = new Worker('render.js');
|
|
|
|
|
render.addEventListener('message', (e) => { this.onRender(e); });
|
|
|
|
|
this.render_.push(render);
|
|
|
|
|
}
|
2019-07-03 22:10:36 +00:00
|
|
|
|
2019-07-03 01:42:17 +00:00
|
|
|
this.unserialize(JSON.parse(localStorage.getItem('currentState')));
|
2019-07-10 21:32:52 +00:00
|
|
|
if (this.editor_.getEntries().length == 0) {
|
|
|
|
|
this.editor_.addHelpAfter();
|
|
|
|
|
}
|
2019-07-10 20:21:46 +00:00
|
|
|
this.editor_.selectNext();
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-03 01:51:13 +00:00
|
|
|
this.observer_ = new MutationObserver(e => { this.onChange(e); });
|
2019-07-10 20:21:46 +00:00
|
|
|
this.observer_.observe(this.editorElem_, {
|
2019-07-03 01:42:17 +00:00
|
|
|
attributes: true,
|
|
|
|
|
attributeFilter: ['data-arch-value'],
|
2019-07-03 01:51:13 +00:00
|
|
|
childList: true,
|
2019-07-03 01:42:17 +00:00
|
|
|
subtree: true,
|
|
|
|
|
});
|
|
|
|
|
|
2019-07-03 01:51:13 +00:00
|
|
|
this.onChange();
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serialize() {
|
|
|
|
|
return {
|
|
|
|
|
version: 1,
|
2019-07-03 22:23:41 +00:00
|
|
|
generation: ++this.generation_,
|
2019-07-03 01:42:17 +00:00
|
|
|
editor: this.editor_.serialize(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unserialize(ser) {
|
|
|
|
|
if (!ser) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (ser.version) {
|
|
|
|
|
case 1:
|
2019-07-03 22:10:36 +00:00
|
|
|
this.generation_ = ser.generation;
|
2019-07-03 01:42:17 +00:00
|
|
|
this.editor_.unserialize(ser.editor);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
console.log('unrecognized localStorage.currentState version', ser);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 01:51:13 +00:00
|
|
|
onChange(e) {
|
2019-07-03 22:10:36 +00:00
|
|
|
this.serialized_ = this.serialize();
|
2019-07-03 22:23:41 +00:00
|
|
|
this.startRender();
|
2019-07-03 22:10:36 +00:00
|
|
|
localStorage.setItem('currentState', JSON.stringify(this.serialized_));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 20:21:46 +00:00
|
|
|
onKeyDown(e) {
|
|
|
|
|
let elem = document.activeElement;
|
|
|
|
|
while (elem) {
|
|
|
|
|
if (elem == this.editorElem_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
elem = elem.parentElement;
|
|
|
|
|
}
|
|
|
|
|
this.editor_.onKeyDown(e);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 22:10:36 +00:00
|
|
|
onRender(e) {
|
2019-07-03 22:23:41 +00:00
|
|
|
this.render_.push(e.target);
|
2019-07-03 22:10:36 +00:00
|
|
|
|
|
|
|
|
if (e.data.generation > this.drawGeneration_) {
|
2019-07-03 22:23:41 +00:00
|
|
|
// Received newer than we've drawn; redraw
|
|
|
|
|
this.drawGeneration_ = e.data.generation;
|
2019-07-10 22:43:29 +00:00
|
|
|
this.grid_.draw(e.data.steps);
|
2019-07-03 22:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-03 22:23:41 +00:00
|
|
|
this.startRender();
|
2019-07-03 22:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startRender() {
|
2019-07-03 22:23:41 +00:00
|
|
|
if (this.generation_ == this.renderGeneration_) {
|
|
|
|
|
// Already sent this generation for rendering
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let render = this.render_.pop();
|
|
|
|
|
if (!render) {
|
|
|
|
|
// Ran out of workers
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.renderGeneration_ = this.serialized_.generation;
|
|
|
|
|
render.postMessage(this.serialized_);
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<!--# include file="Editor.js" -->
|
2019-07-10 22:18:00 +00:00
|
|
|
<!--# include file="Grid.js" -->
|
2019-07-03 01:42:17 +00:00
|
|
|
|
|
|
|
|
<!--# include file="utils.js" -->
|
|
|
|
|
|
|
|
|
|
new Architype(document.getElementById('architype'));
|