Show More
@@ -1,487 +1,488 | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define(["widgets/js/manager", |
|
4 | define(["widgets/js/manager", | |
5 | "underscore", |
|
5 | "underscore", | |
6 | "backbone", |
|
6 | "backbone", | |
7 | "jquery", |
|
7 | "jquery", | |
8 | "base/js/namespace", |
|
8 | "base/js/namespace", | |
9 | ], function(widgetmanager, _, Backbone, $, IPython){ |
|
9 | ], function(widgetmanager, _, Backbone, $, IPython){ | |
10 |
|
10 | |||
11 | var WidgetModel = Backbone.Model.extend({ |
|
11 | var WidgetModel = Backbone.Model.extend({ | |
12 | constructor: function (widget_manager, model_id, comm) { |
|
12 | constructor: function (widget_manager, model_id, comm) { | |
13 | // Constructor |
|
13 | // Constructor | |
14 | // |
|
14 | // | |
15 | // Creates a WidgetModel instance. |
|
15 | // Creates a WidgetModel instance. | |
16 | // |
|
16 | // | |
17 | // Parameters |
|
17 | // Parameters | |
18 | // ---------- |
|
18 | // ---------- | |
19 | // widget_manager : WidgetManager instance |
|
19 | // widget_manager : WidgetManager instance | |
20 | // model_id : string |
|
20 | // model_id : string | |
21 | // An ID unique to this model. |
|
21 | // An ID unique to this model. | |
22 | // comm : Comm instance (optional) |
|
22 | // comm : Comm instance (optional) | |
23 | this.widget_manager = widget_manager; |
|
23 | this.widget_manager = widget_manager; | |
24 | this._buffered_state_diff = {}; |
|
24 | this._buffered_state_diff = {}; | |
25 | this.pending_msgs = 0; |
|
25 | this.pending_msgs = 0; | |
26 | this.msg_buffer = null; |
|
26 | this.msg_buffer = null; | |
27 | this.key_value_lock = null; |
|
27 | this.key_value_lock = null; | |
28 | this.id = model_id; |
|
28 | this.id = model_id; | |
29 | this.views = []; |
|
29 | this.views = []; | |
30 |
|
30 | |||
31 | if (comm !== undefined) { |
|
31 | if (comm !== undefined) { | |
32 | // Remember comm associated with the model. |
|
32 | // Remember comm associated with the model. | |
33 | this.comm = comm; |
|
33 | this.comm = comm; | |
34 | comm.model = this; |
|
34 | comm.model = this; | |
35 |
|
35 | |||
36 | // Hook comm messages up to model. |
|
36 | // Hook comm messages up to model. | |
37 | comm.on_close($.proxy(this._handle_comm_closed, this)); |
|
37 | comm.on_close($.proxy(this._handle_comm_closed, this)); | |
38 | comm.on_msg($.proxy(this._handle_comm_msg, this)); |
|
38 | comm.on_msg($.proxy(this._handle_comm_msg, this)); | |
39 | } |
|
39 | } | |
40 | return Backbone.Model.apply(this); |
|
40 | return Backbone.Model.apply(this); | |
41 | }, |
|
41 | }, | |
42 |
|
42 | |||
43 | send: function (content, callbacks) { |
|
43 | send: function (content, callbacks) { | |
44 | // Send a custom msg over the comm. |
|
44 | // Send a custom msg over the comm. | |
45 | if (this.comm !== undefined) { |
|
45 | if (this.comm !== undefined) { | |
46 | var data = {method: 'custom', content: content}; |
|
46 | var data = {method: 'custom', content: content}; | |
47 | this.comm.send(data, callbacks); |
|
47 | this.comm.send(data, callbacks); | |
48 | this.pending_msgs++; |
|
48 | this.pending_msgs++; | |
49 | } |
|
49 | } | |
50 | }, |
|
50 | }, | |
51 |
|
51 | |||
52 | _handle_comm_closed: function (msg) { |
|
52 | _handle_comm_closed: function (msg) { | |
53 | // Handle when a widget is closed. |
|
53 | // Handle when a widget is closed. | |
54 | this.trigger('comm:close'); |
|
54 | this.trigger('comm:close'); | |
|
55 | this.stopListening(); | |||
55 | this.trigger('destroy', this); |
|
56 | this.trigger('destroy', this); | |
56 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
57 | delete this.comm.model; // Delete ref so GC will collect widget model. | |
57 | delete this.comm; |
|
58 | delete this.comm; | |
58 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
59 | delete this.model_id; // Delete id from model so widget manager cleans up. | |
59 | _.each(this.views, function(view, i) { |
|
60 | _.each(this.views, function(view, i) { | |
60 | view.remove(); |
|
61 | view.remove(); | |
61 | }); |
|
62 | }); | |
62 | }, |
|
63 | }, | |
63 |
|
64 | |||
64 | _handle_comm_msg: function (msg) { |
|
65 | _handle_comm_msg: function (msg) { | |
65 | // Handle incoming comm msg. |
|
66 | // Handle incoming comm msg. | |
66 | var method = msg.content.data.method; |
|
67 | var method = msg.content.data.method; | |
67 | switch (method) { |
|
68 | switch (method) { | |
68 | case 'update': |
|
69 | case 'update': | |
69 | this.apply_update(msg.content.data.state); |
|
70 | this.apply_update(msg.content.data.state); | |
70 | break; |
|
71 | break; | |
71 | case 'custom': |
|
72 | case 'custom': | |
72 | this.trigger('msg:custom', msg.content.data.content); |
|
73 | this.trigger('msg:custom', msg.content.data.content); | |
73 | break; |
|
74 | break; | |
74 | case 'display': |
|
75 | case 'display': | |
75 | this.widget_manager.display_view(msg, this); |
|
76 | this.widget_manager.display_view(msg, this); | |
76 | break; |
|
77 | break; | |
77 | } |
|
78 | } | |
78 | }, |
|
79 | }, | |
79 |
|
80 | |||
80 | apply_update: function (state) { |
|
81 | apply_update: function (state) { | |
81 | // Handle when a widget is updated via the python side. |
|
82 | // Handle when a widget is updated via the python side. | |
82 | var that = this; |
|
83 | var that = this; | |
83 | _.each(state, function(value, key) { |
|
84 | _.each(state, function(value, key) { | |
84 | that.key_value_lock = [key, value]; |
|
85 | that.key_value_lock = [key, value]; | |
85 | try { |
|
86 | try { | |
86 | WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]); |
|
87 | WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]); | |
87 | } finally { |
|
88 | } finally { | |
88 | that.key_value_lock = null; |
|
89 | that.key_value_lock = null; | |
89 | } |
|
90 | } | |
90 | }); |
|
91 | }); | |
91 | }, |
|
92 | }, | |
92 |
|
93 | |||
93 | _handle_status: function (msg, callbacks) { |
|
94 | _handle_status: function (msg, callbacks) { | |
94 | // Handle status msgs. |
|
95 | // Handle status msgs. | |
95 |
|
96 | |||
96 | // execution_state : ('busy', 'idle', 'starting') |
|
97 | // execution_state : ('busy', 'idle', 'starting') | |
97 | if (this.comm !== undefined) { |
|
98 | if (this.comm !== undefined) { | |
98 | if (msg.content.execution_state ==='idle') { |
|
99 | if (msg.content.execution_state ==='idle') { | |
99 | // Send buffer if this message caused another message to be |
|
100 | // Send buffer if this message caused another message to be | |
100 | // throttled. |
|
101 | // throttled. | |
101 | if (this.msg_buffer !== null && |
|
102 | if (this.msg_buffer !== null && | |
102 | (this.get('msg_throttle') || 3) === this.pending_msgs) { |
|
103 | (this.get('msg_throttle') || 3) === this.pending_msgs) { | |
103 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
104 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | |
104 | this.comm.send(data, callbacks); |
|
105 | this.comm.send(data, callbacks); | |
105 | this.msg_buffer = null; |
|
106 | this.msg_buffer = null; | |
106 | } else { |
|
107 | } else { | |
107 | --this.pending_msgs; |
|
108 | --this.pending_msgs; | |
108 | } |
|
109 | } | |
109 | } |
|
110 | } | |
110 | } |
|
111 | } | |
111 | }, |
|
112 | }, | |
112 |
|
113 | |||
113 | callbacks: function(view) { |
|
114 | callbacks: function(view) { | |
114 | // Create msg callbacks for a comm msg. |
|
115 | // Create msg callbacks for a comm msg. | |
115 | var callbacks = this.widget_manager.callbacks(view); |
|
116 | var callbacks = this.widget_manager.callbacks(view); | |
116 |
|
117 | |||
117 | if (callbacks.iopub === undefined) { |
|
118 | if (callbacks.iopub === undefined) { | |
118 | callbacks.iopub = {}; |
|
119 | callbacks.iopub = {}; | |
119 | } |
|
120 | } | |
120 |
|
121 | |||
121 | var that = this; |
|
122 | var that = this; | |
122 | callbacks.iopub.status = function (msg) { |
|
123 | callbacks.iopub.status = function (msg) { | |
123 | that._handle_status(msg, callbacks); |
|
124 | that._handle_status(msg, callbacks); | |
124 | }; |
|
125 | }; | |
125 | return callbacks; |
|
126 | return callbacks; | |
126 | }, |
|
127 | }, | |
127 |
|
128 | |||
128 | set: function(key, val, options) { |
|
129 | set: function(key, val, options) { | |
129 | // Set a value. |
|
130 | // Set a value. | |
130 | var return_value = WidgetModel.__super__.set.apply(this, arguments); |
|
131 | var return_value = WidgetModel.__super__.set.apply(this, arguments); | |
131 |
|
132 | |||
132 | // Backbone only remembers the diff of the most recent set() |
|
133 | // Backbone only remembers the diff of the most recent set() | |
133 | // operation. Calling set multiple times in a row results in a |
|
134 | // operation. Calling set multiple times in a row results in a | |
134 | // loss of diff information. Here we keep our own running diff. |
|
135 | // loss of diff information. Here we keep our own running diff. | |
135 | this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {}); |
|
136 | this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {}); | |
136 | return return_value; |
|
137 | return return_value; | |
137 | }, |
|
138 | }, | |
138 |
|
139 | |||
139 | sync: function (method, model, options) { |
|
140 | sync: function (method, model, options) { | |
140 | // Handle sync to the back-end. Called when a model.save() is called. |
|
141 | // Handle sync to the back-end. Called when a model.save() is called. | |
141 |
|
142 | |||
142 | // Make sure a comm exists. |
|
143 | // Make sure a comm exists. | |
143 | var error = options.error || function() { |
|
144 | var error = options.error || function() { | |
144 | console.error('Backbone sync error:', arguments); |
|
145 | console.error('Backbone sync error:', arguments); | |
145 | }; |
|
146 | }; | |
146 | if (this.comm === undefined) { |
|
147 | if (this.comm === undefined) { | |
147 | error(); |
|
148 | error(); | |
148 | return false; |
|
149 | return false; | |
149 | } |
|
150 | } | |
150 |
|
151 | |||
151 | // Delete any key value pairs that the back-end already knows about. |
|
152 | // Delete any key value pairs that the back-end already knows about. | |
152 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); |
|
153 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); | |
153 | if (this.key_value_lock !== null) { |
|
154 | if (this.key_value_lock !== null) { | |
154 | var key = this.key_value_lock[0]; |
|
155 | var key = this.key_value_lock[0]; | |
155 | var value = this.key_value_lock[1]; |
|
156 | var value = this.key_value_lock[1]; | |
156 | if (attrs[key] === value) { |
|
157 | if (attrs[key] === value) { | |
157 | delete attrs[key]; |
|
158 | delete attrs[key]; | |
158 | } |
|
159 | } | |
159 | } |
|
160 | } | |
160 |
|
161 | |||
161 | // Only sync if there are attributes to send to the back-end. |
|
162 | // Only sync if there are attributes to send to the back-end. | |
162 | attrs = this._pack_models(attrs); |
|
163 | attrs = this._pack_models(attrs); | |
163 | if (_.size(attrs) > 0) { |
|
164 | if (_.size(attrs) > 0) { | |
164 |
|
165 | |||
165 | // If this message was sent via backbone itself, it will not |
|
166 | // If this message was sent via backbone itself, it will not | |
166 | // have any callbacks. It's important that we create callbacks |
|
167 | // have any callbacks. It's important that we create callbacks | |
167 | // so we can listen for status messages, etc... |
|
168 | // so we can listen for status messages, etc... | |
168 | var callbacks = options.callbacks || this.callbacks(); |
|
169 | var callbacks = options.callbacks || this.callbacks(); | |
169 |
|
170 | |||
170 | // Check throttle. |
|
171 | // Check throttle. | |
171 | if (this.pending_msgs >= (this.get('msg_throttle') || 3)) { |
|
172 | if (this.pending_msgs >= (this.get('msg_throttle') || 3)) { | |
172 | // The throttle has been exceeded, buffer the current msg so |
|
173 | // The throttle has been exceeded, buffer the current msg so | |
173 | // it can be sent once the kernel has finished processing |
|
174 | // it can be sent once the kernel has finished processing | |
174 | // some of the existing messages. |
|
175 | // some of the existing messages. | |
175 |
|
176 | |||
176 | // Combine updates if it is a 'patch' sync, otherwise replace updates |
|
177 | // Combine updates if it is a 'patch' sync, otherwise replace updates | |
177 | switch (method) { |
|
178 | switch (method) { | |
178 | case 'patch': |
|
179 | case 'patch': | |
179 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); |
|
180 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); | |
180 | break; |
|
181 | break; | |
181 | case 'update': |
|
182 | case 'update': | |
182 | case 'create': |
|
183 | case 'create': | |
183 | this.msg_buffer = attrs; |
|
184 | this.msg_buffer = attrs; | |
184 | break; |
|
185 | break; | |
185 | default: |
|
186 | default: | |
186 | error(); |
|
187 | error(); | |
187 | return false; |
|
188 | return false; | |
188 | } |
|
189 | } | |
189 | this.msg_buffer_callbacks = callbacks; |
|
190 | this.msg_buffer_callbacks = callbacks; | |
190 |
|
191 | |||
191 | } else { |
|
192 | } else { | |
192 | // We haven't exceeded the throttle, send the message like |
|
193 | // We haven't exceeded the throttle, send the message like | |
193 | // normal. |
|
194 | // normal. | |
194 | var data = {method: 'backbone', sync_data: attrs}; |
|
195 | var data = {method: 'backbone', sync_data: attrs}; | |
195 | this.comm.send(data, callbacks); |
|
196 | this.comm.send(data, callbacks); | |
196 | this.pending_msgs++; |
|
197 | this.pending_msgs++; | |
197 | } |
|
198 | } | |
198 | } |
|
199 | } | |
199 | // Since the comm is a one-way communication, assume the message |
|
200 | // Since the comm is a one-way communication, assume the message | |
200 | // arrived. Don't call success since we don't have a model back from the server |
|
201 | // arrived. Don't call success since we don't have a model back from the server | |
201 | // this means we miss out on the 'sync' event. |
|
202 | // this means we miss out on the 'sync' event. | |
202 | this._buffered_state_diff = {}; |
|
203 | this._buffered_state_diff = {}; | |
203 | }, |
|
204 | }, | |
204 |
|
205 | |||
205 | save_changes: function(callbacks) { |
|
206 | save_changes: function(callbacks) { | |
206 | // Push this model's state to the back-end |
|
207 | // Push this model's state to the back-end | |
207 | // |
|
208 | // | |
208 | // This invokes a Backbone.Sync. |
|
209 | // This invokes a Backbone.Sync. | |
209 | this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks}); |
|
210 | this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks}); | |
210 | }, |
|
211 | }, | |
211 |
|
212 | |||
212 | _pack_models: function(value) { |
|
213 | _pack_models: function(value) { | |
213 | // Replace models with model ids recursively. |
|
214 | // Replace models with model ids recursively. | |
214 | var that = this; |
|
215 | var that = this; | |
215 | var packed; |
|
216 | var packed; | |
216 | if (value instanceof Backbone.Model) { |
|
217 | if (value instanceof Backbone.Model) { | |
217 | return "IPY_MODEL_" + value.id; |
|
218 | return "IPY_MODEL_" + value.id; | |
218 |
|
219 | |||
219 | } else if ($.isArray(value)) { |
|
220 | } else if ($.isArray(value)) { | |
220 | packed = []; |
|
221 | packed = []; | |
221 | _.each(value, function(sub_value, key) { |
|
222 | _.each(value, function(sub_value, key) { | |
222 | packed.push(that._pack_models(sub_value)); |
|
223 | packed.push(that._pack_models(sub_value)); | |
223 | }); |
|
224 | }); | |
224 | return packed; |
|
225 | return packed; | |
225 |
|
226 | |||
226 | } else if (value instanceof Object) { |
|
227 | } else if (value instanceof Object) { | |
227 | packed = {}; |
|
228 | packed = {}; | |
228 | _.each(value, function(sub_value, key) { |
|
229 | _.each(value, function(sub_value, key) { | |
229 | packed[key] = that._pack_models(sub_value); |
|
230 | packed[key] = that._pack_models(sub_value); | |
230 | }); |
|
231 | }); | |
231 | return packed; |
|
232 | return packed; | |
232 |
|
233 | |||
233 | } else { |
|
234 | } else { | |
234 | return value; |
|
235 | return value; | |
235 | } |
|
236 | } | |
236 | }, |
|
237 | }, | |
237 |
|
238 | |||
238 | _unpack_models: function(value) { |
|
239 | _unpack_models: function(value) { | |
239 | // Replace model ids with models recursively. |
|
240 | // Replace model ids with models recursively. | |
240 | var that = this; |
|
241 | var that = this; | |
241 | var unpacked; |
|
242 | var unpacked; | |
242 | if ($.isArray(value)) { |
|
243 | if ($.isArray(value)) { | |
243 | unpacked = []; |
|
244 | unpacked = []; | |
244 | _.each(value, function(sub_value, key) { |
|
245 | _.each(value, function(sub_value, key) { | |
245 | unpacked.push(that._unpack_models(sub_value)); |
|
246 | unpacked.push(that._unpack_models(sub_value)); | |
246 | }); |
|
247 | }); | |
247 | return unpacked; |
|
248 | return unpacked; | |
248 |
|
249 | |||
249 | } else if (value instanceof Object) { |
|
250 | } else if (value instanceof Object) { | |
250 | unpacked = {}; |
|
251 | unpacked = {}; | |
251 | _.each(value, function(sub_value, key) { |
|
252 | _.each(value, function(sub_value, key) { | |
252 | unpacked[key] = that._unpack_models(sub_value); |
|
253 | unpacked[key] = that._unpack_models(sub_value); | |
253 | }); |
|
254 | }); | |
254 | return unpacked; |
|
255 | return unpacked; | |
255 |
|
256 | |||
256 | } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { |
|
257 | } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { | |
257 | var model = this.widget_manager.get_model(value.slice(10, value.length)); |
|
258 | var model = this.widget_manager.get_model(value.slice(10, value.length)); | |
258 | if (model) { |
|
259 | if (model) { | |
259 | return model; |
|
260 | return model; | |
260 | } else { |
|
261 | } else { | |
261 | return value; |
|
262 | return value; | |
262 | } |
|
263 | } | |
263 | } else { |
|
264 | } else { | |
264 | return value; |
|
265 | return value; | |
265 | } |
|
266 | } | |
266 | }, |
|
267 | }, | |
267 |
|
268 | |||
268 | }); |
|
269 | }); | |
269 | widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel); |
|
270 | widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel); | |
270 |
|
271 | |||
271 |
|
272 | |||
272 | var WidgetView = Backbone.View.extend({ |
|
273 | var WidgetView = Backbone.View.extend({ | |
273 | initialize: function(parameters) { |
|
274 | initialize: function(parameters) { | |
274 | // Public constructor. |
|
275 | // Public constructor. | |
275 | this.model.on('change',this.update,this); |
|
276 | this.model.on('change',this.update,this); | |
276 | this.options = parameters.options; |
|
277 | this.options = parameters.options; | |
277 | this.child_model_views = {}; |
|
278 | this.child_model_views = {}; | |
278 | this.child_views = {}; |
|
279 | this.child_views = {}; | |
279 | this.model.views.push(this); |
|
280 | this.model.views.push(this); | |
280 | this.id = this.id || IPython.utils.uuid(); |
|
281 | this.id = this.id || IPython.utils.uuid(); | |
281 | this.on('displayed', function() { |
|
282 | this.on('displayed', function() { | |
282 | this.is_displayed = true; |
|
283 | this.is_displayed = true; | |
283 | }, this); |
|
284 | }, this); | |
284 | }, |
|
285 | }, | |
285 |
|
286 | |||
286 | update: function(){ |
|
287 | update: function(){ | |
287 | // Triggered on model change. |
|
288 | // Triggered on model change. | |
288 | // |
|
289 | // | |
289 | // Update view to be consistent with this.model |
|
290 | // Update view to be consistent with this.model | |
290 | }, |
|
291 | }, | |
291 |
|
292 | |||
292 | create_child_view: function(child_model, options) { |
|
293 | create_child_view: function(child_model, options) { | |
293 | // Create and return a child view. |
|
294 | // Create and return a child view. | |
294 | // |
|
295 | // | |
295 | // -given a model and (optionally) a view name if the view name is |
|
296 | // -given a model and (optionally) a view name if the view name is | |
296 | // not given, it defaults to the model's default view attribute. |
|
297 | // not given, it defaults to the model's default view attribute. | |
297 |
|
298 | |||
298 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior |
|
299 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior | |
299 | // it would be great to have the widget manager add the cell metadata |
|
300 | // it would be great to have the widget manager add the cell metadata | |
300 | // to the subview without having to add it here. |
|
301 | // to the subview without having to add it here. | |
301 | options = $.extend({ parent: this }, options || {}); |
|
302 | options = $.extend({ parent: this }, options || {}); | |
302 | var child_view = this.model.widget_manager.create_view(child_model, options, this); |
|
303 | var child_view = this.model.widget_manager.create_view(child_model, options, this); | |
303 |
|
304 | |||
304 | // Associate the view id with the model id. |
|
305 | // Associate the view id with the model id. | |
305 | if (this.child_model_views[child_model.id] === undefined) { |
|
306 | if (this.child_model_views[child_model.id] === undefined) { | |
306 | this.child_model_views[child_model.id] = []; |
|
307 | this.child_model_views[child_model.id] = []; | |
307 | } |
|
308 | } | |
308 | this.child_model_views[child_model.id].push(child_view.id); |
|
309 | this.child_model_views[child_model.id].push(child_view.id); | |
309 |
|
310 | |||
310 | // Remember the view by id. |
|
311 | // Remember the view by id. | |
311 | this.child_views[child_view.id] = child_view; |
|
312 | this.child_views[child_view.id] = child_view; | |
312 | return child_view; |
|
313 | return child_view; | |
313 | }, |
|
314 | }, | |
314 |
|
315 | |||
315 | pop_child_view: function(child_model) { |
|
316 | pop_child_view: function(child_model) { | |
316 | // Delete a child view that was previously created using create_child_view. |
|
317 | // Delete a child view that was previously created using create_child_view. | |
317 | var view_ids = this.child_model_views[child_model.id]; |
|
318 | var view_ids = this.child_model_views[child_model.id]; | |
318 | if (view_ids !== undefined) { |
|
319 | if (view_ids !== undefined) { | |
319 |
|
320 | |||
320 | // Only delete the first view in the list. |
|
321 | // Only delete the first view in the list. | |
321 | var view_id = view_ids[0]; |
|
322 | var view_id = view_ids[0]; | |
322 | var view = this.child_views[view_id]; |
|
323 | var view = this.child_views[view_id]; | |
323 | delete this.child_views[view_id]; |
|
324 | delete this.child_views[view_id]; | |
324 | view_ids.splice(0,1); |
|
325 | view_ids.splice(0,1); | |
325 | child_model.views.pop(view); |
|
326 | child_model.views.pop(view); | |
326 |
|
327 | |||
327 | // Remove the view list specific to this model if it is empty. |
|
328 | // Remove the view list specific to this model if it is empty. | |
328 | if (view_ids.length === 0) { |
|
329 | if (view_ids.length === 0) { | |
329 | delete this.child_model_views[child_model.id]; |
|
330 | delete this.child_model_views[child_model.id]; | |
330 | } |
|
331 | } | |
331 | return view; |
|
332 | return view; | |
332 | } |
|
333 | } | |
333 | return null; |
|
334 | return null; | |
334 | }, |
|
335 | }, | |
335 |
|
336 | |||
336 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
337 | do_diff: function(old_list, new_list, removed_callback, added_callback) { | |
337 | // Difference a changed list and call remove and add callbacks for |
|
338 | // Difference a changed list and call remove and add callbacks for | |
338 | // each removed and added item in the new list. |
|
339 | // each removed and added item in the new list. | |
339 | // |
|
340 | // | |
340 | // Parameters |
|
341 | // Parameters | |
341 | // ---------- |
|
342 | // ---------- | |
342 | // old_list : array |
|
343 | // old_list : array | |
343 | // new_list : array |
|
344 | // new_list : array | |
344 | // removed_callback : Callback(item) |
|
345 | // removed_callback : Callback(item) | |
345 | // Callback that is called for each item removed. |
|
346 | // Callback that is called for each item removed. | |
346 | // added_callback : Callback(item) |
|
347 | // added_callback : Callback(item) | |
347 | // Callback that is called for each item added. |
|
348 | // Callback that is called for each item added. | |
348 |
|
349 | |||
349 | // Walk the lists until an unequal entry is found. |
|
350 | // Walk the lists until an unequal entry is found. | |
350 | var i; |
|
351 | var i; | |
351 | for (i = 0; i < new_list.length; i++) { |
|
352 | for (i = 0; i < new_list.length; i++) { | |
352 | if (i >= old_list.length || new_list[i] !== old_list[i]) { |
|
353 | if (i >= old_list.length || new_list[i] !== old_list[i]) { | |
353 | break; |
|
354 | break; | |
354 | } |
|
355 | } | |
355 | } |
|
356 | } | |
356 |
|
357 | |||
357 | // Remove the non-matching items from the old list. |
|
358 | // Remove the non-matching items from the old list. | |
358 | for (var j = i; j < old_list.length; j++) { |
|
359 | for (var j = i; j < old_list.length; j++) { | |
359 | removed_callback(old_list[j]); |
|
360 | removed_callback(old_list[j]); | |
360 | } |
|
361 | } | |
361 |
|
362 | |||
362 | // Add the rest of the new list items. |
|
363 | // Add the rest of the new list items. | |
363 | for (; i < new_list.length; i++) { |
|
364 | for (; i < new_list.length; i++) { | |
364 | added_callback(new_list[i]); |
|
365 | added_callback(new_list[i]); | |
365 | } |
|
366 | } | |
366 | }, |
|
367 | }, | |
367 |
|
368 | |||
368 | callbacks: function(){ |
|
369 | callbacks: function(){ | |
369 | // Create msg callbacks for a comm msg. |
|
370 | // Create msg callbacks for a comm msg. | |
370 | return this.model.callbacks(this); |
|
371 | return this.model.callbacks(this); | |
371 | }, |
|
372 | }, | |
372 |
|
373 | |||
373 | render: function(){ |
|
374 | render: function(){ | |
374 | // Render the view. |
|
375 | // Render the view. | |
375 | // |
|
376 | // | |
376 | // By default, this is only called the first time the view is created |
|
377 | // By default, this is only called the first time the view is created | |
377 | }, |
|
378 | }, | |
378 |
|
379 | |||
379 | show: function(){ |
|
380 | show: function(){ | |
380 | // Show the widget-area |
|
381 | // Show the widget-area | |
381 | if (this.options && this.options.cell && |
|
382 | if (this.options && this.options.cell && | |
382 | this.options.cell.widget_area !== undefined) { |
|
383 | this.options.cell.widget_area !== undefined) { | |
383 | this.options.cell.widget_area.show(); |
|
384 | this.options.cell.widget_area.show(); | |
384 | } |
|
385 | } | |
385 | }, |
|
386 | }, | |
386 |
|
387 | |||
387 | send: function (content) { |
|
388 | send: function (content) { | |
388 | // Send a custom msg associated with this view. |
|
389 | // Send a custom msg associated with this view. | |
389 | this.model.send(content, this.callbacks()); |
|
390 | this.model.send(content, this.callbacks()); | |
390 | }, |
|
391 | }, | |
391 |
|
392 | |||
392 | touch: function () { |
|
393 | touch: function () { | |
393 | this.model.save_changes(this.callbacks()); |
|
394 | this.model.save_changes(this.callbacks()); | |
394 | }, |
|
395 | }, | |
395 |
|
396 | |||
396 | after_displayed: function (callback, context) { |
|
397 | after_displayed: function (callback, context) { | |
397 | // Calls the callback right away is the view is already displayed |
|
398 | // Calls the callback right away is the view is already displayed | |
398 | // otherwise, register the callback to the 'displayed' event. |
|
399 | // otherwise, register the callback to the 'displayed' event. | |
399 | if (this.is_displayed) { |
|
400 | if (this.is_displayed) { | |
400 | callback.apply(context); |
|
401 | callback.apply(context); | |
401 | } else { |
|
402 | } else { | |
402 | this.on('displayed', callback, context); |
|
403 | this.on('displayed', callback, context); | |
403 | } |
|
404 | } | |
404 | }, |
|
405 | }, | |
405 | }); |
|
406 | }); | |
406 |
|
407 | |||
407 |
|
408 | |||
408 | var DOMWidgetView = WidgetView.extend({ |
|
409 | var DOMWidgetView = WidgetView.extend({ | |
409 | initialize: function (parameters) { |
|
410 | initialize: function (parameters) { | |
410 | // Public constructor |
|
411 | // Public constructor | |
411 | DOMWidgetView.__super__.initialize.apply(this, [parameters]); |
|
412 | DOMWidgetView.__super__.initialize.apply(this, [parameters]); | |
412 | this.on('displayed', this.show, this); |
|
413 | this.on('displayed', this.show, this); | |
413 | this.after_displayed(function() { |
|
414 | this.after_displayed(function() { | |
414 | this.update_visible(this.model, this.model.get("visible")); |
|
415 | this.update_visible(this.model, this.model.get("visible")); | |
415 | this.update_css(this.model, this.model.get("_css")); |
|
416 | this.update_css(this.model, this.model.get("_css")); | |
416 | }, this); |
|
417 | }, this); | |
417 | this.model.on('msg:custom', this.on_msg, this); |
|
418 | this.model.on('msg:custom', this.on_msg, this); | |
418 | this.model.on('change:visible', this.update_visible, this); |
|
419 | this.model.on('change:visible', this.update_visible, this); | |
419 | this.model.on('change:_css', this.update_css, this); |
|
420 | this.model.on('change:_css', this.update_css, this); | |
420 | }, |
|
421 | }, | |
421 |
|
422 | |||
422 | on_msg: function(msg) { |
|
423 | on_msg: function(msg) { | |
423 | // Handle DOM specific msgs. |
|
424 | // Handle DOM specific msgs. | |
424 | switch(msg.msg_type) { |
|
425 | switch(msg.msg_type) { | |
425 | case 'add_class': |
|
426 | case 'add_class': | |
426 | this.add_class(msg.selector, msg.class_list); |
|
427 | this.add_class(msg.selector, msg.class_list); | |
427 | break; |
|
428 | break; | |
428 | case 'remove_class': |
|
429 | case 'remove_class': | |
429 | this.remove_class(msg.selector, msg.class_list); |
|
430 | this.remove_class(msg.selector, msg.class_list); | |
430 | break; |
|
431 | break; | |
431 | } |
|
432 | } | |
432 | }, |
|
433 | }, | |
433 |
|
434 | |||
434 | add_class: function (selector, class_list) { |
|
435 | add_class: function (selector, class_list) { | |
435 | // Add a DOM class to an element. |
|
436 | // Add a DOM class to an element. | |
436 | this._get_selector_element(selector).addClass(class_list); |
|
437 | this._get_selector_element(selector).addClass(class_list); | |
437 | }, |
|
438 | }, | |
438 |
|
439 | |||
439 | remove_class: function (selector, class_list) { |
|
440 | remove_class: function (selector, class_list) { | |
440 | // Remove a DOM class from an element. |
|
441 | // Remove a DOM class from an element. | |
441 | this._get_selector_element(selector).removeClass(class_list); |
|
442 | this._get_selector_element(selector).removeClass(class_list); | |
442 | }, |
|
443 | }, | |
443 |
|
444 | |||
444 | update_visible: function(model, value) { |
|
445 | update_visible: function(model, value) { | |
445 | // Update visibility |
|
446 | // Update visibility | |
446 | this.$el.toggle(value); |
|
447 | this.$el.toggle(value); | |
447 | }, |
|
448 | }, | |
448 |
|
449 | |||
449 | update_css: function (model, css) { |
|
450 | update_css: function (model, css) { | |
450 | // Update the css styling of this view. |
|
451 | // Update the css styling of this view. | |
451 | var e = this.$el; |
|
452 | var e = this.$el; | |
452 | if (css === undefined) {return;} |
|
453 | if (css === undefined) {return;} | |
453 | for (var i = 0; i < css.length; i++) { |
|
454 | for (var i = 0; i < css.length; i++) { | |
454 | // Apply the css traits to all elements that match the selector. |
|
455 | // Apply the css traits to all elements that match the selector. | |
455 | var selector = css[i][0]; |
|
456 | var selector = css[i][0]; | |
456 | var elements = this._get_selector_element(selector); |
|
457 | var elements = this._get_selector_element(selector); | |
457 | if (elements.length > 0) { |
|
458 | if (elements.length > 0) { | |
458 | var trait_key = css[i][1]; |
|
459 | var trait_key = css[i][1]; | |
459 | var trait_value = css[i][2]; |
|
460 | var trait_value = css[i][2]; | |
460 | elements.css(trait_key ,trait_value); |
|
461 | elements.css(trait_key ,trait_value); | |
461 | } |
|
462 | } | |
462 | } |
|
463 | } | |
463 | }, |
|
464 | }, | |
464 |
|
465 | |||
465 | _get_selector_element: function (selector) { |
|
466 | _get_selector_element: function (selector) { | |
466 | // Get the elements via the css selector. |
|
467 | // Get the elements via the css selector. | |
467 | var elements; |
|
468 | var elements; | |
468 | if (!selector) { |
|
469 | if (!selector) { | |
469 | elements = this.$el; |
|
470 | elements = this.$el; | |
470 | } else { |
|
471 | } else { | |
471 | elements = this.$el.find(selector).addBack(selector); |
|
472 | elements = this.$el.find(selector).addBack(selector); | |
472 | } |
|
473 | } | |
473 | return elements; |
|
474 | return elements; | |
474 | }, |
|
475 | }, | |
475 | }); |
|
476 | }); | |
476 |
|
477 | |||
477 | var widget = { |
|
478 | var widget = { | |
478 | 'WidgetModel': WidgetModel, |
|
479 | 'WidgetModel': WidgetModel, | |
479 | 'WidgetView': WidgetView, |
|
480 | 'WidgetView': WidgetView, | |
480 | 'DOMWidgetView': DOMWidgetView, |
|
481 | 'DOMWidgetView': DOMWidgetView, | |
481 | }; |
|
482 | }; | |
482 |
|
483 | |||
483 | // For backwards compatability. |
|
484 | // For backwards compatability. | |
484 | $.extend(IPython, widget); |
|
485 | $.extend(IPython, widget); | |
485 |
|
486 | |||
486 | return widget; |
|
487 | return widget; | |
487 | }); |
|
488 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now