Show More
@@ -1,213 +1,209 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 | WidgetManager.prototype.handle_msg = function(msg, model) { |
|
69 | WidgetManager.prototype.handle_msg = function(msg, model) { | |
70 | var method = msg.content.data.method; |
|
70 | var method = msg.content.data.method; | |
71 | switch (method) { |
|
71 | switch (method) { | |
72 | case 'display': |
|
72 | case 'display': | |
73 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
73 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
74 | if (cell === null) { |
|
74 | if (cell === null) { | |
75 | console.log("Could not determine where the display" + |
|
75 | console.log("Could not determine where the display" + | |
76 | " message was from. Widget will not be displayed"); |
|
76 | " message was from. Widget will not be displayed"); | |
77 | } else { |
|
77 | } else { | |
78 | var view = this.create_view(model, |
|
78 | var view = this.create_view(model, | |
79 | msg.content.data.view_name, cell); |
|
79 | msg.content.data.view_name, cell); | |
80 | if (view !== undefined |
|
80 | if (view !== undefined | |
81 | && cell.widget_subarea !== undefined |
|
81 | && cell.widget_subarea !== undefined | |
82 | && cell.widget_subarea !== null) { |
|
82 | && cell.widget_subarea !== null) { | |
83 | cell.widget_area.show(); |
|
83 | cell.widget_area.show(); | |
84 | cell.widget_subarea.append(view.$el); |
|
84 | cell.widget_subarea.append(view.$el); | |
85 | } |
|
85 | } | |
86 | } |
|
86 | } | |
87 | break; |
|
87 | break; | |
88 | case 'set_snapshot': |
|
|||
89 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
|||
90 | cell.metadata.snapshot = msg.content.data.snapshot; |
|
|||
91 | break; |
|
|||
92 | } |
|
88 | } | |
93 | } |
|
89 | } | |
94 |
|
90 | |||
95 | WidgetManager.prototype.create_view = function(model, view_name, cell) { |
|
91 | WidgetManager.prototype.create_view = function(model, view_name, cell) { | |
96 | view_name = view_name || model.get('default_view_name'); |
|
92 | view_name = view_name || model.get('default_view_name'); | |
97 | var ViewType = this.widget_view_types[view_name]; |
|
93 | var ViewType = this.widget_view_types[view_name]; | |
98 | if (ViewType !== undefined && ViewType !== null) { |
|
94 | if (ViewType !== undefined && ViewType !== null) { | |
99 | var view = new ViewType({model: model, widget_manager: this, cell: cell}); |
|
95 | var view = new ViewType({model: model, widget_manager: this, cell: cell}); | |
100 | view.render(); |
|
96 | view.render(); | |
101 | model.views.push(view); |
|
97 | model.views.push(view); | |
102 | /* |
|
98 | /* | |
103 | // jng: Handle when the view element is remove from the page. |
|
99 | // jng: Handle when the view element is remove from the page. | |
104 | // observe the view destruction event and do this. We may need |
|
100 | // observe the view destruction event and do this. We may need | |
105 | // to override the view's remove method to trigger this event. |
|
101 | // to override the view's remove method to trigger this event. | |
106 | var that = this; |
|
102 | var that = this; | |
107 | view.$el.on("remove", function () { |
|
103 | view.$el.on("remove", function () { | |
108 | var index = that.views.indexOf(view); |
|
104 | var index = that.views.indexOf(view); | |
109 | if (index > -1) { |
|
105 | if (index > -1) { | |
110 | that.views.splice(index, 1); |
|
106 | that.views.splice(index, 1); | |
111 | } |
|
107 | } | |
112 | view.remove(); // Clean-up view |
|
108 | view.remove(); // Clean-up view | |
113 |
|
109 | |||
114 | // Close the comm if there are no views left. |
|
110 | // Close the comm if there are no views left. | |
115 | if (that.views.length() === 0) { |
|
111 | if (that.views.length() === 0) { | |
116 | //jng: trigger comm close event |
|
112 | //jng: trigger comm close event | |
117 | } |
|
113 | } | |
118 |
|
114 | |||
119 |
|
115 | |||
120 | if (that.comm !== undefined) { |
|
116 | if (that.comm !== undefined) { | |
121 | that.comm.close(); |
|
117 | that.comm.close(); | |
122 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
118 | delete that.comm.model; // Delete ref so GC will collect widget model. | |
123 | delete that.comm; |
|
119 | delete that.comm; | |
124 | } |
|
120 | } | |
125 | delete that.widget_id; // Delete id from model so widget manager cleans up. |
|
121 | delete that.widget_id; // Delete id from model so widget manager cleans up. | |
126 | }); |
|
122 | }); | |
127 | */ |
|
123 | */ | |
128 | return view; |
|
124 | return view; | |
129 | } |
|
125 | } | |
130 | }, |
|
126 | }, | |
131 |
|
127 | |||
132 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
128 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
133 | var cell = null; |
|
129 | var cell = null; | |
134 | // First, check to see if the msg was triggered by cell execution. |
|
130 | // First, check to see if the msg was triggered by cell execution. | |
135 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
131 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
136 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
132 | cell = IPython.notebook.get_msg_cell(msg_id); | |
137 | } |
|
133 | } | |
138 | if (cell !== null) { |
|
134 | if (cell !== null) { | |
139 | return cell |
|
135 | return cell | |
140 | } |
|
136 | } | |
141 | // Second, check to see if a get_cell callback was defined |
|
137 | // Second, check to see if a get_cell callback was defined | |
142 | // for the message. get_cell callbacks are registered for |
|
138 | // for the message. get_cell callbacks are registered for | |
143 | // widget messages, so this block is actually checking to see if the |
|
139 | // widget messages, so this block is actually checking to see if the | |
144 | // message was triggered by a widget. |
|
140 | // message was triggered by a widget. | |
145 | var kernel = this.get_kernel(); |
|
141 | var kernel = this.get_kernel(); | |
146 | if (kernel !== undefined && kernel !== null) { |
|
142 | if (kernel !== undefined && kernel !== null) { | |
147 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
143 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
148 | if (callbacks !== undefined && |
|
144 | if (callbacks !== undefined && | |
149 | callbacks.iopub !== undefined && |
|
145 | callbacks.iopub !== undefined && | |
150 | callbacks.iopub.get_cell !== undefined) { |
|
146 | callbacks.iopub.get_cell !== undefined) { | |
151 |
|
147 | |||
152 | return callbacks.iopub.get_cell(); |
|
148 | return callbacks.iopub.get_cell(); | |
153 | } |
|
149 | } | |
154 | } |
|
150 | } | |
155 |
|
151 | |||
156 | // Not triggered by a cell or widget (no get_cell callback |
|
152 | // Not triggered by a cell or widget (no get_cell callback | |
157 | // exists). |
|
153 | // exists). | |
158 | return null; |
|
154 | return null; | |
159 | }; |
|
155 | }; | |
160 |
|
156 | |||
161 |
|
157 | |||
162 | WidgetManager.prototype.get_model = function (widget_id) { |
|
158 | WidgetManager.prototype.get_model = function (widget_id) { | |
163 | var model = this._model_instances[widget_id]; |
|
159 | var model = this._model_instances[widget_id]; | |
164 | if (model !== undefined && model.id == widget_id) { |
|
160 | if (model !== undefined && model.id == widget_id) { | |
165 | return model; |
|
161 | return model; | |
166 | } |
|
162 | } | |
167 | return null; |
|
163 | return null; | |
168 | }; |
|
164 | }; | |
169 |
|
165 | |||
170 |
|
166 | |||
171 | WidgetManager.prototype.get_kernel = function () { |
|
167 | WidgetManager.prototype.get_kernel = function () { | |
172 | if (this.comm_manager === null) { |
|
168 | if (this.comm_manager === null) { | |
173 | return null; |
|
169 | return null; | |
174 | } else { |
|
170 | } else { | |
175 | return this.comm_manager.kernel; |
|
171 | return this.comm_manager.kernel; | |
176 | } |
|
172 | } | |
177 | }; |
|
173 | }; | |
178 |
|
174 | |||
179 |
|
175 | |||
180 | WidgetManager.prototype.on_create_widget = function (callback) { |
|
176 | WidgetManager.prototype.on_create_widget = function (callback) { | |
181 | this._create_widget_callback = callback; |
|
177 | this._create_widget_callback = callback; | |
182 | }; |
|
178 | }; | |
183 |
|
179 | |||
184 |
|
180 | |||
185 | WidgetManager.prototype._handle_create_widget = function (widget_model) { |
|
181 | WidgetManager.prototype._handle_create_widget = function (widget_model) { | |
186 | if (this._create_widget_callback) { |
|
182 | if (this._create_widget_callback) { | |
187 | try { |
|
183 | try { | |
188 | this._create_widget_callback(widget_model); |
|
184 | this._create_widget_callback(widget_model); | |
189 | } catch (e) { |
|
185 | } catch (e) { | |
190 | console.log("Exception in WidgetManager callback", e, widget_model); |
|
186 | console.log("Exception in WidgetManager callback", e, widget_model); | |
191 | } |
|
187 | } | |
192 | } |
|
188 | } | |
193 | }; |
|
189 | }; | |
194 |
|
190 | |||
195 |
|
191 | |||
196 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
192 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
197 | var widget_type_name = msg.content.target_name; |
|
193 | var widget_type_name = msg.content.target_name; | |
198 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); |
|
194 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); | |
199 | this._model_instances[comm.comm_id] = widget_model; |
|
195 | this._model_instances[comm.comm_id] = widget_model; | |
200 | this._handle_create_widget(widget_model); |
|
196 | this._handle_create_widget(widget_model); | |
201 | }; |
|
197 | }; | |
202 |
|
198 | |||
203 | //-------------------------------------------------------------------- |
|
199 | //-------------------------------------------------------------------- | |
204 | // Init code |
|
200 | // Init code | |
205 | //-------------------------------------------------------------------- |
|
201 | //-------------------------------------------------------------------- | |
206 | IPython.WidgetManager = WidgetManager; |
|
202 | IPython.WidgetManager = WidgetManager; | |
207 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { |
|
203 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { | |
208 | IPython.widget_manager = new WidgetManager(); |
|
204 | IPython.widget_manager = new WidgetManager(); | |
209 | } |
|
205 | } | |
210 |
|
206 | |||
211 | return IPython.widget_manager; |
|
207 | return IPython.widget_manager; | |
212 | }); |
|
208 | }); | |
213 | }()); |
|
209 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now