Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagemath.github.io
Path: blob/master/eval/eval.js
2457 views
1
/*
2
* demo for an interactive database of Sage example
3
* copyright: Harald Schilly <[email protected]>
4
* license: apache 2.0
5
*/
6
7
/* this neat helper comes from stackoverflow and various blogs. it
8
* extracts a certain parameter from a query in the url (GET request) */
9
function getParameterByName(name) {
10
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
11
return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : null;
12
}
13
14
var mysagecell = {};
15
16
/* helper, used twice
17
* TODO is there a way to dynamically replace the content of the codemirror code ? */
18
function initSageCell(doClick) {
19
sagecell.init(function() {
20
mysagecell = singlecell.makeSagecell({
21
inputLocation: "#sageInput",
22
/* outputLocation: '#sageOutput', */
23
replaceOutput: true,
24
hide: ["messages", "computationID", "files", "sageMode", "editorToggle", "sessionTitle", "done"],
25
evalButtonText: "Evaluate"
26
});
27
});
28
}
29
30
/* initialize interactive code */
31
$(function() {
32
33
var c = getParameterByName("code");
34
if (c) { $("#sageInput").html(unescape(c)); }
35
36
initSageCell(c);
37
});
38
39
/* contains the 2-level database of examples */
40
var DB = {}
41
var DB_subdir = "eval";
42
var DB_files = Array("db1.json", "db2.json", "eberhart.json", "novoseltsev.json");
43
var $descr = "";
44
45
function updateDescr(html) {
46
var c = "";
47
if (CAT1) {
48
c += "<span style='color: grey;'>" + CAT1;
49
if (CAT2) { c+= ">" + CAT2 + "</span><br/>";
50
if (html) { c+= html; }
51
} else { c+= "</span><br/>"; }
52
}
53
$descr.html(c);
54
}
55
56
/* initializes the interactive database */
57
$(function() {
58
var cnt = 0;
59
var nbex = 0;
60
$descr = jQuery("#db-descr");
61
var $nbex = jQuery("#nb-examples");
62
for (var i in DB_files) {
63
jQuery.getJSON(DB_subdir +"/"+ DB_files[i], function(data) {
64
jQuery.each(data, function(key, item) {
65
var cat = item["cat"];
66
if (!(cat[0] in DB)) { DB[cat[0]] = {}; }
67
var DB1 = DB[cat[0]];
68
if (!(cat[1] in DB1)) { DB1[cat[1]] = {}; }
69
DB1[cat[1]][key] = item;;
70
nbex++;
71
});
72
})
73
.error(function() { alert("json parser error, most likely …");})
74
/* and populate UI when we have all files */
75
.success(function() {
76
$nbex.text(nbex);
77
cnt++;
78
if (cnt == DB_files.length) {
79
$descr.empty();
80
populateLists(jQuery);
81
} else {
82
$descr.html("progress: " + ((cnt/DB_files.length)*100.0) + "%");
83
}
84
});
85
}
86
});
87
88
/* selection state vars */
89
var CAT1 = "";
90
var CAT2 = "";
91
var EX = "";
92
93
/* takes a map and returns an array, sorted by map's keys */
94
function mapsort(m) {
95
var ret = [];
96
for (var k in m) { ret.push([k, m[k]]); }
97
ret.sort(function(a,b) { return a[0] < b[0] ? -1 : ( a[0] > b[0] ? 1 : 0); });
98
return ret;
99
}
100
101
/* store the state in the URI, exid is the id of the example - or nothing */
102
function storeState() {
103
// TODO re-enable this once #restoreState() works
104
return
105
var s = "";
106
if (CAT1) {
107
s += encodeURI(CAT1);
108
if (CAT2) {
109
s += "/" + encodeURI(CAT2);
110
if (EX) { s += "/" + encodeURI(EX); }
111
}
112
}
113
window.location.hash = s;
114
}
115
116
/* get the state+example after the # in the URI, "/" delmimiter */
117
function restoreState() {
118
var state = window.location.hash.substring(1).split("/")
119
if (state.length >= 0) CAT1 = decodeURI(state[0]);
120
if (state.length >= 1) CAT2 = decodeURI(state[1]);
121
if (state.length >= 2) {
122
EX = decodeURI(state[2]);
123
showExample();
124
}
125
/* TODO menu isn't updated, but that's a bit harder */
126
}
127
128
/* this function actually adds the content to the DB and makes it interactive.
129
* it is only called once! jQuery's "on" is used to dispatch all the clicks in
130
* only a few handlers. */
131
function populateLists($) {
132
var $db1 = $("#db1").empty();
133
var $db2 = $("#db2").empty();
134
var $exs = $("#exs").empty();
135
136
$.each(mapsort(DB), function(idx, item) {
137
var key1 = item[0];
138
var $li = $("<div>" + key1 + "</div>");
139
$li.prop("key", key1);
140
$db1.append($li);
141
});
142
143
/* topic column logic */
144
$("#db1>div").click(function() {
145
$("#db1>div").removeClass("selected");
146
$(this).addClass("selected");
147
$db2.empty();
148
$exs.empty();
149
var key1 = $(this).prop("key");
150
CAT1 = key1;
151
CAT2 = "";
152
EX = "";
153
//storeState();
154
$.each(mapsort(DB[key1]), function(idx, item) {
155
var key2 = item[0];
156
var $li2 = $("<div>" + key2 + "</div>");
157
$li2.prop("key", key2);
158
$db2.append($li2);
159
});
160
//updateDescr();
161
});
162
163
/* subtopic column logic, when clicking on a subtopic, it
164
* clears the list of examples and adds the matching ones */
165
$("#db").on("click", "#db2>div", function(event) {
166
$this = $(this);
167
$exs.empty();
168
$("#db2>div").removeClass("selected");
169
$this.addClass("selected");
170
CAT2 = $this.prop("key");
171
EX = "";
172
//storeState();
173
$.each(mapsort(DB[CAT1][CAT2]), function(idx, entry) {
174
var key = entry[0];
175
var item = entry[1];
176
var $ex = $("<div>"+key+"</div>");
177
$ex.prop("key", key);
178
$exs.append($ex);
179
});
180
//updateDescr();
181
});
182
183
/* examples clumn, on click load the code */
184
$("#db").on("click", "#exs>div", function(event) {
185
$this = $(this);
186
$("#exs>div").removeClass("selected");
187
$this.addClass("selected");
188
EX = $this.prop("key");
189
storeState();
190
showExample();
191
});
192
193
/* this could also be done via the :hover CSS selector, but maybe we want to add some
194
* additional UI aides or updates later */
195
$("#db").on("mouseover", "#db div>div", function() { $(this).addClass("hover"); });
196
$("#db").on("mouseout", "#db div>div", function() { $(this).removeClass("hover"); });
197
198
restoreState();
199
}
200
201
function showExample() {
202
var item = DB[CAT1][CAT2][EX];
203
var code = item["code"].join("\n"); // the code is an array of strings
204
updateDescr("<strong>" + EX + "</strong>: " + item["descr"]);
205
jQuery('.CodeMirror').get(0).CodeMirror.setValue(code);
206
//$("input.sagecell_evalButton").click();
207
mysagecell.submit();
208
}
209
210