Show More
@@ -1,464 +1,454 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 | "use strict"; |
|
18 | "use strict"; | |
19 |
|
19 | |||
20 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
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' |
|
21 | // syncronously load everything within this file when it is being 'required' | |
22 | // elsewhere. |
|
22 | // elsewhere. | |
23 | define(["components/underscore/underscore-min", |
|
23 | define(["components/underscore/underscore-min", | |
24 | "components/backbone/backbone-min", |
|
24 | "components/backbone/backbone-min", | |
25 | ], function(){ |
|
25 | ], function(){ | |
26 |
|
26 | |||
27 | // Only run once on a notebook. |
|
27 | // Only run once on a notebook. | |
28 | if (IPython.notebook.widget_manager == undefined) { |
|
28 | if (IPython.notebook.widget_manager == undefined) { | |
29 |
|
29 | |||
30 | //-------------------------------------------------------------------- |
|
30 | //-------------------------------------------------------------------- | |
31 | // WidgetModel class |
|
31 | // WidgetModel class | |
32 | //-------------------------------------------------------------------- |
|
32 | //-------------------------------------------------------------------- | |
33 | var WidgetModel = Backbone.Model.extend({ |
|
33 | var WidgetModel = Backbone.Model.extend({ | |
34 | constructor: function(comm_manager, comm, widget_view_types) { |
|
34 | constructor: function(comm_manager, comm, widget_view_types) { | |
35 | this.comm_manager = comm_manager; |
|
35 | this.comm_manager = comm_manager; | |
36 | this.widget_view_types = widget_view_types; |
|
36 | this.widget_view_types = widget_view_types; | |
37 | this.pending_msgs = 0; |
|
37 | this.pending_msgs = 0; | |
38 | this.msg_throttle = 3; |
|
38 | this.msg_throttle = 3; | |
39 | this.msg_buffer = {}; |
|
39 | this.msg_buffer = {}; | |
40 | this.views = {}; |
|
40 | this.views = {}; | |
41 |
|
41 | |||
42 | // Remember comm associated with the model. |
|
42 | // Remember comm associated with the model. | |
43 | this.comm = comm; |
|
43 | this.comm = comm; | |
44 | comm.model = this; |
|
44 | comm.model = this; | |
45 |
|
45 | |||
46 | // Hook comm messages up to model. |
|
46 | // Hook comm messages up to model. | |
47 | comm.on_close($.proxy(this.handle_comm_closed, this)); |
|
47 | comm.on_close($.proxy(this.handle_comm_closed, this)); | |
48 | comm.on_msg($.proxy(this.handle_comm_msg, this)); |
|
48 | comm.on_msg($.proxy(this.handle_comm_msg, this)); | |
49 |
|
49 | |||
50 | return Backbone.Model.apply(this); |
|
50 | return Backbone.Model.apply(this); | |
51 | }, |
|
51 | }, | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | update_other_views: function(caller) { |
|
54 | update_other_views: function(caller) { | |
55 | this.last_modified_view = caller; |
|
55 | this.last_modified_view = caller; | |
56 | this.save(this.changedAttributes(), {patch: true}); |
|
56 | this.save(this.changedAttributes(), {patch: true}); | |
57 |
|
57 | |||
58 |
for (var |
|
58 | for (var output_area in this.views) { | |
59 |
var views = this.views[ |
|
59 | var views = this.views[output_area]; | |
60 | for (var view_index in views) { |
|
60 | for (var view_index in views) { | |
61 | var view = views[view_index]; |
|
61 | var view = views[view_index]; | |
62 | if (view !== caller) { |
|
62 | if (view !== caller) { | |
63 | view.update(); |
|
63 | view.update(); | |
64 | } |
|
64 | } | |
65 | } |
|
65 | } | |
66 | } |
|
66 | } | |
67 | }, |
|
67 | }, | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | handle_status: function (output_area, msg) { |
|
70 | handle_status: function (output_area, msg) { | |
71 | //execution_state : ('busy', 'idle', 'starting') |
|
71 | //execution_state : ('busy', 'idle', 'starting') | |
72 | if (msg.content.execution_state=='idle') { |
|
72 | if (msg.content.execution_state=='idle') { | |
73 |
|
73 | |||
74 | // Send buffer if this message caused another message to be |
|
74 | // Send buffer if this message caused another message to be | |
75 | // throttled. |
|
75 | // throttled. | |
76 | if (this.msg_throttle == this.pending_msgs && |
|
76 | if (this.msg_throttle == this.pending_msgs && | |
77 | this.msg_buffer.length > 0) { |
|
77 | this.msg_buffer.length > 0) { | |
78 |
|
78 | |||
79 | var output_area = this._get_msg_output_area(msg); |
|
79 | var output_area = this._get_msg_output_area(msg); | |
80 | var callbacks = this._make_callbacks(output_area); |
|
80 | var callbacks = this._make_callbacks(output_area); | |
81 | var data = {sync_method: 'patch', sync_data: this.msg_buffer}; |
|
81 | var data = {sync_method: 'patch', sync_data: this.msg_buffer}; | |
82 | comm.send(data, callbacks); |
|
82 | comm.send(data, callbacks); | |
83 | this.msg_buffer = {}; |
|
83 | this.msg_buffer = {}; | |
84 | } else { |
|
84 | } else { | |
85 |
|
85 | |||
86 | // Only decrease the pending message count if the buffer |
|
86 | // Only decrease the pending message count if the buffer | |
87 | // doesn't get flushed (sent). |
|
87 | // doesn't get flushed (sent). | |
88 | --this.pending_msgs; |
|
88 | --this.pending_msgs; | |
89 | } |
|
89 | } | |
90 | } |
|
90 | } | |
91 | }, |
|
91 | }, | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | // Custom syncronization logic. |
|
94 | // Custom syncronization logic. | |
95 | handle_sync: function (method, options) { |
|
95 | handle_sync: function (method, options) { | |
96 | var model_json = this.toJSON(); |
|
96 | var model_json = this.toJSON(); | |
97 |
|
97 | |||
98 | // Only send updated state if the state hasn't been changed |
|
98 | // Only send updated state if the state hasn't been changed | |
99 | // during an update. |
|
99 | // during an update. | |
100 | if (!this.updating) { |
|
100 | if (!this.updating) { | |
101 | if (this.pending_msgs >= this.msg_throttle) { |
|
101 | if (this.pending_msgs >= this.msg_throttle) { | |
102 | // The throttle has been exceeded, buffer the current msg so |
|
102 | // The throttle has been exceeded, buffer the current msg so | |
103 | // it can be sent once the kernel has finished processing |
|
103 | // it can be sent once the kernel has finished processing | |
104 | // some of the existing messages. |
|
104 | // some of the existing messages. | |
105 | if (method=='patch') { |
|
105 | if (method=='patch') { | |
106 | for (var attr in options.attrs) { |
|
106 | for (var attr in options.attrs) { | |
107 | this.msg_buffer[attr] = options.attrs[attr]; |
|
107 | this.msg_buffer[attr] = options.attrs[attr]; | |
108 | } |
|
108 | } | |
109 | } else { |
|
109 | } else { | |
110 | this.msg_buffer = $.extend({}, model_json); // Copy |
|
110 | this.msg_buffer = $.extend({}, model_json); // Copy | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | } else { |
|
113 | } else { | |
114 | // We haven't exceeded the throttle, send the message like |
|
114 | // We haven't exceeded the throttle, send the message like | |
115 | // normal. If this is a patch operation, just send the |
|
115 | // normal. If this is a patch operation, just send the | |
116 | // changes. |
|
116 | // changes. | |
117 | var send_json = model_json; |
|
117 | var send_json = model_json; | |
118 | if (method=='patch') { |
|
118 | if (method=='patch') { | |
119 | send_json = {}; |
|
119 | send_json = {}; | |
120 | for (var attr in options.attrs) { |
|
120 | for (var attr in options.attrs) { | |
121 | send_json[attr] = options.attrs[attr]; |
|
121 | send_json[attr] = options.attrs[attr]; | |
122 | } |
|
122 | } | |
123 | } |
|
123 | } | |
124 |
|
124 | |||
125 | var data = {sync_method: method, sync_data: send_json}; |
|
125 | var data = {sync_method: method, sync_data: send_json}; | |
126 |
var output_area = this. |
|
126 | var output_area = this.last_modified_view.output_area; | |
127 | var callbacks = this._make_callbacks(output_area); |
|
127 | var callbacks = this._make_callbacks(output_area); | |
128 | this.comm.send(data, callbacks); |
|
128 | this.comm.send(data, callbacks); | |
129 | this.pending_msgs++; |
|
129 | this.pending_msgs++; | |
130 | } |
|
130 | } | |
131 | } |
|
131 | } | |
132 |
|
132 | |||
133 | // Since the comm is a one-way communication, assume the message |
|
133 | // Since the comm is a one-way communication, assume the message | |
134 | // arrived. |
|
134 | // arrived. | |
135 | return model_json; |
|
135 | return model_json; | |
136 | }, |
|
136 | }, | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | // Handle incomming comm msg. |
|
139 | // Handle incomming comm msg. | |
140 | handle_comm_msg: function (msg) { |
|
140 | handle_comm_msg: function (msg) { | |
141 | var method = msg.content.data.method; |
|
141 | var method = msg.content.data.method; | |
142 | switch (method){ |
|
142 | switch (method){ | |
143 | case 'display': |
|
143 | case 'display': | |
144 |
|
144 | |||
145 | // Try to get the cell index. |
|
145 | // Try to get the cell index. | |
146 |
var |
|
146 | var output_area = this._get_output_area(msg.parent_header.msg_id); | |
147 |
if ( |
|
147 | if (output_area == null) { | |
148 | console.log("Could not determine where the display" + |
|
148 | console.log("Could not determine where the display" + | |
149 | " message was from. Widget will not be displayed") |
|
149 | " message was from. Widget will not be displayed") | |
150 | } else { |
|
150 | } else { | |
151 | this.display_view(msg.content.data.view_name, |
|
151 | this.display_view(msg.content.data.view_name, | |
152 | msg.content.data.parent, |
|
152 | msg.content.data.parent, | |
153 |
|
|
153 | output_area); | |
154 | } |
|
154 | } | |
155 | break; |
|
155 | break; | |
156 | case 'update': |
|
156 | case 'update': | |
157 | this.handle_update(msg.content.data.state); |
|
157 | this.handle_update(msg.content.data.state); | |
158 | break; |
|
158 | break; | |
159 | } |
|
159 | } | |
160 | }, |
|
160 | }, | |
161 |
|
161 | |||
162 |
|
162 | |||
163 | // Handle when a widget is updated via the python side. |
|
163 | // Handle when a widget is updated via the python side. | |
164 | handle_update: function (state) { |
|
164 | handle_update: function (state) { | |
165 | this.updating = true; |
|
165 | this.updating = true; | |
166 | try { |
|
166 | try { | |
167 | for (var key in state) { |
|
167 | for (var key in state) { | |
168 | if (state.hasOwnProperty(key)) { |
|
168 | if (state.hasOwnProperty(key)) { | |
169 | if (key == "_css"){ |
|
169 | if (key == "_css"){ | |
170 | this.css = state[key]; |
|
170 | this.css = state[key]; | |
171 | } else { |
|
171 | } else { | |
172 | this.set(key, state[key]); |
|
172 | this.set(key, state[key]); | |
173 | } |
|
173 | } | |
174 | } |
|
174 | } | |
175 | } |
|
175 | } | |
176 | this.id = this.comm.comm_id; |
|
176 | this.id = this.comm.comm_id; | |
177 | this.save(); |
|
177 | this.save(); | |
178 | } finally { |
|
178 | } finally { | |
179 | this.updating = false; |
|
179 | this.updating = false; | |
180 | } |
|
180 | } | |
181 | }, |
|
181 | }, | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | // Handle when a widget is closed. |
|
184 | // Handle when a widget is closed. | |
185 | handle_comm_closed: function (msg) { |
|
185 | handle_comm_closed: function (msg) { | |
186 |
for (var |
|
186 | for (var output_area in this.views) { | |
187 |
var views = this.views[ |
|
187 | var views = this.views[output_area]; | |
188 | for (var view_index in views) { |
|
188 | for (var view_index in views) { | |
189 | var view = views[view_index]; |
|
189 | var view = views[view_index]; | |
190 | view.remove(); |
|
190 | view.remove(); | |
191 | } |
|
191 | } | |
192 | } |
|
192 | } | |
193 | }, |
|
193 | }, | |
194 |
|
194 | |||
195 |
|
195 | |||
196 | // Create view that represents the model. |
|
196 | // Create view that represents the model. | |
197 |
display_view: function (view_name, parent_comm_id, |
|
197 | display_view: function (view_name, parent_comm_id, output_area) { | |
198 | var new_views = []; |
|
198 | var new_views = []; | |
199 |
|
199 | |||
200 | var displayed = false; |
|
200 | var displayed = false; | |
201 | if (parent_comm_id != undefined) { |
|
201 | if (parent_comm_id != undefined) { | |
202 | var parent_comm = this.comm_manager.comms[parent_comm_id]; |
|
202 | var parent_comm = this.comm_manager.comms[parent_comm_id]; | |
203 | var parent_model = parent_comm.model; |
|
203 | var parent_model = parent_comm.model; | |
204 |
var parent_views = parent_model.views[ |
|
204 | var parent_views = parent_model.views[output_area]; | |
205 | for (var parent_view_index in parent_views) { |
|
205 | for (var parent_view_index in parent_views) { | |
206 | var parent_view = parent_views[parent_view_index]; |
|
206 | var parent_view = parent_views[parent_view_index]; | |
207 | if (parent_view.display_child != undefined) { |
|
207 | if (parent_view.display_child != undefined) { | |
208 |
var view = this._create_view(view_name, |
|
208 | var view = this._create_view(view_name, output_area); | |
209 | new_views.push(view); |
|
209 | new_views.push(view); | |
210 | parent_view.display_child(view); |
|
210 | parent_view.display_child(view); | |
211 | displayed = true; |
|
211 | displayed = true; | |
212 | } |
|
212 | } | |
213 | } |
|
213 | } | |
214 | } |
|
214 | } | |
215 |
|
215 | |||
216 | if (!displayed) { |
|
216 | if (!displayed) { | |
217 | // No parent view is defined or exists. Add the view's |
|
217 | // No parent view is defined or exists. Add the view's | |
218 | // element to cell's widget div. |
|
218 | // element to cell's widget div. | |
219 |
var view = this._create_view(view_name, |
|
219 | var view = this._create_view(view_name, output_area); | |
220 | new_views.push(view); |
|
220 | new_views.push(view); | |
221 | var cell = IPython.notebook.get_cell(cell_index); |
|
221 | output_area.element.find('.widget-area').find('.widget-subarea') | |
222 | cell.element.find('.widget-area').find('.widget-subarea') |
|
|||
223 | .append(view.$el) |
|
222 | .append(view.$el) | |
224 | .parent().show(); // Show the widget_area (parent of widget_subarea) |
|
223 | .parent().show(); // Show the widget_area (parent of widget_subarea) | |
225 |
|
224 | |||
226 | } |
|
225 | } | |
227 |
|
226 | |||
228 | for (var view_index in new_views) { |
|
227 | for (var view_index in new_views) { | |
229 | var view = new_views[view_index]; |
|
228 | var view = new_views[view_index]; | |
230 | view.update(); |
|
229 | view.update(); | |
231 | } |
|
230 | } | |
232 | }, |
|
231 | }, | |
233 |
|
232 | |||
234 |
|
233 | |||
235 | // Create a view |
|
234 | // Create a view | |
236 |
_create_view: function (view_name, |
|
235 | _create_view: function (view_name, output_area) { | |
237 | var view = new this.widget_view_types[view_name]({model: this}); |
|
236 | var view = new this.widget_view_types[view_name]({model: this}); | |
238 | view.render(); |
|
237 | view.render(); | |
239 |
if (this.views[ |
|
238 | if (this.views[output_area]==undefined) { | |
240 |
this.views[ |
|
239 | this.views[output_area] = [] | |
241 | } |
|
240 | } | |
242 |
this.views[ |
|
241 | this.views[output_area].push(view); | |
243 | view.cell_index = cell_index; |
|
242 | view.output_area = output_area; | |
244 |
|
243 | |||
245 | // Handle when the view element is remove from the page. |
|
244 | // Handle when the view element is remove from the page. | |
246 | var that = this; |
|
245 | var that = this; | |
247 | view.$el.on("remove", function(){ |
|
246 | view.$el.on("remove", function(){ | |
248 |
var index = that.views[ |
|
247 | var index = that.views[output_area].indexOf(view); | |
249 | if (index > -1) { |
|
248 | if (index > -1) { | |
250 |
that.views[ |
|
249 | that.views[output_area].splice(index, 1); | |
251 | } |
|
250 | } | |
252 | view.remove(); // Clean-up view |
|
251 | view.remove(); // Clean-up view | |
253 |
if (that.views[ |
|
252 | if (that.views[output_area].length()==0) { | |
254 |
delete that.views[ |
|
253 | delete that.views[output_area]; | |
255 | } |
|
254 | } | |
256 |
|
255 | |||
257 | // Close the comm if there are no views left. |
|
256 | // Close the comm if there are no views left. | |
258 | if (that.views.length()==0) { |
|
257 | if (that.views.length()==0) { | |
259 | that.comm.close(); |
|
258 | that.comm.close(); | |
260 | } |
|
259 | } | |
261 | }); |
|
260 | }); | |
262 | return view; |
|
261 | return view; | |
263 | }, |
|
262 | }, | |
264 |
|
263 | |||
265 |
|
264 | |||
266 | // Build a callback dict. |
|
265 | // Build a callback dict. | |
267 | _make_callbacks: function (output_area) { |
|
266 | _make_callbacks: function (output_area) { | |
268 | var callbacks = {}; |
|
267 | var callbacks = {}; | |
269 | if (output_area != null) { |
|
268 | if (output_area != null) { | |
270 | var that = this; |
|
269 | var that = this; | |
271 | callbacks = { |
|
270 | callbacks = { | |
272 | iopub : { |
|
271 | iopub : { | |
273 | output : $.proxy(output_area.handle_output, output_area), |
|
272 | output : $.proxy(output_area.handle_output, output_area), | |
274 | clear_output : $.proxy(output_area.handle_clear_output, output_area), |
|
273 | clear_output : $.proxy(output_area.handle_clear_output, output_area), | |
275 | status : function(msg){ |
|
274 | status : function(msg){ | |
276 | that.handle_status(output_area, msg); |
|
275 | that.handle_status(output_area, msg); | |
277 | }, |
|
276 | }, | |
278 |
get_ |
|
277 | get_output_area : function() { | |
279 | if (that.last_modified_view != undefined && |
|
278 | if (that.last_modified_view != undefined && | |
280 |
that.last_modified_view. |
|
279 | that.last_modified_view.output_area != undefined) { | |
281 |
return that.last_modified_view. |
|
280 | return that.last_modified_view.output_area; | |
282 | } else { |
|
281 | } else { | |
283 |
return |
|
282 | return null | |
284 | } |
|
283 | } | |
285 | }, |
|
284 | }, | |
286 | }, |
|
285 | }, | |
287 | }; |
|
286 | }; | |
288 | } |
|
287 | } | |
289 | return callbacks; |
|
288 | return callbacks; | |
290 | }, |
|
289 | }, | |
291 |
|
290 | |||
292 |
|
291 | |||
293 | // Get the cell index corresponding to the msg_id. |
|
292 | // Get the cell index corresponding to the msg_id. | |
294 | _get_cell_index: function (msg_id) { |
|
293 | // output_area is a JQuery DOM element handle that has widget_area | |
|
294 | // and nested widget_subarea elements. | |||
|
295 | _get_output_area: function (msg_id) { | |||
295 |
|
296 | |||
296 | // First, guess cell.execute triggered |
|
297 | // First, guess cell.execute triggered | |
297 | var cells = IPython.notebook.get_cells(); |
|
298 | var cells = IPython.notebook.get_cells(); | |
298 | for (var cell_index in cells) { |
|
299 | for (var cell_index in cells) { | |
299 | if (cells[cell_index].last_msg_id == msg_id) { |
|
300 | if (cells[cell_index].last_msg_id == msg_id) { | |
300 |
r |
|
301 | var cell = IPython.notebook.get_cell(cell_index) | |
|
302 | return cell.output_area; | |||
301 | } |
|
303 | } | |
302 | } |
|
304 | } | |
303 |
|
305 | |||
304 | // Second, guess widget triggered |
|
306 | // Second, guess widget triggered | |
305 | var callbacks = this.comm_manager.kernel.get_callbacks_for_msg(msg_id) |
|
307 | var callbacks = this.comm_manager.kernel.get_callbacks_for_msg(msg_id) | |
306 |
if (callbacks != undefined && callbacks.iopub != undefined && callbacks.iopub.get_ |
|
308 | if (callbacks != undefined && callbacks.iopub != undefined && callbacks.iopub.get_output_area != undefined) { | |
307 |
var |
|
309 | var output_area = callbacks.iopub.get_output_area(); | |
308 |
if ( |
|
310 | if (output_area != null) { | |
309 |
return |
|
311 | return output_area; | |
310 | } |
|
312 | } | |
311 | } |
|
313 | } | |
312 |
|
314 | |||
313 | // Not triggered by a widget or a cell |
|
315 | // Not triggered by a widget or a cell | |
314 |
return |
|
316 | return null; | |
315 | }, |
|
|||
316 |
|
||||
317 |
|
||||
318 | // Get the cell output area corresponding to the view. |
|
|||
319 | _get_view_output_area: function (view) { |
|
|||
320 | return this._get_cell_output_area(view.cell_index); |
|
|||
321 | }, |
|
317 | }, | |
322 |
|
318 | |||
323 |
|
||||
324 | // Get the cell output area corresponding to the cell index. |
|
|||
325 | _get_cell_output_area: function (cell_index) { |
|
|||
326 | var cell = IPython.notebook.get_cell(cell_index) |
|
|||
327 | return cell.output_area; |
|
|||
328 | }, |
|
|||
329 | }); |
|
319 | }); | |
330 |
|
320 | |||
331 |
|
321 | |||
332 | //-------------------------------------------------------------------- |
|
322 | //-------------------------------------------------------------------- | |
333 | // WidgetView class |
|
323 | // WidgetView class | |
334 | //-------------------------------------------------------------------- |
|
324 | //-------------------------------------------------------------------- | |
335 | var WidgetView = Backbone.View.extend({ |
|
325 | var WidgetView = Backbone.View.extend({ | |
336 |
|
326 | |||
337 | initialize: function() { |
|
327 | initialize: function() { | |
338 | this.visible = true; |
|
328 | this.visible = true; | |
339 | this.model.on('change',this.update,this); |
|
329 | this.model.on('change',this.update,this); | |
340 | this._add_class_calls = this.model.get('_add_class')[0]; |
|
330 | this._add_class_calls = this.model.get('_add_class')[0]; | |
341 | this._remove_class_calls = this.model.get('_remove_class')[0]; |
|
331 | this._remove_class_calls = this.model.get('_remove_class')[0]; | |
342 | }, |
|
332 | }, | |
343 |
|
333 | |||
344 | update: function() { |
|
334 | update: function() { | |
345 | if (this.model.get('visible') != undefined) { |
|
335 | if (this.model.get('visible') != undefined) { | |
346 | if (this.visible != this.model.get('visible')) { |
|
336 | if (this.visible != this.model.get('visible')) { | |
347 | this.visible = this.model.get('visible'); |
|
337 | this.visible = this.model.get('visible'); | |
348 | if (this.visible) { |
|
338 | if (this.visible) { | |
349 | this.$el.show(); |
|
339 | this.$el.show(); | |
350 | } else { |
|
340 | } else { | |
351 | this.$el.hide(); |
|
341 | this.$el.hide(); | |
352 | } |
|
342 | } | |
353 | } |
|
343 | } | |
354 | } |
|
344 | } | |
355 |
|
345 | |||
356 | if (this.model.css != undefined) { |
|
346 | if (this.model.css != undefined) { | |
357 | for (var selector in this.model.css) { |
|
347 | for (var selector in this.model.css) { | |
358 | if (this.model.css.hasOwnProperty(selector)) { |
|
348 | if (this.model.css.hasOwnProperty(selector)) { | |
359 |
|
349 | |||
360 | // Apply the css traits to all elements that match the selector. |
|
350 | // Apply the css traits to all elements that match the selector. | |
361 | var elements = this.get_selector_element(selector); |
|
351 | var elements = this.get_selector_element(selector); | |
362 | if (elements.length > 0) { |
|
352 | if (elements.length > 0) { | |
363 | var css_traits = this.model.css[selector]; |
|
353 | var css_traits = this.model.css[selector]; | |
364 | for (var css_key in css_traits) { |
|
354 | for (var css_key in css_traits) { | |
365 | if (css_traits.hasOwnProperty(css_key)) { |
|
355 | if (css_traits.hasOwnProperty(css_key)) { | |
366 | elements.css(css_key, css_traits[css_key]); |
|
356 | elements.css(css_key, css_traits[css_key]); | |
367 | } |
|
357 | } | |
368 | } |
|
358 | } | |
369 | } |
|
359 | } | |
370 | } |
|
360 | } | |
371 | } |
|
361 | } | |
372 | } |
|
362 | } | |
373 |
|
363 | |||
374 | var add_class = this.model.get('_add_class'); |
|
364 | var add_class = this.model.get('_add_class'); | |
375 | if (add_class != undefined){ |
|
365 | if (add_class != undefined){ | |
376 | var add_class_calls = add_class[0]; |
|
366 | var add_class_calls = add_class[0]; | |
377 | if (add_class_calls > this._add_class_calls) { |
|
367 | if (add_class_calls > this._add_class_calls) { | |
378 | this._add_class_calls = add_class_calls; |
|
368 | this._add_class_calls = add_class_calls; | |
379 | var elements = this.get_selector_element(add_class[1]); |
|
369 | var elements = this.get_selector_element(add_class[1]); | |
380 | if (elements.length > 0) { |
|
370 | if (elements.length > 0) { | |
381 | elements.addClass(add_class[2]); |
|
371 | elements.addClass(add_class[2]); | |
382 | } |
|
372 | } | |
383 | } |
|
373 | } | |
384 | } |
|
374 | } | |
385 |
|
375 | |||
386 | var remove_class = this.model.get('_remove_class'); |
|
376 | var remove_class = this.model.get('_remove_class'); | |
387 | if (remove_class != undefined){ |
|
377 | if (remove_class != undefined){ | |
388 | var remove_class_calls = remove_class[0]; |
|
378 | var remove_class_calls = remove_class[0]; | |
389 | if (remove_class_calls > this._remove_class_calls) { |
|
379 | if (remove_class_calls > this._remove_class_calls) { | |
390 | this._remove_class_calls = remove_class_calls; |
|
380 | this._remove_class_calls = remove_class_calls; | |
391 | var elements = this.get_selector_element(remove_class[1]); |
|
381 | var elements = this.get_selector_element(remove_class[1]); | |
392 | if (elements.length > 0) { |
|
382 | if (elements.length > 0) { | |
393 | elements.removeClass(remove_class[2]); |
|
383 | elements.removeClass(remove_class[2]); | |
394 | } |
|
384 | } | |
395 | } |
|
385 | } | |
396 | } |
|
386 | } | |
397 | }, |
|
387 | }, | |
398 |
|
388 | |||
399 | get_selector_element: function(selector) { |
|
389 | get_selector_element: function(selector) { | |
400 | // Get the elements via the css selector. If the selector is |
|
390 | // Get the elements via the css selector. If the selector is | |
401 | // blank, apply the style to the $el_to_style element. If |
|
391 | // blank, apply the style to the $el_to_style element. If | |
402 | // the $el_to_style element is not defined, use apply the |
|
392 | // the $el_to_style element is not defined, use apply the | |
403 | // style to the view's element. |
|
393 | // style to the view's element. | |
404 | var elements = this.$el.find(selector); |
|
394 | var elements = this.$el.find(selector); | |
405 | if (selector=='') { |
|
395 | if (selector=='') { | |
406 | if (this.$el_to_style == undefined) { |
|
396 | if (this.$el_to_style == undefined) { | |
407 | elements = this.$el; |
|
397 | elements = this.$el; | |
408 | } else { |
|
398 | } else { | |
409 | elements = this.$el_to_style; |
|
399 | elements = this.$el_to_style; | |
410 | } |
|
400 | } | |
411 | } |
|
401 | } | |
412 | return elements; |
|
402 | return elements; | |
413 | }, |
|
403 | }, | |
414 | }); |
|
404 | }); | |
415 |
|
405 | |||
416 |
|
406 | |||
417 | //-------------------------------------------------------------------- |
|
407 | //-------------------------------------------------------------------- | |
418 | // WidgetManager class |
|
408 | // WidgetManager class | |
419 | //-------------------------------------------------------------------- |
|
409 | //-------------------------------------------------------------------- | |
420 | var WidgetManager = function(comm_manager){ |
|
410 | var WidgetManager = function(comm_manager){ | |
421 | this.comm_manager = comm_manager; |
|
411 | this.comm_manager = comm_manager; | |
422 | this.widget_model_types = {}; |
|
412 | this.widget_model_types = {}; | |
423 | this.widget_view_types = {}; |
|
413 | this.widget_view_types = {}; | |
424 |
|
414 | |||
425 | var that = this; |
|
415 | var that = this; | |
426 | Backbone.sync = function(method, model, options, error) { |
|
416 | Backbone.sync = function(method, model, options, error) { | |
427 | var result = model.handle_sync(method, options); |
|
417 | var result = model.handle_sync(method, options); | |
428 | if (options.success) { |
|
418 | if (options.success) { | |
429 | options.success(result); |
|
419 | options.success(result); | |
430 | } |
|
420 | } | |
431 | }; |
|
421 | }; | |
432 | } |
|
422 | } | |
433 |
|
423 | |||
434 |
|
424 | |||
435 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
425 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
436 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
426 | // Register the widget with the comm manager. Make sure to pass this object's context | |
437 | // in so `this` works in the call back. |
|
427 | // in so `this` works in the call back. | |
438 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); |
|
428 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); | |
439 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
429 | this.widget_model_types[widget_model_name] = widget_model_type; | |
440 | } |
|
430 | } | |
441 |
|
431 | |||
442 |
|
432 | |||
443 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
433 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
444 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
434 | this.widget_view_types[widget_view_name] = widget_view_type; | |
445 | } |
|
435 | } | |
446 |
|
436 | |||
447 |
|
437 | |||
448 | WidgetManager.prototype.handle_com_open = function (comm, msg) { |
|
438 | WidgetManager.prototype.handle_com_open = function (comm, msg) { | |
449 | var widget_type_name = msg.content.target_name; |
|
439 | var widget_type_name = msg.content.target_name; | |
450 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types); |
|
440 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types); | |
451 | } |
|
441 | } | |
452 |
|
442 | |||
453 |
|
443 | |||
454 | //-------------------------------------------------------------------- |
|
444 | //-------------------------------------------------------------------- | |
455 | // Init code |
|
445 | // Init code | |
456 | //-------------------------------------------------------------------- |
|
446 | //-------------------------------------------------------------------- | |
457 | IPython.WidgetManager = WidgetManager; |
|
447 | IPython.WidgetManager = WidgetManager; | |
458 | IPython.WidgetModel = WidgetModel; |
|
448 | IPython.WidgetModel = WidgetModel; | |
459 | IPython.WidgetView = WidgetView; |
|
449 | IPython.WidgetView = WidgetView; | |
460 |
|
450 | |||
461 | IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager); |
|
451 | IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager); | |
462 |
|
452 | |||
463 | }; |
|
453 | }; | |
464 | }); |
|
454 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now