-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmain.js
139 lines (128 loc) · 4.19 KB
/
main.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
import sagecell from "./sagecell";
import cell from "./cell";
import "./jquery-global";
import { console } from "./console";
(function () {
var ga = document.createElement("script");
ga.type = "text/javascript";
ga.async = true;
ga.src =
("https:" == document.location.protocol
? "https://ssl"
: "http://www") + ".google-analytics.com/ga.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(ga, s);
})();
/**
* Creates a promise and hoists its `resolve` method so that
* it can be called externally.
*/
function makeResolvablePromise() {
const ret = { promise: null, resolve: null, state: "pending" };
ret.promise = new Promise((resolve) => {
ret.resolve = (...args) => {
ret.state = "fulfilled";
return resolve(...args);
};
});
return ret;
}
// Set up the global sagecell variable. This needs to be done right away because other
// scripts want to access window.sagecell.
Object.assign(sagecell, {
templates: {
minimal: {
// for an evaluate button and nothing else.
editor: "textarea-readonly",
hide: ["editor", "files", "permalink"],
},
restricted: {
// to display/evaluate code that can't be edited.
editor: "codemirror-readonly",
hide: ["files", "permalink"],
},
},
allLanguages: [
"sage",
"gap",
"gp",
"html",
"macaulay2",
"maxima",
"octave",
"python",
"r",
"singular",
],
// makeSagecell must be available as soon as the script loads,
// but we may not be ready to process data right away, so we
// provide a wrapper that will poll until sagecell is ready.
makeSagecell: function (args) {
// Clients expect to receive a `cellInfo` object right away.
// However, this object cannot be made available until the page loads.
// If we're not ready, we return a stub object that gets updated with
// the proper data when it becomes available.
if (sagecell._initPromise.state === "pending") {
const ret = {};
sagecell._initPromise.promise
.then(() => {
const cellInfo = window.sagecell._makeSagecell(args);
Object.assign(ret, cellInfo);
})
.catch((e) => {
console.warn("Encountered error in makeSagecell", e);
});
return ret;
} else {
return window.sagecell._makeSagecell(args);
}
},
_initPromise: makeResolvablePromise(),
quietMode: false,
});
// Purely for backwards compatibility
window.singlecell = sagecell;
window.singlecell.makeSinglecell = window.singlecell.makeSagecell;
/**
* Retrieve the kernel index associated with `key`. If
* needed, this function will push `null` onto the kernel
* stack, providing a space for the kernel to be initialized.
*/
function linkKeyToIndex(key) {
sagecell.linkKeys = sagecell.linkKeys || {};
if (key in sagecell.linkKeys) {
return sagecell.linkKeys[key];
}
sagecell.kernels = sagecell.kernels || [];
// Make sure we have a kernel to share for our new key.
const index = sagecell.kernels.push(null) - 1;
sagecell.linkKeys[key] = index;
return index;
}
sagecell._makeSagecell = function (args) {
console.info("sagecell.makeSagecell called");
// If `args.linkKey` is set, we force the `linked` option to be true.
if (args.linkKey) {
args = Object.assign({}, args, { linked: true });
}
var cellInfo = {};
if (args.linked && args.linkKey) {
cell.make(args, cellInfo, linkKeyToIndex(args.linkKey));
} else {
cell.make(args, cellInfo);
}
console.info("sagecell.makeSagecell finished");
return cellInfo;
};
sagecell.deleteSagecell = function (cellInfo) {
cell.delete(cellInfo);
};
sagecell.moveInputForm = function (cellInfo) {
cell.moveInputForm(cellInfo);
};
sagecell.restoreInputForm = function (cellInfo) {
cell.restoreInputForm(cellInfo);
};
sagecell._initPromise.resolve();
export default sagecell;
export { sagecell };