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