Show More
@@ -1,743 +1,735 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define(["widgets/js/manager", |
|
5 | 5 | "underscore", |
|
6 | 6 | "backbone", |
|
7 | 7 | "jquery", |
|
8 | 8 | "base/js/utils", |
|
9 | 9 | "base/js/namespace", |
|
10 | 10 | ], function(widgetmanager, _, Backbone, $, utils, IPython){ |
|
11 | 11 | |
|
12 | 12 | var WidgetModel = Backbone.Model.extend({ |
|
13 | 13 | constructor: function (widget_manager, model_id, comm) { |
|
14 | 14 | /** |
|
15 | 15 | * Constructor |
|
16 | 16 | * |
|
17 | 17 | * Creates a WidgetModel instance. |
|
18 | 18 | * |
|
19 | 19 | * Parameters |
|
20 | 20 | * ---------- |
|
21 | 21 | * widget_manager : WidgetManager instance |
|
22 | 22 | * model_id : string |
|
23 | 23 | * An ID unique to this model. |
|
24 | 24 | * comm : Comm instance (optional) |
|
25 | 25 | */ |
|
26 | 26 | this.widget_manager = widget_manager; |
|
27 | 27 | this.state_change = Promise.resolve(); |
|
28 | 28 | this._buffered_state_diff = {}; |
|
29 | 29 | this.pending_msgs = 0; |
|
30 | 30 | this.msg_buffer = null; |
|
31 | 31 | this.state_lock = null; |
|
32 | 32 | this.id = model_id; |
|
33 | 33 | this.views = {}; |
|
34 | 34 | this._resolve_received_state = {}; |
|
35 | 35 | |
|
36 | 36 | if (comm !== undefined) { |
|
37 | 37 | // Remember comm associated with the model. |
|
38 | 38 | this.comm = comm; |
|
39 | 39 | comm.model = this; |
|
40 | 40 | |
|
41 | 41 | // Hook comm messages up to model. |
|
42 | 42 | comm.on_close($.proxy(this._handle_comm_closed, this)); |
|
43 | 43 | comm.on_msg($.proxy(this._handle_comm_msg, this)); |
|
44 | 44 | |
|
45 | 45 | // Assume the comm is alive. |
|
46 | 46 | this.set_comm_live(true); |
|
47 | 47 | } else { |
|
48 | 48 | this.set_comm_live(false); |
|
49 | 49 | } |
|
50 | 50 | return Backbone.Model.apply(this); |
|
51 | 51 | }, |
|
52 | 52 | |
|
53 | 53 | send: function (content, callbacks) { |
|
54 | 54 | /** |
|
55 | 55 | * Send a custom msg over the comm. |
|
56 | 56 | */ |
|
57 | 57 | if (this.comm !== undefined) { |
|
58 | 58 | var data = {method: 'custom', content: content}; |
|
59 | 59 | this.comm.send(data, callbacks); |
|
60 | 60 | this.pending_msgs++; |
|
61 | 61 | } |
|
62 | 62 | }, |
|
63 | 63 | |
|
64 | 64 | request_state: function(callbacks) { |
|
65 | 65 | /** |
|
66 | 66 | * Request a state push from the back-end. |
|
67 | 67 | */ |
|
68 | 68 | if (!this.comm) { |
|
69 | 69 | console.error("Could not request_state because comm doesn't exist!"); |
|
70 | 70 | return; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | var msg_id = this.comm.send({method: 'request_state'}, callbacks || this.widget_manager.callbacks()); |
|
74 | 74 | |
|
75 | 75 | // Promise that is resolved when a state is received |
|
76 | 76 | // from the back-end. |
|
77 | 77 | var that = this; |
|
78 | 78 | var received_state = new Promise(function(resolve) { |
|
79 | 79 | that._resolve_received_state[msg_id] = resolve; |
|
80 | 80 | }); |
|
81 | 81 | return received_state; |
|
82 | 82 | }, |
|
83 | 83 | |
|
84 | 84 | set_comm_live: function(live) { |
|
85 | 85 | /** |
|
86 | 86 | * Change the comm_live state of the model. |
|
87 | 87 | */ |
|
88 | 88 | if (this.comm_live === undefined || this.comm_live != live) { |
|
89 | 89 | this.comm_live = live; |
|
90 | 90 | this.trigger(live ? 'comm:live' : 'comm:dead', {model: this}); |
|
91 | 91 | } |
|
92 | 92 | }, |
|
93 | 93 | |
|
94 | 94 | close: function(comm_closed) { |
|
95 | 95 | /** |
|
96 | 96 | * Close model |
|
97 | 97 | */ |
|
98 | 98 | if (this.comm && !comm_closed) { |
|
99 | 99 | this.comm.close(); |
|
100 | 100 | } |
|
101 | 101 | this.stopListening(); |
|
102 | 102 | this.trigger('destroy', this); |
|
103 | 103 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
104 | 104 | delete this.comm; |
|
105 | 105 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
106 | 106 | _.each(this.views, function(v, id, views) { |
|
107 | 107 | v.then(function(view) { |
|
108 | 108 | view.remove(); |
|
109 | 109 | delete views[id]; |
|
110 | 110 | }); |
|
111 | 111 | }); |
|
112 | 112 | }, |
|
113 | 113 | |
|
114 | 114 | _handle_comm_closed: function (msg) { |
|
115 | 115 | /** |
|
116 | 116 | * Handle when a widget is closed. |
|
117 | 117 | */ |
|
118 | 118 | this.trigger('comm:close'); |
|
119 | 119 | this.close(true); |
|
120 | 120 | }, |
|
121 | 121 | |
|
122 | 122 | _handle_comm_msg: function (msg) { |
|
123 | 123 | /** |
|
124 | 124 | * Handle incoming comm msg. |
|
125 | 125 | */ |
|
126 | 126 | var method = msg.content.data.method; |
|
127 | 127 | var that = this; |
|
128 | 128 | switch (method) { |
|
129 | 129 | case 'update': |
|
130 | 130 | this.state_change = this.state_change |
|
131 | 131 | .then(function() { |
|
132 | 132 | return that.set_state(msg.content.data.state); |
|
133 | 133 | }).catch(utils.reject("Couldn't process update msg for model id '" + String(that.id) + "'", true)) |
|
134 | 134 | .then(function() { |
|
135 | 135 | var parent_id = msg.parent_header.msg_id; |
|
136 | 136 | if (that._resolve_received_state[parent_id] !== undefined) { |
|
137 | 137 | that._resolve_received_state[parent_id].call(); |
|
138 | 138 | delete that._resolve_received_state[parent_id]; |
|
139 | 139 | } |
|
140 | 140 | }).catch(utils.reject("Couldn't resolve state request promise", true)); |
|
141 | 141 | break; |
|
142 | 142 | case 'custom': |
|
143 | 143 | this.trigger('msg:custom', msg.content.data.content); |
|
144 | 144 | break; |
|
145 | 145 | case 'display': |
|
146 | 146 | this.widget_manager.display_view(msg, this) |
|
147 | 147 | .catch(utils.reject('Could not process display view msg', true)); |
|
148 | 148 | break; |
|
149 | 149 | } |
|
150 | 150 | }, |
|
151 | 151 | |
|
152 | 152 | set_state: function (state) { |
|
153 | 153 | var that = this; |
|
154 | 154 | // Handle when a widget is updated via the python side. |
|
155 | 155 | return this._unpack_models(state).then(function(state) { |
|
156 | 156 | that.state_lock = state; |
|
157 | 157 | try { |
|
158 | 158 | WidgetModel.__super__.set.call(that, state); |
|
159 | 159 | } finally { |
|
160 | 160 | that.state_lock = null; |
|
161 | 161 | } |
|
162 | 162 | }).catch(utils.reject("Couldn't set model state", true)); |
|
163 | 163 | }, |
|
164 | 164 | |
|
165 | 165 | get_state: function() { |
|
166 | 166 | // Get the serializable state of the model. |
|
167 | 167 | state = this.toJSON(); |
|
168 | 168 | for (var key in state) { |
|
169 | 169 | if (state.hasOwnProperty(key)) { |
|
170 | 170 | state[key] = this._pack_models(state[key]); |
|
171 | 171 | } |
|
172 | 172 | } |
|
173 | 173 | return state; |
|
174 | 174 | }, |
|
175 | 175 | |
|
176 | 176 | _handle_status: function (msg, callbacks) { |
|
177 | 177 | /** |
|
178 | 178 | * Handle status msgs. |
|
179 | 179 | * |
|
180 | 180 | * execution_state : ('busy', 'idle', 'starting') |
|
181 | 181 | */ |
|
182 | 182 | if (this.comm !== undefined) { |
|
183 | 183 | if (msg.content.execution_state ==='idle') { |
|
184 | 184 | // Send buffer if this message caused another message to be |
|
185 | 185 | // throttled. |
|
186 | 186 | if (this.msg_buffer !== null && |
|
187 | 187 | (this.get('msg_throttle') || 3) === this.pending_msgs) { |
|
188 | 188 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
189 | 189 | this.comm.send(data, callbacks); |
|
190 | 190 | this.msg_buffer = null; |
|
191 | 191 | } else { |
|
192 | 192 | --this.pending_msgs; |
|
193 | 193 | } |
|
194 | 194 | } |
|
195 | 195 | } |
|
196 | 196 | }, |
|
197 | 197 | |
|
198 | 198 | callbacks: function(view) { |
|
199 | 199 | /** |
|
200 | 200 | * Create msg callbacks for a comm msg. |
|
201 | 201 | */ |
|
202 | 202 | var callbacks = this.widget_manager.callbacks(view); |
|
203 | 203 | |
|
204 | 204 | if (callbacks.iopub === undefined) { |
|
205 | 205 | callbacks.iopub = {}; |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | var that = this; |
|
209 | 209 | callbacks.iopub.status = function (msg) { |
|
210 | 210 | that._handle_status(msg, callbacks); |
|
211 | 211 | }; |
|
212 | 212 | return callbacks; |
|
213 | 213 | }, |
|
214 | 214 | |
|
215 | 215 | set: function(key, val, options) { |
|
216 | 216 | /** |
|
217 | 217 | * Set a value. |
|
218 | 218 | */ |
|
219 | 219 | var return_value = WidgetModel.__super__.set.apply(this, arguments); |
|
220 | 220 | |
|
221 | 221 | // Backbone only remembers the diff of the most recent set() |
|
222 | 222 | // operation. Calling set multiple times in a row results in a |
|
223 | 223 | // loss of diff information. Here we keep our own running diff. |
|
224 | 224 | this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {}); |
|
225 | 225 | return return_value; |
|
226 | 226 | }, |
|
227 | 227 | |
|
228 | 228 | sync: function (method, model, options) { |
|
229 | 229 | /** |
|
230 | 230 | * Handle sync to the back-end. Called when a model.save() is called. |
|
231 | 231 | * |
|
232 | 232 | * Make sure a comm exists. |
|
233 | 233 | */ |
|
234 | 234 | var error = options.error || function() { |
|
235 | 235 | console.error('Backbone sync error:', arguments); |
|
236 | 236 | }; |
|
237 | 237 | if (this.comm === undefined) { |
|
238 | 238 | error(); |
|
239 | 239 | return false; |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | // Delete any key value pairs that the back-end already knows about. |
|
243 | 243 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); |
|
244 | 244 | if (this.state_lock !== null) { |
|
245 | 245 | var keys = Object.keys(this.state_lock); |
|
246 | 246 | for (var i=0; i<keys.length; i++) { |
|
247 | 247 | var key = keys[i]; |
|
248 | 248 | if (attrs[key] === this.state_lock[key]) { |
|
249 | 249 | delete attrs[key]; |
|
250 | 250 | } |
|
251 | 251 | } |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | // Only sync if there are attributes to send to the back-end. |
|
255 | 255 | attrs = this._pack_models(attrs); |
|
256 | 256 | if (_.size(attrs) > 0) { |
|
257 | 257 | |
|
258 | 258 | // If this message was sent via backbone itself, it will not |
|
259 | 259 | // have any callbacks. It's important that we create callbacks |
|
260 | 260 | // so we can listen for status messages, etc... |
|
261 | 261 | var callbacks = options.callbacks || this.callbacks(); |
|
262 | 262 | |
|
263 | 263 | // Check throttle. |
|
264 | 264 | if (this.pending_msgs >= (this.get('msg_throttle') || 3)) { |
|
265 | 265 | // The throttle has been exceeded, buffer the current msg so |
|
266 | 266 | // it can be sent once the kernel has finished processing |
|
267 | 267 | // some of the existing messages. |
|
268 | 268 | |
|
269 | 269 | // Combine updates if it is a 'patch' sync, otherwise replace updates |
|
270 | 270 | switch (method) { |
|
271 | 271 | case 'patch': |
|
272 | 272 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); |
|
273 | 273 | break; |
|
274 | 274 | case 'update': |
|
275 | 275 | case 'create': |
|
276 | 276 | this.msg_buffer = attrs; |
|
277 | 277 | break; |
|
278 | 278 | default: |
|
279 | 279 | error(); |
|
280 | 280 | return false; |
|
281 | 281 | } |
|
282 | 282 | this.msg_buffer_callbacks = callbacks; |
|
283 | 283 | |
|
284 | 284 | } else { |
|
285 | 285 | // We haven't exceeded the throttle, send the message like |
|
286 | 286 | // normal. |
|
287 | 287 | var data = {method: 'backbone', sync_data: attrs}; |
|
288 | 288 | this.comm.send(data, callbacks); |
|
289 | 289 | this.pending_msgs++; |
|
290 | 290 | } |
|
291 | 291 | } |
|
292 | 292 | // Since the comm is a one-way communication, assume the message |
|
293 | 293 | // arrived. Don't call success since we don't have a model back from the server |
|
294 | 294 | // this means we miss out on the 'sync' event. |
|
295 | 295 | this._buffered_state_diff = {}; |
|
296 | 296 | }, |
|
297 | 297 | |
|
298 | 298 | save_changes: function(callbacks) { |
|
299 | 299 | /** |
|
300 | 300 | * Push this model's state to the back-end |
|
301 | 301 | * |
|
302 | 302 | * This invokes a Backbone.Sync. |
|
303 | 303 | */ |
|
304 | 304 | this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks}); |
|
305 | 305 | }, |
|
306 | 306 | |
|
307 | 307 | _pack_models: function(value) { |
|
308 | 308 | /** |
|
309 | 309 | * Replace models with model ids recursively. |
|
310 | 310 | */ |
|
311 | 311 | var that = this; |
|
312 | 312 | var packed; |
|
313 | 313 | if (value instanceof Backbone.Model) { |
|
314 | 314 | return "IPY_MODEL_" + value.id; |
|
315 | 315 | |
|
316 | 316 | } else if ($.isArray(value)) { |
|
317 | 317 | packed = []; |
|
318 | 318 | _.each(value, function(sub_value, key) { |
|
319 | 319 | packed.push(that._pack_models(sub_value)); |
|
320 | 320 | }); |
|
321 | 321 | return packed; |
|
322 | 322 | } else if (value instanceof Date || value instanceof String) { |
|
323 | 323 | return value; |
|
324 | 324 | } else if (value instanceof Object) { |
|
325 | 325 | packed = {}; |
|
326 | 326 | _.each(value, function(sub_value, key) { |
|
327 | 327 | packed[key] = that._pack_models(sub_value); |
|
328 | 328 | }); |
|
329 | 329 | return packed; |
|
330 | 330 | |
|
331 | 331 | } else { |
|
332 | 332 | return value; |
|
333 | 333 | } |
|
334 | 334 | }, |
|
335 | 335 | |
|
336 | 336 | _unpack_models: function(value) { |
|
337 | 337 | /** |
|
338 | 338 | * Replace model ids with models recursively. |
|
339 | 339 | */ |
|
340 | 340 | var that = this; |
|
341 | 341 | var unpacked; |
|
342 | 342 | if ($.isArray(value)) { |
|
343 | 343 | unpacked = []; |
|
344 | 344 | _.each(value, function(sub_value, key) { |
|
345 | 345 | unpacked.push(that._unpack_models(sub_value)); |
|
346 | 346 | }); |
|
347 | 347 | return Promise.all(unpacked); |
|
348 | 348 | } else if (value instanceof Object) { |
|
349 | 349 | unpacked = {}; |
|
350 | 350 | _.each(value, function(sub_value, key) { |
|
351 | 351 | unpacked[key] = that._unpack_models(sub_value); |
|
352 | 352 | }); |
|
353 | 353 | return utils.resolve_promises_dict(unpacked); |
|
354 | 354 | } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { |
|
355 | 355 | // get_model returns a promise already |
|
356 | 356 | return this.widget_manager.get_model(value.slice(10, value.length)); |
|
357 | 357 | } else { |
|
358 | 358 | return Promise.resolve(value); |
|
359 | 359 | } |
|
360 | 360 | }, |
|
361 | 361 | |
|
362 | 362 | on_some_change: function(keys, callback, context) { |
|
363 | 363 | /** |
|
364 | 364 | * on_some_change(["key1", "key2"], foo, context) differs from |
|
365 | 365 | * on("change:key1 change:key2", foo, context). |
|
366 | 366 | * If the widget attributes key1 and key2 are both modified, |
|
367 | 367 | * the second form will result in foo being called twice |
|
368 | 368 | * while the first will call foo only once. |
|
369 | 369 | */ |
|
370 | 370 | this.on('change', function() { |
|
371 | 371 | if (keys.some(this.hasChanged, this)) { |
|
372 | 372 | callback.apply(context); |
|
373 | 373 | } |
|
374 | 374 | }, this); |
|
375 | 375 | |
|
376 | 376 | }, |
|
377 | 377 | }); |
|
378 | 378 | widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel); |
|
379 | 379 | |
|
380 | 380 | |
|
381 | 381 | var WidgetView = Backbone.View.extend({ |
|
382 | 382 | initialize: function(parameters) { |
|
383 | 383 | /** |
|
384 | 384 | * Public constructor. |
|
385 | 385 | */ |
|
386 | 386 | this.model.on('change',this.update,this); |
|
387 | 387 | this.options = parameters.options; |
|
388 | 388 | this.on('displayed', function() { |
|
389 | 389 | this.is_displayed = true; |
|
390 | 390 | }, this); |
|
391 | 391 | }, |
|
392 | 392 | |
|
393 | 393 | update: function(){ |
|
394 | 394 | /** |
|
395 | 395 | * Triggered on model change. |
|
396 | 396 | * |
|
397 | 397 | * Update view to be consistent with this.model |
|
398 | 398 | */ |
|
399 | 399 | }, |
|
400 | 400 | |
|
401 | 401 | create_child_view: function(child_model, options) { |
|
402 | 402 | /** |
|
403 | 403 | * Create and promise that resolves to a child view of a given model |
|
404 | 404 | */ |
|
405 | 405 | var that = this; |
|
406 | 406 | options = $.extend({ parent: this }, options || {}); |
|
407 | 407 | return this.model.widget_manager.create_view(child_model, options).catch(utils.reject("Couldn't create child view"), true); |
|
408 | 408 | }, |
|
409 | 409 | |
|
410 | 410 | callbacks: function(){ |
|
411 | 411 | /** |
|
412 | 412 | * Create msg callbacks for a comm msg. |
|
413 | 413 | */ |
|
414 | 414 | return this.model.callbacks(this); |
|
415 | 415 | }, |
|
416 | 416 | |
|
417 | 417 | render: function(){ |
|
418 | 418 | /** |
|
419 | 419 | * Render the view. |
|
420 | 420 | * |
|
421 | 421 | * By default, this is only called the first time the view is created |
|
422 | 422 | */ |
|
423 | 423 | }, |
|
424 | 424 | |
|
425 | 425 | send: function (content) { |
|
426 | 426 | /** |
|
427 | 427 | * Send a custom msg associated with this view. |
|
428 | 428 | */ |
|
429 | 429 | this.model.send(content, this.callbacks()); |
|
430 | 430 | }, |
|
431 | 431 | |
|
432 | 432 | touch: function () { |
|
433 | 433 | this.model.save_changes(this.callbacks()); |
|
434 | 434 | }, |
|
435 | 435 | |
|
436 | 436 | after_displayed: function (callback, context) { |
|
437 | 437 | /** |
|
438 | 438 | * Calls the callback right away is the view is already displayed |
|
439 | 439 | * otherwise, register the callback to the 'displayed' event. |
|
440 | 440 | */ |
|
441 | 441 | if (this.is_displayed) { |
|
442 | 442 | callback.apply(context); |
|
443 | 443 | } else { |
|
444 | 444 | this.on('displayed', callback, context); |
|
445 | 445 | } |
|
446 | 446 | }, |
|
447 | 447 | |
|
448 | 448 | remove: function () { |
|
449 | 449 | // Raise a remove event when the view is removed. |
|
450 | 450 | WidgetView.__super__.remove.apply(this, arguments); |
|
451 | 451 | this.trigger('remove'); |
|
452 | 452 | } |
|
453 | 453 | }); |
|
454 | 454 | |
|
455 | 455 | |
|
456 | 456 | var DOMWidgetView = WidgetView.extend({ |
|
457 | 457 | initialize: function (parameters) { |
|
458 | 458 | /** |
|
459 | 459 | * Public constructor |
|
460 | 460 | */ |
|
461 | 461 | DOMWidgetView.__super__.initialize.apply(this, [parameters]); |
|
462 | 462 | this.model.on('change:visible', this.update_visible, this); |
|
463 | 463 | this.model.on('change:_css', this.update_css, this); |
|
464 | 464 | |
|
465 | 465 | this.model.on('change:_dom_classes', function(model, new_classes) { |
|
466 | 466 | var old_classes = model.previous('_dom_classes'); |
|
467 | 467 | this.update_classes(old_classes, new_classes); |
|
468 | 468 | }, this); |
|
469 | 469 | |
|
470 | 470 | this.model.on('change:color', function (model, value) { |
|
471 | 471 | this.update_attr('color', value); }, this); |
|
472 | 472 | |
|
473 | 473 | this.model.on('change:background_color', function (model, value) { |
|
474 | 474 | this.update_attr('background', value); }, this); |
|
475 | 475 | |
|
476 | 476 | this.model.on('change:width', function (model, value) { |
|
477 | 477 | this.update_attr('width', value); }, this); |
|
478 | 478 | |
|
479 | 479 | this.model.on('change:height', function (model, value) { |
|
480 | 480 | this.update_attr('height', value); }, this); |
|
481 | 481 | |
|
482 | 482 | this.model.on('change:border_color', function (model, value) { |
|
483 | 483 | this.update_attr('border-color', value); }, this); |
|
484 | 484 | |
|
485 | 485 | this.model.on('change:border_width', function (model, value) { |
|
486 | 486 | this.update_attr('border-width', value); }, this); |
|
487 | 487 | |
|
488 | 488 | this.model.on('change:border_style', function (model, value) { |
|
489 | 489 | this.update_attr('border-style', value); }, this); |
|
490 | 490 | |
|
491 | 491 | this.model.on('change:font_style', function (model, value) { |
|
492 | 492 | this.update_attr('font-style', value); }, this); |
|
493 | 493 | |
|
494 | 494 | this.model.on('change:font_weight', function (model, value) { |
|
495 | 495 | this.update_attr('font-weight', value); }, this); |
|
496 | 496 | |
|
497 | 497 | this.model.on('change:font_size', function (model, value) { |
|
498 | 498 | this.update_attr('font-size', this._default_px(value)); }, this); |
|
499 | 499 | |
|
500 | 500 | this.model.on('change:font_family', function (model, value) { |
|
501 | 501 | this.update_attr('font-family', value); }, this); |
|
502 | 502 | |
|
503 | 503 | this.model.on('change:padding', function (model, value) { |
|
504 | 504 | this.update_attr('padding', value); }, this); |
|
505 | 505 | |
|
506 | 506 | this.model.on('change:margin', function (model, value) { |
|
507 | 507 | this.update_attr('margin', this._default_px(value)); }, this); |
|
508 | 508 | |
|
509 | 509 | this.model.on('change:border_radius', function (model, value) { |
|
510 | 510 | this.update_attr('border-radius', this._default_px(value)); }, this); |
|
511 | 511 | |
|
512 | 512 | this.after_displayed(function() { |
|
513 | 513 | this.update_visible(this.model, this.model.get("visible")); |
|
514 | 514 | this.update_classes([], this.model.get('_dom_classes')); |
|
515 | 515 | |
|
516 | 516 | this.update_attr('color', this.model.get('color')); |
|
517 | 517 | this.update_attr('background', this.model.get('background_color')); |
|
518 | 518 | this.update_attr('width', this.model.get('width')); |
|
519 | 519 | this.update_attr('height', this.model.get('height')); |
|
520 | 520 | this.update_attr('border-color', this.model.get('border_color')); |
|
521 | 521 | this.update_attr('border-width', this.model.get('border_width')); |
|
522 | 522 | this.update_attr('border-style', this.model.get('border_style')); |
|
523 | 523 | this.update_attr('font-style', this.model.get('font_style')); |
|
524 | 524 | this.update_attr('font-weight', this.model.get('font_weight')); |
|
525 | 525 | this.update_attr('font-size', this.model.get('font_size')); |
|
526 | 526 | this.update_attr('font-family', this.model.get('font_family')); |
|
527 | 527 | this.update_attr('padding', this.model.get('padding')); |
|
528 | 528 | this.update_attr('margin', this.model.get('margin')); |
|
529 | 529 | this.update_attr('border-radius', this.model.get('border_radius')); |
|
530 | 530 | |
|
531 | 531 | this.update_css(this.model, this.model.get("_css")); |
|
532 | 532 | }, this); |
|
533 | 533 | }, |
|
534 | 534 | |
|
535 | 535 | _default_px: function(value) { |
|
536 | 536 | /** |
|
537 | 537 | * Makes browser interpret a numerical string as a pixel value. |
|
538 | 538 | */ |
|
539 | 539 | if (/^\d+\.?(\d+)?$/.test(value.trim())) { |
|
540 | 540 | return value.trim() + 'px'; |
|
541 | 541 | } |
|
542 | 542 | return value; |
|
543 | 543 | }, |
|
544 | 544 | |
|
545 | 545 | update_attr: function(name, value) { |
|
546 | 546 | /** |
|
547 | 547 | * Set a css attr of the widget view. |
|
548 | 548 | */ |
|
549 | 549 | this.$el.css(name, value); |
|
550 | 550 | }, |
|
551 | 551 | |
|
552 | 552 | update_visible: function(model, value) { |
|
553 | 553 | /** |
|
554 | 554 | * Update visibility |
|
555 | 555 | */ |
|
556 | 556 | this.$el.toggle(value); |
|
557 | 557 | }, |
|
558 | 558 | |
|
559 | 559 | update_css: function (model, css) { |
|
560 | 560 | /** |
|
561 | 561 | * Update the css styling of this view. |
|
562 | 562 | */ |
|
563 | 563 | var e = this.$el; |
|
564 | 564 | if (css === undefined) {return;} |
|
565 | 565 | for (var i = 0; i < css.length; i++) { |
|
566 | 566 | // Apply the css traits to all elements that match the selector. |
|
567 | 567 | var selector = css[i][0]; |
|
568 | 568 | var elements = this._get_selector_element(selector); |
|
569 | 569 | if (elements.length > 0) { |
|
570 | 570 | var trait_key = css[i][1]; |
|
571 | 571 | var trait_value = css[i][2]; |
|
572 | 572 | elements.css(trait_key ,trait_value); |
|
573 | 573 | } |
|
574 | 574 | } |
|
575 | 575 | }, |
|
576 | 576 | |
|
577 | 577 | update_classes: function (old_classes, new_classes, $el) { |
|
578 | 578 | /** |
|
579 | 579 | * Update the DOM classes applied to an element, default to this.$el. |
|
580 | 580 | */ |
|
581 | 581 | if ($el===undefined) { |
|
582 | 582 | $el = this.$el; |
|
583 | 583 | } |
|
584 | 584 | _.difference(old_classes, new_classes).map(function(c) {$el.removeClass(c);}) |
|
585 | 585 | _.difference(new_classes, old_classes).map(function(c) {$el.addClass(c);}) |
|
586 | 586 | }, |
|
587 | 587 | |
|
588 | 588 | update_mapped_classes: function(class_map, trait_name, previous_trait_value, $el) { |
|
589 | 589 | /** |
|
590 | 590 | * Update the DOM classes applied to the widget based on a single |
|
591 | 591 | * trait's value. |
|
592 | 592 | * |
|
593 | 593 | * Given a trait value classes map, this function automatically |
|
594 | 594 | * handles applying the appropriate classes to the widget element |
|
595 | 595 | * and removing classes that are no longer valid. |
|
596 | 596 | * |
|
597 | 597 | * Parameters |
|
598 | 598 | * ---------- |
|
599 | 599 | * class_map: dictionary |
|
600 | 600 | * Dictionary of trait values to class lists. |
|
601 | 601 | * Example: |
|
602 | 602 | * { |
|
603 | 603 | * success: ['alert', 'alert-success'], |
|
604 | 604 | * info: ['alert', 'alert-info'], |
|
605 | 605 | * warning: ['alert', 'alert-warning'], |
|
606 | 606 | * danger: ['alert', 'alert-danger'] |
|
607 | 607 | * }; |
|
608 | 608 | * trait_name: string |
|
609 | 609 | * Name of the trait to check the value of. |
|
610 | 610 | * previous_trait_value: optional string, default '' |
|
611 | 611 | * Last trait value |
|
612 | 612 | * $el: optional jQuery element handle, defaults to this.$el |
|
613 | 613 | * Element that the classes are applied to. |
|
614 | 614 | */ |
|
615 | 615 | var key = previous_trait_value; |
|
616 | 616 | if (key === undefined) { |
|
617 | 617 | key = this.model.previous(trait_name); |
|
618 | 618 | } |
|
619 | 619 | var old_classes = class_map[key] ? class_map[key] : []; |
|
620 | 620 | key = this.model.get(trait_name); |
|
621 | 621 | var new_classes = class_map[key] ? class_map[key] : []; |
|
622 | 622 | |
|
623 | 623 | this.update_classes(old_classes, new_classes, $el || this.$el); |
|
624 | 624 | }, |
|
625 | 625 | |
|
626 | 626 | _get_selector_element: function (selector) { |
|
627 | 627 | /** |
|
628 | 628 | * Get the elements via the css selector. |
|
629 | 629 | */ |
|
630 | 630 | var elements; |
|
631 | 631 | if (!selector) { |
|
632 | 632 | elements = this.$el; |
|
633 | 633 | } else { |
|
634 | 634 | elements = this.$el.find(selector).addBack(selector); |
|
635 | 635 | } |
|
636 | 636 | return elements; |
|
637 | 637 | }, |
|
638 | 638 | |
|
639 | 639 | typeset: function(element, text){ |
|
640 | 640 | utils.typeset.apply(null, arguments); |
|
641 | 641 | }, |
|
642 | 642 | }); |
|
643 | 643 | |
|
644 | 644 | |
|
645 | 645 | var ViewList = function(create_view, remove_view, context) { |
|
646 | 646 | /** |
|
647 | 647 | * - create_view and remove_view are default functions called when adding or removing views |
|
648 | 648 | * - create_view takes a model and returns a view or a promise for a view for that model |
|
649 | 649 | * - remove_view takes a view and destroys it (including calling `view.remove()`) |
|
650 | 650 | * - each time the update() function is called with a new list, the create and remove |
|
651 | 651 | * callbacks will be called in an order so that if you append the views created in the |
|
652 | 652 | * create callback and remove the views in the remove callback, you will duplicate |
|
653 | 653 | * the order of the list. |
|
654 | 654 | * - the remove callback defaults to just removing the view (e.g., pass in null for the second parameter) |
|
655 | 655 | * - the context defaults to the created ViewList. If you pass another context, the create and remove |
|
656 | 656 | * will be called in that context. |
|
657 | 657 | */ |
|
658 | 658 | |
|
659 | 659 | this.initialize.apply(this, arguments); |
|
660 | 660 | }; |
|
661 | 661 | |
|
662 | 662 | _.extend(ViewList.prototype, { |
|
663 | 663 | initialize: function(create_view, remove_view, context) { |
|
664 | this.state_change = Promise.resolve(); | |
|
665 | 664 | this._handler_context = context || this; |
|
666 | 665 | this._models = []; |
|
667 | this.views = []; | |
|
666 | this.views = []; // list of promises for views | |
|
668 | 667 | this._create_view = create_view; |
|
669 | 668 | this._remove_view = remove_view || function(view) {view.remove();}; |
|
670 | 669 | }, |
|
671 | 670 | |
|
672 | 671 | update: function(new_models, create_view, remove_view, context) { |
|
673 | 672 | /** |
|
674 | 673 | * the create_view, remove_view, and context arguments override the defaults |
|
675 | 674 | * specified when the list is created. |
|
676 | * returns a promise that resolves after this update is done | |
|
675 | * after this function, the .views attribute is a list of promises for views | |
|
676 | * if you want to perform some action on the list of views, do something like | |
|
677 | * `Promise.all(myviewlist.views).then(function(views) {...});` | |
|
677 | 678 | */ |
|
678 | 679 | var remove = remove_view || this._remove_view; |
|
679 | 680 | var create = create_view || this._create_view; |
|
680 | if (create === undefined || remove === undefined){ | |
|
681 | console.error("Must define a create a remove function"); | |
|
682 | } | |
|
683 | 681 | var context = context || this._handler_context; |
|
684 |
var |
|
|
685 | var that = this; | |
|
686 | this.state_change = this.state_change.then(function() { | |
|
687 | var i; | |
|
688 | // first, skip past the beginning of the lists if they are identical | |
|
689 | for (i = 0; i < new_models.length; i++) { | |
|
690 | if (i >= that._models.length || new_models[i] !== that._models[i]) { | |
|
691 | break; | |
|
692 | } | |
|
693 | } | |
|
694 | var first_removed = i; | |
|
695 | // Remove the non-matching items from the old list. | |
|
696 | for (var j = first_removed; j < that._models.length; j++) { | |
|
697 | remove.call(context, that.views[j]); | |
|
698 | } | |
|
699 | ||
|
700 | // Add the rest of the new list items. | |
|
701 | for (; i < new_models.length; i++) { | |
|
702 | added_views.push(create.call(context, new_models[i])); | |
|
682 | var i = 0; | |
|
683 | // first, skip past the beginning of the lists if they are identical | |
|
684 | for (; i < new_models.length; i++) { | |
|
685 | if (i >= this._models.length || new_models[i] !== this._models[i]) { | |
|
686 | break; | |
|
703 | 687 | } |
|
704 | // make a copy of the input array | |
|
705 | that._models = new_models.slice(); | |
|
706 | return Promise.all(added_views).then(function(added) { | |
|
707 | Array.prototype.splice.apply(that.views, [first_removed, that.views.length].concat(added)); | |
|
708 | return that.views; | |
|
688 | } | |
|
689 | ||
|
690 | var first_removed = i; | |
|
691 | // Remove the non-matching items from the old list. | |
|
692 | var removed = this.views.splice(first_removed, this.views.length-first_removed); | |
|
693 | for (var j = 0; j < removed.length; j++) { | |
|
694 | removed[j].then(function(view) { | |
|
695 | remove.call(context, view) | |
|
709 | 696 | }); |
|
710 |
} |
|
|
711 | return this.state_change; | |
|
697 | } | |
|
698 | ||
|
699 | // Add the rest of the new list items. | |
|
700 | for (; i < new_models.length; i++) { | |
|
701 | this.views.push(Promise.resolve(create.call(context, new_models[i]))); | |
|
702 | } | |
|
703 | // make a copy of the input array | |
|
704 | this._models = new_models.slice(); | |
|
712 | 705 | }, |
|
713 | 706 | |
|
714 | 707 | remove: function() { |
|
715 | 708 | /** |
|
716 | 709 | * removes every view in the list; convenience function for `.update([])` |
|
717 | 710 | * that should be faster |
|
718 | 711 | * returns a promise that resolves after this removal is done |
|
719 | 712 | */ |
|
720 | 713 | var that = this; |
|
721 | this.state_change = this.state_change.then(function() { | |
|
714 | return Promise.all(this.views).then(function(views) { | |
|
722 | 715 | for (var i = 0; i < that.views.length; i++) { |
|
723 |
that._remove_view.call(that._handler_context, |
|
|
716 | that._remove_view.call(that._handler_context, views[i]); | |
|
724 | 717 | } |
|
725 | that._models = []; | |
|
726 | 718 | that.views = []; |
|
719 | that._models = []; | |
|
727 | 720 | }); |
|
728 | return this.state_change; | |
|
729 | 721 | }, |
|
730 | 722 | }); |
|
731 | 723 | |
|
732 | 724 | var widget = { |
|
733 | 725 | 'WidgetModel': WidgetModel, |
|
734 | 726 | 'WidgetView': WidgetView, |
|
735 | 727 | 'DOMWidgetView': DOMWidgetView, |
|
736 | 728 | 'ViewList': ViewList, |
|
737 | 729 | }; |
|
738 | 730 | |
|
739 | 731 | // For backwards compatability. |
|
740 | 732 | $.extend(IPython, widget); |
|
741 | 733 | |
|
742 | 734 | return widget; |
|
743 | 735 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now