-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.js
96 lines (86 loc) · 2.51 KB
/
calc.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
(function() {
"use strict";
var calcInput, calcNormalize, calcOutput, calcButton,
fmt = {
left: '<span class="bra"><',
centerLeft: '<span class="ket">|',
centerRight: '|</span>',
right: '></span>',
i: '<span class=i>i</span>'
},
hasHistory;
document.addEventListener('DOMContentLoaded', function() {
/*
// one could use github.com/pyramids/needjs to load
// integrity-verified subresources, with the added benefits
// over current (html5) subresource integrity that
// a) the integrity check happens in all browsers and
// b) fallbacks can be provided in case the check fails
need(
init, [
'//cdnjs.cloudflare.com/ajax/libs/mathjs/3.4.1/math.min.js'
, '//res/math.min.js', ''
], '6f8f3abe87aa63ce39fcfacab6d2fae3b54bab07fad615d893e3f348f57e8db7'
);
*/
init();
});
function init() {
calcInput = document.getElementById("calcinput");
calcNormalize = document.getElementById("calcnormalize");
calcOutput = document.getElementById("calcoutput");
calcButton = document.getElementById("calcbutton");
calcInput.addEventListener('change', calcEvent);
calcNormalize.addEventListener('change', calcEvent);
calcButton.addEventListener('click', calcReuse);
hasHistory = !!(window.history && window.history.pushState);
if (hasHistory) {
document.getElementById("history-instruction").classList.remove('gone');
window.onpopstate = historyEvent;
}
}
function historyEvent(event) {
calcInput.value = event.state.input;
calcNormalize.checked = !!event.state.normalize;
calc(event.state);
}
function calcEvent(event, reuse) {
var state = calc({
input: calcinput.value
}, reuse);
if (hasHistory) {
window.history.pushState(state, /* title */ state.output);
}
}
function calcReuse(event) {
calcEvent(event, true);
}
function calc(state, reuse) {
var r;
try {
r = dirac(state.input);
state.normalize = !!calcNormalize.checked;
if (state.normalize && !r.isScalar() && !r.isOperator()) {
try {
r.normalize();
} catch (e) {
// don't output an error if only normalization
// failed (which would happen on operators as that
// is unimplemented)
}
}
if (reuse) {
calcinput.value = '(' + r.toString() + ')';
}
r = r.toString(fmt);
calcOutput.classList.remove("error");
} catch (e) {
r = '';
calcOutput.classList.add("error");
}
//calcOutput.textContent = r;
calcOutput.innerHTML = r;
state.output = r;
return state;
}
})();