diff --git a/IPython/html/static/notebook/js/comm.js b/IPython/html/static/notebook/js/comm.js new file mode 100644 index 000000000..c141df500 --- /dev/null +++ b/IPython/html/static/notebook/js/comm.js @@ -0,0 +1,150 @@ +//---------------------------------------------------------------------------- +// Copyright (C) 2013 The IPython Development Team +// +// Distributed under the terms of the BSD License. The full license is in +// the file COPYING, distributed as part of this software. +//---------------------------------------------------------------------------- + +//============================================================================ +// Comm and CommManager bases +//============================================================================ +/** + * Base Comm classes + * @module IPython + * @namespace IPython + * @submodule comm + */ + +var IPython = (function (IPython) { + "use strict"; + + //----------------------------------------------------------------------- + // CommManager class + //----------------------------------------------------------------------- + + var CommManager = function (kernel) { + this.comms = {}; + this.targets = {comm : Comm}; + if (kernel !== undefined) { + this.init_kernel(kernel); + } + }; + + CommManager.prototype.init_kernel = function (kernel) { + // connect the kernel, and register message handlers + this.kernel = kernel; + var msg_types = ['comm_open', 'comm_msg', 'comm_close']; + for (var i = 0; i < msg_types.length; i++) { + var msg_type = msg_types[i]; + kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); + } + }; + + CommManager.prototype.register_target = function (target, constructor) { + // Register a constructor for a given target key + this.targets[target] = constructor; + }; + + CommManager.prototype.register_comm = function (comm) { + // Register a comm in the mapping + this.comms[comm.comm_id] = comm; + comm.kernel = this.kernel; + return comm.comm_id; + }; + + CommManager.prototype.unregister_comm = function (comm_id) { + // Remove a comm from the mapping + delete this.comms[comm_id]; + }; + + // comm message handlers + + CommManager.prototype.comm_open = function (msg) { + var content = msg.content; + var callback = this.targets[content.target]; + if (callback === undefined) { + console.log("No such target registered: ", content.target); + console.log("Available targets are: ", this.targets); + return; + } + var comm = new Comm(content.comm_id); + this.register_comm(comm); + callback(comm); + comm.handle_open(content.data); + }; + + CommManager.prototype.comm_close = function (msg) { + var content = msg.content; + var comm = this.comms[content.comm_id]; + if (comm === undefined) { + return; + } + delete this.comms[content.comm_id]; + comm.handle_close(content.data); + }; + + CommManager.prototype.comm_msg = function (msg) { + var content = msg.content; + var comm = this.comms[content.comm_id]; + if (comm === undefined) { + return; + } + comm.handle_msg(content.data); + }; + + //----------------------------------------------------------------------- + // Comm base class + //----------------------------------------------------------------------- + + var Comm = function (comm_id) { + this.comm_id = comm_id; + this.target = 'comm'; + }; + + // methods for sending messages + Comm.prototype.open = function (data) { + var content = { + comm_id : this.comm_id, + target : this.target, + data : data || {}, + }; + this.kernel.send_shell_message("comm_open", content); + }; + + Comm.prototype.send = function (data) { + var content = { + comm_id : this.comm_id, + data : data || {}, + }; + return this.kernel.send_shell_message("comm_msg", content); + }; + + Comm.prototype.close = function (data) { + var content = { + comm_id : this.comm_id, + data : data || {}, + }; + return this.kernel.send_shell_message("comm_close", content); + }; + + // methods for handling incoming messages + + Comm.prototype.handle_open = function (data) { + $([this]).trigger("comm_open", data); + }; + + Comm.prototype.handle_msg = function (data) { + $([this]).trigger("comm_msg", data); + }; + + Comm.prototype.handle_close = function (data) { + $([this]).trigger("comm_close", data); + }; + + IPython.CommManager = CommManager; + IPython.Comm = Comm; + + return IPython; + +}(IPython)); + diff --git a/IPython/html/static/notebook/js/main.js b/IPython/html/static/notebook/js/main.js index 1747c752c..c518af49e 100644 --- a/IPython/html/static/notebook/js/main.js +++ b/IPython/html/static/notebook/js/main.js @@ -68,7 +68,7 @@ function (marked) { IPython.tooltip = new IPython.Tooltip() IPython.notification_area = new IPython.NotificationArea('#notification_area') IPython.notification_area.init_notification_widgets(); - IPython.widget_manager = new IPython.WidgetManager(); + IPython.comm_manager = new IPython.CommManager(); IPython.layout_manager.do_resize(); @@ -95,7 +95,7 @@ function (marked) { } IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval); // only do this once - IPython.widget_manager.init_kernel(IPython.notebook.kernel); + IPython.comm_manager.init_kernel(IPython.notebook.kernel); $([IPython.events]).off('notebook_loaded.Notebook', first_load); }; diff --git a/IPython/html/static/notebook/js/widget.js b/IPython/html/static/notebook/js/widget.js deleted file mode 100644 index 94e2b720f..000000000 --- a/IPython/html/static/notebook/js/widget.js +++ /dev/null @@ -1,148 +0,0 @@ -//---------------------------------------------------------------------------- -// Copyright (C) 2013 The IPython Development Team -// -// Distributed under the terms of the BSD License. The full license is in -// the file COPYING, distributed as part of this software. -//---------------------------------------------------------------------------- - -//============================================================================ -// Widget and WidgetManager bases -//============================================================================ -/** - * Base Widget classes - * @module IPython - * @namespace IPython - * @submodule widget - */ - -var IPython = (function (IPython) { - "use strict"; - - //----------------------------------------------------------------------- - // WidgetManager class - //----------------------------------------------------------------------- - - var WidgetManager = function (kernel) { - this.widgets = {}; - this.widget_types = {widget : Widget}; - if (kernel !== undefined) { - this.init_kernel(kernel); - } - }; - - WidgetManager.prototype.init_kernel = function (kernel) { - // connect the kernel, and register message handlers - this.kernel = kernel; - var msg_types = ['widget_create', 'widget_destroy', 'widget_update']; - for (var i = 0; i < msg_types.length; i++) { - var msg_type = msg_types[i]; - kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this)); - } - }; - - WidgetManager.prototype.register_widget_type = function (widget_type, constructor) { - // Register a constructor for a given widget type name - this.widget_types[widget_type] = constructor; - }; - - WidgetManager.prototype.register_widget = function (widget) { - // Register a widget in the mapping - this.widgets[widget.widget_id] = widget; - widget.kernel = this.kernel; - return widget.widget_id; - }; - - WidgetManager.prototype.unregister_widget = function (widget_id) { - // Remove a widget from the mapping - delete this.widgets[widget_id]; - }; - - // widget message handlers - - WidgetManager.prototype.widget_create = function (msg) { - var content = msg.content; - var constructor = this.widget_types[content.widget_type]; - if (constructor === undefined) { - console.log("No such widget type registered: ", content.widget_type); - console.log("Available widget types are: ", this.widget_types); - return; - } - var widget = new constructor(content.widget_id); - this.register_widget(widget); - widget.handle_create(content.data); - - this.widgets[content.widget_id] = widget; - }; - - WidgetManager.prototype.widget_destroy = function (msg) { - var content = msg.content; - var widget = this.widgets[content.widget_id]; - if (widget === undefined) { - return; - } - delete this.widgets[content.widget_id]; - widget.handle_destroy(content.data); - }; - - WidgetManager.prototype.widget_update = function (msg) { - var content = msg.content; - var widget = this.widgets[content.widget_id]; - if (widget === undefined) { - return; - } - widget.handle_update(content.data); - }; - - //----------------------------------------------------------------------- - // Widget base class - //----------------------------------------------------------------------- - - var Widget = function (widget_id) { - this.widget_id = widget_id; - this.widget_type = 'widget'; - }; - - // methods for sending messages - Widget.prototype.create = function (data) { - var content = { - widget_id : this.widget_id, - widget_type : this.widget_type, - data : data || {}, - }; - this.kernel.send_shell_message("widget_create", content); - }; - - Widget.prototype.update = function (data) { - var content = { - widget_id : this.widget_id, - data : data || {}, - }; - this.kernel.send_shell_message("widget_update", content); - }; - - Widget.prototype.destroy = function (data) { - var content = { - widget_id : this.widget_id, - data : data || {}, - }; - this.kernel.send_shell_message("widget_destroy", content); - }; - - // methods for handling incoming messages - - Widget.prototype.handle_create = function (data) { - }; - - Widget.prototype.handle_update = function (data) { - }; - - Widget.prototype.handle_destroy = function (data) { - }; - - IPython.WidgetManager = WidgetManager; - IPython.Widget = Widget; - - return IPython; - -}(IPython)); - diff --git a/IPython/html/templates/notebook.html b/IPython/html/templates/notebook.html index 212bb9e36..cbe276323 100644 --- a/IPython/html/templates/notebook.html +++ b/IPython/html/templates/notebook.html @@ -253,7 +253,7 @@ class="notebook_app" - +