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