Show More
@@ -1,461 +1,460 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // Kernel |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | * An extendable module that provide base functionnality to create cell for notebook. | |
|
14 | 13 | * @module IPython |
|
15 | 14 | * @namespace IPython |
|
16 | 15 | * @submodule Kernel |
|
17 | 16 | */ |
|
18 | 17 | |
|
19 | 18 | var IPython = (function (IPython) { |
|
20 | 19 | |
|
21 | 20 | var utils = IPython.utils; |
|
22 | 21 | |
|
23 | 22 | // Initialization and connection. |
|
24 | 23 | /** |
|
25 | 24 | * A Kernel Class to communicate with the Python kernel |
|
26 | 25 | * @Class Kernel |
|
27 | 26 | */ |
|
28 | 27 | var Kernel = function (base_url) { |
|
29 | 28 | this.kernel_id = null; |
|
30 | 29 | this.shell_channel = null; |
|
31 | 30 | this.iopub_channel = null; |
|
32 | 31 | this.base_url = base_url; |
|
33 | 32 | this.running = false; |
|
34 | 33 | this.username = "username"; |
|
35 | 34 | this.session_id = utils.uuid(); |
|
36 | 35 | this._msg_callbacks = {}; |
|
37 | 36 | |
|
38 | 37 | if (typeof(WebSocket) !== 'undefined') { |
|
39 | 38 | this.WebSocket = WebSocket; |
|
40 | 39 | } else if (typeof(MozWebSocket) !== 'undefined') { |
|
41 | 40 | this.WebSocket = MozWebSocket; |
|
42 | 41 | } else { |
|
43 | 42 | alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox β₯ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.'); |
|
44 | 43 | }; |
|
45 | 44 | }; |
|
46 | 45 | |
|
47 | 46 | |
|
48 | 47 | Kernel.prototype._get_msg = function (msg_type, content) { |
|
49 | 48 | var msg = { |
|
50 | 49 | header : { |
|
51 | 50 | msg_id : utils.uuid(), |
|
52 | 51 | username : this.username, |
|
53 | 52 | session : this.session_id, |
|
54 | 53 | msg_type : msg_type |
|
55 | 54 | }, |
|
56 | 55 | metadata : {}, |
|
57 | 56 | content : content, |
|
58 | 57 | parent_header : {} |
|
59 | 58 | }; |
|
60 | 59 | return msg; |
|
61 | 60 | }; |
|
62 | 61 | |
|
63 | 62 | /** |
|
64 | 63 | * Start the Python kernel |
|
65 | 64 | * @method start |
|
66 | 65 | */ |
|
67 | 66 | Kernel.prototype.start = function (notebook_id) { |
|
68 | 67 | var that = this; |
|
69 | 68 | if (!this.running) { |
|
70 | 69 | var qs = $.param({notebook:notebook_id}); |
|
71 | 70 | var url = this.base_url + '?' + qs; |
|
72 | 71 | $.post(url, |
|
73 | 72 | $.proxy(that._kernel_started,that), |
|
74 | 73 | 'json' |
|
75 | 74 | ); |
|
76 | 75 | }; |
|
77 | 76 | }; |
|
78 | 77 | |
|
79 | 78 | /** |
|
80 | 79 | * Restart the python kernel. |
|
81 | 80 | * |
|
82 | 81 | * Emit a 'status_restarting.Kernel' event with |
|
83 | 82 | * the current object as parameter |
|
84 | 83 | * |
|
85 | 84 | * @method restart |
|
86 | 85 | */ |
|
87 | 86 | Kernel.prototype.restart = function () { |
|
88 | 87 | $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this}); |
|
89 | 88 | var that = this; |
|
90 | 89 | if (this.running) { |
|
91 | 90 | this.stop_channels(); |
|
92 | 91 | var url = this.kernel_url + "/restart"; |
|
93 | 92 | $.post(url, |
|
94 | 93 | $.proxy(that._kernel_started, that), |
|
95 | 94 | 'json' |
|
96 | 95 | ); |
|
97 | 96 | }; |
|
98 | 97 | }; |
|
99 | 98 | |
|
100 | 99 | |
|
101 | 100 | Kernel.prototype._kernel_started = function (json) { |
|
102 | 101 | console.log("Kernel started: ", json.kernel_id); |
|
103 | 102 | this.running = true; |
|
104 | 103 | this.kernel_id = json.kernel_id; |
|
105 | 104 | this.ws_url = json.ws_url; |
|
106 | 105 | this.kernel_url = this.base_url + "/" + this.kernel_id; |
|
107 | 106 | this.start_channels(); |
|
108 | 107 | this.shell_channel.onmessage = $.proxy(this._handle_shell_reply,this); |
|
109 | 108 | this.iopub_channel.onmessage = $.proxy(this._handle_iopub_reply,this); |
|
110 | 109 | $([IPython.events]).trigger('status_started.Kernel', {kernel: this}); |
|
111 | 110 | }; |
|
112 | 111 | |
|
113 | 112 | |
|
114 | 113 | Kernel.prototype._websocket_closed = function(ws_url, early){ |
|
115 | 114 | var msg; |
|
116 | 115 | var parent_item = $('body'); |
|
117 | 116 | if (early) { |
|
118 | 117 | msg = "Websocket connection to " + ws_url + " could not be established." + |
|
119 | 118 | " You will NOT be able to run code." + |
|
120 | 119 | " Your browser may not be compatible with the websocket version in the server," + |
|
121 | 120 | " or if the url does not look right, there could be an error in the" + |
|
122 | 121 | " server's configuration."; |
|
123 | 122 | } else { |
|
124 | 123 | IPython.notification_area.widget('kernel').set_message('Reconnecting Websockets', 1000); |
|
125 | 124 | this.start_channels(); |
|
126 | 125 | return; |
|
127 | 126 | } |
|
128 | 127 | var dialog = $('<div/>'); |
|
129 | 128 | dialog.html(msg); |
|
130 | 129 | parent_item.append(dialog); |
|
131 | 130 | dialog.dialog({ |
|
132 | 131 | resizable: false, |
|
133 | 132 | modal: true, |
|
134 | 133 | title: "Websocket closed", |
|
135 | 134 | closeText: "", |
|
136 | 135 | close: function(event, ui) {$(this).dialog('destroy').remove();}, |
|
137 | 136 | buttons : { |
|
138 | 137 | "OK": function () { |
|
139 | 138 | $(this).dialog('close'); |
|
140 | 139 | } |
|
141 | 140 | } |
|
142 | 141 | }); |
|
143 | 142 | |
|
144 | 143 | }; |
|
145 | 144 | |
|
146 | 145 | /** |
|
147 | 146 | * Start the `shell`and `iopub` channels. |
|
148 | 147 | * Will stop and restart them if they already exist. |
|
149 | 148 | * |
|
150 | 149 | * @method start_channels |
|
151 | 150 | */ |
|
152 | 151 | Kernel.prototype.start_channels = function () { |
|
153 | 152 | var that = this; |
|
154 | 153 | this.stop_channels(); |
|
155 | 154 | var ws_url = this.ws_url + this.kernel_url; |
|
156 | 155 | console.log("Starting WS:", ws_url); |
|
157 | 156 | this.shell_channel = new this.WebSocket(ws_url + "/shell"); |
|
158 | 157 | this.iopub_channel = new this.WebSocket(ws_url + "/iopub"); |
|
159 | 158 | send_cookie = function(){ |
|
160 | 159 | this.send(document.cookie); |
|
161 | 160 | }; |
|
162 | 161 | var already_called_onclose = false; // only alert once |
|
163 | 162 | ws_closed_early = function(evt){ |
|
164 | 163 | if (already_called_onclose){ |
|
165 | 164 | return; |
|
166 | 165 | } |
|
167 | 166 | already_called_onclose = true; |
|
168 | 167 | if ( ! evt.wasClean ){ |
|
169 | 168 | that._websocket_closed(ws_url, true); |
|
170 | 169 | } |
|
171 | 170 | }; |
|
172 | 171 | ws_closed_late = function(evt){ |
|
173 | 172 | if (already_called_onclose){ |
|
174 | 173 | return; |
|
175 | 174 | } |
|
176 | 175 | already_called_onclose = true; |
|
177 | 176 | if ( ! evt.wasClean ){ |
|
178 | 177 | that._websocket_closed(ws_url, false); |
|
179 | 178 | } |
|
180 | 179 | }; |
|
181 | 180 | this.shell_channel.onopen = send_cookie; |
|
182 | 181 | this.shell_channel.onclose = ws_closed_early; |
|
183 | 182 | this.iopub_channel.onopen = send_cookie; |
|
184 | 183 | this.iopub_channel.onclose = ws_closed_early; |
|
185 | 184 | // switch from early-close to late-close message after 1s |
|
186 | 185 | setTimeout(function(){ |
|
187 | 186 | that.shell_channel.onclose = ws_closed_late; |
|
188 | 187 | that.iopub_channel.onclose = ws_closed_late; |
|
189 | 188 | }, 1000); |
|
190 | 189 | }; |
|
191 | 190 | |
|
192 | 191 | /** |
|
193 | 192 | * Start the `shell`and `iopub` channels. |
|
194 | 193 | * @method stop_channels |
|
195 | 194 | */ |
|
196 | 195 | Kernel.prototype.stop_channels = function () { |
|
197 | 196 | if (this.shell_channel !== null) { |
|
198 | 197 | this.shell_channel.onclose = function (evt) {}; |
|
199 | 198 | this.shell_channel.close(); |
|
200 | 199 | this.shell_channel = null; |
|
201 | 200 | }; |
|
202 | 201 | if (this.iopub_channel !== null) { |
|
203 | 202 | this.iopub_channel.onclose = function (evt) {}; |
|
204 | 203 | this.iopub_channel.close(); |
|
205 | 204 | this.iopub_channel = null; |
|
206 | 205 | }; |
|
207 | 206 | }; |
|
208 | 207 | |
|
209 | 208 | // Main public methods. |
|
210 | 209 | |
|
211 | 210 | /** |
|
212 | 211 | * Get info on object asynchronoulsy |
|
213 | 212 | * |
|
214 | 213 | * @async |
|
215 | 214 | * @param objname {string} |
|
216 | 215 | * @param callback {dict} |
|
217 | 216 | * @method object_info_request |
|
218 | 217 | * |
|
219 | 218 | * @example |
|
220 | 219 | * |
|
221 | 220 | * When calling this method pass a callbacks structure of the form: |
|
222 | 221 | * |
|
223 | 222 | * callbacks = { |
|
224 | 223 | * 'object_info_reply': object_info_reply_callback |
|
225 | 224 | * } |
|
226 | 225 | * |
|
227 | 226 | * The `object_info_reply_callback` will be passed the content object of the |
|
228 | 227 | * |
|
229 | 228 | * `object_into_reply` message documented in |
|
230 | 229 | * [IPython dev documentation](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information) |
|
231 | 230 | */ |
|
232 | 231 | Kernel.prototype.object_info_request = function (objname, callbacks) { |
|
233 | 232 | if(typeof(objname)!=null && objname!=null) |
|
234 | 233 | { |
|
235 | 234 | var content = { |
|
236 | 235 | oname : objname.toString(), |
|
237 | 236 | }; |
|
238 | 237 | var msg = this._get_msg("object_info_request", content); |
|
239 | 238 | this.shell_channel.send(JSON.stringify(msg)); |
|
240 | 239 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
241 | 240 | return msg.header.msg_id; |
|
242 | 241 | } |
|
243 | 242 | return; |
|
244 | 243 | } |
|
245 | 244 | |
|
246 | 245 | /** |
|
247 | 246 | * Execute given code into kernel, and pass result to callback. |
|
248 | 247 | * |
|
249 | 248 | * @async |
|
250 | 249 | * @method execute |
|
251 | 250 | * @param {string} code |
|
252 | 251 | * @param callback {Object} With the following keys |
|
253 | 252 | * @param callback.'execute_reply' {function} |
|
254 | 253 | * @param callback.'output' {function} |
|
255 | 254 | * @param callback.'clear_output' {function} |
|
256 | 255 | * @param callback.'set_next_input' {function} |
|
257 | 256 | * @param {object} [options] |
|
258 | 257 | * @param [options.silent=false] {Boolean} |
|
259 | 258 | * @param [options.user_expressions=empty_dict] {Dict} |
|
260 | 259 | * @param [options.user_variables=empty_list] {List od Strings} |
|
261 | 260 | * @param [options.allow_stdin=false] {Boolean} true|false |
|
262 | 261 | * |
|
263 | 262 | * @example |
|
264 | 263 | * |
|
265 | 264 | * The options object should contain the options for the execute call. Its default |
|
266 | 265 | * values are: |
|
267 | 266 | * |
|
268 | 267 | * options = { |
|
269 | 268 | * silent : true, |
|
270 | 269 | * user_variables : [], |
|
271 | 270 | * user_expressions : {}, |
|
272 | 271 | * allow_stdin : false |
|
273 | 272 | * } |
|
274 | 273 | * |
|
275 | 274 | * When calling this method pass a callbacks structure of the form: |
|
276 | 275 | * |
|
277 | 276 | * callbacks = { |
|
278 | 277 | * 'execute_reply': execute_reply_callback, |
|
279 | 278 | * 'output': output_callback, |
|
280 | 279 | * 'clear_output': clear_output_callback, |
|
281 | 280 | * 'set_next_input': set_next_input_callback |
|
282 | 281 | * } |
|
283 | 282 | * |
|
284 | 283 | * The `execute_reply_callback` will be passed the content and metadata |
|
285 | 284 | * objects of the `execute_reply` message documented |
|
286 | 285 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#execute) |
|
287 | 286 | * |
|
288 | 287 | * The `output_callback` will be passed `msg_type` ('stream','display_data','pyout','pyerr') |
|
289 | 288 | * of the output and the content and metadata objects of the PUB/SUB channel that contains the |
|
290 | 289 | * output: |
|
291 | 290 | * |
|
292 | 291 | * http://ipython.org/ipython-doc/dev/development/messaging.html#messages-on-the-pub-sub-socket |
|
293 | 292 | * |
|
294 | 293 | * The `clear_output_callback` will be passed a content object that contains |
|
295 | 294 | * stdout, stderr and other fields that are booleans, as well as the metadata object. |
|
296 | 295 | * |
|
297 | 296 | * The `set_next_input_callback` will be passed the text that should become the next |
|
298 | 297 | * input cell. |
|
299 | 298 | */ |
|
300 | 299 | Kernel.prototype.execute = function (code, callbacks, options) { |
|
301 | 300 | |
|
302 | 301 | var content = { |
|
303 | 302 | code : code, |
|
304 | 303 | silent : true, |
|
305 | 304 | user_variables : [], |
|
306 | 305 | user_expressions : {}, |
|
307 | 306 | allow_stdin : false |
|
308 | 307 | }; |
|
309 | 308 | $.extend(true, content, options) |
|
310 | 309 | $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content}); |
|
311 | 310 | var msg = this._get_msg("execute_request", content); |
|
312 | 311 | this.shell_channel.send(JSON.stringify(msg)); |
|
313 | 312 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
314 | 313 | return msg.header.msg_id; |
|
315 | 314 | }; |
|
316 | 315 | |
|
317 | 316 | /** |
|
318 | 317 | * When calling this method pass a callbacks structure of the form: |
|
319 | 318 | * |
|
320 | 319 | * callbacks = { |
|
321 | 320 | * 'complete_reply': complete_reply_callback |
|
322 | 321 | * } |
|
323 | 322 | * |
|
324 | 323 | * The `complete_reply_callback` will be passed the content object of the |
|
325 | 324 | * `complete_reply` message documented |
|
326 | 325 | * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete) |
|
327 | 326 | * |
|
328 | 327 | * @method complete |
|
329 | 328 | * @param line {integer} |
|
330 | 329 | * @param cursor_pos {integer} |
|
331 | 330 | * @param {dict} callbacks |
|
332 | 331 | * @param callbacks.complete_reply {function} `complete_reply_callback` |
|
333 | 332 | * |
|
334 | 333 | */ |
|
335 | 334 | Kernel.prototype.complete = function (line, cursor_pos, callbacks) { |
|
336 | 335 | callbacks = callbacks || {}; |
|
337 | 336 | var content = { |
|
338 | 337 | text : '', |
|
339 | 338 | line : line, |
|
340 | 339 | cursor_pos : cursor_pos |
|
341 | 340 | }; |
|
342 | 341 | var msg = this._get_msg("complete_request", content); |
|
343 | 342 | this.shell_channel.send(JSON.stringify(msg)); |
|
344 | 343 | this.set_callbacks_for_msg(msg.header.msg_id, callbacks); |
|
345 | 344 | return msg.header.msg_id; |
|
346 | 345 | }; |
|
347 | 346 | |
|
348 | 347 | |
|
349 | 348 | Kernel.prototype.interrupt = function () { |
|
350 | 349 | if (this.running) { |
|
351 | 350 | $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this}); |
|
352 | 351 | $.post(this.kernel_url + "/interrupt"); |
|
353 | 352 | }; |
|
354 | 353 | }; |
|
355 | 354 | |
|
356 | 355 | |
|
357 | 356 | Kernel.prototype.kill = function () { |
|
358 | 357 | if (this.running) { |
|
359 | 358 | this.running = false; |
|
360 | 359 | var settings = { |
|
361 | 360 | cache : false, |
|
362 | 361 | type : "DELETE" |
|
363 | 362 | }; |
|
364 | 363 | $.ajax(this.kernel_url, settings); |
|
365 | 364 | }; |
|
366 | 365 | }; |
|
367 | 366 | |
|
368 | 367 | |
|
369 | 368 | // Reply handlers. |
|
370 | 369 | |
|
371 | 370 | Kernel.prototype.get_callbacks_for_msg = function (msg_id) { |
|
372 | 371 | var callbacks = this._msg_callbacks[msg_id]; |
|
373 | 372 | return callbacks; |
|
374 | 373 | }; |
|
375 | 374 | |
|
376 | 375 | |
|
377 | 376 | Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { |
|
378 | 377 | this._msg_callbacks[msg_id] = callbacks || {}; |
|
379 | 378 | } |
|
380 | 379 | |
|
381 | 380 | |
|
382 | 381 | Kernel.prototype._handle_shell_reply = function (e) { |
|
383 | 382 | reply = $.parseJSON(e.data); |
|
384 | 383 | $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply}); |
|
385 | 384 | var header = reply.header; |
|
386 | 385 | var content = reply.content; |
|
387 | 386 | var metadata = reply.metadata; |
|
388 | 387 | var msg_type = header.msg_type; |
|
389 | 388 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); |
|
390 | 389 | if (callbacks !== undefined) { |
|
391 | 390 | var cb = callbacks[msg_type]; |
|
392 | 391 | if (cb !== undefined) { |
|
393 | 392 | cb(content, metadata); |
|
394 | 393 | } |
|
395 | 394 | }; |
|
396 | 395 | |
|
397 | 396 | if (content.payload !== undefined) { |
|
398 | 397 | var payload = content.payload || []; |
|
399 | 398 | this._handle_payload(callbacks, payload); |
|
400 | 399 | } |
|
401 | 400 | }; |
|
402 | 401 | |
|
403 | 402 | |
|
404 | 403 | Kernel.prototype._handle_payload = function (callbacks, payload) { |
|
405 | 404 | var l = payload.length; |
|
406 | 405 | // Payloads are handled by triggering events because we don't want the Kernel |
|
407 | 406 | // to depend on the Notebook or Pager classes. |
|
408 | 407 | for (var i=0; i<l; i++) { |
|
409 | 408 | if (payload[i].source === 'IPython.zmq.page.page') { |
|
410 | 409 | var data = {'text':payload[i].text} |
|
411 | 410 | $([IPython.events]).trigger('open_with_text.Pager', data); |
|
412 | 411 | } else if (payload[i].source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input') { |
|
413 | 412 | if (callbacks.set_next_input !== undefined) { |
|
414 | 413 | callbacks.set_next_input(payload[i].text) |
|
415 | 414 | } |
|
416 | 415 | } |
|
417 | 416 | }; |
|
418 | 417 | }; |
|
419 | 418 | |
|
420 | 419 | |
|
421 | 420 | Kernel.prototype._handle_iopub_reply = function (e) { |
|
422 | 421 | var reply = $.parseJSON(e.data); |
|
423 | 422 | var content = reply.content; |
|
424 | 423 | var msg_type = reply.header.msg_type; |
|
425 | 424 | var metadata = reply.metadata; |
|
426 | 425 | var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); |
|
427 | 426 | if (msg_type !== 'status' && callbacks === undefined) { |
|
428 | 427 | // Message not from one of this notebook's cells and there are no |
|
429 | 428 | // callbacks to handle it. |
|
430 | 429 | return; |
|
431 | 430 | } |
|
432 | 431 | var output_types = ['stream','display_data','pyout','pyerr']; |
|
433 | 432 | if (output_types.indexOf(msg_type) >= 0) { |
|
434 | 433 | var cb = callbacks['output']; |
|
435 | 434 | if (cb !== undefined) { |
|
436 | 435 | cb(msg_type, content, metadata); |
|
437 | 436 | } |
|
438 | 437 | } else if (msg_type === 'status') { |
|
439 | 438 | if (content.execution_state === 'busy') { |
|
440 | 439 | $([IPython.events]).trigger('status_busy.Kernel', {kernel: this}); |
|
441 | 440 | } else if (content.execution_state === 'idle') { |
|
442 | 441 | $([IPython.events]).trigger('status_idle.Kernel', {kernel: this}); |
|
443 | 442 | } else if (content.execution_state === 'dead') { |
|
444 | 443 | this.stop_channels(); |
|
445 | 444 | $([IPython.events]).trigger('status_dead.Kernel', {kernel: this}); |
|
446 | 445 | }; |
|
447 | 446 | } else if (msg_type === 'clear_output') { |
|
448 | 447 | var cb = callbacks['clear_output']; |
|
449 | 448 | if (cb !== undefined) { |
|
450 | 449 | cb(content, metadata); |
|
451 | 450 | } |
|
452 | 451 | }; |
|
453 | 452 | }; |
|
454 | 453 | |
|
455 | 454 | |
|
456 | 455 | IPython.Kernel = Kernel; |
|
457 | 456 | |
|
458 | 457 | return IPython; |
|
459 | 458 | |
|
460 | 459 | }(IPython)); |
|
461 | 460 |
@@ -1,96 +1,111 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // ToolBar |
|
10 | 10 | //============================================================================ |
|
11 | /** | |
|
12 | * @module IPython | |
|
13 | * @namespace IPython | |
|
14 | * @submodule ToolBar | |
|
15 | */ | |
|
11 | 16 | |
|
12 | 17 | var IPython = (function (IPython) { |
|
13 | 18 | |
|
19 | /** | |
|
20 | * A generic toolbar on which one can add button | |
|
21 | * @class ToolBar | |
|
22 | * @constructor | |
|
23 | * @param {Dom object} selector | |
|
24 | */ | |
|
14 | 25 | var ToolBar = function (selector) { |
|
15 | 26 | this.selector = selector; |
|
16 | 27 | if (this.selector !== undefined) { |
|
17 | 28 | this.element = $(selector); |
|
18 | 29 | this.style(); |
|
19 | 30 | } |
|
20 | 31 | }; |
|
21 | 32 | |
|
22 | // add a group of button into the current toolbar. | |
|
23 | // | |
|
24 | // First argument : Mandatory | |
|
25 | // list of dict as argument, each dict should contain | |
|
26 | // 3 mandatory keys and values : | |
|
27 | // label : string -- the text to show on hover | |
|
28 | // icon : string -- the jQuery-ui icon to add on this button | |
|
29 | // callback : function -- the callback to execute on a click | |
|
30 | // | |
|
31 | // and optionally an 'id' key that is assigned to the button element | |
|
32 | // | |
|
33 | // Second Argument, optional, | |
|
34 | // string reprensenting the id to give to the button group. | |
|
35 | // | |
|
36 | // Example | |
|
37 | // | |
|
38 | // IPython.toolbar.add_button_group([ | |
|
39 | // {label:'my button', | |
|
40 | // icon:'ui-icon-disk', | |
|
41 | // callback:function(){alert('hoho'), | |
|
42 | // id : 'my_button_id', // this is optional | |
|
43 |
|
|
|
44 | // }, | |
|
45 | // {label:'my second button', | |
|
46 | // icon:'ui-icon-scissors', | |
|
47 | // callback:function(){alert('be carefull I cut')} | |
|
48 | // } | |
|
49 | // ], | |
|
50 | // "my_button_group_id" | |
|
51 | // ) | |
|
52 |
|
|
|
33 | /** | |
|
34 | * add a group of button into the current toolbar. | |
|
35 | * | |
|
36 | * | |
|
37 | * @example | |
|
38 | * | |
|
39 | * IPython.toolbar.add_button_group([ | |
|
40 | * { | |
|
41 | * label:'my button', | |
|
42 | * icon:'ui-icon-disk', | |
|
43 | * callback:function(){alert('hoho'), | |
|
44 | * id : 'my_button_id', // this is optional | |
|
45 | * }, | |
|
46 | * { | |
|
47 | * label:'my second button', | |
|
48 | * icon:'ui-icon-scissors', | |
|
49 | * callback:function(){alert('be carefull I cut')} | |
|
50 | * } | |
|
51 | * ], | |
|
52 | * "my_button_group_id" | |
|
53 | * ) | |
|
54 | * | |
|
55 | * @method add_buttons_group | |
|
56 | * @param list {List} | |
|
57 | * List of button of the group, with the following paramter for each : | |
|
58 | * @param list.label {string} text to show on button hover | |
|
59 | * @param list.icon {string} icon to choose from [jQuery ThemeRoller](http://jqueryui.com/themeroller/) | |
|
60 | * @param list.callback {function} function to be called on button click | |
|
61 | * @param [list.id] {String} id to give to the button | |
|
62 | * @param [group_id] {String} optionnal id to give to the group | |
|
63 | * | |
|
64 | */ | |
|
53 | 65 | ToolBar.prototype.add_buttons_group = function (list, group_id) { |
|
54 | 66 | var span_group = $('<span/>'); |
|
55 | 67 | if( group_id != undefined ) { |
|
56 | 68 | span_group.attr('id',group_id); |
|
57 | 69 | } |
|
58 | 70 | for(var el in list) { |
|
59 | 71 | var button = $('<button/>').button({ |
|
60 | 72 | icons : {primary : list[el].icon}, |
|
61 | 73 | text : false, |
|
62 | 74 | label : list[el].label |
|
63 | 75 | }); |
|
64 | 76 | var id = list[el].id; |
|
65 | 77 | if( id != undefined ) |
|
66 | 78 | button.attr('id',id); |
|
67 | 79 | var fun = list[el].callback; |
|
68 | 80 | button.click(fun); |
|
69 | 81 | span_group.append(button); |
|
70 | 82 | } |
|
71 | 83 | span_group.buttonset(); |
|
72 | 84 | $(this.selector).append(span_group); |
|
73 | 85 | }; |
|
74 | 86 | |
|
75 | 87 | ToolBar.prototype.style = function () { |
|
76 | 88 | this.element.addClass('border-box-sizing'). |
|
77 | 89 | addClass('ui-widget ui-widget-content toolbar'). |
|
78 | 90 | css('border-top-style','none'). |
|
79 | 91 | css('border-left-style','none'). |
|
80 | 92 | css('border-right-style','none'); |
|
81 | 93 | }; |
|
82 | 94 | |
|
83 | ||
|
95 | /** | |
|
96 | * Show and hide toolbar | |
|
97 | * @method toggle | |
|
98 | */ | |
|
84 | 99 | ToolBar.prototype.toggle = function () { |
|
85 | 100 | this.element.toggle(); |
|
86 | 101 | if (IPython.layout_manager != undefined) { |
|
87 | 102 | IPython.layout_manager.do_resize(); |
|
88 | 103 | } |
|
89 | 104 | }; |
|
90 | 105 | |
|
91 | 106 | |
|
92 | 107 | IPython.ToolBar = ToolBar; |
|
93 | 108 | |
|
94 | 109 | return IPython; |
|
95 | 110 | |
|
96 | 111 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now