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