##// END OF EJS Templates
Added support for multiple kernels.
Jonathan Frederic -
Show More
@@ -25,53 +25,67
25 25 "backbone",
26 26 ], function (underscore, backbone) {
27 27
28 // Backbone.sync method must be in widgetmanager.js file instead of
29 // widget.js so it can be overwritten for different contexts.
30 28 Backbone.sync = function (method, model, options) {
29 // Sync widget models to back-end.
30 //
31 // Backbone.sync method must be in widgetmanager.js file instead of
32 // widget.js so it can be overwritten for different contexts.
31 33 var result = model._handle_sync(method, options);
32 34 if (options.success) {
33 35 options.success(result);
34 36 }
35 37 };
36 38
39
37 40 //--------------------------------------------------------------------
38 41 // WidgetManager class
39 42 //--------------------------------------------------------------------
40 var WidgetManager = function () {
41 this.comm_manager = null;
42 this._model_types = {}; /* Dictionary of model type names
43 (target_name) and model types. */
44 this._view_types = {}; /* Dictionary of view names and view types. */
45 this._models = {}; /* Dictionary of model ids and model instances */
46 };
47
43 var WidgetManager = function (comm_manager) {
44 // Public constructor
45 WidgetManager._managers.push(this);
48 46
49 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
47 // Attach a comm manager to the
50 48 this.comm_manager = comm_manager;
51 49
52 50 // Register already-registered widget model types with the comm manager.
53 for (var widget_model_name in this._model_types) {
54 // TODO: Should not be a for.
55 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
51 for (var name in WidgetManager._model_types) {
52 if (WidgetManager._model_types.hasOwnProperty(name)) {
53 this.comm_manager.register_target(name, $.proxy(this._handle_comm_open, this));
54
55 }
56 56 }
57 57 };
58 58
59 //--------------------------------------------------------------------
60 // Class level
61 //--------------------------------------------------------------------
62 WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
63 WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
64 WidgetManager._models = {}; /* Dictionary of model ids and model instances */
65 WidgetManager._managers = []; /* List of widget managers */
66
67 WidgetManager.register_widget_model = function (model_name, model_type) {
68 // Registers a widget model by name.
69 WidgetManager._model_types[model_name] = model_type;
59 70
60 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
61 71 // Register the widget with the comm manager. Make sure to pass this object's context
62 72 // in so `this` works in the call back.
63 if (this.comm_manager !== null) {
64 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
73 for (var i = 0; i < WidgetManager._managers.length; i++) {
74 var instance = WidgetManager._managers[i];
75 if (instance.comm_manager !== null) {
76 instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
77 }
65 78 }
66 this._model_types[widget_model_name] = widget_model_type;
67 79 };
68 80
69
70 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
71 this._view_types[widget_view_name] = widget_view_type;
81 WidgetManager.register_widget_view = function (view_name, view_type) {
82 // Registers a widget view by name.
83 WidgetManager._view_types[view_name] = view_type;
72 84 };
73 85
74
86 //--------------------------------------------------------------------
87 // Instance level
88 //--------------------------------------------------------------------
75 89 WidgetManager.prototype.display_view = function(msg, model) {
76 90 var cell = this.get_msg_cell(msg.parent_header.msg_id);
77 91 if (cell === null) {
@@ -91,10 +105,9
91 105 }
92 106 },
93 107
94
95 108 WidgetManager.prototype.create_view = function(model, options) {
96 109 var view_name = model.get('view_name');
97 var ViewType = this._view_types[view_name];
110 var ViewType = WidgetManager._view_types[view_name];
98 111 if (ViewType !== undefined && ViewType !== null) {
99 112 var parameters = {model: model, options: options};
100 113 var view = new ViewType(parameters);
@@ -106,7 +119,6
106 119 }
107 120 },
108 121
109
110 122 WidgetManager.prototype.get_msg_cell = function (msg_id) {
111 123 var cell = null;
112 124 // First, check to see if the msg was triggered by cell execution.
@@ -120,10 +132,7
120 132 // for the message. get_cell callbacks are registered for
121 133 // widget messages, so this block is actually checking to see if the
122 134 // message was triggered by a widget.
123 var kernel = null;
124 if (this.comm_manager !== null) {
125 kernel = this.comm_manager.kernel;
126 }
135 var kernel = this.comm_manager.kernel;
127 136 if (kernel !== undefined && kernel !== null) {
128 137 var callbacks = kernel.get_callbacks_for_msg(msg_id);
129 138 if (callbacks !== undefined &&
@@ -175,30 +184,22
175 184 return callbacks;
176 185 };
177 186
178
179 187 WidgetManager.prototype.get_model = function (model_id) {
180 var model = this._models[model_id];
188 var model = WidgetManager._models[model_id];
181 189 if (model !== undefined && model.id == model_id) {
182 190 return model;
183 191 }
184 192 return null;
185 193 };
186 194
187
188 195 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
196 var model_id = comm.comm_id;
189 197 var widget_type_name = msg.content.target_name;
190 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
191 this._models[comm.comm_id] = widget_model; // comm_id == model_id
198 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
199 WidgetManager._models[model_id] = widget_model;
192 200 };
193 201
194 //--------------------------------------------------------------------
195 // Init code
196 //--------------------------------------------------------------------
197 202 IPython.WidgetManager = WidgetManager;
198 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
199 IPython.widget_manager = new WidgetManager();
200 }
201
202 return IPython.widget_manager;
203 return IPython.WidgetManager;
203 204 });
204 205 }());
@@ -47,9 +47,7 var IPython = (function (IPython) {
47 47 this.bind_events();
48 48 this.init_iopub_handlers();
49 49 this.comm_manager = new IPython.CommManager(this);
50 // TODO: make the comm manager an arg to the widget manager initialization
51 this.widget_manager = new IPython.WidgetManager();
52 this.widget_manager.attach_comm_manager(this.comm_manager);
50 this.widget_manager = new IPython.WidgetManager(this.comm_manager);
53 51 };
54 52
55 53
General Comments 0
You need to be logged in to leave comments. Login now