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