Show More
@@ -1,688 +1,690 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'codemirror/lib/codemirror', |
|
7 | 'codemirror/lib/codemirror', | |
8 | ], function(IPython, $, CodeMirror){ |
|
8 | ], function(IPython, $, CodeMirror){ | |
9 | "use strict"; |
|
9 | "use strict"; | |
10 |
|
10 | |||
11 | IPython.load_extensions = function () { |
|
11 | IPython.load_extensions = function () { | |
12 | // load one or more IPython notebook extensions with requirejs |
|
12 | // load one or more IPython notebook extensions with requirejs | |
13 |
|
13 | |||
14 | var extensions = []; |
|
14 | var extensions = []; | |
15 | var extension_names = arguments; |
|
15 | var extension_names = arguments; | |
16 | for (var i = 0; i < extension_names.length; i++) { |
|
16 | for (var i = 0; i < extension_names.length; i++) { | |
17 | extensions.push("nbextensions/" + arguments[i]); |
|
17 | extensions.push("nbextensions/" + arguments[i]); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | require(extensions, |
|
20 | require(extensions, | |
21 | function () { |
|
21 | function () { | |
22 | for (var i = 0; i < arguments.length; i++) { |
|
22 | for (var i = 0; i < arguments.length; i++) { | |
23 | var ext = arguments[i]; |
|
23 | var ext = arguments[i]; | |
24 | var ext_name = extension_names[i]; |
|
24 | var ext_name = extension_names[i]; | |
25 | // success callback |
|
25 | // success callback | |
26 | console.log("Loaded extension: " + ext_name); |
|
26 | console.log("Loaded extension: " + ext_name); | |
27 | if (ext && ext.load_ipython_extension !== undefined) { |
|
27 | if (ext && ext.load_ipython_extension !== undefined) { | |
28 | ext.load_ipython_extension(); |
|
28 | ext.load_ipython_extension(); | |
29 | } |
|
29 | } | |
30 | } |
|
30 | } | |
31 | }, |
|
31 | }, | |
32 | function (err) { |
|
32 | function (err) { | |
33 | // failure callback |
|
33 | // failure callback | |
34 | console.log("Failed to load extension(s):", err.requireModules, err); |
|
34 | console.log("Failed to load extension(s):", err.requireModules, err); | |
35 | } |
|
35 | } | |
36 | ); |
|
36 | ); | |
37 | }; |
|
37 | }; | |
38 |
|
38 | |||
39 | //============================================================================ |
|
39 | //============================================================================ | |
40 | // Cross-browser RegEx Split |
|
40 | // Cross-browser RegEx Split | |
41 | //============================================================================ |
|
41 | //============================================================================ | |
42 |
|
42 | |||
43 | // This code has been MODIFIED from the code licensed below to not replace the |
|
43 | // This code has been MODIFIED from the code licensed below to not replace the | |
44 | // default browser split. The license is reproduced here. |
|
44 | // default browser split. The license is reproduced here. | |
45 |
|
45 | |||
46 | // see http://blog.stevenlevithan.com/archives/cross-browser-split for more info: |
|
46 | // see http://blog.stevenlevithan.com/archives/cross-browser-split for more info: | |
47 | /*! |
|
47 | /*! | |
48 | * Cross-Browser Split 1.1.1 |
|
48 | * Cross-Browser Split 1.1.1 | |
49 | * Copyright 2007-2012 Steven Levithan <stevenlevithan.com> |
|
49 | * Copyright 2007-2012 Steven Levithan <stevenlevithan.com> | |
50 | * Available under the MIT License |
|
50 | * Available under the MIT License | |
51 | * ECMAScript compliant, uniform cross-browser split method |
|
51 | * ECMAScript compliant, uniform cross-browser split method | |
52 | */ |
|
52 | */ | |
53 |
|
53 | |||
54 | /** |
|
54 | /** | |
55 | * Splits a string into an array of strings using a regex or string |
|
55 | * Splits a string into an array of strings using a regex or string | |
56 | * separator. Matches of the separator are not included in the result array. |
|
56 | * separator. Matches of the separator are not included in the result array. | |
57 | * However, if `separator` is a regex that contains capturing groups, |
|
57 | * However, if `separator` is a regex that contains capturing groups, | |
58 | * backreferences are spliced into the result each time `separator` is |
|
58 | * backreferences are spliced into the result each time `separator` is | |
59 | * matched. Fixes browser bugs compared to the native |
|
59 | * matched. Fixes browser bugs compared to the native | |
60 | * `String.prototype.split` and can be used reliably cross-browser. |
|
60 | * `String.prototype.split` and can be used reliably cross-browser. | |
61 | * @param {String} str String to split. |
|
61 | * @param {String} str String to split. | |
62 | * @param {RegExp|String} separator Regex or string to use for separating |
|
62 | * @param {RegExp|String} separator Regex or string to use for separating | |
63 | * the string. |
|
63 | * the string. | |
64 | * @param {Number} [limit] Maximum number of items to include in the result |
|
64 | * @param {Number} [limit] Maximum number of items to include in the result | |
65 | * array. |
|
65 | * array. | |
66 | * @returns {Array} Array of substrings. |
|
66 | * @returns {Array} Array of substrings. | |
67 | * @example |
|
67 | * @example | |
68 | * |
|
68 | * | |
69 | * // Basic use |
|
69 | * // Basic use | |
70 | * regex_split('a b c d', ' '); |
|
70 | * regex_split('a b c d', ' '); | |
71 | * // -> ['a', 'b', 'c', 'd'] |
|
71 | * // -> ['a', 'b', 'c', 'd'] | |
72 | * |
|
72 | * | |
73 | * // With limit |
|
73 | * // With limit | |
74 | * regex_split('a b c d', ' ', 2); |
|
74 | * regex_split('a b c d', ' ', 2); | |
75 | * // -> ['a', 'b'] |
|
75 | * // -> ['a', 'b'] | |
76 | * |
|
76 | * | |
77 | * // Backreferences in result array |
|
77 | * // Backreferences in result array | |
78 | * regex_split('..word1 word2..', /([a-z]+)(\d+)/i); |
|
78 | * regex_split('..word1 word2..', /([a-z]+)(\d+)/i); | |
79 | * // -> ['..', 'word', '1', ' ', 'word', '2', '..'] |
|
79 | * // -> ['..', 'word', '1', ' ', 'word', '2', '..'] | |
80 | */ |
|
80 | */ | |
81 | var regex_split = function (str, separator, limit) { |
|
81 | var regex_split = function (str, separator, limit) { | |
82 | // If `separator` is not a regex, use `split` |
|
82 | // If `separator` is not a regex, use `split` | |
83 | if (Object.prototype.toString.call(separator) !== "[object RegExp]") { |
|
83 | if (Object.prototype.toString.call(separator) !== "[object RegExp]") { | |
84 | return split.call(str, separator, limit); |
|
84 | return split.call(str, separator, limit); | |
85 | } |
|
85 | } | |
86 | var output = [], |
|
86 | var output = [], | |
87 | flags = (separator.ignoreCase ? "i" : "") + |
|
87 | flags = (separator.ignoreCase ? "i" : "") + | |
88 | (separator.multiline ? "m" : "") + |
|
88 | (separator.multiline ? "m" : "") + | |
89 | (separator.extended ? "x" : "") + // Proposed for ES6 |
|
89 | (separator.extended ? "x" : "") + // Proposed for ES6 | |
90 | (separator.sticky ? "y" : ""), // Firefox 3+ |
|
90 | (separator.sticky ? "y" : ""), // Firefox 3+ | |
91 | lastLastIndex = 0, |
|
91 | lastLastIndex = 0, | |
92 | // Make `global` and avoid `lastIndex` issues by working with a copy |
|
92 | // Make `global` and avoid `lastIndex` issues by working with a copy | |
93 | separator = new RegExp(separator.source, flags + "g"), |
|
93 | separator = new RegExp(separator.source, flags + "g"), | |
94 | separator2, match, lastIndex, lastLength; |
|
94 | separator2, match, lastIndex, lastLength; | |
95 | str += ""; // Type-convert |
|
95 | str += ""; // Type-convert | |
96 |
|
96 | |||
97 | var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"; |
|
97 | var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined"; | |
98 | if (!compliantExecNpcg) { |
|
98 | if (!compliantExecNpcg) { | |
99 | // Doesn't need flags gy, but they don't hurt |
|
99 | // Doesn't need flags gy, but they don't hurt | |
100 | separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); |
|
100 | separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); | |
101 | } |
|
101 | } | |
102 | /* Values for `limit`, per the spec: |
|
102 | /* Values for `limit`, per the spec: | |
103 | * If undefined: 4294967295 // Math.pow(2, 32) - 1 |
|
103 | * If undefined: 4294967295 // Math.pow(2, 32) - 1 | |
104 | * If 0, Infinity, or NaN: 0 |
|
104 | * If 0, Infinity, or NaN: 0 | |
105 | * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; |
|
105 | * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; | |
106 | * If negative number: 4294967296 - Math.floor(Math.abs(limit)) |
|
106 | * If negative number: 4294967296 - Math.floor(Math.abs(limit)) | |
107 | * If other: Type-convert, then use the above rules |
|
107 | * If other: Type-convert, then use the above rules | |
108 | */ |
|
108 | */ | |
109 | limit = typeof(limit) === "undefined" ? |
|
109 | limit = typeof(limit) === "undefined" ? | |
110 | -1 >>> 0 : // Math.pow(2, 32) - 1 |
|
110 | -1 >>> 0 : // Math.pow(2, 32) - 1 | |
111 | limit >>> 0; // ToUint32(limit) |
|
111 | limit >>> 0; // ToUint32(limit) | |
112 | while (match = separator.exec(str)) { |
|
112 | while (match = separator.exec(str)) { | |
113 | // `separator.lastIndex` is not reliable cross-browser |
|
113 | // `separator.lastIndex` is not reliable cross-browser | |
114 | lastIndex = match.index + match[0].length; |
|
114 | lastIndex = match.index + match[0].length; | |
115 | if (lastIndex > lastLastIndex) { |
|
115 | if (lastIndex > lastLastIndex) { | |
116 | output.push(str.slice(lastLastIndex, match.index)); |
|
116 | output.push(str.slice(lastLastIndex, match.index)); | |
117 | // Fix browsers whose `exec` methods don't consistently return `undefined` for |
|
117 | // Fix browsers whose `exec` methods don't consistently return `undefined` for | |
118 | // nonparticipating capturing groups |
|
118 | // nonparticipating capturing groups | |
119 | if (!compliantExecNpcg && match.length > 1) { |
|
119 | if (!compliantExecNpcg && match.length > 1) { | |
120 | match[0].replace(separator2, function () { |
|
120 | match[0].replace(separator2, function () { | |
121 | for (var i = 1; i < arguments.length - 2; i++) { |
|
121 | for (var i = 1; i < arguments.length - 2; i++) { | |
122 | if (typeof(arguments[i]) === "undefined") { |
|
122 | if (typeof(arguments[i]) === "undefined") { | |
123 | match[i] = undefined; |
|
123 | match[i] = undefined; | |
124 | } |
|
124 | } | |
125 | } |
|
125 | } | |
126 | }); |
|
126 | }); | |
127 | } |
|
127 | } | |
128 | if (match.length > 1 && match.index < str.length) { |
|
128 | if (match.length > 1 && match.index < str.length) { | |
129 | Array.prototype.push.apply(output, match.slice(1)); |
|
129 | Array.prototype.push.apply(output, match.slice(1)); | |
130 | } |
|
130 | } | |
131 | lastLength = match[0].length; |
|
131 | lastLength = match[0].length; | |
132 | lastLastIndex = lastIndex; |
|
132 | lastLastIndex = lastIndex; | |
133 | if (output.length >= limit) { |
|
133 | if (output.length >= limit) { | |
134 | break; |
|
134 | break; | |
135 | } |
|
135 | } | |
136 | } |
|
136 | } | |
137 | if (separator.lastIndex === match.index) { |
|
137 | if (separator.lastIndex === match.index) { | |
138 | separator.lastIndex++; // Avoid an infinite loop |
|
138 | separator.lastIndex++; // Avoid an infinite loop | |
139 | } |
|
139 | } | |
140 | } |
|
140 | } | |
141 | if (lastLastIndex === str.length) { |
|
141 | if (lastLastIndex === str.length) { | |
142 | if (lastLength || !separator.test("")) { |
|
142 | if (lastLength || !separator.test("")) { | |
143 | output.push(""); |
|
143 | output.push(""); | |
144 | } |
|
144 | } | |
145 | } else { |
|
145 | } else { | |
146 | output.push(str.slice(lastLastIndex)); |
|
146 | output.push(str.slice(lastLastIndex)); | |
147 | } |
|
147 | } | |
148 | return output.length > limit ? output.slice(0, limit) : output; |
|
148 | return output.length > limit ? output.slice(0, limit) : output; | |
149 | }; |
|
149 | }; | |
150 |
|
150 | |||
151 | //============================================================================ |
|
151 | //============================================================================ | |
152 | // End contributed Cross-browser RegEx Split |
|
152 | // End contributed Cross-browser RegEx Split | |
153 | //============================================================================ |
|
153 | //============================================================================ | |
154 |
|
154 | |||
155 |
|
155 | |||
156 | var uuid = function () { |
|
156 | var uuid = function () { | |
157 | // http://www.ietf.org/rfc/rfc4122.txt |
|
157 | // http://www.ietf.org/rfc/rfc4122.txt | |
158 | var s = []; |
|
158 | var s = []; | |
159 | var hexDigits = "0123456789ABCDEF"; |
|
159 | var hexDigits = "0123456789ABCDEF"; | |
160 | for (var i = 0; i < 32; i++) { |
|
160 | for (var i = 0; i < 32; i++) { | |
161 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); |
|
161 | s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); | |
162 | } |
|
162 | } | |
163 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 |
|
163 | s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 | |
164 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 |
|
164 | s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 | |
165 |
|
165 | |||
166 | var uuid = s.join(""); |
|
166 | var uuid = s.join(""); | |
167 | return uuid; |
|
167 | return uuid; | |
168 | }; |
|
168 | }; | |
169 |
|
169 | |||
170 |
|
170 | |||
171 | //Fix raw text to parse correctly in crazy XML |
|
171 | //Fix raw text to parse correctly in crazy XML | |
172 | function xmlencode(string) { |
|
172 | function xmlencode(string) { | |
173 | return string.replace(/\&/g,'&'+'amp;') |
|
173 | return string.replace(/\&/g,'&'+'amp;') | |
174 | .replace(/</g,'&'+'lt;') |
|
174 | .replace(/</g,'&'+'lt;') | |
175 | .replace(/>/g,'&'+'gt;') |
|
175 | .replace(/>/g,'&'+'gt;') | |
176 | .replace(/\'/g,'&'+'apos;') |
|
176 | .replace(/\'/g,'&'+'apos;') | |
177 | .replace(/\"/g,'&'+'quot;') |
|
177 | .replace(/\"/g,'&'+'quot;') | |
178 | .replace(/`/g,'&'+'#96;'); |
|
178 | .replace(/`/g,'&'+'#96;'); | |
179 | } |
|
179 | } | |
180 |
|
180 | |||
181 |
|
181 | |||
182 | //Map from terminal commands to CSS classes |
|
182 | //Map from terminal commands to CSS classes | |
183 | var ansi_colormap = { |
|
183 | var ansi_colormap = { | |
184 | "01":"ansibold", |
|
184 | "01":"ansibold", | |
185 |
|
185 | |||
186 | "30":"ansiblack", |
|
186 | "30":"ansiblack", | |
187 | "31":"ansired", |
|
187 | "31":"ansired", | |
188 | "32":"ansigreen", |
|
188 | "32":"ansigreen", | |
189 | "33":"ansiyellow", |
|
189 | "33":"ansiyellow", | |
190 | "34":"ansiblue", |
|
190 | "34":"ansiblue", | |
191 | "35":"ansipurple", |
|
191 | "35":"ansipurple", | |
192 | "36":"ansicyan", |
|
192 | "36":"ansicyan", | |
193 | "37":"ansigray", |
|
193 | "37":"ansigray", | |
194 |
|
194 | |||
195 | "40":"ansibgblack", |
|
195 | "40":"ansibgblack", | |
196 | "41":"ansibgred", |
|
196 | "41":"ansibgred", | |
197 | "42":"ansibggreen", |
|
197 | "42":"ansibggreen", | |
198 | "43":"ansibgyellow", |
|
198 | "43":"ansibgyellow", | |
199 | "44":"ansibgblue", |
|
199 | "44":"ansibgblue", | |
200 | "45":"ansibgpurple", |
|
200 | "45":"ansibgpurple", | |
201 | "46":"ansibgcyan", |
|
201 | "46":"ansibgcyan", | |
202 | "47":"ansibggray" |
|
202 | "47":"ansibggray" | |
203 | }; |
|
203 | }; | |
204 |
|
204 | |||
205 | function _process_numbers(attrs, numbers) { |
|
205 | function _process_numbers(attrs, numbers) { | |
206 | // process ansi escapes |
|
206 | // process ansi escapes | |
207 | var n = numbers.shift(); |
|
207 | var n = numbers.shift(); | |
208 | if (ansi_colormap[n]) { |
|
208 | if (ansi_colormap[n]) { | |
209 | if ( ! attrs["class"] ) { |
|
209 | if ( ! attrs["class"] ) { | |
210 | attrs["class"] = ansi_colormap[n]; |
|
210 | attrs["class"] = ansi_colormap[n]; | |
211 | } else { |
|
211 | } else { | |
212 | attrs["class"] += " " + ansi_colormap[n]; |
|
212 | attrs["class"] += " " + ansi_colormap[n]; | |
213 | } |
|
213 | } | |
214 | } else if (n == "38" || n == "48") { |
|
214 | } else if (n == "38" || n == "48") { | |
215 | // VT100 256 color or 24 bit RGB |
|
215 | // VT100 256 color or 24 bit RGB | |
216 | if (numbers.length < 2) { |
|
216 | if (numbers.length < 2) { | |
217 | console.log("Not enough fields for VT100 color", numbers); |
|
217 | console.log("Not enough fields for VT100 color", numbers); | |
218 | return; |
|
218 | return; | |
219 | } |
|
219 | } | |
220 |
|
220 | |||
221 | var index_or_rgb = numbers.shift(); |
|
221 | var index_or_rgb = numbers.shift(); | |
222 | var r,g,b; |
|
222 | var r,g,b; | |
223 | if (index_or_rgb == "5") { |
|
223 | if (index_or_rgb == "5") { | |
224 | // 256 color |
|
224 | // 256 color | |
225 | var idx = parseInt(numbers.shift()); |
|
225 | var idx = parseInt(numbers.shift()); | |
226 | if (idx < 16) { |
|
226 | if (idx < 16) { | |
227 | // indexed ANSI |
|
227 | // indexed ANSI | |
228 | // ignore bright / non-bright distinction |
|
228 | // ignore bright / non-bright distinction | |
229 | idx = idx % 8; |
|
229 | idx = idx % 8; | |
230 | var ansiclass = ansi_colormap[n[0] + (idx % 8).toString()]; |
|
230 | var ansiclass = ansi_colormap[n[0] + (idx % 8).toString()]; | |
231 | if ( ! attrs["class"] ) { |
|
231 | if ( ! attrs["class"] ) { | |
232 | attrs["class"] = ansiclass; |
|
232 | attrs["class"] = ansiclass; | |
233 | } else { |
|
233 | } else { | |
234 | attrs["class"] += " " + ansiclass; |
|
234 | attrs["class"] += " " + ansiclass; | |
235 | } |
|
235 | } | |
236 | return; |
|
236 | return; | |
237 | } else if (idx < 232) { |
|
237 | } else if (idx < 232) { | |
238 | // 216 color 6x6x6 RGB |
|
238 | // 216 color 6x6x6 RGB | |
239 | idx = idx - 16; |
|
239 | idx = idx - 16; | |
240 | b = idx % 6; |
|
240 | b = idx % 6; | |
241 | g = Math.floor(idx / 6) % 6; |
|
241 | g = Math.floor(idx / 6) % 6; | |
242 | r = Math.floor(idx / 36) % 6; |
|
242 | r = Math.floor(idx / 36) % 6; | |
243 | // convert to rgb |
|
243 | // convert to rgb | |
244 | r = (r * 51); |
|
244 | r = (r * 51); | |
245 | g = (g * 51); |
|
245 | g = (g * 51); | |
246 | b = (b * 51); |
|
246 | b = (b * 51); | |
247 | } else { |
|
247 | } else { | |
248 | // grayscale |
|
248 | // grayscale | |
249 | idx = idx - 231; |
|
249 | idx = idx - 231; | |
250 | // it's 1-24 and should *not* include black or white, |
|
250 | // it's 1-24 and should *not* include black or white, | |
251 | // so a 26 point scale |
|
251 | // so a 26 point scale | |
252 | r = g = b = Math.floor(idx * 256 / 26); |
|
252 | r = g = b = Math.floor(idx * 256 / 26); | |
253 | } |
|
253 | } | |
254 | } else if (index_or_rgb == "2") { |
|
254 | } else if (index_or_rgb == "2") { | |
255 | // Simple 24 bit RGB |
|
255 | // Simple 24 bit RGB | |
256 | if (numbers.length > 3) { |
|
256 | if (numbers.length > 3) { | |
257 | console.log("Not enough fields for RGB", numbers); |
|
257 | console.log("Not enough fields for RGB", numbers); | |
258 | return; |
|
258 | return; | |
259 | } |
|
259 | } | |
260 | r = numbers.shift(); |
|
260 | r = numbers.shift(); | |
261 | g = numbers.shift(); |
|
261 | g = numbers.shift(); | |
262 | b = numbers.shift(); |
|
262 | b = numbers.shift(); | |
263 | } else { |
|
263 | } else { | |
264 | console.log("unrecognized control", numbers); |
|
264 | console.log("unrecognized control", numbers); | |
265 | return; |
|
265 | return; | |
266 | } |
|
266 | } | |
267 | if (r !== undefined) { |
|
267 | if (r !== undefined) { | |
268 | // apply the rgb color |
|
268 | // apply the rgb color | |
269 | var line; |
|
269 | var line; | |
270 | if (n == "38") { |
|
270 | if (n == "38") { | |
271 | line = "color: "; |
|
271 | line = "color: "; | |
272 | } else { |
|
272 | } else { | |
273 | line = "background-color: "; |
|
273 | line = "background-color: "; | |
274 | } |
|
274 | } | |
275 | line = line + "rgb(" + r + "," + g + "," + b + ");"; |
|
275 | line = line + "rgb(" + r + "," + g + "," + b + ");"; | |
276 | if ( !attrs.style ) { |
|
276 | if ( !attrs.style ) { | |
277 | attrs.style = line; |
|
277 | attrs.style = line; | |
278 | } else { |
|
278 | } else { | |
279 | attrs.style += " " + line; |
|
279 | attrs.style += " " + line; | |
280 | } |
|
280 | } | |
281 | } |
|
281 | } | |
282 | } |
|
282 | } | |
283 | } |
|
283 | } | |
284 |
|
284 | |||
285 | function ansispan(str) { |
|
285 | function ansispan(str) { | |
286 | // ansispan function adapted from github.com/mmalecki/ansispan (MIT License) |
|
286 | // ansispan function adapted from github.com/mmalecki/ansispan (MIT License) | |
287 | // regular ansi escapes (using the table above) |
|
287 | // regular ansi escapes (using the table above) | |
288 | var is_open = false; |
|
288 | var is_open = false; | |
289 | return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) { |
|
289 | return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) { | |
290 | if (!pattern) { |
|
290 | if (!pattern) { | |
291 | // [(01|22|39|)m close spans |
|
291 | // [(01|22|39|)m close spans | |
292 | if (is_open) { |
|
292 | if (is_open) { | |
293 | is_open = false; |
|
293 | is_open = false; | |
294 | return "</span>"; |
|
294 | return "</span>"; | |
295 | } else { |
|
295 | } else { | |
296 | return ""; |
|
296 | return ""; | |
297 | } |
|
297 | } | |
298 | } else { |
|
298 | } else { | |
299 | is_open = true; |
|
299 | is_open = true; | |
300 |
|
300 | |||
301 | // consume sequence of color escapes |
|
301 | // consume sequence of color escapes | |
302 | var numbers = pattern.match(/\d+/g); |
|
302 | var numbers = pattern.match(/\d+/g); | |
303 | var attrs = {}; |
|
303 | var attrs = {}; | |
304 | while (numbers.length > 0) { |
|
304 | while (numbers.length > 0) { | |
305 | _process_numbers(attrs, numbers); |
|
305 | _process_numbers(attrs, numbers); | |
306 | } |
|
306 | } | |
307 |
|
307 | |||
308 | var span = "<span "; |
|
308 | var span = "<span "; | |
309 | for (var attr in attrs) { |
|
309 | for (var attr in attrs) { | |
310 | var value = attrs[attr]; |
|
310 | var value = attrs[attr]; | |
311 | span = span + " " + attr + '="' + attrs[attr] + '"'; |
|
311 | span = span + " " + attr + '="' + attrs[attr] + '"'; | |
312 | } |
|
312 | } | |
313 | return span + ">"; |
|
313 | return span + ">"; | |
314 | } |
|
314 | } | |
315 | }); |
|
315 | }); | |
316 | } |
|
316 | } | |
317 |
|
317 | |||
318 | // Transform ANSI color escape codes into HTML <span> tags with css |
|
318 | // Transform ANSI color escape codes into HTML <span> tags with css | |
319 | // classes listed in the above ansi_colormap object. The actual color used |
|
319 | // classes listed in the above ansi_colormap object. The actual color used | |
320 | // are set in the css file. |
|
320 | // are set in the css file. | |
321 | function fixConsole(txt) { |
|
321 | function fixConsole(txt) { | |
322 | txt = xmlencode(txt); |
|
322 | txt = xmlencode(txt); | |
323 | var re = /\033\[([\dA-Fa-f;]*?)m/; |
|
323 | var re = /\033\[([\dA-Fa-f;]*?)m/; | |
324 | var opened = false; |
|
324 | var opened = false; | |
325 | var cmds = []; |
|
325 | var cmds = []; | |
326 | var opener = ""; |
|
326 | var opener = ""; | |
327 | var closer = ""; |
|
327 | var closer = ""; | |
328 |
|
328 | |||
329 | // Strip all ANSI codes that are not color related. Matches |
|
329 | // Strip all ANSI codes that are not color related. Matches | |
330 | // all ANSI codes that do not end with "m". |
|
330 | // all ANSI codes that do not end with "m". | |
331 | var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; |
|
331 | var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g; | |
332 | txt = txt.replace(ignored_re, ""); |
|
332 | txt = txt.replace(ignored_re, ""); | |
333 |
|
333 | |||
334 | // color ansi codes |
|
334 | // color ansi codes | |
335 | txt = ansispan(txt); |
|
335 | txt = ansispan(txt); | |
336 | return txt; |
|
336 | return txt; | |
337 | } |
|
337 | } | |
338 |
|
338 | |||
339 | // Remove chunks that should be overridden by the effect of |
|
339 | // Remove chunks that should be overridden by the effect of | |
340 | // carriage return characters |
|
340 | // carriage return characters | |
341 | function fixCarriageReturn(txt) { |
|
341 | function fixCarriageReturn(txt) { | |
342 | var tmp = txt; |
|
342 | var tmp = txt; | |
343 | do { |
|
343 | do { | |
344 | txt = tmp; |
|
344 | txt = tmp; | |
345 | tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline |
|
345 | tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline | |
346 | tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line |
|
346 | tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line | |
347 | } while (tmp.length < txt.length); |
|
347 | } while (tmp.length < txt.length); | |
348 | return txt; |
|
348 | return txt; | |
349 | } |
|
349 | } | |
350 |
|
350 | |||
351 | // Locate any URLs and convert them to a anchor tag |
|
351 | // Locate any URLs and convert them to a anchor tag | |
352 | function autoLinkUrls(txt) { |
|
352 | function autoLinkUrls(txt) { | |
353 | return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi, |
|
353 | return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi, | |
354 | "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>"); |
|
354 | "$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>"); | |
355 | } |
|
355 | } | |
356 |
|
356 | |||
357 | var points_to_pixels = function (points) { |
|
357 | var points_to_pixels = function (points) { | |
358 | // A reasonably good way of converting between points and pixels. |
|
358 | // A reasonably good way of converting between points and pixels. | |
359 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); |
|
359 | var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>'); | |
360 | $(body).append(test); |
|
360 | $(body).append(test); | |
361 | var pixel_per_point = test.width()/10000; |
|
361 | var pixel_per_point = test.width()/10000; | |
362 | test.remove(); |
|
362 | test.remove(); | |
363 | return Math.floor(points*pixel_per_point); |
|
363 | return Math.floor(points*pixel_per_point); | |
364 | }; |
|
364 | }; | |
365 |
|
365 | |||
366 | var always_new = function (constructor) { |
|
366 | var always_new = function (constructor) { | |
367 | // wrapper around contructor to avoid requiring `var a = new constructor()` |
|
367 | // wrapper around contructor to avoid requiring `var a = new constructor()` | |
368 | // useful for passing constructors as callbacks, |
|
368 | // useful for passing constructors as callbacks, | |
369 | // not for programmer laziness. |
|
369 | // not for programmer laziness. | |
370 | // from http://programmers.stackexchange.com/questions/118798 |
|
370 | // from http://programmers.stackexchange.com/questions/118798 | |
371 | return function () { |
|
371 | return function () { | |
372 | var obj = Object.create(constructor.prototype); |
|
372 | var obj = Object.create(constructor.prototype); | |
373 | constructor.apply(obj, arguments); |
|
373 | constructor.apply(obj, arguments); | |
374 | return obj; |
|
374 | return obj; | |
375 | }; |
|
375 | }; | |
376 | }; |
|
376 | }; | |
377 |
|
377 | |||
378 | var url_path_join = function () { |
|
378 | var url_path_join = function () { | |
379 | // join a sequence of url components with '/' |
|
379 | // join a sequence of url components with '/' | |
380 | var url = ''; |
|
380 | var url = ''; | |
381 | for (var i = 0; i < arguments.length; i++) { |
|
381 | for (var i = 0; i < arguments.length; i++) { | |
382 | if (arguments[i] === '') { |
|
382 | if (arguments[i] === '') { | |
383 | continue; |
|
383 | continue; | |
384 | } |
|
384 | } | |
385 | if (url.length > 0 && url[url.length-1] != '/') { |
|
385 | if (url.length > 0 && url[url.length-1] != '/') { | |
386 | url = url + '/' + arguments[i]; |
|
386 | url = url + '/' + arguments[i]; | |
387 | } else { |
|
387 | } else { | |
388 | url = url + arguments[i]; |
|
388 | url = url + arguments[i]; | |
389 | } |
|
389 | } | |
390 | } |
|
390 | } | |
391 | url = url.replace(/\/\/+/, '/'); |
|
391 | url = url.replace(/\/\/+/, '/'); | |
392 | return url; |
|
392 | return url; | |
393 | }; |
|
393 | }; | |
394 |
|
394 | |||
395 | var url_path_split = function (path) { |
|
395 | var url_path_split = function (path) { | |
396 | // Like os.path.split for URLs. |
|
396 | // Like os.path.split for URLs. | |
397 | // Always returns two strings, the directory path and the base filename |
|
397 | // Always returns two strings, the directory path and the base filename | |
398 |
|
398 | |||
399 | var idx = path.lastIndexOf('/'); |
|
399 | var idx = path.lastIndexOf('/'); | |
400 | if (idx === -1) { |
|
400 | if (idx === -1) { | |
401 | return ['', path]; |
|
401 | return ['', path]; | |
402 | } else { |
|
402 | } else { | |
403 | return [ path.slice(0, idx), path.slice(idx + 1) ]; |
|
403 | return [ path.slice(0, idx), path.slice(idx + 1) ]; | |
404 | } |
|
404 | } | |
405 | }; |
|
405 | }; | |
406 |
|
406 | |||
407 | var parse_url = function (url) { |
|
407 | var parse_url = function (url) { | |
408 | // an `a` element with an href allows attr-access to the parsed segments of a URL |
|
408 | // an `a` element with an href allows attr-access to the parsed segments of a URL | |
409 | // a = parse_url("http://localhost:8888/path/name#hash") |
|
409 | // a = parse_url("http://localhost:8888/path/name#hash") | |
410 | // a.protocol = "http:" |
|
410 | // a.protocol = "http:" | |
411 | // a.host = "localhost:8888" |
|
411 | // a.host = "localhost:8888" | |
412 | // a.hostname = "localhost" |
|
412 | // a.hostname = "localhost" | |
413 | // a.port = 8888 |
|
413 | // a.port = 8888 | |
414 | // a.pathname = "/path/name" |
|
414 | // a.pathname = "/path/name" | |
415 | // a.hash = "#hash" |
|
415 | // a.hash = "#hash" | |
416 | var a = document.createElement("a"); |
|
416 | var a = document.createElement("a"); | |
417 | a.href = url; |
|
417 | a.href = url; | |
418 | return a; |
|
418 | return a; | |
419 | }; |
|
419 | }; | |
420 |
|
420 | |||
421 | var encode_uri_components = function (uri) { |
|
421 | var encode_uri_components = function (uri) { | |
422 | // encode just the components of a multi-segment uri, |
|
422 | // encode just the components of a multi-segment uri, | |
423 | // leaving '/' separators |
|
423 | // leaving '/' separators | |
424 | return uri.split('/').map(encodeURIComponent).join('/'); |
|
424 | return uri.split('/').map(encodeURIComponent).join('/'); | |
425 | }; |
|
425 | }; | |
426 |
|
426 | |||
427 | var url_join_encode = function () { |
|
427 | var url_join_encode = function () { | |
428 | // join a sequence of url components with '/', |
|
428 | // join a sequence of url components with '/', | |
429 | // encoding each component with encodeURIComponent |
|
429 | // encoding each component with encodeURIComponent | |
430 | return encode_uri_components(url_path_join.apply(null, arguments)); |
|
430 | return encode_uri_components(url_path_join.apply(null, arguments)); | |
431 | }; |
|
431 | }; | |
432 |
|
432 | |||
433 |
|
433 | |||
434 | var splitext = function (filename) { |
|
434 | var splitext = function (filename) { | |
435 | // mimic Python os.path.splitext |
|
435 | // mimic Python os.path.splitext | |
436 | // Returns ['base', '.ext'] |
|
436 | // Returns ['base', '.ext'] | |
437 | var idx = filename.lastIndexOf('.'); |
|
437 | var idx = filename.lastIndexOf('.'); | |
438 | if (idx > 0) { |
|
438 | if (idx > 0) { | |
439 | return [filename.slice(0, idx), filename.slice(idx)]; |
|
439 | return [filename.slice(0, idx), filename.slice(idx)]; | |
440 | } else { |
|
440 | } else { | |
441 | return [filename, '']; |
|
441 | return [filename, '']; | |
442 | } |
|
442 | } | |
443 | }; |
|
443 | }; | |
444 |
|
444 | |||
445 |
|
445 | |||
446 | var escape_html = function (text) { |
|
446 | var escape_html = function (text) { | |
447 | // escape text to HTML |
|
447 | // escape text to HTML | |
448 | return $("<div/>").text(text).html(); |
|
448 | return $("<div/>").text(text).html(); | |
449 | }; |
|
449 | }; | |
450 |
|
450 | |||
451 |
|
451 | |||
452 | var get_body_data = function(key) { |
|
452 | var get_body_data = function(key) { | |
453 | // get a url-encoded item from body.data and decode it |
|
453 | // get a url-encoded item from body.data and decode it | |
454 | // we should never have any encoded URLs anywhere else in code |
|
454 | // we should never have any encoded URLs anywhere else in code | |
455 | // until we are building an actual request |
|
455 | // until we are building an actual request | |
456 | return decodeURIComponent($('body').data(key)); |
|
456 | return decodeURIComponent($('body').data(key)); | |
457 | }; |
|
457 | }; | |
458 |
|
458 | |||
459 | var to_absolute_cursor_pos = function (cm, cursor) { |
|
459 | var to_absolute_cursor_pos = function (cm, cursor) { | |
460 | // get the absolute cursor position from CodeMirror's col, ch |
|
460 | // get the absolute cursor position from CodeMirror's col, ch | |
461 | if (!cursor) { |
|
461 | if (!cursor) { | |
462 | cursor = cm.getCursor(); |
|
462 | cursor = cm.getCursor(); | |
463 | } |
|
463 | } | |
464 | var cursor_pos = cursor.ch; |
|
464 | var cursor_pos = cursor.ch; | |
465 | for (var i = 0; i < cursor.line; i++) { |
|
465 | for (var i = 0; i < cursor.line; i++) { | |
466 | cursor_pos += cm.getLine(i).length + 1; |
|
466 | cursor_pos += cm.getLine(i).length + 1; | |
467 | } |
|
467 | } | |
468 | return cursor_pos; |
|
468 | return cursor_pos; | |
469 | }; |
|
469 | }; | |
470 |
|
470 | |||
471 | var from_absolute_cursor_pos = function (cm, cursor_pos) { |
|
471 | var from_absolute_cursor_pos = function (cm, cursor_pos) { | |
472 | // turn absolute cursor postion into CodeMirror col, ch cursor |
|
472 | // turn absolute cursor postion into CodeMirror col, ch cursor | |
473 | var i, line; |
|
473 | var i, line; | |
474 | var offset = 0; |
|
474 | var offset = 0; | |
475 | for (i = 0, line=cm.getLine(i); line !== undefined; i++, line=cm.getLine(i)) { |
|
475 | for (i = 0, line=cm.getLine(i); line !== undefined; i++, line=cm.getLine(i)) { | |
476 | if (offset + line.length < cursor_pos) { |
|
476 | if (offset + line.length < cursor_pos) { | |
477 | offset += line.length + 1; |
|
477 | offset += line.length + 1; | |
478 | } else { |
|
478 | } else { | |
479 | return { |
|
479 | return { | |
480 | line : i, |
|
480 | line : i, | |
481 | ch : cursor_pos - offset, |
|
481 | ch : cursor_pos - offset, | |
482 | }; |
|
482 | }; | |
483 | } |
|
483 | } | |
484 | } |
|
484 | } | |
485 | // reached end, return endpoint |
|
485 | // reached end, return endpoint | |
486 | return { |
|
486 | return { | |
487 | ch : line.length - 1, |
|
487 | ch : line.length - 1, | |
488 | line : i - 1, |
|
488 | line : i - 1, | |
489 | }; |
|
489 | }; | |
490 | }; |
|
490 | }; | |
491 |
|
491 | |||
492 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript |
|
492 | // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript | |
493 | var browser = (function() { |
|
493 | var browser = (function() { | |
494 | if (typeof navigator === 'undefined') { |
|
494 | if (typeof navigator === 'undefined') { | |
495 | // navigator undefined in node |
|
495 | // navigator undefined in node | |
496 | return 'None'; |
|
496 | return 'None'; | |
497 | } |
|
497 | } | |
498 | var N= navigator.appName, ua= navigator.userAgent, tem; |
|
498 | var N= navigator.appName, ua= navigator.userAgent, tem; | |
499 | var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); |
|
499 | var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); | |
500 | if (M && (tem= ua.match(/version\/([\.\d]+)/i)) !== null) M[2]= tem[1]; |
|
500 | if (M && (tem= ua.match(/version\/([\.\d]+)/i)) !== null) M[2]= tem[1]; | |
501 | M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; |
|
501 | M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; | |
502 | return M; |
|
502 | return M; | |
503 | })(); |
|
503 | })(); | |
504 |
|
504 | |||
505 | // http://stackoverflow.com/questions/11219582/how-to-detect-my-browser-version-and-operating-system-using-javascript |
|
505 | // http://stackoverflow.com/questions/11219582/how-to-detect-my-browser-version-and-operating-system-using-javascript | |
506 | var platform = (function () { |
|
506 | var platform = (function () { | |
507 | if (typeof navigator === 'undefined') { |
|
507 | if (typeof navigator === 'undefined') { | |
508 | // navigator undefined in node |
|
508 | // navigator undefined in node | |
509 | return 'None'; |
|
509 | return 'None'; | |
510 | } |
|
510 | } | |
511 | var OSName="None"; |
|
511 | var OSName="None"; | |
512 | if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; |
|
512 | if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows"; | |
513 | if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; |
|
513 | if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS"; | |
514 | if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; |
|
514 | if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX"; | |
515 | if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; |
|
515 | if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux"; | |
516 | return OSName; |
|
516 | return OSName; | |
517 | })(); |
|
517 | })(); | |
518 |
|
518 | |||
519 | var is_or_has = function (a, b) { |
|
519 | var is_or_has = function (a, b) { | |
520 | // Is b a child of a or a itself? |
|
520 | // Is b a child of a or a itself? | |
521 | return a.has(b).length !==0 || a.is(b); |
|
521 | return a.has(b).length !==0 || a.is(b); | |
522 | }; |
|
522 | }; | |
523 |
|
523 | |||
524 | var is_focused = function (e) { |
|
524 | var is_focused = function (e) { | |
525 | // Is element e, or one of its children focused? |
|
525 | // Is element e, or one of its children focused? | |
526 | e = $(e); |
|
526 | e = $(e); | |
527 | var target = $(document.activeElement); |
|
527 | var target = $(document.activeElement); | |
528 | if (target.length > 0) { |
|
528 | if (target.length > 0) { | |
529 | if (is_or_has(e, target)) { |
|
529 | if (is_or_has(e, target)) { | |
530 | return true; |
|
530 | return true; | |
531 | } else { |
|
531 | } else { | |
532 | return false; |
|
532 | return false; | |
533 | } |
|
533 | } | |
534 | } else { |
|
534 | } else { | |
535 | return false; |
|
535 | return false; | |
536 | } |
|
536 | } | |
537 | }; |
|
537 | }; | |
538 |
|
538 | |||
539 | var mergeopt = function(_class, options, overwrite){ |
|
539 | var mergeopt = function(_class, options, overwrite){ | |
540 | options = options || {}; |
|
540 | options = options || {}; | |
541 | overwrite = overwrite || {}; |
|
541 | overwrite = overwrite || {}; | |
542 | return $.extend(true, {}, _class.options_default, options, overwrite); |
|
542 | return $.extend(true, {}, _class.options_default, options, overwrite); | |
543 | }; |
|
543 | }; | |
544 |
|
544 | |||
545 | var ajax_error_msg = function (jqXHR) { |
|
545 | var ajax_error_msg = function (jqXHR) { | |
546 | // Return a JSON error message if there is one, |
|
546 | // Return a JSON error message if there is one, | |
547 | // otherwise the basic HTTP status text. |
|
547 | // otherwise the basic HTTP status text. | |
548 | if (jqXHR.responseJSON && jqXHR.responseJSON.traceback) { |
|
548 | if (jqXHR.responseJSON && jqXHR.responseJSON.traceback) { | |
549 | return jqXHR.responseJSON.traceback; |
|
549 | return jqXHR.responseJSON.traceback; | |
550 | } else if (jqXHR.responseJSON && jqXHR.responseJSON.message) { |
|
550 | } else if (jqXHR.responseJSON && jqXHR.responseJSON.message) { | |
551 | return jqXHR.responseJSON.message; |
|
551 | return jqXHR.responseJSON.message; | |
552 | } else { |
|
552 | } else { | |
553 | return jqXHR.statusText; |
|
553 | return jqXHR.statusText; | |
554 | } |
|
554 | } | |
555 | }; |
|
555 | }; | |
556 | var log_ajax_error = function (jqXHR, status, error) { |
|
556 | var log_ajax_error = function (jqXHR, status, error) { | |
557 | // log ajax failures with informative messages |
|
557 | // log ajax failures with informative messages | |
558 | var msg = "API request failed (" + jqXHR.status + "): "; |
|
558 | var msg = "API request failed (" + jqXHR.status + "): "; | |
559 | console.log(jqXHR); |
|
559 | console.log(jqXHR); | |
560 | msg += ajax_error_msg(jqXHR); |
|
560 | msg += ajax_error_msg(jqXHR); | |
561 | console.log(msg); |
|
561 | console.log(msg); | |
562 | }; |
|
562 | }; | |
563 |
|
563 | |||
564 | var requireCodeMirrorMode = function (mode, callback, errback) { |
|
564 | var requireCodeMirrorMode = function (mode, callback, errback) { | |
565 | // load a mode with requirejs |
|
565 | // load a mode with requirejs | |
566 | if (typeof mode != "string") mode = mode.name; |
|
566 | if (typeof mode != "string") mode = mode.name; | |
567 | if (CodeMirror.modes.hasOwnProperty(mode)) { |
|
567 | if (CodeMirror.modes.hasOwnProperty(mode)) { | |
568 | callback(CodeMirror.modes.mode); |
|
568 | callback(CodeMirror.modes.mode); | |
569 | return; |
|
569 | return; | |
570 | } |
|
570 | } | |
571 | require([ |
|
571 | require([ | |
572 | // might want to use CodeMirror.modeURL here |
|
572 | // might want to use CodeMirror.modeURL here | |
573 | ['codemirror/mode', mode, mode].join('/'), |
|
573 | ['codemirror/mode', mode, mode].join('/'), | |
574 | ], callback, errback |
|
574 | ], callback, errback | |
575 | ); |
|
575 | ); | |
576 | }; |
|
576 | }; | |
577 |
|
577 | |||
578 | /** Error type for wrapped XHR errors. */ |
|
578 | /** Error type for wrapped XHR errors. */ | |
579 | var XHR_ERROR = 'XhrError'; |
|
579 | var XHR_ERROR = 'XhrError'; | |
580 |
|
580 | |||
581 | /** |
|
581 | /** | |
582 | * Wraps an AJAX error as an Error object. |
|
582 | * Wraps an AJAX error as an Error object. | |
583 | */ |
|
583 | */ | |
584 | var wrap_ajax_error = function (jqXHR, status, error) { |
|
584 | var wrap_ajax_error = function (jqXHR, status, error) { | |
585 | var wrapped_error = new Error(ajax_error_msg(jqXHR)); |
|
585 | var wrapped_error = new Error(ajax_error_msg(jqXHR)); | |
586 | wrapped_error.name = XHR_ERROR; |
|
586 | wrapped_error.name = XHR_ERROR; | |
587 | // provide xhr response |
|
587 | // provide xhr response | |
588 | wrapped_error.xhr = jqXHR; |
|
588 | wrapped_error.xhr = jqXHR; | |
589 | wrapped_error.xhr_status = status; |
|
589 | wrapped_error.xhr_status = status; | |
590 | wrapped_error.xhr_error = error; |
|
590 | wrapped_error.xhr_error = error; | |
591 | return wrapped_error; |
|
591 | return wrapped_error; | |
592 | }; |
|
592 | }; | |
593 |
|
593 | |||
594 | var promising_ajax = function(url, settings) { |
|
594 | var promising_ajax = function(url, settings) { | |
595 | // Like $.ajax, but returning an ES6 promise. success and error settings |
|
595 | // Like $.ajax, but returning an ES6 promise. success and error settings | |
596 | // will be ignored. |
|
596 | // will be ignored. | |
597 | return new Promise(function(resolve, reject) { |
|
597 | return new Promise(function(resolve, reject) { | |
598 | settings.success = function(data, status, jqXHR) { |
|
598 | settings.success = function(data, status, jqXHR) { | |
599 | resolve(data); |
|
599 | resolve(data); | |
600 | }; |
|
600 | }; | |
601 | settings.error = function(jqXHR, status, error) { |
|
601 | settings.error = function(jqXHR, status, error) { | |
602 | log_ajax_error(jqXHR, status, error); |
|
602 | log_ajax_error(jqXHR, status, error); | |
603 | reject(wrap_ajax_error(jqXHR, status, error)); |
|
603 | reject(wrap_ajax_error(jqXHR, status, error)); | |
604 | }; |
|
604 | }; | |
605 | $.ajax(url, settings); |
|
605 | $.ajax(url, settings); | |
606 | }); |
|
606 | }); | |
607 | }; |
|
607 | }; | |
608 |
|
608 | |||
609 | var load = function(class_name, module_name, registry) { |
|
609 | var load = function(class_name, module_name, registry) { | |
610 | // Tries to load a class |
|
610 | // Tries to load a class | |
611 | // |
|
611 | // | |
612 | // Tries to load a class from a module using require.js, if a module |
|
612 | // Tries to load a class from a module using require.js, if a module | |
613 | // is specified, otherwise tries to load a class from the global |
|
613 | // is specified, otherwise tries to load a class from the global | |
614 | // registry, if the global registry is provided. |
|
614 | // registry, if the global registry is provided. | |
615 | return new Promise(function(resolve, reject) { |
|
615 | return new Promise(function(resolve, reject) { | |
616 |
|
616 | |||
617 | // Try loading the view module using require.js |
|
617 | // Try loading the view module using require.js | |
618 | if (module_name) { |
|
618 | if (module_name) { | |
619 | require([module_name], function(module) { |
|
619 | require([module_name], function(module) { | |
620 | if (module[class_name] === undefined) { |
|
620 | if (module[class_name] === undefined) { | |
621 |
|
|
621 | console.error('Class '+class_name+' not found in module '+module_name) | |
|
622 | reject(); | |||
622 | } else { |
|
623 | } else { | |
623 | resolve(module[class_name]); |
|
624 | resolve(module[class_name]); | |
624 | } |
|
625 | } | |
625 | }, reject); |
|
626 | }, reject); | |
626 | } else { |
|
627 | } else { | |
627 | if (registry && registry[class_name]) { |
|
628 | if (registry && registry[class_name]) { | |
628 | resolve(registry[class_name]); |
|
629 | resolve(registry[class_name]); | |
629 | } else { |
|
630 | } else { | |
630 |
|
|
631 | console.error('Class '+class_name+' not found in registry ', registry); | |
|
632 | reject(); | |||
631 | } |
|
633 | } | |
632 | } |
|
634 | } | |
633 | }); |
|
635 | }); | |
634 | }; |
|
636 | }; | |
635 |
|
637 | |||
636 | var resolve_dict = function(d) { |
|
638 | var resolve_dict = function(d) { | |
637 | var keys = Object.keys(d); |
|
639 | var keys = Object.keys(d); | |
638 | var values = []; |
|
640 | var values = []; | |
639 | keys.forEach(function(key) { |
|
641 | keys.forEach(function(key) { | |
640 | values.push(key); |
|
642 | values.push(key); | |
641 | }); |
|
643 | }); | |
642 | return Promise.all(values).then(function(v) { |
|
644 | return Promise.all(values).then(function(v) { | |
643 | d = {}; |
|
645 | d = {}; | |
644 | for(var i=0; i<keys.length; i++) { |
|
646 | for(var i=0; i<keys.length; i++) { | |
645 | d[keys[i]] = v[i]; |
|
647 | d[keys[i]] = v[i]; | |
646 | } |
|
648 | } | |
647 | return d; |
|
649 | return d; | |
648 | }); |
|
650 | }); | |
649 | }; |
|
651 | }; | |
650 |
|
652 | |||
651 | var utils = { |
|
653 | var utils = { | |
652 | regex_split : regex_split, |
|
654 | regex_split : regex_split, | |
653 | uuid : uuid, |
|
655 | uuid : uuid, | |
654 | fixConsole : fixConsole, |
|
656 | fixConsole : fixConsole, | |
655 | fixCarriageReturn : fixCarriageReturn, |
|
657 | fixCarriageReturn : fixCarriageReturn, | |
656 | autoLinkUrls : autoLinkUrls, |
|
658 | autoLinkUrls : autoLinkUrls, | |
657 | points_to_pixels : points_to_pixels, |
|
659 | points_to_pixels : points_to_pixels, | |
658 | get_body_data : get_body_data, |
|
660 | get_body_data : get_body_data, | |
659 | parse_url : parse_url, |
|
661 | parse_url : parse_url, | |
660 | url_path_split : url_path_split, |
|
662 | url_path_split : url_path_split, | |
661 | url_path_join : url_path_join, |
|
663 | url_path_join : url_path_join, | |
662 | url_join_encode : url_join_encode, |
|
664 | url_join_encode : url_join_encode, | |
663 | encode_uri_components : encode_uri_components, |
|
665 | encode_uri_components : encode_uri_components, | |
664 | splitext : splitext, |
|
666 | splitext : splitext, | |
665 | escape_html : escape_html, |
|
667 | escape_html : escape_html, | |
666 | always_new : always_new, |
|
668 | always_new : always_new, | |
667 | to_absolute_cursor_pos : to_absolute_cursor_pos, |
|
669 | to_absolute_cursor_pos : to_absolute_cursor_pos, | |
668 | from_absolute_cursor_pos : from_absolute_cursor_pos, |
|
670 | from_absolute_cursor_pos : from_absolute_cursor_pos, | |
669 | browser : browser, |
|
671 | browser : browser, | |
670 | platform: platform, |
|
672 | platform: platform, | |
671 | is_or_has : is_or_has, |
|
673 | is_or_has : is_or_has, | |
672 | is_focused : is_focused, |
|
674 | is_focused : is_focused, | |
673 | mergeopt: mergeopt, |
|
675 | mergeopt: mergeopt, | |
674 | ajax_error_msg : ajax_error_msg, |
|
676 | ajax_error_msg : ajax_error_msg, | |
675 | log_ajax_error : log_ajax_error, |
|
677 | log_ajax_error : log_ajax_error, | |
676 | requireCodeMirrorMode : requireCodeMirrorMode, |
|
678 | requireCodeMirrorMode : requireCodeMirrorMode, | |
677 | XHR_ERROR : XHR_ERROR, |
|
679 | XHR_ERROR : XHR_ERROR, | |
678 | wrap_ajax_error : wrap_ajax_error, |
|
680 | wrap_ajax_error : wrap_ajax_error, | |
679 | promising_ajax : promising_ajax, |
|
681 | promising_ajax : promising_ajax, | |
680 | load: load, |
|
682 | load: load, | |
681 | resolve_dict: resolve_dict, |
|
683 | resolve_dict: resolve_dict, | |
682 | }; |
|
684 | }; | |
683 |
|
685 | |||
684 | // Backwards compatability. |
|
686 | // Backwards compatability. | |
685 | IPython.utils = utils; |
|
687 | IPython.utils = utils; | |
686 |
|
688 | |||
687 | return utils; |
|
689 | return utils; | |
688 | }); |
|
690 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now