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