-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
259 lines (237 loc) · 6.63 KB
/
script.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
console.log('hi');
var BLANK = /^\s*$/;
var INDENTS = /^\s*/;
var POINTS = /\s\[\s*(-?\d+)\s*\]/;
var MULTIPLIER = /\s[x*]\s*(\d+)/;
var ANNOTATION = /.+\:.+/;
var HEADER = /^\s*(\#+)(.+)/;
var STYLE = /^\s*\!(.+)/;
var state = function() {
var LEVELS = ["detachment", "category", "unit", "option"];
var indents = [];
var total = 0;
var lastLevel = {};
return {
addIndent: function(newIndent) {
if(!indents.includes(newIndent)) {
indents.push(newIndent);
indents.sort(function (a, b) { return a - b; });
}
},
getLevel: function(indent) {
var index = indents.indexOf(indent);
if(index > -1 && index < LEVELS.length) {
return LEVELS[index];
}
return "err";
},
clear: function() {
indents = [];
total = 0;
},
levelDiff: function(prevIndent, newIndent) {
var prevIndex = indents.indexOf(prevIndent);
var newIndex = indents.indexOf(newIndent);
if(prevIndex > -1 && newIndex > -1) {
return prevIndex - newIndex;
}
return -1;
},
addTotal: function(value) {
total += value;
},
getTotal: function() {
return total;
},
getParent: function(indent) {
var index = indents.indexOf(indent);
if (index > 0 && index < LEVELS.length) {
var parentLevel = LEVELS[index - 1];
return lastLevel[parentLevel];
}
return null;
},
lastLine: function(indent, line) {
var index = indents.indexOf(indent);
if(index > -1 && index < LEVELS.length) {
var level = LEVELS[index];
lastLevel[level] = line;
console.log("lastLevel", level, line);
}
}
};
}();
function parseCosts(line) {
var points = line.text.match(POINTS);
if (points && points.length > 0) {
var cost = parseInt(points[1], 10);
if (!isNaN(cost)) {
line.cost = cost;
line.multiplier = 1;
line.total = cost;
var mult = line.text.match(MULTIPLIER);
if(mult && mult.length > 1) {
var multiplier = parseInt(mult[1], 10);
if(!isNaN(multiplier)) {
line.multiplier = multiplier;
line.total = cost * multiplier;
}
}
state.addTotal(line.total);
}
}
}
function parseLine(lines, text) {
if (text.match(BLANK)) {
return;
}
var indents = text.match(INDENTS)[0].length;
state.addIndent(indents);
var line = {text: text, indents: indents};
parseCosts(line);
var headerMatch = text.match(HEADER);
if (headerMatch) {
line.header = headerMatch[1].length;
line.text = headerMatch[2];
}
var styleMatch = text.match(STYLE);
if (styleMatch) {
line.style = true;
line.text = styleMatch[1];
}
lines.push(line);
var parent = state.getParent(indents);
console.log("parent", indents, parent);
if (parent) {
if (!parent.children) {
parent.children = [];
}
parent.children.push(line);
}
state.lastLine(indents, line);
}
function sumTotal(line) {
var sum = line.total || 0;
if (line.children) {
for (var i = 0; i < line.children.length; i++) {
sum += sumTotal(line.children[i]);
}
}
return sum;
}
function renderLine(line, klass) {
var annotation = line.text.match(ANNOTATION);
var spanclass = annotation ? "annotation text" : "text";
var r = "<div class=\"" + klass + "\"><div class=\"inner\"><span class=\"" + spanclass + "\">" +
line.text + "</span>";
r += "<span class=\"total\">" + (line.total ? line.total : '') + "</span>";
r += "<span class=\"sumtotal\">" + (!klass.includes("option") ? sumTotal(line) : '') + "</span>";
r += "</div>";
return r;
}
function renderHeader(line) {
return "<h" + line.header + ">" + line.text + "</h" + line.header + ">";
}
function renderFooter() {
return "<div class=\"footer\">Edward's Simple Army Editor - http://roster.ordoacerbus.com/</div>";
}
function renderLines(lines) {
var prevIndent;
var r = "";
var s = "";
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
if(line.style) {
s += "#result " + line.text + "\n"
continue;
}
if(line.header) {
r += renderHeader(line);
continue;
}
var diff = state.levelDiff(prevIndent, line.indents);
if(diff > -1) {
for(var l = diff + 1; l > 0; l--) {
r+= "</div>";
}
}
var klass = "row " + state.getLevel(line.indents);
if(diff > 0) {
klass += " drop1";
}
r += renderLine(line, klass);
prevIndent = line.indents;
}
var diff = state.levelDiff(prevIndent, 0);
if(diff > -1) {
for(var l = diff + 1; l > 0; l--) {
r+= "</div>";
}
}
r += "<div class=\"row totalrow\"><span>Total: </span><span class=\"total\">" + state.getTotal() + "</span></div>";
return "<style>\n" + s + "</style>\n" + r + renderFooter();
}
function textChange(cm, target) {
state.clear();
var lines = [];
cm.eachLine(function (lh) {
parseLine(lines, lh.text);
});
var outputText = renderLines(lines);
target.innerHTML = outputText;
}
function setSelection(selectId, optionSelection){
const selectElement = document.getElementById(selectId);
const selectOptions = selectElement.options;
for (var option, index = 0; option = selectOptions[index]; index++) {
if (option.value == optionSelection) {
selectElement.selectedIndex = index;
break;
}
}
}
window.onload = function () {
var source = document.getElementById('source');
var loaded = false;
if (typeof(Storage) !== "undefined") {
var stored = localStorage.getItem("roster");
source.value = stored;
loaded = true;
}
var cm = CodeMirror.fromTextArea(source, {
lineNumbers: true
});
var result = document.getElementById('result');
var timer;
function wrapper2() {
textChange(cm, result);
if (typeof(Storage) !== "undefined") {
cm.save();
localStorage.setItem("roster", source.value);
}
}
function wrapper() {
clearTimeout(timer);
timer = setTimeout(wrapper2, 500);
}
cm.on('change', wrapper);
if(loaded) {
wrapper2();
}
const fontSelectElement = document.getElementById('font-selector');
fontSelectElement.addEventListener('change', (event) => {
const fontFamilyValue = event.target.value
if (typeof(Storage) !== "undefined") {
localStorage.setItem("font-selection", fontFamilyValue);
}
document.body.style.fontFamily = fontFamilyValue;
});
// set the font from last time it was selected
if (typeof(Storage) !== "undefined") {
const savedFontFamily = localStorage.getItem("font-selection");
if (savedFontFamily !== null) {
setSelection("font-selector", savedFontFamily);
document.body.style.fontFamily = savedFontFamily;
}
}
}