Show More
@@ -1,413 +1,414 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 | // Base Widget Model and View classes |
|
9 | // Base Widget Model and View classes | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 | define(["notebook/js/widgetmanager", |
|
17 | define(["notebook/js/widgetmanager", | |
18 | "underscore", |
|
18 | "underscore", | |
19 | "backbone"], |
|
19 | "backbone"], | |
20 | function(WidgetManager, Underscore, Backbone){ |
|
20 | function(WidgetManager, Underscore, Backbone){ | |
21 |
|
21 | |||
22 | var WidgetModel = Backbone.Model.extend({ |
|
22 | var WidgetModel = Backbone.Model.extend({ | |
23 | constructor: function (widget_manager, model_id, comm) { |
|
23 | constructor: function (widget_manager, model_id, comm) { | |
24 | // Construcctor |
|
24 | // Construcctor | |
25 | // |
|
25 | // | |
26 | // Creates a WidgetModel instance. |
|
26 | // Creates a WidgetModel instance. | |
27 | // |
|
27 | // | |
28 | // Parameters |
|
28 | // Parameters | |
29 | // ---------- |
|
29 | // ---------- | |
30 | // widget_manager : WidgetManager instance |
|
30 | // widget_manager : WidgetManager instance | |
31 | // model_id : string |
|
31 | // model_id : string | |
32 | // An ID unique to this model. |
|
32 | // An ID unique to this model. | |
33 | // comm : Comm instance (optional) |
|
33 | // comm : Comm instance (optional) | |
34 | this.widget_manager = widget_manager; |
|
34 | this.widget_manager = widget_manager; | |
35 | this.pending_msgs = 0; |
|
35 | this.pending_msgs = 0; | |
36 | this.msg_throttle = 2; |
|
36 | this.msg_throttle = 2; | |
37 | this.msg_buffer = null; |
|
37 | this.msg_buffer = null; | |
38 | this.key_value_lock = null; |
|
38 | this.key_value_lock = null; | |
39 | this.id = model_id; |
|
39 | this.id = model_id; | |
40 | this.views = []; |
|
40 | this.views = []; | |
41 |
|
41 | |||
42 | if (comm !== undefined) { |
|
42 | if (comm !== undefined) { | |
43 | // Remember comm associated with the model. |
|
43 | // Remember comm associated with the model. | |
44 | this.comm = comm; |
|
44 | this.comm = comm; | |
45 | comm.model = this; |
|
45 | comm.model = this; | |
46 |
|
46 | |||
47 | // Hook comm messages up to model. |
|
47 | // Hook comm messages up to model. | |
48 | comm.on_close($.proxy(this._handle_comm_closed, this)); |
|
48 | comm.on_close($.proxy(this._handle_comm_closed, this)); | |
49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); |
|
49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); | |
50 | } |
|
50 | } | |
51 | return Backbone.Model.apply(this); |
|
51 | return Backbone.Model.apply(this); | |
52 | }, |
|
52 | }, | |
53 |
|
53 | |||
54 | send: function (content, callbacks) { |
|
54 | send: function (content, callbacks) { | |
55 | // Send a custom msg over the comm. |
|
55 | // Send a custom msg over the comm. | |
56 | if (this.comm !== undefined) { |
|
56 | if (this.comm !== undefined) { | |
57 | var data = {method: 'custom', content: content}; |
|
57 | var data = {method: 'custom', content: content}; | |
58 | this.comm.send(data, callbacks); |
|
58 | this.comm.send(data, callbacks); | |
59 | } |
|
59 | } | |
60 | }, |
|
60 | }, | |
61 |
|
61 | |||
62 | _handle_comm_closed: function (msg) { |
|
62 | _handle_comm_closed: function (msg) { | |
63 | // Handle when a widget is closed. |
|
63 | // Handle when a widget is closed. | |
64 | this.trigger('comm:close'); |
|
64 | this.trigger('comm:close'); | |
65 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
65 | delete this.comm.model; // Delete ref so GC will collect widget model. | |
66 | delete this.comm; |
|
66 | delete this.comm; | |
67 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
67 | delete this.model_id; // Delete id from model so widget manager cleans up. | |
68 | // TODO: Handle deletion, like this.destroy(), and delete views, etc. |
|
68 | // TODO: Handle deletion, like this.destroy(), and delete views, etc. | |
69 | }, |
|
69 | }, | |
70 |
|
70 | |||
71 | _handle_comm_msg: function (msg) { |
|
71 | _handle_comm_msg: function (msg) { | |
72 | // Handle incoming comm msg. |
|
72 | // Handle incoming comm msg. | |
73 | var method = msg.content.data.method; |
|
73 | var method = msg.content.data.method; | |
74 | switch (method) { |
|
74 | switch (method) { | |
75 | case 'update': |
|
75 | case 'update': | |
76 | this.apply_update(msg.content.data.state); |
|
76 | this.apply_update(msg.content.data.state); | |
77 | break; |
|
77 | break; | |
78 | case 'custom': |
|
78 | case 'custom': | |
79 | this.trigger('msg:custom', msg.content.data.content); |
|
79 | this.trigger('msg:custom', msg.content.data.content); | |
80 | break; |
|
80 | break; | |
81 | case 'display': |
|
81 | case 'display': | |
82 | this.widget_manager.display_view(msg, this); |
|
82 | this.widget_manager.display_view(msg, this); | |
83 | break; |
|
83 | break; | |
84 | } |
|
84 | } | |
85 | }, |
|
85 | }, | |
86 |
|
86 | |||
87 | apply_update: function (state) { |
|
87 | apply_update: function (state) { | |
88 | // Handle when a widget is updated via the python side. |
|
88 | // Handle when a widget is updated via the python side. | |
89 | for (var key in state) { |
|
89 | for (var key in state) { | |
90 | if (state.hasOwnProperty(key)) { |
|
90 | if (state.hasOwnProperty(key)) { | |
91 | var value = state[key]; |
|
91 | var value = state[key]; | |
92 | this.key_value_lock = [key, value]; |
|
92 | this.key_value_lock = [key, value]; | |
93 | try { |
|
93 | try { | |
94 | this.set(key, this._unpack_models(value)); |
|
94 | this.set(key, this._unpack_models(value)); | |
95 | } finally { |
|
95 | } finally { | |
96 | this.key_value_lock = null; |
|
96 | this.key_value_lock = null; | |
97 | } |
|
97 | } | |
98 | } |
|
98 | } | |
99 | } |
|
99 | } | |
100 | }, |
|
100 | }, | |
101 |
|
101 | |||
102 | _handle_status: function (msg, callbacks) { |
|
102 | _handle_status: function (msg, callbacks) { | |
103 | // Handle status msgs. |
|
103 | // Handle status msgs. | |
104 |
|
104 | |||
105 | // execution_state : ('busy', 'idle', 'starting') |
|
105 | // execution_state : ('busy', 'idle', 'starting') | |
106 | if (this.comm !== undefined) { |
|
106 | if (this.comm !== undefined) { | |
107 | if (msg.content.execution_state ==='idle') { |
|
107 | if (msg.content.execution_state ==='idle') { | |
108 | // Send buffer if this message caused another message to be |
|
108 | // Send buffer if this message caused another message to be | |
109 | // throttled. |
|
109 | // throttled. | |
110 | if (this.msg_buffer !== null && |
|
110 | if (this.msg_buffer !== null && | |
111 | this.msg_throttle === this.pending_msgs) { |
|
111 | this.msg_throttle === this.pending_msgs) { | |
112 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
112 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | |
113 | this.comm.send(data, callbacks); |
|
113 | this.comm.send(data, callbacks); | |
114 | this.msg_buffer = null; |
|
114 | this.msg_buffer = null; | |
115 | } else { |
|
115 | } else { | |
116 | --this.pending_msgs; |
|
116 | --this.pending_msgs; | |
117 | } |
|
117 | } | |
118 | } |
|
118 | } | |
119 | } |
|
119 | } | |
120 | }, |
|
120 | }, | |
121 |
|
121 | |||
122 | callbacks: function(view) { |
|
122 | callbacks: function(view) { | |
123 | // Create msg callbacks for a comm msg. |
|
123 | // Create msg callbacks for a comm msg. | |
124 | var callbacks = this.widget_manager.callbacks(view); |
|
124 | var callbacks = this.widget_manager.callbacks(view); | |
125 |
|
125 | |||
126 | if (callbacks.iopub === undefined) { |
|
126 | if (callbacks.iopub === undefined) { | |
127 | callbacks.iopub = {}; |
|
127 | callbacks.iopub = {}; | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | var that = this; |
|
130 | var that = this; | |
131 | callbacks.iopub.status = function (msg) { |
|
131 | callbacks.iopub.status = function (msg) { | |
132 | that._handle_status(msg, callbacks); |
|
132 | that._handle_status(msg, callbacks); | |
133 | } |
|
133 | } | |
134 | return callbacks; |
|
134 | return callbacks; | |
135 | }, |
|
135 | }, | |
136 |
|
136 | |||
137 | sync: function (method, model, options) { |
|
137 | sync: function (method, model, options) { | |
138 |
|
138 | |||
139 | // Make sure a comm exists. |
|
139 | // Make sure a comm exists. | |
140 | var error = options.error || function() { |
|
140 | var error = options.error || function() { | |
141 | console.error('Backbone sync error:', arguments); |
|
141 | console.error('Backbone sync error:', arguments); | |
142 | } |
|
142 | } | |
143 | if (this.comm === undefined) { |
|
143 | if (this.comm === undefined) { | |
144 | error(); |
|
144 | error(); | |
145 | return false; |
|
145 | return false; | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | // Delete any key value pairs that the back-end already knows about. |
|
148 | // Delete any key value pairs that the back-end already knows about. | |
149 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); |
|
149 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); | |
150 | if (this.key_value_lock !== null) { |
|
150 | if (this.key_value_lock !== null) { | |
151 | var key = this.key_value_lock[0]; |
|
151 | var key = this.key_value_lock[0]; | |
152 | var value = this.key_value_lock[1]; |
|
152 | var value = this.key_value_lock[1]; | |
153 | if (attrs[key] === value) { |
|
153 | if (attrs[key] === value) { | |
154 | delete attrs[key]; |
|
154 | delete attrs[key]; | |
155 | } |
|
155 | } | |
156 | } |
|
156 | } | |
157 |
|
157 | |||
158 | // Only sync if there are attributes to send to the back-end. |
|
158 | // Only sync if there are attributes to send to the back-end. | |
159 | if (_.size(attrs) !== 0) { |
|
159 | if (_.size(attrs) !== 0) { | |
160 | var callbacks = options.callbacks || {}; |
|
160 | var callbacks = options.callbacks || {}; | |
161 | if (this.pending_msgs >= this.msg_throttle) { |
|
161 | if (this.pending_msgs >= this.msg_throttle) { | |
162 | // The throttle has been exceeded, buffer the current msg so |
|
162 | // The throttle has been exceeded, buffer the current msg so | |
163 | // it can be sent once the kernel has finished processing |
|
163 | // it can be sent once the kernel has finished processing | |
164 | // some of the existing messages. |
|
164 | // some of the existing messages. | |
165 |
|
165 | |||
166 | // Combine updates if it is a 'patch' sync, otherwise replace updates |
|
166 | // Combine updates if it is a 'patch' sync, otherwise replace updates | |
167 | switch (method) { |
|
167 | switch (method) { | |
168 | case 'patch': |
|
168 | case 'patch': | |
169 | this.msg_buffer = _.extend(this.msg_buffer || {}, attrs); |
|
169 | this.msg_buffer = _.extend(this.msg_buffer || {}, attrs); | |
170 | break; |
|
170 | break; | |
171 | case 'update': |
|
171 | case 'update': | |
|
172 | case 'create': | |||
172 | this.msg_buffer = attrs; |
|
173 | this.msg_buffer = attrs; | |
173 | break; |
|
174 | break; | |
174 | default: |
|
175 | default: | |
175 | error(); |
|
176 | error(); | |
176 | return false; |
|
177 | return false; | |
177 | } |
|
178 | } | |
178 | this.msg_buffer_callbacks = callbacks; |
|
179 | this.msg_buffer_callbacks = callbacks; | |
179 |
|
180 | |||
180 | } else { |
|
181 | } else { | |
181 | // We haven't exceeded the throttle, send the message like |
|
182 | // We haven't exceeded the throttle, send the message like | |
182 | // normal. If this is a patch operation, just send the |
|
183 | // normal. If this is a patch operation, just send the | |
183 | // changes. |
|
184 | // changes. | |
184 | var data = {method: 'backbone', sync_data: attrs}; |
|
185 | var data = {method: 'backbone', sync_data: attrs}; | |
185 | this.comm.send(data, callbacks); |
|
186 | this.comm.send(data, callbacks); | |
186 | this.pending_msgs++; |
|
187 | this.pending_msgs++; | |
187 | } |
|
188 | } | |
188 | } |
|
189 | } | |
189 | // Since the comm is a one-way communication, assume the message |
|
190 | // Since the comm is a one-way communication, assume the message | |
190 | // arrived. Don't call success since we don't have a model back from the server |
|
191 | // arrived. Don't call success since we don't have a model back from the server | |
191 | // this means we miss out on the 'sync' event. |
|
192 | // this means we miss out on the 'sync' event. | |
192 | }, |
|
193 | }, | |
193 |
|
194 | |||
194 | save_changes: function(callbacks) { |
|
195 | save_changes: function(callbacks) { | |
195 | // Push this model's state to the back-end |
|
196 | // Push this model's state to the back-end | |
196 | // |
|
197 | // | |
197 | // This invokes a Backbone.Sync. |
|
198 | // This invokes a Backbone.Sync. | |
198 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); |
|
199 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); | |
199 | }, |
|
200 | }, | |
200 |
|
201 | |||
201 | _pack_models: function(value) { |
|
202 | _pack_models: function(value) { | |
202 | // Replace models with model ids recursively. |
|
203 | // Replace models with model ids recursively. | |
203 | if (value instanceof Backbone.Model) { |
|
204 | if (value instanceof Backbone.Model) { | |
204 | return value.id; |
|
205 | return value.id; | |
205 | } else if (value instanceof Object) { |
|
206 | } else if (value instanceof Object) { | |
206 | var packed = {}; |
|
207 | var packed = {}; | |
207 | for (var key in value) { |
|
208 | for (var key in value) { | |
208 | packed[key] = this._pack_models(value[key]); |
|
209 | packed[key] = this._pack_models(value[key]); | |
209 | } |
|
210 | } | |
210 | return packed; |
|
211 | return packed; | |
211 | } else { |
|
212 | } else { | |
212 | return value; |
|
213 | return value; | |
213 | } |
|
214 | } | |
214 | }, |
|
215 | }, | |
215 |
|
216 | |||
216 | _unpack_models: function(value) { |
|
217 | _unpack_models: function(value) { | |
217 | // Replace model ids with models recursively. |
|
218 | // Replace model ids with models recursively. | |
218 | if (value instanceof Object) { |
|
219 | if (value instanceof Object) { | |
219 | var unpacked = {}; |
|
220 | var unpacked = {}; | |
220 | for (var key in value) { |
|
221 | for (var key in value) { | |
221 | unpacked[key] = this._unpack_models(value[key]); |
|
222 | unpacked[key] = this._unpack_models(value[key]); | |
222 | } |
|
223 | } | |
223 | return unpacked; |
|
224 | return unpacked; | |
224 | } else { |
|
225 | } else { | |
225 | var model = this.widget_manager.get_model(value); |
|
226 | var model = this.widget_manager.get_model(value); | |
226 | if (model !== null) { |
|
227 | if (model !== null) { | |
227 | return model; |
|
228 | return model; | |
228 | } else { |
|
229 | } else { | |
229 | return value; |
|
230 | return value; | |
230 | } |
|
231 | } | |
231 | } |
|
232 | } | |
232 | }, |
|
233 | }, | |
233 |
|
234 | |||
234 | }); |
|
235 | }); | |
235 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); |
|
236 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); | |
236 |
|
237 | |||
237 |
|
238 | |||
238 | var WidgetView = Backbone.View.extend({ |
|
239 | var WidgetView = Backbone.View.extend({ | |
239 | initialize: function(parameters) { |
|
240 | initialize: function(parameters) { | |
240 | // Public constructor. |
|
241 | // Public constructor. | |
241 | this.model.on('change',this.update,this); |
|
242 | this.model.on('change',this.update,this); | |
242 | this.options = parameters.options; |
|
243 | this.options = parameters.options; | |
243 | this.child_views = []; |
|
244 | this.child_views = []; | |
244 | this.model.views.push(this); |
|
245 | this.model.views.push(this); | |
245 | }, |
|
246 | }, | |
246 |
|
247 | |||
247 | update: function(){ |
|
248 | update: function(){ | |
248 | // Triggered on model change. |
|
249 | // Triggered on model change. | |
249 | // |
|
250 | // | |
250 | // Update view to be consistent with this.model |
|
251 | // Update view to be consistent with this.model | |
251 | }, |
|
252 | }, | |
252 |
|
253 | |||
253 | create_child_view: function(child_model, options) { |
|
254 | create_child_view: function(child_model, options) { | |
254 | // Create and return a child view. |
|
255 | // Create and return a child view. | |
255 | // |
|
256 | // | |
256 | // -given a model and (optionally) a view name if the view name is |
|
257 | // -given a model and (optionally) a view name if the view name is | |
257 | // not given, it defaults to the model's default view attribute. |
|
258 | // not given, it defaults to the model's default view attribute. | |
258 |
|
259 | |||
259 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior |
|
260 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior | |
260 | // it would be great to have the widget manager add the cell metadata |
|
261 | // it would be great to have the widget manager add the cell metadata | |
261 | // to the subview without having to add it here. |
|
262 | // to the subview without having to add it here. | |
262 | options = options || {}; |
|
263 | options = options || {}; | |
263 | options.cell = this.options.cell; |
|
264 | options.cell = this.options.cell; | |
264 | var child_view = this.model.widget_manager.create_view(child_model, options); |
|
265 | var child_view = this.model.widget_manager.create_view(child_model, options); | |
265 | this.child_views[child_model.id] = child_view; |
|
266 | this.child_views[child_model.id] = child_view; | |
266 | return child_view; |
|
267 | return child_view; | |
267 | }, |
|
268 | }, | |
268 |
|
269 | |||
269 | delete_child_view: function(child_model, options) { |
|
270 | delete_child_view: function(child_model, options) { | |
270 | // Delete a child view that was previously created using create_child_view. |
|
271 | // Delete a child view that was previously created using create_child_view. | |
271 | var view = this.child_views[child_model.id]; |
|
272 | var view = this.child_views[child_model.id]; | |
272 | delete this.child_views[child_model.id]; |
|
273 | delete this.child_views[child_model.id]; | |
273 | view.remove(); |
|
274 | view.remove(); | |
274 | }, |
|
275 | }, | |
275 |
|
276 | |||
276 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
277 | do_diff: function(old_list, new_list, removed_callback, added_callback) { | |
277 | // Difference a changed list and call remove and add callbacks for |
|
278 | // Difference a changed list and call remove and add callbacks for | |
278 | // each removed and added item in the new list. |
|
279 | // each removed and added item in the new list. | |
279 | // |
|
280 | // | |
280 | // Parameters |
|
281 | // Parameters | |
281 | // ---------- |
|
282 | // ---------- | |
282 | // old_list : array |
|
283 | // old_list : array | |
283 | // new_list : array |
|
284 | // new_list : array | |
284 | // removed_callback : Callback(item) |
|
285 | // removed_callback : Callback(item) | |
285 | // Callback that is called for each item removed. |
|
286 | // Callback that is called for each item removed. | |
286 | // added_callback : Callback(item) |
|
287 | // added_callback : Callback(item) | |
287 | // Callback that is called for each item added. |
|
288 | // Callback that is called for each item added. | |
288 |
|
289 | |||
289 |
|
290 | |||
290 | // removed items |
|
291 | // removed items | |
291 | _.each(_.difference(old_list, new_list), function(item, index, list) { |
|
292 | _.each(_.difference(old_list, new_list), function(item, index, list) { | |
292 | removed_callback(item); |
|
293 | removed_callback(item); | |
293 | }, this); |
|
294 | }, this); | |
294 |
|
295 | |||
295 | // added items |
|
296 | // added items | |
296 | _.each(_.difference(new_list, old_list), function(item, index, list) { |
|
297 | _.each(_.difference(new_list, old_list), function(item, index, list) { | |
297 | added_callback(item); |
|
298 | added_callback(item); | |
298 | }, this); |
|
299 | }, this); | |
299 | }, |
|
300 | }, | |
300 |
|
301 | |||
301 | callbacks: function(){ |
|
302 | callbacks: function(){ | |
302 | // Create msg callbacks for a comm msg. |
|
303 | // Create msg callbacks for a comm msg. | |
303 | return this.model.callbacks(this); |
|
304 | return this.model.callbacks(this); | |
304 | }, |
|
305 | }, | |
305 |
|
306 | |||
306 | render: function(){ |
|
307 | render: function(){ | |
307 | // Render the view. |
|
308 | // Render the view. | |
308 | // |
|
309 | // | |
309 | // By default, this is only called the first time the view is created |
|
310 | // By default, this is only called the first time the view is created | |
310 | }, |
|
311 | }, | |
311 |
|
312 | |||
312 | send: function (content) { |
|
313 | send: function (content) { | |
313 | // Send a custom msg associated with this view. |
|
314 | // Send a custom msg associated with this view. | |
314 | this.model.send(content, this.callbacks()); |
|
315 | this.model.send(content, this.callbacks()); | |
315 | }, |
|
316 | }, | |
316 |
|
317 | |||
317 | touch: function () { |
|
318 | touch: function () { | |
318 | this.model.save_changes(this.callbacks()); |
|
319 | this.model.save_changes(this.callbacks()); | |
319 | }, |
|
320 | }, | |
320 |
|
321 | |||
321 | }); |
|
322 | }); | |
322 |
|
323 | |||
323 |
|
324 | |||
324 | var DOMWidgetView = WidgetView.extend({ |
|
325 | var DOMWidgetView = WidgetView.extend({ | |
325 | initialize: function (options) { |
|
326 | initialize: function (options) { | |
326 | // Public constructor |
|
327 | // Public constructor | |
327 |
|
328 | |||
328 | // In the future we may want to make changes more granular |
|
329 | // In the future we may want to make changes more granular | |
329 | // (e.g., trigger on visible:change). |
|
330 | // (e.g., trigger on visible:change). | |
330 | this.model.on('change', this.update, this); |
|
331 | this.model.on('change', this.update, this); | |
331 | this.model.on('msg:custom', this.on_msg, this); |
|
332 | this.model.on('msg:custom', this.on_msg, this); | |
332 | DOMWidgetView.__super__.initialize.apply(this, arguments); |
|
333 | DOMWidgetView.__super__.initialize.apply(this, arguments); | |
333 | }, |
|
334 | }, | |
334 |
|
335 | |||
335 | on_msg: function(msg) { |
|
336 | on_msg: function(msg) { | |
336 | // Handle DOM specific msgs. |
|
337 | // Handle DOM specific msgs. | |
337 | switch(msg.msg_type) { |
|
338 | switch(msg.msg_type) { | |
338 | case 'add_class': |
|
339 | case 'add_class': | |
339 | this.add_class(msg.selector, msg.class_list); |
|
340 | this.add_class(msg.selector, msg.class_list); | |
340 | break; |
|
341 | break; | |
341 | case 'remove_class': |
|
342 | case 'remove_class': | |
342 | this.remove_class(msg.selector, msg.class_list); |
|
343 | this.remove_class(msg.selector, msg.class_list); | |
343 | break; |
|
344 | break; | |
344 | } |
|
345 | } | |
345 | }, |
|
346 | }, | |
346 |
|
347 | |||
347 | add_class: function (selector, class_list) { |
|
348 | add_class: function (selector, class_list) { | |
348 | // Add a DOM class to an element. |
|
349 | // Add a DOM class to an element. | |
349 | this._get_selector_element(selector).addClass(class_list); |
|
350 | this._get_selector_element(selector).addClass(class_list); | |
350 | }, |
|
351 | }, | |
351 |
|
352 | |||
352 | remove_class: function (selector, class_list) { |
|
353 | remove_class: function (selector, class_list) { | |
353 | // Remove a DOM class from an element. |
|
354 | // Remove a DOM class from an element. | |
354 | this._get_selector_element(selector).removeClass(class_list); |
|
355 | this._get_selector_element(selector).removeClass(class_list); | |
355 | }, |
|
356 | }, | |
356 |
|
357 | |||
357 | update: function () { |
|
358 | update: function () { | |
358 | // Update the contents of this view |
|
359 | // Update the contents of this view | |
359 | // |
|
360 | // | |
360 | // Called when the model is changed. The model may have been |
|
361 | // Called when the model is changed. The model may have been | |
361 | // changed by another view or by a state update from the back-end. |
|
362 | // changed by another view or by a state update from the back-end. | |
362 | // The very first update seems to happen before the element is |
|
363 | // The very first update seems to happen before the element is | |
363 | // finished rendering so we use setTimeout to give the element time |
|
364 | // finished rendering so we use setTimeout to give the element time | |
364 | // to render |
|
365 | // to render | |
365 | var e = this.$el; |
|
366 | var e = this.$el; | |
366 | var visible = this.model.get('visible'); |
|
367 | var visible = this.model.get('visible'); | |
367 | setTimeout(function() {e.toggle(visible)},0); |
|
368 | setTimeout(function() {e.toggle(visible)},0); | |
368 |
|
369 | |||
369 | var css = this.model.get('_css'); |
|
370 | var css = this.model.get('_css'); | |
370 | if (css === undefined) {return;} |
|
371 | if (css === undefined) {return;} | |
371 | for (var selector in css) { |
|
372 | for (var selector in css) { | |
372 | if (css.hasOwnProperty(selector)) { |
|
373 | if (css.hasOwnProperty(selector)) { | |
373 | // Apply the css traits to all elements that match the selector. |
|
374 | // Apply the css traits to all elements that match the selector. | |
374 | var elements = this._get_selector_element(selector); |
|
375 | var elements = this._get_selector_element(selector); | |
375 | if (elements.length > 0) { |
|
376 | if (elements.length > 0) { | |
376 | var css_traits = css[selector]; |
|
377 | var css_traits = css[selector]; | |
377 | for (var css_key in css_traits) { |
|
378 | for (var css_key in css_traits) { | |
378 | if (css_traits.hasOwnProperty(css_key)) { |
|
379 | if (css_traits.hasOwnProperty(css_key)) { | |
379 | elements.css(css_key, css_traits[css_key]); |
|
380 | elements.css(css_key, css_traits[css_key]); | |
380 | } |
|
381 | } | |
381 | } |
|
382 | } | |
382 | } |
|
383 | } | |
383 | } |
|
384 | } | |
384 | } |
|
385 | } | |
385 | }, |
|
386 | }, | |
386 |
|
387 | |||
387 | _get_selector_element: function (selector) { |
|
388 | _get_selector_element: function (selector) { | |
388 | // Get the elements via the css selector. |
|
389 | // Get the elements via the css selector. | |
389 |
|
390 | |||
390 | // If the selector is blank, apply the style to the $el_to_style |
|
391 | // If the selector is blank, apply the style to the $el_to_style | |
391 | // element. If the $el_to_style element is not defined, use apply |
|
392 | // element. If the $el_to_style element is not defined, use apply | |
392 | // the style to the view's element. |
|
393 | // the style to the view's element. | |
393 | var elements; |
|
394 | var elements; | |
394 | if (selector === undefined || selector === null || selector === '') { |
|
395 | if (selector === undefined || selector === null || selector === '') { | |
395 | if (this.$el_to_style === undefined) { |
|
396 | if (this.$el_to_style === undefined) { | |
396 | elements = this.$el; |
|
397 | elements = this.$el; | |
397 | } else { |
|
398 | } else { | |
398 | elements = this.$el_to_style; |
|
399 | elements = this.$el_to_style; | |
399 | } |
|
400 | } | |
400 | } else { |
|
401 | } else { | |
401 | elements = this.$el.find(selector); |
|
402 | elements = this.$el.find(selector); | |
402 | } |
|
403 | } | |
403 | return elements; |
|
404 | return elements; | |
404 | }, |
|
405 | }, | |
405 | }); |
|
406 | }); | |
406 |
|
407 | |||
407 | IPython.WidgetModel = WidgetModel; |
|
408 | IPython.WidgetModel = WidgetModel; | |
408 | IPython.WidgetView = WidgetView; |
|
409 | IPython.WidgetView = WidgetView; | |
409 | IPython.DOMWidgetView = DOMWidgetView; |
|
410 | IPython.DOMWidgetView = DOMWidgetView; | |
410 |
|
411 | |||
411 | // Pass through WidgetManager namespace. |
|
412 | // Pass through WidgetManager namespace. | |
412 | return WidgetManager; |
|
413 | return WidgetManager; | |
413 | }); |
|
414 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now