Show More
@@ -1,218 +1,215 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | "underscore", |
|
5 | "underscore", | |
6 | "backbone", |
|
6 | "backbone", | |
7 | "jquery", |
|
7 | "jquery", | |
8 | "base/js/namespace" |
|
8 | "base/js/namespace" | |
9 | ], function (_, Backbone, $, IPython) { |
|
9 | ], function (_, Backbone, $, IPython) { | |
10 | "use strict"; |
|
10 | "use strict"; | |
11 | //-------------------------------------------------------------------- |
|
11 | //-------------------------------------------------------------------- | |
12 | // WidgetManager class |
|
12 | // WidgetManager class | |
13 | //-------------------------------------------------------------------- |
|
13 | //-------------------------------------------------------------------- | |
14 | var WidgetManager = function (comm_manager, notebook) { |
|
14 | var WidgetManager = function (comm_manager, notebook) { | |
15 | // Public constructor |
|
15 | // Public constructor | |
16 | WidgetManager._managers.push(this); |
|
16 | WidgetManager._managers.push(this); | |
17 |
|
17 | |||
18 | // Attach a comm manager to the |
|
18 | // Attach a comm manager to the | |
19 | this.keyboard_manager = notebook.keyboard_manager; |
|
19 | this.keyboard_manager = notebook.keyboard_manager; | |
20 | this.notebook = notebook; |
|
20 | this.notebook = notebook; | |
21 | this.comm_manager = comm_manager; |
|
21 | this.comm_manager = comm_manager; | |
22 | this._models = {}; /* Dictionary of model ids and model instances */ |
|
22 | this._models = {}; /* Dictionary of model ids and model instances */ | |
23 |
|
23 | |||
24 | // Register already-registered widget model types with the comm manager. |
|
24 | // Register already-registered widget model types with the comm manager. | |
25 | var that = this; |
|
25 | var that = this; | |
26 | _.each(WidgetManager._model_types, function(model_type, model_name) { |
|
26 | _.each(WidgetManager._model_types, function(model_type, model_name) { | |
27 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); |
|
27 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); | |
28 | }); |
|
28 | }); | |
29 | }; |
|
29 | }; | |
30 |
|
30 | |||
31 | //-------------------------------------------------------------------- |
|
31 | //-------------------------------------------------------------------- | |
32 | // Class level |
|
32 | // Class level | |
33 | //-------------------------------------------------------------------- |
|
33 | //-------------------------------------------------------------------- | |
34 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ |
|
34 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ | |
35 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ |
|
35 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ | |
36 | WidgetManager._managers = []; /* List of widget managers */ |
|
36 | WidgetManager._managers = []; /* List of widget managers */ | |
37 |
|
37 | |||
38 | WidgetManager.register_widget_model = function (model_name, model_type) { |
|
38 | WidgetManager.register_widget_model = function (model_name, model_type) { | |
39 | // Registers a widget model by name. |
|
39 | // Registers a widget model by name. | |
40 | WidgetManager._model_types[model_name] = model_type; |
|
40 | WidgetManager._model_types[model_name] = model_type; | |
41 |
|
41 | |||
42 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
42 | // Register the widget with the comm manager. Make sure to pass this object's context | |
43 | // in so `this` works in the call back. |
|
43 | // in so `this` works in the call back. | |
44 | _.each(WidgetManager._managers, function(instance, i) { |
|
44 | _.each(WidgetManager._managers, function(instance, i) { | |
45 | if (instance.comm_manager !== null) { |
|
45 | if (instance.comm_manager !== null) { | |
46 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); |
|
46 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); | |
47 | } |
|
47 | } | |
48 | }); |
|
48 | }); | |
49 | }; |
|
49 | }; | |
50 |
|
50 | |||
51 | WidgetManager.register_widget_view = function (view_name, view_type) { |
|
51 | WidgetManager.register_widget_view = function (view_name, view_type) { | |
52 | // Registers a widget view by name. |
|
52 | // Registers a widget view by name. | |
53 | WidgetManager._view_types[view_name] = view_type; |
|
53 | WidgetManager._view_types[view_name] = view_type; | |
54 | }; |
|
54 | }; | |
55 |
|
55 | |||
56 | //-------------------------------------------------------------------- |
|
56 | //-------------------------------------------------------------------- | |
57 | // Instance level |
|
57 | // Instance level | |
58 | //-------------------------------------------------------------------- |
|
58 | //-------------------------------------------------------------------- | |
59 | WidgetManager.prototype.display_view = function(msg, model) { |
|
59 | WidgetManager.prototype.display_view = function(msg, model) { | |
60 | // Displays a view for a particular model. |
|
60 | // Displays a view for a particular model. | |
61 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
61 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
62 | if (cell === null) { |
|
62 | if (cell === null) { | |
63 | console.log("Could not determine where the display" + |
|
63 | console.log("Could not determine where the display" + | |
64 | " message was from. Widget will not be displayed"); |
|
64 | " message was from. Widget will not be displayed"); | |
65 | } else { |
|
65 | } else { | |
66 | var that = this; |
|
66 | var that = this; | |
67 | this.create_view(model, {cell: cell, callback: function(view) { |
|
67 | this.create_view(model, {cell: cell, callback: function(view) { | |
68 | if (view === null) { |
|
|||
69 | console.error("View creation failed", model); |
|
|||
70 | } |
|
|||
71 | that._handle_display_view(view); |
|
68 | that._handle_display_view(view); | |
72 | if (cell.widget_subarea) { |
|
69 | if (cell.widget_subarea) { | |
73 | cell.widget_subarea.append(view.$el); |
|
70 | cell.widget_subarea.append(view.$el); | |
74 | } |
|
71 | } | |
75 | view.trigger('displayed'); |
|
72 | view.trigger('displayed'); | |
76 | }}); |
|
73 | }}); | |
77 | } |
|
74 | } | |
78 | }; |
|
75 | }; | |
79 |
|
76 | |||
80 | WidgetManager.prototype._handle_display_view = function (view) { |
|
77 | WidgetManager.prototype._handle_display_view = function (view) { | |
81 | // Have the IPython keyboard manager disable its event |
|
78 | // Have the IPython keyboard manager disable its event | |
82 | // handling so the widget can capture keyboard input. |
|
79 | // handling so the widget can capture keyboard input. | |
83 | // Note, this is only done on the outer most widgets. |
|
80 | // Note, this is only done on the outer most widgets. | |
84 | if (this.keyboard_manager) { |
|
81 | if (this.keyboard_manager) { | |
85 | this.keyboard_manager.register_events(view.$el); |
|
82 | this.keyboard_manager.register_events(view.$el); | |
86 |
|
83 | |||
87 | if (view.additional_elements) { |
|
84 | if (view.additional_elements) { | |
88 | for (var i = 0; i < view.additional_elements.length; i++) { |
|
85 | for (var i = 0; i < view.additional_elements.length; i++) { | |
89 | this.keyboard_manager.register_events(view.additional_elements[i]); |
|
86 | this.keyboard_manager.register_events(view.additional_elements[i]); | |
90 | } |
|
87 | } | |
91 | } |
|
88 | } | |
92 | } |
|
89 | } | |
93 | }; |
|
90 | }; | |
94 |
|
91 | |||
95 |
|
92 | |||
96 | WidgetManager.prototype.create_view = function(model, options) { |
|
93 | WidgetManager.prototype.create_view = function(model, options) { | |
97 | // Creates a view for a particular model. |
|
94 | // Creates a view for a particular model. | |
98 |
|
95 | |||
99 | var view_name = model.get('_view_name'); |
|
96 | var view_name = model.get('_view_name'); | |
100 | var view_mod = model.get('_view_module'); |
|
97 | var view_mod = model.get('_view_module'); | |
101 | var errback = options.errback || function(err) {console.log(err);}; |
|
98 | var errback = options.errback || function(err) {console.log(err);}; | |
102 |
|
99 | |||
103 | var instantiate_view = function(ViewType) { |
|
100 | var instantiate_view = function(ViewType) { | |
104 | if (ViewType) { |
|
101 | if (ViewType) { | |
105 | // If a view is passed into the method, use that view's cell as |
|
102 | // If a view is passed into the method, use that view's cell as | |
106 | // the cell for the view that is created. |
|
103 | // the cell for the view that is created. | |
107 | options = options || {}; |
|
104 | options = options || {}; | |
108 | if (options.parent !== undefined) { |
|
105 | if (options.parent !== undefined) { | |
109 | options.cell = options.parent.options.cell; |
|
106 | options.cell = options.parent.options.cell; | |
110 | } |
|
107 | } | |
111 |
|
108 | |||
112 | // Create and render the view... |
|
109 | // Create and render the view... | |
113 | var parameters = {model: model, options: options}; |
|
110 | var parameters = {model: model, options: options}; | |
114 | var view = new ViewType(parameters); |
|
111 | var view = new ViewType(parameters); | |
115 | view.render(); |
|
112 | view.render(); | |
116 | model.on('destroy', view.remove, view); |
|
113 | model.on('destroy', view.remove, view); | |
117 | options.callback(view); |
|
114 | options.callback(view); | |
118 | } else { |
|
115 | } else { | |
119 | errback({unknown_view: true, view_name: view_name, |
|
116 | errback({unknown_view: true, view_name: view_name, | |
120 | view_module: view_mod}); |
|
117 | view_module: view_mod}); | |
121 | } |
|
118 | } | |
122 | }; |
|
119 | }; | |
123 |
|
120 | |||
124 | if (view_mod) { |
|
121 | if (view_mod) { | |
125 | require([view_mod], function(module) { |
|
122 | require([view_mod], function(module) { | |
126 | instantiate_view(module[view_name]); |
|
123 | instantiate_view(module[view_name]); | |
127 | }, errback); |
|
124 | }, errback); | |
128 | } else { |
|
125 | } else { | |
129 | instantiate_view(WidgetManager._view_types[view_name]); |
|
126 | instantiate_view(WidgetManager._view_types[view_name]); | |
130 | } |
|
127 | } | |
131 | }; |
|
128 | }; | |
132 |
|
129 | |||
133 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
130 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
134 | var cell = null; |
|
131 | var cell = null; | |
135 | // First, check to see if the msg was triggered by cell execution. |
|
132 | // First, check to see if the msg was triggered by cell execution. | |
136 | if (this.notebook) { |
|
133 | if (this.notebook) { | |
137 | cell = this.notebook.get_msg_cell(msg_id); |
|
134 | cell = this.notebook.get_msg_cell(msg_id); | |
138 | } |
|
135 | } | |
139 | if (cell !== null) { |
|
136 | if (cell !== null) { | |
140 | return cell; |
|
137 | return cell; | |
141 | } |
|
138 | } | |
142 | // Second, check to see if a get_cell callback was defined |
|
139 | // Second, check to see if a get_cell callback was defined | |
143 | // for the message. get_cell callbacks are registered for |
|
140 | // for the message. get_cell callbacks are registered for | |
144 | // widget messages, so this block is actually checking to see if the |
|
141 | // widget messages, so this block is actually checking to see if the | |
145 | // message was triggered by a widget. |
|
142 | // message was triggered by a widget. | |
146 | var kernel = this.comm_manager.kernel; |
|
143 | var kernel = this.comm_manager.kernel; | |
147 | if (kernel) { |
|
144 | if (kernel) { | |
148 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
145 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
149 | if (callbacks && callbacks.iopub && |
|
146 | if (callbacks && callbacks.iopub && | |
150 | callbacks.iopub.get_cell !== undefined) { |
|
147 | callbacks.iopub.get_cell !== undefined) { | |
151 | return callbacks.iopub.get_cell(); |
|
148 | return callbacks.iopub.get_cell(); | |
152 | } |
|
149 | } | |
153 | } |
|
150 | } | |
154 |
|
151 | |||
155 | // Not triggered by a cell or widget (no get_cell callback |
|
152 | // Not triggered by a cell or widget (no get_cell callback | |
156 | // exists). |
|
153 | // exists). | |
157 | return null; |
|
154 | return null; | |
158 | }; |
|
155 | }; | |
159 |
|
156 | |||
160 | WidgetManager.prototype.callbacks = function (view) { |
|
157 | WidgetManager.prototype.callbacks = function (view) { | |
161 | // callback handlers specific a view |
|
158 | // callback handlers specific a view | |
162 | var callbacks = {}; |
|
159 | var callbacks = {}; | |
163 | if (view && view.options.cell) { |
|
160 | if (view && view.options.cell) { | |
164 |
|
161 | |||
165 | // Try to get output handlers |
|
162 | // Try to get output handlers | |
166 | var cell = view.options.cell; |
|
163 | var cell = view.options.cell; | |
167 | var handle_output = null; |
|
164 | var handle_output = null; | |
168 | var handle_clear_output = null; |
|
165 | var handle_clear_output = null; | |
169 | if (cell.output_area) { |
|
166 | if (cell.output_area) { | |
170 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
167 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
171 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
168 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
172 | } |
|
169 | } | |
173 |
|
170 | |||
174 | // Create callback dict using what is known |
|
171 | // Create callback dict using what is known | |
175 | var that = this; |
|
172 | var that = this; | |
176 | callbacks = { |
|
173 | callbacks = { | |
177 | iopub : { |
|
174 | iopub : { | |
178 | output : handle_output, |
|
175 | output : handle_output, | |
179 | clear_output : handle_clear_output, |
|
176 | clear_output : handle_clear_output, | |
180 |
|
177 | |||
181 | // Special function only registered by widget messages. |
|
178 | // Special function only registered by widget messages. | |
182 | // Allows us to get the cell for a message so we know |
|
179 | // Allows us to get the cell for a message so we know | |
183 | // where to add widgets if the code requires it. |
|
180 | // where to add widgets if the code requires it. | |
184 | get_cell : function () { |
|
181 | get_cell : function () { | |
185 | return cell; |
|
182 | return cell; | |
186 | }, |
|
183 | }, | |
187 | }, |
|
184 | }, | |
188 | }; |
|
185 | }; | |
189 | } |
|
186 | } | |
190 | return callbacks; |
|
187 | return callbacks; | |
191 | }; |
|
188 | }; | |
192 |
|
189 | |||
193 | WidgetManager.prototype.get_model = function (model_id) { |
|
190 | WidgetManager.prototype.get_model = function (model_id) { | |
194 | // Look-up a model instance by its id. |
|
191 | // Look-up a model instance by its id. | |
195 | var model = this._models[model_id]; |
|
192 | var model = this._models[model_id]; | |
196 | if (model !== undefined && model.id == model_id) { |
|
193 | if (model !== undefined && model.id == model_id) { | |
197 | return model; |
|
194 | return model; | |
198 | } |
|
195 | } | |
199 | return null; |
|
196 | return null; | |
200 | }; |
|
197 | }; | |
201 |
|
198 | |||
202 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
199 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
203 | // Handle when a comm is opened. |
|
200 | // Handle when a comm is opened. | |
204 | var that = this; |
|
201 | var that = this; | |
205 | var model_id = comm.comm_id; |
|
202 | var model_id = comm.comm_id; | |
206 | var widget_type_name = msg.content.target_name; |
|
203 | var widget_type_name = msg.content.target_name; | |
207 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); |
|
204 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
208 | widget_model.on('comm:close', function () { |
|
205 | widget_model.on('comm:close', function () { | |
209 | delete that._models[model_id]; |
|
206 | delete that._models[model_id]; | |
210 | }); |
|
207 | }); | |
211 | this._models[model_id] = widget_model; |
|
208 | this._models[model_id] = widget_model; | |
212 | }; |
|
209 | }; | |
213 |
|
210 | |||
214 | // Backwards compatability. |
|
211 | // Backwards compatability. | |
215 | IPython.WidgetManager = WidgetManager; |
|
212 | IPython.WidgetManager = WidgetManager; | |
216 |
|
213 | |||
217 | return {'WidgetManager': WidgetManager}; |
|
214 | return {'WidgetManager': WidgetManager}; | |
218 | }); |
|
215 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now