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