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