Show More
@@ -1,275 +1,269 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // WidgetModel, WidgetView, and WidgetManager |
|
9 | // WidgetModel, WidgetView, and WidgetManager | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * Base Widget classes |
|
12 | * Base Widget classes | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule widget |
|
15 | * @submodule widget | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | (function () { |
|
18 | (function () { | |
19 | "use strict"; |
|
19 | "use strict"; | |
20 |
|
20 | |||
21 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
21 | // Use require.js 'define' method so that require.js is intelligent enough to | |
22 | // syncronously load everything within this file when it is being 'required' |
|
22 | // syncronously load everything within this file when it is being 'required' | |
23 | // elsewhere. |
|
23 | // elsewhere. | |
24 | define(["underscore", |
|
24 | define(["underscore", | |
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 |
|
28 | // Backbone.sync method must be in widgetmanager.js file instead of | |
29 | // widget.js so it can be overwritten for different contexts. |
|
29 | // widget.js so it can be overwritten for different contexts. | |
30 | Backbone.sync = function (method, model, options, error) { |
|
30 | Backbone.sync = function (method, model, options, error) { | |
31 | var result = model._handle_sync(method, options); |
|
31 | var result = model._handle_sync(method, options); | |
32 | if (options.success) { |
|
32 | if (options.success) { | |
33 | options.success(result); |
|
33 | options.success(result); | |
34 | } |
|
34 | } | |
35 | }; |
|
35 | }; | |
36 |
|
36 | |||
37 | //-------------------------------------------------------------------- |
|
37 | //-------------------------------------------------------------------- | |
38 | // WidgetManager class |
|
38 | // WidgetManager class | |
39 | //-------------------------------------------------------------------- |
|
39 | //-------------------------------------------------------------------- | |
40 | var WidgetManager = function () { |
|
40 | var WidgetManager = function () { | |
41 | this.comm_manager = null; |
|
41 | this.comm_manager = null; | |
42 | this._model_types = {}; /* Dictionary of model type names |
|
42 | this._model_types = {}; /* Dictionary of model type names | |
43 | (target_name) and model types. */ |
|
43 | (target_name) and model types. */ | |
44 | this._view_types = {}; /* Dictionary of view names and view types. */ |
|
44 | this._view_types = {}; /* Dictionary of view names and view types. */ | |
45 | this._models = {}; /* Dictionary of model ids and model instances */ |
|
45 | this._models = {}; /* Dictionary of model ids and model instances */ | |
46 | }; |
|
46 | }; | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { |
|
49 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { | |
50 | this.comm_manager = comm_manager; |
|
50 | this.comm_manager = comm_manager; | |
51 |
|
51 | |||
52 | // Register already-registered widget model types with the comm manager. |
|
52 | // Register already-registered widget model types with the comm manager. | |
53 | for (var widget_model_name in this._model_types) { |
|
53 | for (var widget_model_name in this._model_types) { | |
54 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
54 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); | |
55 | } |
|
55 | } | |
56 | }; |
|
56 | }; | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
59 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
60 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
60 | // Register the widget with the comm manager. Make sure to pass this object's context | |
61 | // in so `this` works in the call back. |
|
61 | // in so `this` works in the call back. | |
62 | if (this.comm_manager !== null) { |
|
62 | if (this.comm_manager !== null) { | |
63 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
63 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); | |
64 | } |
|
64 | } | |
65 | this._model_types[widget_model_name] = widget_model_type; |
|
65 | this._model_types[widget_model_name] = widget_model_type; | |
66 | }; |
|
66 | }; | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
69 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
70 | this._view_types[widget_view_name] = widget_view_type; |
|
70 | this._view_types[widget_view_name] = widget_view_type; | |
71 | }; |
|
71 | }; | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | WidgetManager.prototype.display_view = function(msg_id, model) { |
|
74 | WidgetManager.prototype.display_view = function(msg_id, model) { | |
75 | var cell = this.get_msg_cell(msg_id); |
|
75 | var cell = this.get_msg_cell(msg_id); | |
76 | if (cell === null) { |
|
76 | if (cell === null) { | |
77 | console.log("Could not determine where the display" + |
|
77 | console.log("Could not determine where the display" + | |
78 | " message was from. Widget will not be displayed"); |
|
78 | " message was from. Widget will not be displayed"); | |
79 | } else { |
|
79 | } else { | |
80 | var view = this.create_view(model); |
|
80 | var view = this.create_view(model); | |
81 | if (view !== undefined |
|
81 | if (view !== undefined | |
82 | && cell.widget_subarea !== undefined |
|
82 | && cell.widget_subarea !== undefined | |
83 | && cell.widget_subarea !== null) { |
|
83 | && cell.widget_subarea !== null) { | |
84 |
|
84 | |||
85 | view.cell = cell; |
|
85 | view.cell = cell; | |
86 | cell.widget_area.show(); |
|
86 | cell.widget_area.show(); | |
87 | cell.widget_subarea.append(view.$el); |
|
87 | cell.widget_subarea.append(view.$el); | |
88 | } |
|
88 | } | |
89 | } |
|
89 | } | |
90 | }, |
|
90 | }, | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | <<<<<<< HEAD |
|
93 | <<<<<<< HEAD | |
94 | <<<<<<< HEAD |
|
94 | <<<<<<< HEAD | |
95 | <<<<<<< HEAD |
|
95 | <<<<<<< HEAD | |
96 | WidgetManager.prototype.create_view = function(model, view_name, cell) { |
|
96 | WidgetManager.prototype.create_view = function(model, view_name, cell) { | |
97 | ======= |
|
97 | ======= | |
98 | WidgetManager.prototype.create_view = function(model, view_name, options) { |
|
98 | WidgetManager.prototype.create_view = function(model, view_name, options) { | |
99 | >>>>>>> Completely remove cell from model and view. |
|
99 | >>>>>>> Completely remove cell from model and view. | |
100 | view_name = view_name || model.get('default_view_name'); |
|
100 | view_name = view_name || model.get('default_view_name'); | |
101 | <<<<<<< HEAD |
|
101 | <<<<<<< HEAD | |
102 | ======= |
|
102 | ======= | |
103 | WidgetManager.prototype.create_view = function(model, view_name, cell, options) { |
|
103 | WidgetManager.prototype.create_view = function(model, view_name, cell, options) { | |
104 | view_name = view_name || model.get('default_view_name'); |
|
104 | view_name = view_name || model.get('default_view_name'); | |
105 | >>>>>>> Add widget view options in creating child views |
|
105 | >>>>>>> Add widget view options in creating child views | |
106 | var ViewType = this.widget_view_types[view_name]; |
|
106 | var ViewType = this.widget_view_types[view_name]; | |
107 | ======= |
|
107 | ======= | |
108 | ======= |
|
108 | ======= | |
109 | WidgetManager.prototype.create_view = function(model, options) { |
|
109 | WidgetManager.prototype.create_view = function(model, options) { | |
110 | var view_name = model.get('view_name'); |
|
110 | var view_name = model.get('view_name'); | |
111 | >>>>>>> remove msg.content.data.view_name and corrosponding create_view param |
|
111 | >>>>>>> remove msg.content.data.view_name and corrosponding create_view param | |
112 | var ViewType = this._view_types[view_name]; |
|
112 | var ViewType = this._view_types[view_name]; | |
113 | >>>>>>> _model_types, _view_types, _models - and document what keys and values are |
|
113 | >>>>>>> _model_types, _view_types, _models - and document what keys and values are | |
114 | if (ViewType !== undefined && ViewType !== null) { |
|
114 | if (ViewType !== undefined && ViewType !== null) { | |
115 | var parameters = {model: model, options: options}; |
|
115 | var parameters = {model: model, options: options}; | |
116 | var view = new ViewType(parameters); |
|
116 | var view = new ViewType(parameters); | |
117 | view.render(); |
|
117 | view.render(); | |
118 | model.views.push(view); |
|
118 | model.views.push(view); | |
119 | model.on('destroy', view.remove, view); |
|
119 | model.on('destroy', view.remove, view); | |
120 | <<<<<<< HEAD |
|
120 | <<<<<<< HEAD | |
121 | <<<<<<< HEAD |
|
121 | <<<<<<< HEAD | |
122 | /* |
|
122 | /* | |
123 | // TODO: handle view deletion. Don't forget to delete child views |
|
123 | // TODO: handle view deletion. Don't forget to delete child views | |
124 | var that = this; |
|
124 | var that = this; | |
125 | view.$el.on("remove", function () { |
|
125 | view.$el.on("remove", function () { | |
126 | var index = that.views.indexOf(view); |
|
126 | var index = that.views.indexOf(view); | |
127 | if (index > -1) { |
|
127 | if (index > -1) { | |
128 | that.views.splice(index, 1); |
|
128 | that.views.splice(index, 1); | |
129 | ======= |
|
129 | ======= | |
130 | /* |
|
130 | /* | |
131 | // TODO: handle view deletion. Don't forget to delete child views |
|
131 | // TODO: handle view deletion. Don't forget to delete child views | |
132 | var that = this; |
|
132 | var that = this; | |
133 | view.$el.on("remove", function () { |
|
133 | view.$el.on("remove", function () { | |
134 | var index = that.views.indexOf(view); |
|
134 | var index = that.views.indexOf(view); | |
135 | if (index > -1) { |
|
135 | if (index > -1) { | |
136 | that.views.splice(index, 1); |
|
136 | that.views.splice(index, 1); | |
137 | } |
|
137 | } | |
138 | view.remove(); // Clean-up view |
|
138 | view.remove(); // Clean-up view | |
139 |
|
139 | |||
140 | // Close the comm if there are no views left. |
|
140 | // Close the comm if there are no views left. | |
141 | if (that.views.length() === 0) { |
|
141 | if (that.views.length() === 0) { | |
142 | //trigger comm close event? |
|
142 | //trigger comm close event? | |
143 | } |
|
143 | } | |
144 |
|
144 | |||
145 |
|
145 | |||
146 | if (that.comm !== undefined) { |
|
146 | if (that.comm !== undefined) { | |
147 | that.comm.close(); |
|
147 | that.comm.close(); | |
148 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
148 | delete that.comm.model; // Delete ref so GC will collect widget model. | |
149 | delete that.comm; |
|
149 | delete that.comm; | |
150 | >>>>>>> Add widget view options in creating child views |
|
150 | >>>>>>> Add widget view options in creating child views | |
151 | } |
|
151 | } | |
152 | view.remove(); // Clean-up view |
|
152 | view.remove(); // Clean-up view | |
153 |
|
153 | |||
154 | // Close the comm if there are no views left. |
|
154 | // Close the comm if there are no views left. | |
155 | if (that.views.length() === 0) { |
|
155 | if (that.views.length() === 0) { | |
156 | //trigger comm close event? |
|
156 | //trigger comm close event? | |
157 | } |
|
157 | } | |
158 |
|
158 | |||
159 |
|
159 | |||
160 | if (that.comm !== undefined) { |
|
160 | if (that.comm !== undefined) { | |
161 | that.comm.close(); |
|
161 | that.comm.close(); | |
162 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
162 | delete that.comm.model; // Delete ref so GC will collect widget model. | |
163 | delete that.comm; |
|
163 | delete that.comm; | |
164 | } |
|
164 | } | |
165 | delete that.model_id; // Delete id from model so widget manager cleans up. |
|
165 | delete that.model_id; // Delete id from model so widget manager cleans up. | |
166 | }); |
|
166 | }); | |
167 | */ |
|
167 | */ | |
168 | ======= |
|
168 | ======= | |
169 | >>>>>>> remove msg.content.data.view_name and corrosponding create_view param |
|
169 | >>>>>>> remove msg.content.data.view_name and corrosponding create_view param | |
170 | return view; |
|
170 | return view; | |
171 | } |
|
171 | } | |
172 | }, |
|
172 | }, | |
173 |
|
173 | |||
174 |
|
174 | |||
175 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
175 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
176 | var cell = null; |
|
176 | var cell = null; | |
177 | // First, check to see if the msg was triggered by cell execution. |
|
177 | // First, check to see if the msg was triggered by cell execution. | |
178 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
178 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
179 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
179 | cell = IPython.notebook.get_msg_cell(msg_id); | |
180 | } |
|
180 | } | |
181 | if (cell !== null) { |
|
181 | if (cell !== null) { | |
182 | return cell |
|
182 | return cell | |
183 | } |
|
183 | } | |
184 | // Second, check to see if a get_cell callback was defined |
|
184 | // Second, check to see if a get_cell callback was defined | |
185 | // for the message. get_cell callbacks are registered for |
|
185 | // for the message. get_cell callbacks are registered for | |
186 | // widget messages, so this block is actually checking to see if the |
|
186 | // widget messages, so this block is actually checking to see if the | |
187 | // message was triggered by a widget. |
|
187 | // message was triggered by a widget. | |
188 |
var kernel = |
|
188 | var kernel = null; | |
|
189 | if (this.comm_manager !== null) { | |||
|
190 | kernel = this.comm_manager.kernel; | |||
|
191 | } | |||
189 | if (kernel !== undefined && kernel !== null) { |
|
192 | if (kernel !== undefined && kernel !== null) { | |
190 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
193 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
191 | if (callbacks !== undefined && |
|
194 | if (callbacks !== undefined && | |
192 | callbacks.iopub !== undefined && |
|
195 | callbacks.iopub !== undefined && | |
193 | callbacks.iopub.get_cell !== undefined) { |
|
196 | callbacks.iopub.get_cell !== undefined) { | |
194 |
|
197 | |||
195 | return callbacks.iopub.get_cell(); |
|
198 | return callbacks.iopub.get_cell(); | |
196 | } |
|
199 | } | |
197 | } |
|
200 | } | |
198 |
|
201 | |||
199 | // Not triggered by a cell or widget (no get_cell callback |
|
202 | // Not triggered by a cell or widget (no get_cell callback | |
200 | // exists). |
|
203 | // exists). | |
201 | return null; |
|
204 | return null; | |
202 | }; |
|
205 | }; | |
203 |
|
206 | |||
204 | WidgetManager.prototype.callbacks = function (view) { |
|
207 | WidgetManager.prototype.callbacks = function (view) { | |
205 | // callback handlers specific a view |
|
208 | // callback handlers specific a view | |
206 | var callbacks = {}; |
|
209 | var callbacks = {}; | |
207 | var cell = view.cell; |
|
210 | var cell = view.cell; | |
208 | if (cell !== null) { |
|
211 | if (cell !== null) { | |
209 | // Try to get output handlers |
|
212 | // Try to get output handlers | |
210 | var handle_output = null; |
|
213 | var handle_output = null; | |
211 | var handle_clear_output = null; |
|
214 | var handle_clear_output = null; | |
212 | if (cell.output_area !== undefined && cell.output_area !== null) { |
|
215 | if (cell.output_area !== undefined && cell.output_area !== null) { | |
213 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
216 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
214 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
217 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
215 | } |
|
218 | } | |
216 |
|
219 | |||
217 | // Create callback dict using what is known |
|
220 | // Create callback dict using what is known | |
218 | var that = this; |
|
221 | var that = this; | |
219 | callbacks = { |
|
222 | callbacks = { | |
220 | iopub : { |
|
223 | iopub : { | |
221 | output : handle_output, |
|
224 | output : handle_output, | |
222 | clear_output : handle_clear_output, |
|
225 | clear_output : handle_clear_output, | |
223 |
|
226 | |||
224 | status : function (msg) { |
|
227 | status : function (msg) { | |
225 | view.model._handle_status(msg, that.callbacks(view)); |
|
228 | view.model._handle_status(msg, that.callbacks(view)); | |
226 | }, |
|
229 | }, | |
227 |
|
230 | |||
228 | // Special function only registered by widget messages. |
|
231 | // Special function only registered by widget messages. | |
229 | // Allows us to get the cell for a message so we know |
|
232 | // Allows us to get the cell for a message so we know | |
230 | // where to add widgets if the code requires it. |
|
233 | // where to add widgets if the code requires it. | |
231 | get_cell : function () { |
|
234 | get_cell : function () { | |
232 | return cell; |
|
235 | return cell; | |
233 | }, |
|
236 | }, | |
234 | }, |
|
237 | }, | |
235 | }; |
|
238 | }; | |
236 | } |
|
239 | } | |
237 | return callbacks; |
|
240 | return callbacks; | |
238 | }; |
|
241 | }; | |
239 |
|
242 | |||
240 |
|
243 | |||
241 | WidgetManager.prototype.get_model = function (model_id) { |
|
244 | WidgetManager.prototype.get_model = function (model_id) { | |
242 | var model = this._models[model_id]; |
|
245 | var model = this._models[model_id]; | |
243 | if (model !== undefined && model.id == model_id) { |
|
246 | if (model !== undefined && model.id == model_id) { | |
244 | return model; |
|
247 | return model; | |
245 | } |
|
248 | } | |
246 | return null; |
|
249 | return null; | |
247 | }; |
|
250 | }; | |
248 |
|
251 | |||
249 |
|
252 | |||
250 | WidgetManager.prototype.get_kernel = function () { |
|
|||
251 | if (this.comm_manager === null) { |
|
|||
252 | return null; |
|
|||
253 | } else { |
|
|||
254 | return this.comm_manager.kernel; |
|
|||
255 | } |
|
|||
256 | }; |
|
|||
257 |
|
||||
258 |
|
||||
259 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
253 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
260 | var widget_type_name = msg.content.target_name; |
|
254 | var widget_type_name = msg.content.target_name; | |
261 | var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm); |
|
255 | var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm); | |
262 | this._models[comm.comm_id] = widget_model; // comm_id == model_id |
|
256 | this._models[comm.comm_id] = widget_model; // comm_id == model_id | |
263 | }; |
|
257 | }; | |
264 |
|
258 | |||
265 | //-------------------------------------------------------------------- |
|
259 | //-------------------------------------------------------------------- | |
266 | // Init code |
|
260 | // Init code | |
267 | //-------------------------------------------------------------------- |
|
261 | //-------------------------------------------------------------------- | |
268 | IPython.WidgetManager = WidgetManager; |
|
262 | IPython.WidgetManager = WidgetManager; | |
269 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { |
|
263 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { | |
270 | IPython.widget_manager = new WidgetManager(); |
|
264 | IPython.widget_manager = new WidgetManager(); | |
271 | } |
|
265 | } | |
272 |
|
266 | |||
273 | return IPython.widget_manager; |
|
267 | return IPython.widget_manager; | |
274 | }); |
|
268 | }); | |
275 | }()); |
|
269 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now