-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-view.ts
130 lines (106 loc) · 3.37 KB
/
app-view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import 'mithril';
import _ = require('lodash');
import { Builder, parseGrammar } from './builder';
import { CanvasRenderer } from './canvas-renderer';
import { Evaluator, Grammar, EvaluationType, EvaluationStats } from './evaluator';
let m = (window as any).m;
interface MithrilModel {
(newValue?: any) : any;
}
function makeModel(initialValue): MithrilModel {
let value = initialValue;
return function (newValue) {
if (arguments.length) {
value = newValue;
}
return value;
}
}
const midSquareGrammarStr = `
startshape init
rule init {
square [h 100 sat 0.5 b 0.5]
square [h 200 sat 0.7 b 0.7 a 0.5 s 0.5]
}
rule square {
SQUARE [r 45 h 45]
}`;
export class AppView {
grammarModel: MithrilModel;
stats: EvaluationStats
canvas: HTMLCanvasElement;
renderer: CanvasRenderer;
constructor(vnode) {
this.grammarModel = makeModel(midSquareGrammarStr);
}
oninit (vnode) {
console.log("stats-comp-initialized");
}
oncreate (vnode) {
console.log("stats-comp-DOM created");
this.canvas = document.getElementById('CFDG') as HTMLCanvasElement;
this.renderer = new CanvasRenderer(this.canvas);
}
onupdate (vnode) {
console.log("stats-comp-DOM updated");
}
onbeforeremove (vnode) {
console.log("stats-comp-onbeforemove");
}
onremove (vnode) {
console.log("stats-comp-removing DOM element");
}
onbeforeupdate (vnode, old) {
return true
}
view (vnode) {
return m('div', {}, [
this._grammarView(),
this._report()
]);
}
_grammarView() {
return m('div', {}, [
m('textarea', { rows: "25", style: { width: '350px' },
value: this.grammarModel(),
onchange: m.withAttr('value', this.grammarModel)
}),
m('div', { className: 'button-row'}, [
m('button', {onclick: () => this._evaluateGrammar()}, 'Evaluate')
])
]);
}
_report() {
if (!this.stats) {
return m('div');
}
let rulesEntries = Array.from(this.stats.rules.entries());
let rules: any[] = [m('div', {}, 'Rules')].concat(rulesEntries.map(entry => {
return this._statLine(entry[0], entry[1].count);
}));
let shapes: any[] = _.map(this.stats.shapeStats(), (count, shapeName) => {
return this._statLine(shapeName, count);
});
return m('div', { className: 'stat-report' }, [
this._statLine('Time', this.stats.evaluationTime),
this._statLine('Nb Shapes', this.stats.shapes.length),
m('div', {}, shapes),
this._statLine('Nb Rules', this.stats.totalRules()),
m('div', {}, rules)
]);
}
_statLine (label: string, statVdom) {
return m('div', {className: 'stat-line'}, [
m('span', {className: 'stat-label'}, label),
m('span', { className: 'stat-value' }, statVdom)
]);
}
_evaluateGrammar () {
let grammarStr = this.grammarModel();
let grammar = new Grammar(parseGrammar(grammarStr));
let evaluator = new Evaluator(grammar);
evaluator.evaluate(EvaluationType.RulesSync);
this.renderer.render(evaluator.shapes);
this.stats = evaluator.stats;
}
}