Show More
@@ -1,216 +1,210 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 | //-------------------------------------------------------------------- |
|
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 | && cell.widget_subarea !== null) { |
|
|||
87 |
|
86 | |||
88 | cell.widget_area.show(); |
|
87 | cell.widget_area.show(); | |
89 | cell.widget_subarea.append(view.$el); |
|
88 | cell.widget_subarea.append(view.$el); | |
90 | } |
|
89 | } | |
91 | } |
|
90 | } | |
92 | }; |
|
91 | }; | |
93 |
|
92 | |||
94 | WidgetManager.prototype.create_view = function(model, options, view) { |
|
93 | WidgetManager.prototype.create_view = function(model, options, view) { | |
95 | // Creates a view for a particular model. |
|
94 | // Creates a view for a particular model. | |
96 | var view_name = model.get('_view_name'); |
|
95 | var view_name = model.get('_view_name'); | |
97 | var ViewType = WidgetManager._view_types[view_name]; |
|
96 | var ViewType = WidgetManager._view_types[view_name]; | |
98 |
if (ViewType |
|
97 | if (ViewType) { | |
99 |
|
98 | |||
100 | // If a view is passed into the method, use that view's cell as |
|
99 | // If a view is passed into the method, use that view's cell as | |
101 | // the cell for the view that is created. |
|
100 | // the cell for the view that is created. | |
102 | options = options || {}; |
|
101 | options = options || {}; | |
103 | if (view !== undefined) { |
|
102 | if (view !== undefined) { | |
104 | options.cell = view.options.cell; |
|
103 | options.cell = view.options.cell; | |
105 | } |
|
104 | } | |
106 |
|
105 | |||
107 | // Create and render the view... |
|
106 | // Create and render the view... | |
108 | var parameters = {model: model, options: options}; |
|
107 | var parameters = {model: model, options: options}; | |
109 |
|
|
108 | view = new ViewType(parameters); | |
110 | view.render(); |
|
109 | view.render(); | |
111 | model.views.push(view); |
|
110 | model.views.push(view); | |
112 | model.on('destroy', view.remove, view); |
|
111 | model.on('destroy', view.remove, view); | |
113 |
|
112 | |||
114 | this._handle_new_view(view); |
|
113 | this._handle_new_view(view); | |
115 | return view; |
|
114 | return view; | |
116 | } |
|
115 | } | |
117 | return null; |
|
116 | return null; | |
118 | }; |
|
117 | }; | |
119 |
|
118 | |||
120 | WidgetManager.prototype._handle_new_view = function (view) { |
|
119 | WidgetManager.prototype._handle_new_view = function (view) { | |
121 | // Called when a view has been created and rendered. |
|
120 | // Called when a view has been created and rendered. | |
122 |
|
121 | |||
123 | // If the view has a well defined element, inform the keyboard |
|
122 | // If the view has a well defined element, inform the keyboard | |
124 | // manager about the view's element, so as the element can |
|
123 | // manager about the view's element, so as the element can | |
125 | // escape the dreaded command mode. |
|
124 | // escape the dreaded command mode. | |
126 |
if (view.$el |
|
125 | if (view.$el) { | |
127 | IPython.keyboard_manager.register_events(view.$el); |
|
126 | IPython.keyboard_manager.register_events(view.$el); | |
128 | } |
|
127 | } | |
129 | } |
|
128 | }; | |
130 |
|
129 | |||
131 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
130 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
132 | var cell = null; |
|
131 | var cell = null; | |
133 | // 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. | |
134 |
if (IPython.notebook |
|
133 | if (IPython.notebook) { | |
135 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
134 | cell = IPython.notebook.get_msg_cell(msg_id); | |
136 | } |
|
135 | } | |
137 | if (cell !== null) { |
|
136 | if (cell !== null) { | |
138 | return cell |
|
137 | return cell; | |
139 | } |
|
138 | } | |
140 | // Second, check to see if a get_cell callback was defined |
|
139 | // Second, check to see if a get_cell callback was defined | |
141 | // for the message. get_cell callbacks are registered for |
|
140 | // for the message. get_cell callbacks are registered for | |
142 | // 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 | |
143 | // message was triggered by a widget. |
|
142 | // message was triggered by a widget. | |
144 | var kernel = this.comm_manager.kernel; |
|
143 | var kernel = this.comm_manager.kernel; | |
145 |
if (kernel |
|
144 | if (kernel) { | |
146 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
145 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
147 |
if (callbacks |
|
146 | if (callbacks && callbacks.iopub && | |
148 | callbacks.iopub !== undefined && |
|
|||
149 | callbacks.iopub.get_cell !== undefined) { |
|
147 | callbacks.iopub.get_cell !== undefined) { | |
150 |
|
||||
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 !== undefined && |
|
160 | if (view && view.options.cell) { | |
164 | view !== null && |
|
|||
165 | view.options.cell !== undefined && |
|
|||
166 | view.options.cell !== null) { |
|
|||
167 |
|
161 | |||
168 | // Try to get output handlers |
|
162 | // Try to get output handlers | |
169 | var cell = view.options.cell; |
|
163 | var cell = view.options.cell; | |
170 | var handle_output = null; |
|
164 | var handle_output = null; | |
171 | var handle_clear_output = null; |
|
165 | var handle_clear_output = null; | |
172 |
if (cell.output_area |
|
166 | if (cell.output_area) { | |
173 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
167 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
174 | 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); | |
175 | } |
|
169 | } | |
176 |
|
170 | |||
177 | // Create callback dict using what is known |
|
171 | // Create callback dict using what is known | |
178 | var that = this; |
|
172 | var that = this; | |
179 | callbacks = { |
|
173 | callbacks = { | |
180 | iopub : { |
|
174 | iopub : { | |
181 | output : handle_output, |
|
175 | output : handle_output, | |
182 | clear_output : handle_clear_output, |
|
176 | clear_output : handle_clear_output, | |
183 |
|
177 | |||
184 | // Special function only registered by widget messages. |
|
178 | // Special function only registered by widget messages. | |
185 | // 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 | |
186 | // where to add widgets if the code requires it. |
|
180 | // where to add widgets if the code requires it. | |
187 | get_cell : function () { |
|
181 | get_cell : function () { | |
188 | return cell; |
|
182 | return cell; | |
189 | }, |
|
183 | }, | |
190 | }, |
|
184 | }, | |
191 | }; |
|
185 | }; | |
192 | } |
|
186 | } | |
193 | return callbacks; |
|
187 | return callbacks; | |
194 | }; |
|
188 | }; | |
195 |
|
189 | |||
196 | WidgetManager.prototype.get_model = function (model_id) { |
|
190 | WidgetManager.prototype.get_model = function (model_id) { | |
197 | // Look-up a model instance by its id. |
|
191 | // Look-up a model instance by its id. | |
198 | var model = this._models[model_id]; |
|
192 | var model = this._models[model_id]; | |
199 | if (model !== undefined && model.id == model_id) { |
|
193 | if (model !== undefined && model.id == model_id) { | |
200 | return model; |
|
194 | return model; | |
201 | } |
|
195 | } | |
202 | return null; |
|
196 | return null; | |
203 | }; |
|
197 | }; | |
204 |
|
198 | |||
205 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
199 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
206 | // Handle when a comm is opened. |
|
200 | // Handle when a comm is opened. | |
207 | var model_id = comm.comm_id; |
|
201 | var model_id = comm.comm_id; | |
208 | var widget_type_name = msg.content.target_name; |
|
202 | var widget_type_name = msg.content.target_name; | |
209 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); |
|
203 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
210 | this._models[model_id] = widget_model; |
|
204 | this._models[model_id] = widget_model; | |
211 | }; |
|
205 | }; | |
212 |
|
206 | |||
213 | IPython.WidgetManager = WidgetManager; |
|
207 | IPython.WidgetManager = WidgetManager; | |
214 | return IPython.WidgetManager; |
|
208 | return IPython.WidgetManager; | |
215 | }); |
|
209 | }); | |
216 | }()); |
|
210 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now