From d99e25f245251ceecdd999417359515c7b35ec36 Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 30 Sep 2013 17:24:11 -0700 Subject: [PATCH] refactor js callbacks all callbacks get the whole message --- IPython/html/static/notebook/js/codecell.js | 36 ++++--- IPython/html/static/notebook/js/completer.js | 9 +- IPython/html/static/notebook/js/outputarea.js | 12 ++- IPython/html/static/notebook/js/tooltip.js | 23 ++-- .../html/static/services/kernels/js/kernel.js | 100 +++++++++++------- 5 files changed, 109 insertions(+), 71 deletions(-) diff --git a/IPython/html/static/notebook/js/codecell.js b/IPython/html/static/notebook/js/codecell.js index b823fa2de..95d62699f 100644 --- a/IPython/html/static/notebook/js/codecell.js +++ b/IPython/html/static/notebook/js/codecell.js @@ -244,12 +244,22 @@ var IPython = (function (IPython) { this.set_input_prompt('*'); this.element.addClass("running"); var callbacks = { - 'execute_reply': $.proxy(this._handle_execute_reply, this), - 'output': $.proxy(this.output_area.handle_output, this.output_area), - 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), - 'set_next_input': $.proxy(this._handle_set_next_input, this), - 'input_request': $.proxy(this._handle_input_request, this) - }; + shell : { + reply : $.proxy(this._handle_execute_reply, this), + payload : { + set_next_input : $.proxy(this._handle_set_next_input, this), + page : function (payload) { + $([IPython.events]).trigger('open_with_text.Pager', payload); + } + } + }, + iopub : { + output : $.proxy(this.output_area.handle_output, this.output_area), + clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area), + }, + input : $.proxy(this._handle_input_request, this) + } + this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); }; @@ -257,8 +267,8 @@ var IPython = (function (IPython) { * @method _handle_execute_reply * @private */ - CodeCell.prototype._handle_execute_reply = function (content) { - this.set_input_prompt(content.execution_count); + CodeCell.prototype._handle_execute_reply = function (msg) { + this.set_input_prompt(msg.content.execution_count); this.element.removeClass("running"); $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); } @@ -267,8 +277,8 @@ var IPython = (function (IPython) { * @method _handle_set_next_input * @private */ - CodeCell.prototype._handle_set_next_input = function (text) { - var data = {'cell': this, 'text': text} + CodeCell.prototype._handle_set_next_input = function (payload) { + var data = {'cell': this, 'text': payload.text} $([IPython.events]).trigger('set_next_input.Notebook', data); } @@ -276,8 +286,8 @@ var IPython = (function (IPython) { * @method _handle_input_request * @private */ - CodeCell.prototype._handle_input_request = function (content) { - this.output_area.append_raw_input(content); + CodeCell.prototype._handle_input_request = function (msg) { + this.output_area.append_raw_input(msg); } @@ -438,4 +448,4 @@ var IPython = (function (IPython) { IPython.CodeCell = CodeCell; return IPython; -}(IPython)); \ No newline at end of file +}(IPython)); diff --git a/IPython/html/static/notebook/js/completer.js b/IPython/html/static/notebook/js/completer.js index 992c87f24..b2b77b529 100644 --- a/IPython/html/static/notebook/js/completer.js +++ b/IPython/html/static/notebook/js/completer.js @@ -150,16 +150,17 @@ var IPython = (function (IPython) { matched_text: "" }) } else { - var callbacks = { - 'complete_reply': $.proxy(this.finish_completing, this) - }; + var callbacks = { shell : { + reply: $.proxy(this.finish_completing, this) + }}; this.cell.kernel.complete(line, cur.ch, callbacks); } }; - Completer.prototype.finish_completing = function (content) { + Completer.prototype.finish_completing = function (msg) { // let's build a function that wrap all that stuff into what is needed // for the new completer: + var content = msg.content; var matched_text = content.matched_text; var matches = content.matches; diff --git a/IPython/html/static/notebook/js/outputarea.js b/IPython/html/static/notebook/js/outputarea.js index 330700449..91886df25 100644 --- a/IPython/html/static/notebook/js/outputarea.js +++ b/IPython/html/static/notebook/js/outputarea.js @@ -231,9 +231,10 @@ var IPython = (function (IPython) { }; - OutputArea.prototype.handle_output = function (msg_type, content) { + OutputArea.prototype.handle_output = function (msg) { var json = {}; - json.output_type = msg_type; + var msg_type = json.output_type = msg.header.msg_type; + var content = msg.content; if (msg_type === "stream") { json.text = content.data; json.stream = content.name; @@ -564,9 +565,10 @@ var IPython = (function (IPython) { element.append(toinsert); }; - OutputArea.prototype.append_raw_input = function (content) { + OutputArea.prototype.append_raw_input = function (msg) { var that = this; this.expand(); + var content = msg.content; var area = this.create_output_area(); // disable any other raw_inputs, if they are left around @@ -618,8 +620,8 @@ var IPython = (function (IPython) { } - OutputArea.prototype.handle_clear_output = function (content) { - this.clear_output(content.wait); + OutputArea.prototype.handle_clear_output = function (msg) { + this.clear_output(msg.content.wait); }; diff --git a/IPython/html/static/notebook/js/tooltip.js b/IPython/html/static/notebook/js/tooltip.js index 9c3679e78..ca5788075 100644 --- a/IPython/html/static/notebook/js/tooltip.js +++ b/IPython/html/static/notebook/js/tooltip.js @@ -222,12 +222,12 @@ var IPython = (function (IPython) { Tooltip.prototype._request_tooltip = function (cell, line) { - var callbacks = { - 'object_info_reply': $.proxy(this._show, this) - } + var callbacks = { shell : { + reply : $.proxy(this._show, this) + }}; var oir_token = this.extract_oir_token(line); var msg_id = cell.kernel.object_info_request(oir_token, callbacks); - } + }; // make an imediate completion request Tooltip.prototype.request = function (cell, hide_if_no_docstring) { @@ -301,7 +301,8 @@ var IPython = (function (IPython) { Tooltip.prototype._show = function (reply) { // move the bubble if it is not hidden // otherwise fade it - this.name = reply.name; + var content = reply.content; + this.name = content.name; // do some math to have the tooltip arrow on more or less on left or right // width of the editor @@ -334,20 +335,20 @@ var IPython = (function (IPython) { }); // build docstring - var defstring = reply.call_def; + var defstring = content.call_def; if (defstring == null) { - defstring = reply.init_definition; + defstring = content.init_definition; } if (defstring == null) { - defstring = reply.definition; + defstring = content.definition; } - var docstring = reply.call_docstring; + var docstring = content.call_docstring; if (docstring == null) { - docstring = reply.init_docstring; + docstring = content.init_docstring; } if (docstring == null) { - docstring = reply.docstring; + docstring = content.docstring; } if (docstring == null) { diff --git a/IPython/html/static/services/kernels/js/kernel.js b/IPython/html/static/services/kernels/js/kernel.js index 9a1088046..a8539fb34 100644 --- a/IPython/html/static/services/kernels/js/kernel.js +++ b/IPython/html/static/services/kernels/js/kernel.js @@ -239,7 +239,7 @@ var IPython = (function (IPython) { this.shell_channel.send(JSON.stringify(msg)); this.set_callbacks_for_msg(msg.header.msg_id, callbacks); return msg.header.msg_id; - } + }; /** * Get info on object asynchronoulsy @@ -340,7 +340,7 @@ var IPython = (function (IPython) { allow_stdin : false }; callbacks = callbacks || {}; - if (callbacks.input_request !== undefined) { + if (callbacks.input !== undefined) { content.allow_stdin = true; } $.extend(true, content, options); @@ -431,11 +431,22 @@ var IPython = (function (IPython) { delete this._msg_callbacks[msg_id]; } }; - - + + /* Set callbacks for a particular message. + * Callbacks should be a struct of the following form: + * shell : { + * + * } + + */ Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) { if (callbacks) { - this._msg_callbacks[msg_id] = callbacks; + // shallow-copy mapping, because we will modify it at the top level + var cbcopy = this._msg_callbacks[msg_id] = {}; + cbcopy.shell = callbacks.shell; + cbcopy.iopub = callbacks.iopub; + cbcopy.input = callbacks.input; + this._msg_callbacks[msg_id] = cbcopy; } }; @@ -443,37 +454,40 @@ var IPython = (function (IPython) { Kernel.prototype._handle_shell_reply = function (e) { var reply = $.parseJSON(e.data); $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply}); - var header = reply.header; var content = reply.content; var metadata = reply.metadata; - var msg_type = header.msg_type; - var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id); - if (callbacks !== undefined) { - var cb = callbacks[msg_type]; - if (cb !== undefined) { - cb(content, metadata); - } + var parent_id = reply.parent_header.msg_id; + var callbacks = this.get_callbacks_for_msg(parent_id); + if (!callbacks || !callbacks.shell) { + return; } - - if (content.payload !== undefined) { - var payload = content.payload || []; - this._handle_payload(callbacks, payload); + var shell_callbacks = callbacks.shell; + + // clear callbacks on shell + delete callbacks.shell; + delete callbacks.input; + if (!callbacks.iopub) { + this.clear_callbacks_for_msg(parent_id); + } + + if (shell_callbacks.reply !== undefined) { + shell_callbacks.reply(reply); + } + if (content.payload && shell_callbacks.payload) { + this._handle_payloads(content.payload, shell_callbacks.payload, reply); } }; - Kernel.prototype._handle_payload = function (callbacks, payload) { - var l = payload.length; + Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) { + var l = payloads.length; // Payloads are handled by triggering events because we don't want the Kernel // to depend on the Notebook or Pager classes. for (var i=0; i