Show More
@@ -1,210 +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 () { |
|
31 | var WidgetManager = function () { | |
32 | this.comm_manager = null; |
|
32 | this.comm_manager = null; | |
33 | this.widget_model_types = {}; |
|
33 | this.widget_model_types = {}; | |
34 | this.widget_view_types = {}; |
|
34 | this.widget_view_types = {}; | |
35 | this._model_instances = {}; |
|
35 | this._model_instances = {}; | |
36 |
|
36 | |||
37 | Backbone.sync = function (method, model, options, error) { |
|
37 | Backbone.sync = function (method, model, options, error) { | |
38 | var result = model._handle_sync(method, options); |
|
38 | var result = model._handle_sync(method, options); | |
39 | if (options.success) { |
|
39 | if (options.success) { | |
40 | options.success(result); |
|
40 | options.success(result); | |
41 | } |
|
41 | } | |
42 | }; |
|
42 | }; | |
43 | }; |
|
43 | }; | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { |
|
46 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { | |
47 | this.comm_manager = comm_manager; |
|
47 | this.comm_manager = comm_manager; | |
48 |
|
48 | |||
49 | // Register already-registered widget model types with the comm manager. |
|
49 | // Register already-registered widget model types with the comm manager. | |
50 | for (var widget_model_name in this.widget_model_types) { |
|
50 | for (var widget_model_name in this.widget_model_types) { | |
51 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
51 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); | |
52 | } |
|
52 | } | |
53 | }; |
|
53 | }; | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
56 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
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 | if (this.comm_manager !== null) { |
|
59 | if (this.comm_manager !== null) { | |
60 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
60 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); | |
61 | } |
|
61 | } | |
62 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
62 | this.widget_model_types[widget_model_name] = widget_model_type; | |
63 | }; |
|
63 | }; | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
66 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
67 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
67 | this.widget_view_types[widget_view_name] = widget_view_type; | |
68 | }; |
|
68 | }; | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | WidgetManager.prototype.handle_msg = function(msg, model) { |
|
71 | WidgetManager.prototype.handle_msg = function(msg, model) { | |
72 | var method = msg.content.data.method; |
|
72 | var method = msg.content.data.method; | |
73 | switch (method) { |
|
73 | switch (method) { | |
74 | case 'display': |
|
74 | case 'display': | |
75 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
75 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
76 | if (cell === null) { |
|
76 | if (cell === null) { | |
77 | console.log("Could not determine where the display" + |
|
77 | console.log("Could not determine where the display" + | |
78 | " message was from. Widget will not be displayed"); |
|
78 | " message was from. Widget will not be displayed"); | |
79 | } else { |
|
79 | } else { | |
80 | var view = this.create_view(model, |
|
80 | var view = this.create_view(model, msg.content.data.view_name, cell); | |
81 | msg.content.data.view_name, cell); |
|
81 | if (view !== undefined | |
82 |
|
|
82 | && cell.widget_subarea !== undefined | |
83 |
&& cell.widget_subarea !== |
|
83 | && cell.widget_subarea !== null) { | |
84 | && cell.widget_subarea !== null) { |
|
84 | ||
85 |
|
|
85 | cell.widget_area.show(); | |
86 |
|
|
86 | cell.widget_subarea.append(view.$el); | |
87 | } |
|
87 | } | |
88 | } |
|
88 | } | |
89 | break; |
|
89 | break; | |
90 | } |
|
90 | } | |
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 | WidgetManager.prototype.create_view = function(model, view_name, cell) { |
|
93 | WidgetManager.prototype.create_view = function(model, view_name, cell) { | |
94 | view_name = view_name || model.get('default_view_name'); |
|
94 | view_name = view_name || model.get('default_view_name'); | |
95 | var ViewType = this.widget_view_types[view_name]; |
|
95 | var ViewType = this.widget_view_types[view_name]; | |
96 | if (ViewType !== undefined && ViewType !== null) { |
|
96 | if (ViewType !== undefined && ViewType !== null) { | |
97 | var view = new ViewType({model: model, widget_manager: this, cell: cell}); |
|
97 | var view = new ViewType({model: model, widget_manager: this, cell: cell}); | |
98 | view.render(); |
|
98 | view.render(); | |
99 | model.views.push(view); |
|
99 | model.views.push(view); | |
100 | model.on('destroy', view.remove, view); |
|
100 | model.on('destroy', view.remove, view); | |
101 | /* |
|
101 | /* | |
102 | // TODO: handle view deletion. Don't forget to delete child views |
|
102 | // TODO: handle view deletion. Don't forget to delete child views | |
103 | var that = this; |
|
103 | var that = this; | |
104 | view.$el.on("remove", function () { |
|
104 | view.$el.on("remove", function () { | |
105 | var index = that.views.indexOf(view); |
|
105 | var index = that.views.indexOf(view); | |
106 | if (index > -1) { |
|
106 | if (index > -1) { | |
107 | that.views.splice(index, 1); |
|
107 | that.views.splice(index, 1); | |
108 | } |
|
108 | } | |
109 | view.remove(); // Clean-up view |
|
109 | view.remove(); // Clean-up view | |
110 |
|
110 | |||
111 | // Close the comm if there are no views left. |
|
111 | // Close the comm if there are no views left. | |
112 | if (that.views.length() === 0) { |
|
112 | if (that.views.length() === 0) { | |
113 | //trigger comm close event? |
|
113 | //trigger comm close event? | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | if (that.comm !== undefined) { |
|
117 | if (that.comm !== undefined) { | |
118 | that.comm.close(); |
|
118 | that.comm.close(); | |
119 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
119 | delete that.comm.model; // Delete ref so GC will collect widget model. | |
120 | delete that.comm; |
|
120 | delete that.comm; | |
121 | } |
|
121 | } | |
122 | delete that.model_id; // Delete id from model so widget manager cleans up. |
|
122 | delete that.model_id; // Delete id from model so widget manager cleans up. | |
123 | }); |
|
123 | }); | |
124 | */ |
|
124 | */ | |
125 | return view; |
|
125 | return view; | |
126 | } |
|
126 | } | |
127 | }, |
|
127 | }, | |
128 |
|
128 | |||
129 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
129 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
130 | var cell = null; |
|
130 | var cell = null; | |
131 | // First, check to see if the msg was triggered by cell execution. |
|
131 | // First, check to see if the msg was triggered by cell execution. | |
132 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
132 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
133 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
133 | cell = IPython.notebook.get_msg_cell(msg_id); | |
134 | } |
|
134 | } | |
135 | if (cell !== null) { |
|
135 | if (cell !== null) { | |
136 | return cell |
|
136 | return cell | |
137 | } |
|
137 | } | |
138 | // Second, check to see if a get_cell callback was defined |
|
138 | // Second, check to see if a get_cell callback was defined | |
139 | // for the message. get_cell callbacks are registered for |
|
139 | // for the message. get_cell callbacks are registered for | |
140 | // widget messages, so this block is actually checking to see if the |
|
140 | // widget messages, so this block is actually checking to see if the | |
141 | // message was triggered by a widget. |
|
141 | // message was triggered by a widget. | |
142 | var kernel = this.get_kernel(); |
|
142 | var kernel = this.get_kernel(); | |
143 | if (kernel !== undefined && kernel !== null) { |
|
143 | if (kernel !== undefined && kernel !== null) { | |
144 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
144 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
145 | if (callbacks !== undefined && |
|
145 | if (callbacks !== undefined && | |
146 | callbacks.iopub !== undefined && |
|
146 | callbacks.iopub !== undefined && | |
147 | callbacks.iopub.get_cell !== undefined) { |
|
147 | callbacks.iopub.get_cell !== undefined) { | |
148 |
|
148 | |||
149 | return callbacks.iopub.get_cell(); |
|
149 | return callbacks.iopub.get_cell(); | |
150 | } |
|
150 | } | |
151 | } |
|
151 | } | |
152 |
|
152 | |||
153 | // Not triggered by a cell or widget (no get_cell callback |
|
153 | // Not triggered by a cell or widget (no get_cell callback | |
154 | // exists). |
|
154 | // exists). | |
155 | return null; |
|
155 | return null; | |
156 | }; |
|
156 | }; | |
157 |
|
157 | |||
158 |
|
158 | |||
159 | WidgetManager.prototype.get_model = function (model_id) { |
|
159 | WidgetManager.prototype.get_model = function (model_id) { | |
160 | var model = this._model_instances[model_id]; |
|
160 | var model = this._model_instances[model_id]; | |
161 | if (model !== undefined && model.id == model_id) { |
|
161 | if (model !== undefined && model.id == model_id) { | |
162 | return model; |
|
162 | return model; | |
163 | } |
|
163 | } | |
164 | return null; |
|
164 | return null; | |
165 | }; |
|
165 | }; | |
166 |
|
166 | |||
167 |
|
167 | |||
168 | WidgetManager.prototype.get_kernel = function () { |
|
168 | WidgetManager.prototype.get_kernel = function () { | |
169 | if (this.comm_manager === null) { |
|
169 | if (this.comm_manager === null) { | |
170 | return null; |
|
170 | return null; | |
171 | } else { |
|
171 | } else { | |
172 | return this.comm_manager.kernel; |
|
172 | return this.comm_manager.kernel; | |
173 | } |
|
173 | } | |
174 | }; |
|
174 | }; | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | WidgetManager.prototype.on_create_widget = function (callback) { |
|
177 | WidgetManager.prototype.on_create_widget = function (callback) { | |
178 | this._create_widget_callback = callback; |
|
178 | this._create_widget_callback = callback; | |
179 | }; |
|
179 | }; | |
180 |
|
180 | |||
181 |
|
181 | |||
182 | WidgetManager.prototype._handle_create_widget = function (widget_model) { |
|
182 | WidgetManager.prototype._handle_create_widget = function (widget_model) { | |
183 | if (this._create_widget_callback) { |
|
183 | if (this._create_widget_callback) { | |
184 | try { |
|
184 | try { | |
185 | this._create_widget_callback(widget_model); |
|
185 | this._create_widget_callback(widget_model); | |
186 | } catch (e) { |
|
186 | } catch (e) { | |
187 | console.log("Exception in WidgetManager callback", e, widget_model); |
|
187 | console.log("Exception in WidgetManager callback", e, widget_model); | |
188 | } |
|
188 | } | |
189 | } |
|
189 | } | |
190 | }; |
|
190 | }; | |
191 |
|
191 | |||
192 |
|
192 | |||
193 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
193 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
194 | var widget_type_name = msg.content.target_name; |
|
194 | var widget_type_name = msg.content.target_name; | |
195 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); |
|
195 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); | |
196 | this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id |
|
196 | this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id | |
197 | this._handle_create_widget(widget_model); |
|
197 | this._handle_create_widget(widget_model); | |
198 | }; |
|
198 | }; | |
199 |
|
199 | |||
200 | //-------------------------------------------------------------------- |
|
200 | //-------------------------------------------------------------------- | |
201 | // Init code |
|
201 | // Init code | |
202 | //-------------------------------------------------------------------- |
|
202 | //-------------------------------------------------------------------- | |
203 | IPython.WidgetManager = WidgetManager; |
|
203 | IPython.WidgetManager = WidgetManager; | |
204 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { |
|
204 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { | |
205 | IPython.widget_manager = new WidgetManager(); |
|
205 | IPython.widget_manager = new WidgetManager(); | |
206 | } |
|
206 | } | |
207 |
|
207 | |||
208 | return IPython.widget_manager; |
|
208 | return IPython.widget_manager; | |
209 | }); |
|
209 | }); | |
210 | }()); |
|
210 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now