Show More
This diff has been collapsed as it changes many lines, (1025 lines changed) Show them Hide them | |||
@@ -15,605 +15,608 b'' | |||
|
15 | 15 | * @submodule widget |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | "use strict"; | |
|
18 | (function () { | |
|
19 | "use strict"; | |
|
20 | ||
|
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' | |
|
23 | // elsewhere. | |
|
24 | define(["components/underscore/underscore-min", | |
|
25 | "components/backbone/backbone-min", | |
|
26 | ], function(underscore, backbone){ | |
|
27 | ||
|
28 | ||
|
29 | //-------------------------------------------------------------------- | |
|
30 | // WidgetModel class | |
|
31 | //-------------------------------------------------------------------- | |
|
32 | var WidgetModel = Backbone.Model.extend({ | |
|
33 | constructor: function(comm_manager, comm, widget_manager) { | |
|
34 | this.comm_manager = comm_manager; | |
|
35 | this.widget_manager = widget_manager; | |
|
36 | this.pending_msgs = 0; | |
|
37 | this.msg_throttle = 3; | |
|
38 | this.msg_buffer = null; | |
|
39 | this.views = []; | |
|
40 | this._custom_msg_callbacks = []; | |
|
41 | ||
|
42 | // Remember comm associated with the model. | |
|
43 | this.comm = comm; | |
|
44 | comm.model = this; | |
|
45 | ||
|
46 | // Hook comm messages up to model. | |
|
47 | comm.on_close($.proxy(this._handle_comm_closed, this)); | |
|
48 | comm.on_msg($.proxy(this._handle_comm_msg, this)); | |
|
49 | ||
|
50 | return Backbone.Model.apply(this); | |
|
51 | }, | |
|
52 | ||
|
53 | ||
|
54 | update_other_views: function(caller) { | |
|
55 | this.last_modified_view = caller; | |
|
56 | this.save(this.changedAttributes(), {patch: true}); | |
|
57 | ||
|
58 | for (var view_index in this.views) { | |
|
59 | var view = this.views[view_index]; | |
|
60 | if (view !== caller) { | |
|
61 | view.update(); | |
|
62 | } | |
|
63 | } | |
|
64 | }, | |
|
65 | ||
|
19 | 66 | |
|
20 | // Use require.js 'define' method so that require.js is intelligent enough to | |
|
21 | // syncronously load everything within this file when it is being 'required' | |
|
22 | // elsewhere. | |
|
23 | define(["components/underscore/underscore-min", | |
|
24 | "components/backbone/backbone-min", | |
|
25 | ], function(underscore, backbone){ | |
|
67 | send: function(content, cell) { | |
|
26 | 68 | |
|
69 | // Used the last modified view as the sender of the message. This | |
|
70 | // will insure that any python code triggered by the sent message | |
|
71 | // can create and display widgets and output. | |
|
72 | if (cell === undefined) { | |
|
73 | if (this.last_modified_view !== undefined && | |
|
74 | this.last_modified_view.cell !== undefined) { | |
|
75 | cell = this.last_modified_view.cell; | |
|
76 | } | |
|
77 | } | |
|
78 | var callbacks = this._make_callbacks(cell); | |
|
79 | var data = {method: 'custom', custom_content: content}; | |
|
80 | this.comm.send(data, callbacks); | |
|
81 | }, | |
|
27 | 82 | |
|
28 | //-------------------------------------------------------------------- | |
|
29 | // WidgetModel class | |
|
30 | //-------------------------------------------------------------------- | |
|
31 | var WidgetModel = Backbone.Model.extend({ | |
|
32 | constructor: function(comm_manager, comm, widget_manager) { | |
|
33 | this.comm_manager = comm_manager; | |
|
34 | this.widget_manager = widget_manager; | |
|
35 | this.pending_msgs = 0; | |
|
36 | this.msg_throttle = 3; | |
|
37 | this.msg_buffer = null; | |
|
38 | this.views = []; | |
|
39 | this._custom_msg_callbacks = []; | |
|
40 | 83 | |
|
41 | // Remember comm associated with the model. | |
|
42 | this.comm = comm; | |
|
43 | comm.model = this; | |
|
84 | on_view_displayed: function (callback) { | |
|
85 | this._view_displayed_callback = callback; | |
|
86 | }, | |
|
44 | 87 | |
|
45 | // Hook comm messages up to model. | |
|
46 | comm.on_close($.proxy(this._handle_comm_closed, this)); | |
|
47 | comm.on_msg($.proxy(this._handle_comm_msg, this)); | |
|
48 | 88 | |
|
49 | return Backbone.Model.apply(this); | |
|
50 | }, | |
|
89 | on_close: function (callback) { | |
|
90 | this._close_callback = callback; | |
|
91 | }, | |
|
51 | 92 | |
|
52 | 93 | |
|
53 |
|
|
|
54 | this.last_modified_view = caller; | |
|
55 | this.save(this.changedAttributes(), {patch: true}); | |
|
94 | on_msg: function (callback, remove) { | |
|
95 | if (remove) { | |
|
96 | var found_index = -1; | |
|
97 | for (var index in this._custom_msg_callbacks) { | |
|
98 | if (callback === this._custom_msg_callbacks[index]) { | |
|
99 | found_index = index; | |
|
100 | break; | |
|
101 | } | |
|
102 | } | |
|
56 | 103 | |
|
57 | for (var view_index in this.views) { | |
|
58 | var view = this.views[view_index]; | |
|
59 |
|
|
|
60 | view.update(); | |
|
61 | } | |
|
62 | } | |
|
63 | }, | |
|
64 | ||
|
65 | ||
|
66 | send: function(content, cell) { | |
|
67 | ||
|
68 | // Used the last modified view as the sender of the message. This | |
|
69 | // will insure that any python code triggered by the sent message | |
|
70 | // can create and display widgets and output. | |
|
71 | if (cell === undefined) { | |
|
72 | if (this.last_modified_view != undefined && | |
|
73 | this.last_modified_view.cell != undefined) { | |
|
74 | cell = this.last_modified_view.cell; | |
|
104 | if (found_index >= 0) { | |
|
105 | this._custom_msg_callbacks.splice(found_index, 1); | |
|
106 | } | |
|
107 | } else { | |
|
108 | this._custom_msg_callbacks.push(callback); | |
|
75 | 109 | } |
|
76 | } | |
|
77 | var callbacks = this._make_callbacks(cell); | |
|
78 | var data = {method: 'custom', custom_content: content}; | |
|
79 | this.comm.send(data, callbacks); | |
|
80 | }, | |
|
81 | ||
|
110 | }, | |
|
82 | 111 | |
|
83 | on_view_displayed: function (callback) { | |
|
84 | this._view_displayed_callback = callback; | |
|
85 | }, | |
|
86 | 112 | |
|
87 | ||
|
88 | on_close: function (callback) { | |
|
89 | this._close_callback = callback; | |
|
90 | }, | |
|
91 | ||
|
92 | ||
|
93 | on_msg: function (callback, remove) { | |
|
94 | if (remove) { | |
|
95 | var found_index = -1; | |
|
113 | _handle_custom_msg: function (content) { | |
|
96 | 114 | for (var index in this._custom_msg_callbacks) { |
|
97 | if (callback === this._custom_msg_callbacks[index]) { | |
|
98 |
|
|
|
99 |
|
|
|
115 | try { | |
|
116 | this._custom_msg_callbacks[index](content); | |
|
117 | } catch (e) { | |
|
118 | console.log("Exception in widget model msg callback", e, content); | |
|
100 | 119 | } |
|
101 | 120 | } |
|
121 | }, | |
|
102 | 122 | |
|
103 | if (found_index >= 0) { | |
|
104 | this._custom_msg_callbacks.splice(found_index, 1); | |
|
105 | } | |
|
106 | } else { | |
|
107 | this._custom_msg_callbacks.push(callback) | |
|
108 | } | |
|
109 | }, | |
|
110 | 123 | |
|
111 | ||
|
112 |
_handle_c |
|
|
113 | for (var index in this._custom_msg_callbacks) { | |
|
114 | try { | |
|
115 | this._custom_msg_callbacks[index](content); | |
|
116 | } catch (e) { | |
|
117 | console.log("Exception in widget model msg callback", e, content); | |
|
118 | } | |
|
119 | } | |
|
120 | }, | |
|
124 | // Handle when a widget is closed. | |
|
125 | _handle_comm_closed: function (msg) { | |
|
126 | this._execute_views_method('remove'); | |
|
127 | delete this.comm.model; // Delete ref so GC will collect widget model. | |
|
128 | }, | |
|
121 | 129 | |
|
122 | 130 | |
|
123 |
// Handle |
|
|
124 |
_handle_comm_ |
|
|
125 | this._execute_views_method('remove'); | |
|
126 | delete this.comm.model; // Delete ref so GC will collect widget model. | |
|
127 | }, | |
|
131 | // Handle incomming comm msg. | |
|
132 | _handle_comm_msg: function (msg) { | |
|
133 | var method = msg.content.data.method; | |
|
134 | switch (method){ | |
|
135 | case 'display': | |
|
128 | 136 | |
|
137 | // Try to get the cell. | |
|
138 | var cell = this._get_msg_cell(msg.parent_header.msg_id); | |
|
139 | if (cell === null) { | |
|
140 | console.log("Could not determine where the display" + | |
|
141 | " message was from. Widget will not be displayed"); | |
|
142 | } else { | |
|
143 | this._display_view(msg.content.data.view_name, | |
|
144 | msg.content.data.parent, | |
|
145 | cell); | |
|
146 | } | |
|
147 | break; | |
|
148 | case 'update': | |
|
149 | this._handle_update(msg.content.data.state); | |
|
150 | break; | |
|
151 | case 'add_class': | |
|
152 | case 'remove_class': | |
|
153 | var selector = msg.content.data.selector; | |
|
154 | if (selector === undefined) { | |
|
155 | selector = ''; | |
|
156 | } | |
|
129 | 157 | |
|
130 | // Handle incomming comm msg. | |
|
131 | _handle_comm_msg: function (msg) { | |
|
132 | var method = msg.content.data.method; | |
|
133 | switch (method){ | |
|
134 | case 'display': | |
|
158 | var class_list = msg.content.data.class_list; | |
|
159 | this._execute_views_method(method, selector, class_list); | |
|
160 | break; | |
|
161 | case 'custom': | |
|
162 | this._handle_custom_msg(msg.content.data.custom_content); | |
|
163 | break; | |
|
164 | } | |
|
165 | }, | |
|
135 | 166 | |
|
136 | // Try to get the cell. | |
|
137 | var cell = this._get_msg_cell(msg.parent_header.msg_id); | |
|
138 | if (cell == null) { | |
|
139 | console.log("Could not determine where the display" + | |
|
140 | " message was from. Widget will not be displayed") | |
|
141 | } else { | |
|
142 | this._display_view(msg.content.data.view_name, | |
|
143 | msg.content.data.parent, | |
|
144 | cell); | |
|
145 | } | |
|
146 | break; | |
|
147 | case 'update': | |
|
148 | this._handle_update(msg.content.data.state); | |
|
149 | break; | |
|
150 | case 'add_class': | |
|
151 | case 'remove_class': | |
|
152 | var selector = msg.content.data.selector; | |
|
153 | if (selector === undefined) { | |
|
154 | selector = ''; | |
|
155 | } | |
|
156 | 167 | |
|
157 | var class_list = msg.content.data.class_list; | |
|
158 | this._execute_views_method(method, selector, class_list); | |
|
159 |
|
|
|
160 |
|
|
|
161 | this._handle_custom_msg(msg.content.data.custom_content); | |
|
162 | break; | |
|
163 | } | |
|
164 | }, | |
|
165 | ||
|
166 | ||
|
167 | // Handle when a widget is updated via the python side. | |
|
168 | _handle_update: function (state) { | |
|
169 | this.updating = true; | |
|
170 | try { | |
|
171 | for (var key in state) { | |
|
172 | if (state.hasOwnProperty(key)) { | |
|
173 |
|
|
|
174 | ||
|
175 | // Set the css value of the model as an attribute | |
|
176 | // instead of a backbone trait because we are only | |
|
177 | // interested in backend css -> frontend css. In | |
|
178 | // other words, if the css dict changes in the | |
|
179 | // frontend, we don't need to push the changes to | |
|
180 | // the backend. | |
|
181 | this.css = state[key]; | |
|
182 | } else { | |
|
183 | this.set(key, state[key]); | |
|
168 | // Handle when a widget is updated via the python side. | |
|
169 | _handle_update: function (state) { | |
|
170 | this.updating = true; | |
|
171 | try { | |
|
172 | for (var key in state) { | |
|
173 | if (state.hasOwnProperty(key)) { | |
|
174 | if (key == "_css"){ | |
|
175 | ||
|
176 | // Set the css value of the model as an attribute | |
|
177 | // instead of a backbone trait because we are only | |
|
178 | // interested in backend css -> frontend css. In | |
|
179 | // other words, if the css dict changes in the | |
|
180 | // frontend, we don't need to push the changes to | |
|
181 | // the backend. | |
|
182 | this.css = state[key]; | |
|
183 | } else { | |
|
184 | this.set(key, state[key]); | |
|
185 | } | |
|
184 | 186 | } |
|
185 | 187 | } |
|
188 | this.id = this.comm.comm_id; | |
|
189 | this.save(); | |
|
190 | } finally { | |
|
191 | this.updating = false; | |
|
186 | 192 | } |
|
187 | this.id = this.comm.comm_id; | |
|
188 | this.save(); | |
|
189 | } finally { | |
|
190 | this.updating = false; | |
|
191 | } | |
|
192 | }, | |
|
193 | }, | |
|
193 | 194 | |
|
194 | 195 | |
|
195 | _handle_status: function (cell, msg) { | |
|
196 | //execution_state : ('busy', 'idle', 'starting') | |
|
197 | if (msg.content.execution_state=='idle') { | |
|
198 | ||
|
199 | // Send buffer if this message caused another message to be | |
|
200 | // throttled. | |
|
201 | if (this.msg_buffer != null && | |
|
202 | this.msg_throttle == this.pending_msgs) { | |
|
203 | ||
|
204 |
var c |
|
|
205 | var callbacks = this._make_callbacks(cell); | |
|
206 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | |
|
207 |
this. |
|
|
208 |
|
|
|
209 | } else { | |
|
196 | _handle_status: function (cell, msg) { | |
|
197 | //execution_state : ('busy', 'idle', 'starting') | |
|
198 | if (msg.content.execution_state=='idle') { | |
|
199 | ||
|
200 | // Send buffer if this message caused another message to be | |
|
201 | // throttled. | |
|
202 | if (this.msg_buffer !== null && | |
|
203 | this.msg_throttle == this.pending_msgs) { | |
|
204 | ||
|
205 | var callbacks = this._make_callbacks(cell); | |
|
206 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | |
|
207 | this.comm.send(data, callbacks); | |
|
208 | this.msg_buffer = null; | |
|
209 | } else { | |
|
210 | 210 | |
|
211 | // Only decrease the pending message count if the buffer | |
|
212 | // doesn't get flushed (sent). | |
|
213 | --this.pending_msgs; | |
|
211 | // Only decrease the pending message count if the buffer | |
|
212 | // doesn't get flushed (sent). | |
|
213 | --this.pending_msgs; | |
|
214 | } | |
|
214 | 215 | } |
|
215 | } | |
|
216 | }, | |
|
217 | ||
|
218 | ||
|
219 | // Custom syncronization logic. | |
|
220 | _handle_sync: function (method, options) { | |
|
221 | var model_json = this.toJSON(); | |
|
222 | ||
|
223 | // Only send updated state if the state hasn't been changed | |
|
224 | // during an update. | |
|
225 | if (!this.updating) { | |
|
226 | if (this.pending_msgs >= this.msg_throttle) { | |
|
227 | // The throttle has been exceeded, buffer the current msg so | |
|
228 | // it can be sent once the kernel has finished processing | |
|
229 | // some of the existing messages. | |
|
230 | if (method=='patch') { | |
|
231 | if (this.msg_buffer == null) { | |
|
216 | }, | |
|
217 | ||
|
218 | ||
|
219 | // Custom syncronization logic. | |
|
220 | _handle_sync: function (method, options) { | |
|
221 | var model_json = this.toJSON(); | |
|
222 | var attr; | |
|
223 | ||
|
224 | // Only send updated state if the state hasn't been changed | |
|
225 | // during an update. | |
|
226 | if (!this.updating) { | |
|
227 | if (this.pending_msgs >= this.msg_throttle) { | |
|
228 | // The throttle has been exceeded, buffer the current msg so | |
|
229 | // it can be sent once the kernel has finished processing | |
|
230 | // some of the existing messages. | |
|
231 | if (method=='patch') { | |
|
232 | if (this.msg_buffer === null) { | |
|
233 | this.msg_buffer = $.extend({}, model_json); // Copy | |
|
234 | } | |
|
235 | for (attr in options.attrs) { | |
|
236 | this.msg_buffer[attr] = options.attrs[attr]; | |
|
237 | } | |
|
238 | } else { | |
|
232 | 239 | this.msg_buffer = $.extend({}, model_json); // Copy |
|
233 | 240 | } |
|
234 | for (var attr in options.attrs) { | |
|
235 | this.msg_buffer[attr] = options.attrs[attr]; | |
|
236 | } | |
|
237 | } else { | |
|
238 | this.msg_buffer = $.extend({}, model_json); // Copy | |
|
239 | } | |
|
240 | 241 | |
|
241 | } else { | |
|
242 | // We haven't exceeded the throttle, send the message like | |
|
243 | // normal. If this is a patch operation, just send the | |
|
244 | // changes. | |
|
245 | var send_json = model_json; | |
|
246 | if (method=='patch') { | |
|
247 | send_json = {}; | |
|
248 |
for ( |
|
|
249 | send_json[attr] = options.attrs[attr]; | |
|
242 | } else { | |
|
243 | // We haven't exceeded the throttle, send the message like | |
|
244 | // normal. If this is a patch operation, just send the | |
|
245 | // changes. | |
|
246 | var send_json = model_json; | |
|
247 | if (method=='patch') { | |
|
248 | send_json = {}; | |
|
249 | for (attr in options.attrs) { | |
|
250 | send_json[attr] = options.attrs[attr]; | |
|
251 | } | |
|
250 | 252 | } |
|
251 | } | |
|
252 | 253 | |
|
253 | var data = {method: 'backbone', sync_method: method, sync_data: send_json}; | |
|
254 | var data = {method: 'backbone', sync_method: method, sync_data: send_json}; | |
|
254 | 255 | |
|
255 | var cell = null; | |
|
256 | if (this.last_modified_view != undefined && this.last_modified_view != null) { | |
|
257 | cell = this.last_modified_view.cell; | |
|
256 | var cell = null; | |
|
257 | if (this.last_modified_view !== undefined && this.last_modified_view !== null) { | |
|
258 | cell = this.last_modified_view.cell; | |
|
259 | } | |
|
260 | ||
|
261 | var callbacks = this._make_callbacks(cell); | |
|
262 | this.comm.send(data, callbacks); | |
|
263 | this.pending_msgs++; | |
|
258 | 264 | } |
|
259 | ||
|
260 | var callbacks = this._make_callbacks(cell); | |
|
261 | this.comm.send(data, callbacks); | |
|
262 | this.pending_msgs++; | |
|
263 | 265 | } |
|
264 |
|
|
|
265 | ||
|
266 | // Since the comm is a one-way communication, assume the message | |
|
267 | // arrived. | |
|
268 | return model_json; | |
|
269 | }, | |
|
270 | ||
|
271 | ||
|
272 | _handle_view_displayed: function(view) { | |
|
273 | if (this._view_displayed_callback) { | |
|
274 | try { | |
|
275 | this._view_displayed_callback(view) | |
|
276 | } catch (e) { | |
|
277 | console.log("Exception in widget model view displayed callback", e, view, this); | |
|
266 | ||
|
267 | // Since the comm is a one-way communication, assume the message | |
|
268 | // arrived. | |
|
269 | return model_json; | |
|
270 | }, | |
|
271 | ||
|
272 | ||
|
273 | _handle_view_displayed: function(view) { | |
|
274 | if (this._view_displayed_callback) { | |
|
275 | try { | |
|
276 | this._view_displayed_callback(view); | |
|
277 | } catch (e) { | |
|
278 | console.log("Exception in widget model view displayed callback", e, view, this); | |
|
279 | } | |
|
278 | 280 | } |
|
279 | } | |
|
280 | }, | |
|
281 | }, | |
|
281 | 282 | |
|
282 | 283 | |
|
283 | _execute_views_method: function (/* method_name, [argument0], [argument1], [...] */) { | |
|
284 | var method_name = arguments[0]; | |
|
285 | var args = null; | |
|
286 | if (arguments.length > 1) { | |
|
287 | args = [].splice.call(arguments,1); | |
|
288 | } | |
|
284 | _execute_views_method: function (/* method_name, [argument0], [argument1], [...] */) { | |
|
285 | var method_name = arguments[0]; | |
|
286 | var args = null; | |
|
287 | if (arguments.length > 1) { | |
|
288 | args = [].splice.call(arguments,1); | |
|
289 | } | |
|
289 | 290 | |
|
290 | for (var view_index in this.views) { | |
|
291 | var view = this.views[view_index]; | |
|
292 | var method = view[method_name]; | |
|
293 | if (args === null) { | |
|
294 | method.apply(view); | |
|
295 | } else { | |
|
296 | method.apply(view, args); | |
|
291 | for (var view_index in this.views) { | |
|
292 | var view = this.views[view_index]; | |
|
293 | var method = view[method_name]; | |
|
294 | if (args === null) { | |
|
295 | method.apply(view); | |
|
296 | } else { | |
|
297 | method.apply(view, args); | |
|
298 | } | |
|
297 | 299 | } |
|
298 | } | |
|
299 | }, | |
|
300 | ||
|
301 | ||
|
302 | // Create view that represents the model. | |
|
303 | _display_view: function (view_name, parent_comm_id, cell) { | |
|
304 |
var |
|
|
305 | ||
|
306 | // Try creating and adding the view to it's parent. | |
|
307 | var displayed = false; | |
|
308 | if (parent_comm_id != undefined) { | |
|
309 | var parent_comm = this.comm_manager.comms[parent_comm_id]; | |
|
310 | var parent_model = parent_comm.model; | |
|
311 | var parent_views = parent_model.views; | |
|
312 | for (var parent_view_index in parent_views) { | |
|
313 | var parent_view = parent_views[parent_view_index]; | |
|
314 | if (parent_view.cell === cell) { | |
|
315 | if (parent_view.display_child != undefined) { | |
|
316 |
|
|
|
317 | if (view != null) { | |
|
318 | new_views.push(view); | |
|
319 | parent_view.display_child(view); | |
|
320 | displayed = true; | |
|
321 | this._handle_view_displayed(view); | |
|
322 | } | |
|
323 | } | |
|
300 | }, | |
|
301 | ||
|
302 | ||
|
303 | // Create view that represents the model. | |
|
304 | _display_view: function (view_name, parent_comm_id, cell) { | |
|
305 | var new_views = []; | |
|
306 | var view; | |
|
307 | ||
|
308 | // Try creating and adding the view to it's parent. | |
|
309 | var displayed = false; | |
|
310 | if (parent_comm_id !== undefined) { | |
|
311 | var parent_comm = this.comm_manager.comms[parent_comm_id]; | |
|
312 | var parent_model = parent_comm.model; | |
|
313 | var parent_views = parent_model.views; | |
|
314 | for (var parent_view_index in parent_views) { | |
|
315 | var parent_view = parent_views[parent_view_index]; | |
|
316 | if (parent_view.cell === cell) { | |
|
317 | if (parent_view.display_child !== undefined) { | |
|
318 | view = this._create_view(view_name, cell); | |
|
319 | if (view !== null) { | |
|
320 | new_views.push(view); | |
|
321 | parent_view.display_child(view); | |
|
322 | displayed = true; | |
|
323 | this._handle_view_displayed(view); | |
|
324 | } | |
|
325 | } | |
|
326 | } | |
|
324 | 327 | } |
|
325 | 328 | } |
|
326 | } | |
|
327 | 329 | |
|
328 | // If no parent view is defined or exists. Add the view's | |
|
329 | // element to cell's widget div. | |
|
330 | if (!displayed) { | |
|
331 |
|
|
|
332 | if (view != null) { | |
|
333 | new_views.push(view); | |
|
334 | ||
|
335 | if (cell.widget_subarea != undefined && cell.widget_subarea != null) { | |
|
336 | cell.widget_area.show(); | |
|
337 | cell.widget_subarea.append(view.$el); | |
|
338 | this._handle_view_displayed(view); | |
|
330 | // If no parent view is defined or exists. Add the view's | |
|
331 | // element to cell's widget div. | |
|
332 | if (!displayed) { | |
|
333 | view = this._create_view(view_name, cell); | |
|
334 | if (view !== null) { | |
|
335 | new_views.push(view); | |
|
336 | ||
|
337 | if (cell.widget_subarea !== undefined && cell.widget_subarea !== null) { | |
|
338 | cell.widget_area.show(); | |
|
339 | cell.widget_subarea.append(view.$el); | |
|
340 | this._handle_view_displayed(view); | |
|
341 | } | |
|
339 | 342 | } |
|
340 | 343 | } |
|
341 | } | |
|
342 | 344 | |
|
343 | // Force the new view(s) to update their selves | |
|
344 | for (var view_index in new_views) { | |
|
345 |
|
|
|
346 | view.update(); | |
|
347 | } | |
|
348 | }, | |
|
349 | ||
|
350 | ||
|
351 | // Create a view | |
|
352 | _create_view: function (view_name, cell) { | |
|
353 |
var |
|
|
354 |
if ( |
|
|
355 |
var view = new |
|
|
356 | view.render(); | |
|
357 | this.views.push(view); | |
|
358 | view.cell = cell; | |
|
359 | ||
|
360 | // Handle when the view element is remove from the page. | |
|
361 | var that = this; | |
|
362 | view.$el.on("remove", function(){ | |
|
363 | var index = that.views.indexOf(view); | |
|
364 | if (index > -1) { | |
|
365 | that.views.splice(index, 1); | |
|
366 | } | |
|
367 | view.remove(); // Clean-up view | |
|
368 | ||
|
369 | // Close the comm if there are no views left. | |
|
370 | if (that.views.length()==0) { | |
|
371 | if (that._close_callback) { | |
|
372 | try { | |
|
373 | that._close_callback(that) | |
|
374 | } catch (e) { | |
|
375 | console.log("Exception in widget model close callback", e, that); | |
|
345 | // Force the new view(s) to update their selves | |
|
346 | for (var view_index in new_views) { | |
|
347 | view = new_views[view_index]; | |
|
348 | view.update(); | |
|
349 | } | |
|
350 | }, | |
|
351 | ||
|
352 | ||
|
353 | // Create a view | |
|
354 | _create_view: function (view_name, cell) { | |
|
355 | var ViewType = this.widget_manager.widget_view_types[view_name]; | |
|
356 | if (ViewType !== undefined && ViewType !== null) { | |
|
357 | var view = new ViewType({model: this}); | |
|
358 | view.render(); | |
|
359 | this.views.push(view); | |
|
360 | view.cell = cell; | |
|
361 | ||
|
362 | // Handle when the view element is remove from the page. | |
|
363 | var that = this; | |
|
364 | view.$el.on("remove", function(){ | |
|
365 | var index = that.views.indexOf(view); | |
|
366 | if (index > -1) { | |
|
367 | that.views.splice(index, 1); | |
|
368 | } | |
|
369 | view.remove(); // Clean-up view | |
|
370 | ||
|
371 | // Close the comm if there are no views left. | |
|
372 | if (that.views.length() === 0) { | |
|
373 | if (that._close_callback) { | |
|
374 | try { | |
|
375 | that._close_callback(that); | |
|
376 | } catch (e) { | |
|
377 | console.log("Exception in widget model close callback", e, that); | |
|
378 | } | |
|
376 | 379 | } |
|
380 | that.comm.close(); | |
|
381 | delete that.comm.model; // Delete ref so GC will collect widget model. | |
|
377 | 382 | } |
|
378 |
|
|
|
379 | delete that.comm.model; // Delete ref so GC will collect widget model. | |
|
380 | } | |
|
381 | }); | |
|
382 | return view; | |
|
383 | } | |
|
384 | return null; | |
|
385 | }, | |
|
386 | ||
|
387 | ||
|
388 | // Build a callback dict. | |
|
389 | _make_callbacks: function (cell) { | |
|
390 | var callbacks = {}; | |
|
391 | if (cell != null) { | |
|
392 | ||
|
393 | // Try to get output handlers | |
|
394 | var handle_output = null; | |
|
395 | var handle_clear_output = null; | |
|
396 | if (cell.output_area != undefined && cell.output_area != null) { | |
|
397 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
|
398 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
|
383 | }); | |
|
384 | return view; | |
|
399 | 385 | } |
|
386 | return null; | |
|
387 | }, | |
|
400 | 388 | |
|
401 | // Create callback dict usign what is known | |
|
402 | var that = this; | |
|
403 | callbacks = { | |
|
404 | iopub : { | |
|
405 | output : handle_output, | |
|
406 | clear_output : handle_clear_output, | |
|
407 | 389 | |
|
408 | status : function(msg){ | |
|
409 | that._handle_status(cell, msg); | |
|
410 |
|
|
|
390 | // Build a callback dict. | |
|
391 | _make_callbacks: function (cell) { | |
|
392 | var callbacks = {}; | |
|
393 | if (cell !== null) { | |
|
394 | ||
|
395 | // Try to get output handlers | |
|
396 | var handle_output = null; | |
|
397 | var handle_clear_output = null; | |
|
398 | if (cell.output_area !== undefined && cell.output_area !== null) { | |
|
399 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
|
400 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
|
401 | } | |
|
411 | 402 | |
|
412 | // Special function only registered by widget messages. | |
|
413 | // Allows us to get the cell for a message so we know | |
|
414 | // where to add widgets if the code requires it. | |
|
415 |
|
|
|
416 |
|
|
|
403 | // Create callback dict usign what is known | |
|
404 | var that = this; | |
|
405 | callbacks = { | |
|
406 | iopub : { | |
|
407 | output : handle_output, | |
|
408 | clear_output : handle_clear_output, | |
|
409 | ||
|
410 | status : function(msg){ | |
|
411 | that._handle_status(cell, msg); | |
|
412 | }, | |
|
413 | ||
|
414 | // Special function only registered by widget messages. | |
|
415 | // Allows us to get the cell for a message so we know | |
|
416 | // where to add widgets if the code requires it. | |
|
417 | get_cell : function() { | |
|
418 | return cell; | |
|
419 | }, | |
|
417 | 420 | }, |
|
418 |
} |
|
|
419 |
} |
|
|
420 | } | |
|
421 | return callbacks; | |
|
422 | }, | |
|
421 | }; | |
|
422 | } | |
|
423 | return callbacks; | |
|
424 | }, | |
|
423 | 425 | |
|
424 | 426 | |
|
425 | // Get the output area corresponding to the msg_id. | |
|
426 | // cell is an instance of IPython.Cell | |
|
427 | _get_msg_cell: function (msg_id) { | |
|
427 | // Get the output area corresponding to the msg_id. | |
|
428 | // cell is an instance of IPython.Cell | |
|
429 | _get_msg_cell: function (msg_id) { | |
|
428 | 430 | |
|
429 | // First, check to see if the msg was triggered by cell execution. | |
|
430 | var cell = this.widget_manager.get_msg_cell(msg_id); | |
|
431 | if (cell != null) { | |
|
432 | return cell; | |
|
433 | } | |
|
431 | // First, check to see if the msg was triggered by cell execution. | |
|
432 | var cell = this.widget_manager.get_msg_cell(msg_id); | |
|
433 | if (cell !== null) { | |
|
434 | return cell; | |
|
435 | } | |
|
434 | 436 | |
|
435 | // Second, check to see if a get_cell callback was defined | |
|
436 | // for the message. get_cell callbacks are registered for | |
|
437 | // widget messages, so this block is actually checking to see if the | |
|
438 | // message was triggered by a widget. | |
|
439 | var kernel = this.comm_manager.kernel; | |
|
440 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
|
441 | if (callbacks != undefined && | |
|
442 | callbacks.iopub != undefined && | |
|
443 | callbacks.iopub.get_cell != undefined) { | |
|
444 | ||
|
445 | return callbacks.iopub.get_cell(); | |
|
446 | } | |
|
447 | ||
|
448 | // Not triggered by a cell or widget (no get_cell callback | |
|
449 | // exists). | |
|
450 | return null; | |
|
451 | }, | |
|
437 | // Second, check to see if a get_cell callback was defined | |
|
438 | // for the message. get_cell callbacks are registered for | |
|
439 | // widget messages, so this block is actually checking to see if the | |
|
440 | // message was triggered by a widget. | |
|
441 | var kernel = this.comm_manager.kernel; | |
|
442 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
|
443 | if (callbacks !== undefined && | |
|
444 | callbacks.iopub !== undefined && | |
|
445 | callbacks.iopub.get_cell !== undefined) { | |
|
446 | ||
|
447 | return callbacks.iopub.get_cell(); | |
|
448 | } | |
|
449 | ||
|
450 | // Not triggered by a cell or widget (no get_cell callback | |
|
451 | // exists). | |
|
452 | return null; | |
|
453 | }, | |
|
452 | 454 | |
|
453 | }); | |
|
455 | }); | |
|
454 | 456 | |
|
455 | 457 | |
|
456 | //-------------------------------------------------------------------- | |
|
457 | // WidgetView class | |
|
458 | //-------------------------------------------------------------------- | |
|
459 | var WidgetView = Backbone.View.extend({ | |
|
460 | ||
|
461 | initialize: function() { | |
|
462 | this.visible = true; | |
|
463 | this.model.on('sync',this.update,this); | |
|
464 | }, | |
|
458 | //-------------------------------------------------------------------- | |
|
459 | // WidgetView class | |
|
460 | //-------------------------------------------------------------------- | |
|
461 | var WidgetView = Backbone.View.extend({ | |
|
462 | ||
|
463 | initialize: function() { | |
|
464 | this.visible = true; | |
|
465 | this.model.on('sync',this.update,this); | |
|
466 | }, | |
|
467 | ||
|
468 | add_class: function(selector, class_list){ | |
|
469 | var elements = this._get_selector_element(selector); | |
|
470 | if (elements.length > 0) { | |
|
471 | elements.addClass(class_list); | |
|
472 | } | |
|
473 | }, | |
|
474 | ||
|
475 | remove_class: function(selector, class_list){ | |
|
476 | var elements = this._get_selector_element(selector); | |
|
477 | if (elements.length > 0) { | |
|
478 | elements.removeClass(class_list); | |
|
479 | } | |
|
480 | }, | |
|
465 | 481 | |
|
466 | add_class: function(selector, class_list){ | |
|
467 | var elements = this._get_selector_element(selector); | |
|
468 | if (elements.length > 0) { | |
|
469 | elements.addClass(class_list); | |
|
470 | } | |
|
471 | }, | |
|
472 | 482 | |
|
473 | remove_class: function(selector, class_list){ | |
|
474 | var elements = this._get_selector_element(selector); | |
|
475 | if (elements.length > 0) { | |
|
476 | elements.removeClass(class_list); | |
|
477 | } | |
|
478 | }, | |
|
479 | ||
|
480 | ||
|
481 | send: function(content) { | |
|
482 | this.model.send(content, this.cell); | |
|
483 | }, | |
|
484 | ||
|
485 | update: function() { | |
|
486 | if (this.model.get('visible') != undefined) { | |
|
487 | if (this.visible != this.model.get('visible')) { | |
|
488 | this.visible = this.model.get('visible'); | |
|
489 | if (this.visible) { | |
|
490 | this.$el.show(); | |
|
491 | } else { | |
|
492 | this.$el.hide(); | |
|
483 | send: function(content) { | |
|
484 | this.model.send(content, this.cell); | |
|
485 | }, | |
|
486 | ||
|
487 | update: function() { | |
|
488 | if (this.model.get('visible') !== undefined) { | |
|
489 | if (this.visible != this.model.get('visible')) { | |
|
490 | this.visible = this.model.get('visible'); | |
|
491 | if (this.visible) { | |
|
492 | this.$el.show(); | |
|
493 | } else { | |
|
494 | this.$el.hide(); | |
|
495 | } | |
|
493 | 496 | } |
|
494 | 497 | } |
|
495 | } | |
|
496 | 498 | |
|
497 | if (this.model.css != undefined) { | |
|
498 | for (var selector in this.model.css) { | |
|
499 | if (this.model.css.hasOwnProperty(selector)) { | |
|
500 | ||
|
501 | // Apply the css traits to all elements that match the selector. | |
|
502 | var elements = this._get_selector_element(selector); | |
|
503 | if (elements.length > 0) { | |
|
504 | var css_traits = this.model.css[selector]; | |
|
505 | for (var css_key in css_traits) { | |
|
506 | if (css_traits.hasOwnProperty(css_key)) { | |
|
507 | elements.css(css_key, css_traits[css_key]); | |
|
499 | if (this.model.css !== undefined) { | |
|
500 | for (var selector in this.model.css) { | |
|
501 | if (this.model.css.hasOwnProperty(selector)) { | |
|
502 | ||
|
503 | // Apply the css traits to all elements that match the selector. | |
|
504 | var elements = this._get_selector_element(selector); | |
|
505 | if (elements.length > 0) { | |
|
506 | var css_traits = this.model.css[selector]; | |
|
507 | for (var css_key in css_traits) { | |
|
508 | if (css_traits.hasOwnProperty(css_key)) { | |
|
509 | elements.css(css_key, css_traits[css_key]); | |
|
510 | } | |
|
508 | 511 | } |
|
509 | 512 | } |
|
510 | 513 | } |
|
511 | 514 | } |
|
512 | 515 | } |
|
513 | } | |
|
514 | }, | |
|
515 | ||
|
516 | _get_selector_element: function(selector) { | |
|
517 | // Get the elements via the css selector. If the selector is | |
|
518 | // blank, apply the style to the $el_to_style element. If | |
|
519 | // the $el_to_style element is not defined, use apply the | |
|
520 | // style to the view's element. | |
|
521 | var elements = this.$el.find(selector); | |
|
522 | if (selector===undefined || selector===null || selector=='') { | |
|
523 | if (this.$el_to_style == undefined) { | |
|
524 |
|
|
|
525 | } else { | |
|
526 | elements = this.$el_to_style; | |
|
516 | }, | |
|
517 | ||
|
518 | _get_selector_element: function(selector) { | |
|
519 | // Get the elements via the css selector. If the selector is | |
|
520 | // blank, apply the style to the $el_to_style element. If | |
|
521 | // the $el_to_style element is not defined, use apply the | |
|
522 | // style to the view's element. | |
|
523 | var elements = this.$el.find(selector); | |
|
524 | if (selector === undefined || selector === null || selector === '') { | |
|
525 | if (this.$el_to_style === undefined) { | |
|
526 | elements = this.$el; | |
|
527 | } else { | |
|
528 | elements = this.$el_to_style; | |
|
529 | } | |
|
527 | 530 | } |
|
528 | } | |
|
529 | return elements; | |
|
530 |
} |
|
|
531 | }); | |
|
531 | return elements; | |
|
532 | }, | |
|
533 | }); | |
|
534 | ||
|
535 | ||
|
536 | //-------------------------------------------------------------------- | |
|
537 | // WidgetManager class | |
|
538 | //-------------------------------------------------------------------- | |
|
539 | var WidgetManager = function(){ | |
|
540 | this.comm_manager = null; | |
|
541 | this.widget_model_types = {}; | |
|
542 | this.widget_view_types = {}; | |
|
543 | ||
|
544 | var that = this; | |
|
545 | Backbone.sync = function(method, model, options, error) { | |
|
546 | var result = model._handle_sync(method, options); | |
|
547 | if (options.success) { | |
|
548 | options.success(result); | |
|
549 | } | |
|
550 | }; | |
|
551 | }; | |
|
532 | 552 | |
|
533 | 553 | |
|
534 | //-------------------------------------------------------------------- | |
|
535 | // WidgetManager class | |
|
536 | //-------------------------------------------------------------------- | |
|
537 | var WidgetManager = function(){ | |
|
538 | this.comm_manager = null; | |
|
539 | this.widget_model_types = {}; | |
|
540 | this.widget_view_types = {}; | |
|
541 | ||
|
542 | var that = this; | |
|
543 | Backbone.sync = function(method, model, options, error) { | |
|
544 | var result = model._handle_sync(method, options); | |
|
545 | if (options.success) { | |
|
546 | options.success(result); | |
|
554 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { | |
|
555 | this.comm_manager = comm_manager; | |
|
556 | ||
|
557 | // Register already register widget model types with the comm manager. | |
|
558 | for (var widget_model_name in this.widget_model_types) { | |
|
559 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); | |
|
547 | 560 | } |
|
548 |
}; |
|
|
549 | } | |
|
561 | }; | |
|
550 | 562 | |
|
551 | 563 | |
|
552 |
WidgetManager.prototype. |
|
|
553 | this.comm_manager = comm_manager; | |
|
564 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
|
565 | // Register the widget with the comm manager. Make sure to pass this object's context | |
|
566 | // in so `this` works in the call back. | |
|
567 | if (this.comm_manager !== null) { | |
|
568 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); | |
|
569 | } | |
|
570 | this.widget_model_types[widget_model_name] = widget_model_type; | |
|
571 | }; | |
|
554 | 572 | |
|
555 | // Register already register widget model types with the comm manager. | |
|
556 | for (var widget_model_name in this.widget_model_types) { | |
|
557 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); | |
|
558 | } | |
|
559 | } | |
|
560 | 573 | |
|
574 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
|
575 | this.widget_view_types[widget_view_name] = widget_view_type; | |
|
576 | }; | |
|
561 | 577 | |
|
562 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
|
563 | // Register the widget with the comm manager. Make sure to pass this object's context | |
|
564 | // in so `this` works in the call back. | |
|
565 | if (this.comm_manager!=null) { | |
|
566 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); | |
|
567 | } | |
|
568 | this.widget_model_types[widget_model_name] = widget_model_type; | |
|
569 | } | |
|
570 | 578 | |
|
571 | ||
|
572 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
|
573 | this.widget_view_types[widget_view_name] = widget_view_type; | |
|
574 | } | |
|
579 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
|
580 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
|
581 | return IPython.notebook.get_msg_cell(msg_id); | |
|
582 | } | |
|
583 | }; | |
|
584 | ||
|
585 | ||
|
586 | WidgetManager.prototype.on_create_widget = function (callback) { | |
|
587 | this._create_widget_callback = callback; | |
|
588 | }; | |
|
575 | 589 | |
|
576 | 590 | |
|
577 |
WidgetManager.prototype. |
|
|
578 | if (IPython.notebook != undefined && IPython.notebook != null) { | |
|
579 | return IPython.notebook.get_msg_cell(msg_id); | |
|
580 | } | |
|
581 | } | |
|
582 | ||
|
583 | ||
|
584 | WidgetManager.prototype.on_create_widget = function (callback) { | |
|
585 | this._create_widget_callback = callback; | |
|
586 | } | |
|
587 | ||
|
588 | ||
|
589 | WidgetManager.prototype._handle_create_widget = function (widget_model) { | |
|
590 | if (this._create_widget_callback) { | |
|
591 | try { | |
|
592 | this._create_widget_callback(widget_model); | |
|
593 | } catch (e) { | |
|
594 | console.log("Exception in WidgetManager callback", e, widget_model); | |
|
591 | WidgetManager.prototype._handle_create_widget = function (widget_model) { | |
|
592 | if (this._create_widget_callback) { | |
|
593 | try { | |
|
594 | this._create_widget_callback(widget_model); | |
|
595 | } catch (e) { | |
|
596 | console.log("Exception in WidgetManager callback", e, widget_model); | |
|
597 | } | |
|
595 | 598 | } |
|
596 | } | |
|
597 | } | |
|
599 | }; | |
|
598 | 600 | |
|
599 | 601 | |
|
600 | WidgetManager.prototype._handle_com_open = function (comm, msg) { | |
|
601 | var widget_type_name = msg.content.target_name; | |
|
602 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this); | |
|
603 | this._handle_create_widget(widget_model); | |
|
604 | } | |
|
605 | ||
|
602 | WidgetManager.prototype._handle_com_open = function (comm, msg) { | |
|
603 | var widget_type_name = msg.content.target_name; | |
|
604 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this); | |
|
605 | this._handle_create_widget(widget_model); | |
|
606 | }; | |
|
607 | ||
|
606 | 608 | |
|
607 | //-------------------------------------------------------------------- | |
|
608 | // Init code | |
|
609 | //-------------------------------------------------------------------- | |
|
610 | IPython.WidgetManager = WidgetManager; | |
|
611 | IPython.WidgetModel = WidgetModel; | |
|
612 | IPython.WidgetView = WidgetView; | |
|
609 | //-------------------------------------------------------------------- | |
|
610 | // Init code | |
|
611 | //-------------------------------------------------------------------- | |
|
612 | IPython.WidgetManager = WidgetManager; | |
|
613 | IPython.WidgetModel = WidgetModel; | |
|
614 | IPython.WidgetView = WidgetView; | |
|
613 | 615 | |
|
614 | if (IPython.widget_manager==undefined || IPython.widget_manager==null) { | |
|
615 | IPython.widget_manager = new WidgetManager(); | |
|
616 | } | |
|
616 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { | |
|
617 | IPython.widget_manager = new WidgetManager(); | |
|
618 | } | |
|
617 | 619 | |
|
618 | return IPython.widget_manager; | |
|
619 | }); | |
|
620 | return IPython.widget_manager; | |
|
621 | }); | |
|
622 | }()); No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now