From 14fcdab73bc5d3e0ba440822fb6922e4e6322fd1 Mon Sep 17 00:00:00 2001 From: MinRK Date: Wed, 11 Sep 2013 15:04:45 -0700 Subject: [PATCH] make js / Python widgets symmetrical don't enforce creation on Kernel side also removed weak refs - premature optimization, we can think about this later. --- IPython/html/static/notebook/js/widget.js | 73 ++++++++++++++++------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/IPython/html/static/notebook/js/widget.js b/IPython/html/static/notebook/js/widget.js index e7c667d4d..94e2b720f 100644 --- a/IPython/html/static/notebook/js/widget.js +++ b/IPython/html/static/notebook/js/widget.js @@ -31,6 +31,7 @@ var IPython = (function (IPython) { }; 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++) { @@ -44,6 +45,20 @@ var IPython = (function (IPython) { 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]; @@ -52,7 +67,10 @@ var IPython = (function (IPython) { console.log("Available widget types are: ", this.widget_types); return; } - var widget = new constructor(this.kernel, content); + var widget = new constructor(content.widget_id); + this.register_widget(widget); + widget.handle_create(content.data); + this.widgets[content.widget_id] = widget; }; @@ -79,13 +97,39 @@ var IPython = (function (IPython) { // Widget base class //----------------------------------------------------------------------- - var Widget = function (kernel, content) { - this.kernel = kernel; - if (!content) return; - this.widget_id = content.widget_id; - this.handle_create(content.data); + 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) { }; @@ -95,23 +139,6 @@ var IPython = (function (IPython) { Widget.prototype.handle_destroy = function (data) { }; - 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); - }; - IPython.WidgetManager = WidgetManager; IPython.Widget = Widget;