-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathutils.js
271 lines (261 loc) · 8.89 KB
/
utils.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
259
260
261
262
263
264
265
266
267
268
269
270
271
import $ from "jquery";
import utils from "base/js/utils";
import { URLs } from "./urls";
import { console } from "./console";
/* IPython url_join_encode and url_path_join is used in the cell server with URLs with hostnames, so we make it handle those correctly
this is a temporary kludge. A much better fix would be to introduce a kernel_base_url parameter in the kernel
initialization, which would default to the empty string, and would be prepended to every kernel request. Also, the
ws_host attribute would derive from the kernel_base_url parameter.
Right now, the IPython websocket connection urls are messed up (they prepend a phony ws_host), but that's okay because the regexp
pulls out the kernel id and everything is fine.
We make sure not to apply our handling multiple time (possible when
the embedding script is included many times).
*/
var url_parts = new RegExp("^((([^:/?#]+):)?(//([^/?#]*))?)?(.*)");
function strip_hostname(f) {
if (f._strip_hostname_applied) {
return f;
}
function wrapped() {
// override IPython function to account for leading protocol and hostname
// assume that the first argument has the part to strip off, if any
var hostname = "";
if (arguments.length > 0) {
var parts = arguments[0].match(url_parts);
hostname = parts[1]; // everything up to the url path
arguments[0] = parts[6]; // url path
}
return hostname + f.apply(null, arguments);
}
wrapped._strip_hostname_applied = true;
return wrapped;
}
utils.url_join_encode = strip_hostname(utils.url_join_encode);
utils.url_path_join = strip_hostname(utils.url_path_join);
var ID;
function cellSessionID() {
return (ID = ID || utils.uuid());
}
function sendRequest(method, url, data, callback, files) {
var isXDomain =
URLs.root !==
window.location.protocol + "//" + window.location.host + "/";
method = method.toUpperCase();
var hasFiles = false;
/* files code
if (files === undefined) {
files = [];
}
for (var i = 0; i < files.length; i++) {
if (files[i]) {
hasFiles = true;
break;
}
}
*/
var xhr = new XMLHttpRequest();
var fd = undefined;
if (method === "GET") {
data.rand = Math.random().toString();
}
if (method === "POST" && localStorage.accepted_tos) {
data.accepted_tos = "true";
}
// Format parameters to send as a string or a FormData object
if (window.FormData && method !== "GET") {
fd = new FormData();
for (var k in data) {
if (data.hasOwnProperty(k)) {
fd.append(k, data[k]);
}
}
/* files code
for (var i = 0; i < files.length; i++) {
if (files[i]) {
fd.append("file", files[i]);
}
}
*/
} else {
fd = "";
for (var k in data) {
if (data.hasOwnProperty(k)) {
fd +=
"&" +
encodeURIComponent(k) +
"=" +
encodeURIComponent(data[k]);
}
}
fd = fd.substr(1);
if (fd.length > 0 && method === "GET") {
url += "?" + fd;
fd = undefined;
}
}
if (window.FormData || !(isXDomain /*|| hasFiles*/)) {
// If an XMLHttpRequest is possible, use it
xhr.open(method, url, true);
xhr.withCredentials = true;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 /* DONE */ && callback) {
callback(xhr.responseText);
}
};
if (typeof fd === "string") {
xhr.setRequestHeader(
"Content-type",
"application/x-www-form-urlencoded"
);
}
xhr.send(fd);
} else if (method === "GET") {
// Use JSONP to send cross-domain GET requests
url += (url.indexOf("?") === -1 ? "?" : "&") + "callback=?";
$.getJSON(url, callback);
} else {
// Use a form submission to send POST requests
// Methods such as DELETE and OPTIONS will be sent as POST instead
var iframe = document.createElement("iframe");
iframe.name = utils.uuid();
var form = document.createElement("form", {
method: "POST",
action: url,
target: iframe.name,
});
if (data === undefined) {
data = {};
}
data.method = method;
for (var k in data) {
if (data.hasOwnProperty(k)) {
form.appendChild(
document.createElement("input", { name: k, value: data[k] })
);
}
}
form.appendChild(
document.createElement("input", { name: "frame", value: "on" })
);
/* file code
if (hasFiles) {
form.setAttribute("enctype", "multipart/form-data");
for (var i = 0; i < files.length; i++) {
if (files[i]) {
form.appendChild(files[i]);
}
}
}
*/
form.style.display = iframe.style.display = "none";
document.body.appendChild(iframe);
document.body.appendChild(form);
var listen = function (evt) {
if (
evt.source === iframe.contentWindow &&
evt.origin + "/" === URLs.root
) {
if (window.removeEventListener) {
removeEventListener("message", listen);
} else {
detachEvent("onmessage", listen);
}
callback(evt.data);
document.body.removeChild(iframe);
}
};
if (window.addEventListener) {
window.addEventListener("message", listen);
} else {
window.attachEvent("onmessage", listen);
}
form.submit();
document.body.removeChild(form);
}
}
export default {
always_new: utils.always_new,
cellSessionID: cellSessionID,
createElement: function (type, attrs, children) {
var node = document.createElement(type);
for (var k in attrs) {
if (attrs.hasOwnProperty(k)) {
node.setAttribute(k, attrs[k]);
}
}
if (children) {
for (var i = 0; i < children.length; i++) {
if (typeof children[i] == "string") {
node.appendChild(document.createTextNode(children[i]));
} else {
node.appendChild(children[i]);
}
}
}
return node;
},
fixConsole: utils.fixConsole,
/* var p = proxy(['list', 'of', 'methods'])
will save any method calls in the list. At some later time, you can invoke
each method on an object by doing p._run_callbacks(my_obj) */
proxy: function (methods) {
var proxy = { _callbacks: [] };
$.each(methods, function (i, method) {
proxy[method] = function () {
proxy._callbacks.push([method, arguments]);
console.log("stored proxy for " + method);
};
});
proxy._run_callbacks = function (obj) {
$.each(proxy._callbacks, function (i, cb) {
obj[cb[0]].apply(obj, cb[1]);
});
};
return proxy;
},
sendRequest: sendRequest,
simpleTimer: function () {
var t = new Date().getTime();
console.debug("starting timer from " + t);
return function (reset) {
var old_t = t;
var new_t = new Date().getTime();
if (reset) {
t = new_t;
}
return new_t - old_t + " ms";
};
},
// throttle is from:
// Underscore.js 1.4.3
// http://underscorejs.org
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Underscore may be freely distributed under the MIT license.
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time.
throttle: function (func, wait) {
var context, args, timeout, result;
var previous = 0;
var later = function () {
previous = new Date();
timeout = null;
result = func.apply(context, args);
};
return function () {
var now = new Date();
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
},
uuid: utils.uuid,
};