widget.js
487 lines
| 17.7 KiB
| application/javascript
|
JavascriptLexer
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r14546 | |||
|
r15427 | define(["widgets/js/manager", | ||
|
r14546 | "underscore", | ||
|
r17216 | "backbone", | ||
"jquery", | ||||
"base/js/namespace", | ||||
], function(widgetmanager, _, Backbone, $, IPython){ | ||||
|
r14609 | |||
|
r14546 | var WidgetModel = Backbone.Model.extend({ | ||
constructor: function (widget_manager, model_id, comm) { | ||||
|
r14792 | // Constructor | ||
|
r14561 | // | ||
// Creates a WidgetModel instance. | ||||
// | ||||
// Parameters | ||||
// ---------- | ||||
// widget_manager : WidgetManager instance | ||||
// model_id : string | ||||
// An ID unique to this model. | ||||
// comm : Comm instance (optional) | ||||
|
r14546 | this.widget_manager = widget_manager; | ||
|
r15280 | this._buffered_state_diff = {}; | ||
|
r14546 | this.pending_msgs = 0; | ||
this.msg_buffer = null; | ||||
|
r14563 | this.key_value_lock = null; | ||
|
r14546 | this.id = model_id; | ||
this.views = []; | ||||
if (comm !== undefined) { | ||||
// Remember comm associated with the model. | ||||
this.comm = comm; | ||||
comm.model = this; | ||||
// Hook comm messages up to model. | ||||
comm.on_close($.proxy(this._handle_comm_closed, this)); | ||||
comm.on_msg($.proxy(this._handle_comm_msg, this)); | ||||
} | ||||
return Backbone.Model.apply(this); | ||||
}, | ||||
send: function (content, callbacks) { | ||||
|
r14609 | // Send a custom msg over the comm. | ||
|
r14546 | if (this.comm !== undefined) { | ||
|
r14655 | var data = {method: 'custom', content: content}; | ||
|
r14546 | this.comm.send(data, callbacks); | ||
|
r14741 | this.pending_msgs++; | ||
|
r14546 | } | ||
}, | ||||
_handle_comm_closed: function (msg) { | ||||
|
r14609 | // Handle when a widget is closed. | ||
|
r14546 | this.trigger('comm:close'); | ||
|
r17840 | this.comm.model.trigger('destroy', this.comm.model); | ||
|
r14546 | delete this.comm.model; // Delete ref so GC will collect widget model. | ||
delete this.comm; | ||||
delete this.model_id; // Delete id from model so widget manager cleans up. | ||||
|
r14704 | _.each(this.views, function(view, i) { | ||
view.remove(); | ||||
}); | ||||
|
r14546 | }, | ||
_handle_comm_msg: function (msg) { | ||||
|
r14609 | // Handle incoming comm msg. | ||
|
r14546 | var method = msg.content.data.method; | ||
switch (method) { | ||||
case 'update': | ||||
this.apply_update(msg.content.data.state); | ||||
break; | ||||
case 'custom': | ||||
|
r14655 | this.trigger('msg:custom', msg.content.data.content); | ||
|
r14546 | break; | ||
|
r14559 | case 'display': | ||
|
r14620 | this.widget_manager.display_view(msg, this); | ||
|
r14559 | break; | ||
|
r14546 | } | ||
}, | ||||
apply_update: function (state) { | ||||
|
r14609 | // Handle when a widget is updated via the python side. | ||
|
r14686 | var that = this; | ||
|
r14664 | _.each(state, function(value, key) { | ||
|
r14686 | that.key_value_lock = [key, value]; | ||
|
r14664 | try { | ||
|
r15283 | WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]); | ||
|
r14664 | } finally { | ||
|
r14686 | that.key_value_lock = null; | ||
|
r14546 | } | ||
|
r14664 | }); | ||
|
r14546 | }, | ||
_handle_status: function (msg, callbacks) { | ||||
|
r14609 | // Handle status msgs. | ||
// execution_state : ('busy', 'idle', 'starting') | ||||
|
r14596 | if (this.comm !== undefined) { | ||
if (msg.content.execution_state ==='idle') { | ||||
// Send buffer if this message caused another message to be | ||||
// throttled. | ||||
if (this.msg_buffer !== null && | ||||
|
r15369 | (this.get('msg_throttle') || 3) === this.pending_msgs) { | ||
|
r14596 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | ||
|
r14792 | this.comm.send(data, callbacks); | ||
|
r14596 | this.msg_buffer = null; | ||
} else { | ||||
--this.pending_msgs; | ||||
} | ||||
|
r14546 | } | ||
} | ||||
}, | ||||
|
r14640 | callbacks: function(view) { | ||
|
r14639 | // Create msg callbacks for a comm msg. | ||
|
r14640 | var callbacks = this.widget_manager.callbacks(view); | ||
if (callbacks.iopub === undefined) { | ||||
callbacks.iopub = {}; | ||||
} | ||||
|
r14639 | var that = this; | ||
callbacks.iopub.status = function (msg) { | ||||
that._handle_status(msg, callbacks); | ||||
|
r14792 | }; | ||
|
r14639 | return callbacks; | ||
}, | ||||
|
r14546 | |||
|
r15279 | set: function(key, val, options) { | ||
// Set a value. | ||||
|
r15280 | var return_value = WidgetModel.__super__.set.apply(this, arguments); | ||
// Backbone only remembers the diff of the most recent set() | ||||
|
r15281 | // operation. Calling set multiple times in a row results in a | ||
|
r15280 | // loss of diff information. Here we keep our own running diff. | ||
this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {}); | ||||
return return_value; | ||||
|
r15279 | }, | ||
|
r14639 | sync: function (method, model, options) { | ||
|
r14668 | // Handle sync to the back-end. Called when a model.save() is called. | ||
|
r14640 | |||
// Make sure a comm exists. | ||||
var error = options.error || function() { | ||||
console.error('Backbone sync error:', arguments); | ||||
|
r14792 | }; | ||
|
r14639 | if (this.comm === undefined) { | ||
error(); | ||||
return false; | ||||
} | ||||
|
r14640 | // Delete any key value pairs that the back-end already knows about. | ||
|
r15274 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); | ||
|
r14639 | if (this.key_value_lock !== null) { | ||
|
r14640 | var key = this.key_value_lock[0]; | ||
var value = this.key_value_lock[1]; | ||||
if (attrs[key] === value) { | ||||
delete attrs[key]; | ||||
|
r14639 | } | ||
} | ||||
|
r14640 | // Only sync if there are attributes to send to the back-end. | ||
|
r15279 | attrs = this._pack_models(attrs); | ||
|
r14686 | if (_.size(attrs) > 0) { | ||
|
r14741 | |||
// If this message was sent via backbone itself, it will not | ||||
// have any callbacks. It's important that we create callbacks | ||||
// so we can listen for status messages, etc... | ||||
var callbacks = options.callbacks || this.callbacks(); | ||||
// Check throttle. | ||||
|
r15369 | if (this.pending_msgs >= (this.get('msg_throttle') || 3)) { | ||
|
r14640 | // The throttle has been exceeded, buffer the current msg so | ||
// it can be sent once the kernel has finished processing | ||||
// some of the existing messages. | ||||
// Combine updates if it is a 'patch' sync, otherwise replace updates | ||||
switch (method) { | ||||
case 'patch': | ||||
|
r14662 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); | ||
|
r14640 | break; | ||
case 'update': | ||||
|
r14661 | case 'create': | ||
|
r14640 | this.msg_buffer = attrs; | ||
break; | ||||
default: | ||||
error(); | ||||
return false; | ||||
} | ||||
this.msg_buffer_callbacks = callbacks; | ||||
} else { | ||||
// We haven't exceeded the throttle, send the message like | ||||
|
r14741 | // normal. | ||
|
r14640 | var data = {method: 'backbone', sync_data: attrs}; | ||
this.comm.send(data, callbacks); | ||||
this.pending_msgs++; | ||||
} | ||||
|
r14546 | } | ||
// Since the comm is a one-way communication, assume the message | ||||
|
r14639 | // arrived. Don't call success since we don't have a model back from the server | ||
// this means we miss out on the 'sync' event. | ||||
|
r15280 | this._buffered_state_diff = {}; | ||
|
r14546 | }, | ||
|
r14639 | save_changes: function(callbacks) { | ||
|
r14651 | // Push this model's state to the back-end | ||
// | ||||
// This invokes a Backbone.Sync. | ||||
|
r15280 | this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks}); | ||
|
r14618 | }, | ||
|
r14583 | _pack_models: function(value) { | ||
|
r14609 | // Replace models with model ids recursively. | ||
|
r17199 | var that = this; | ||
var packed; | ||||
|
r14583 | if (value instanceof Backbone.Model) { | ||
|
r17262 | return "IPY_MODEL_" + value.id; | ||
|
r15063 | |||
} else if ($.isArray(value)) { | ||||
|
r17199 | packed = []; | ||
|
r15063 | _.each(value, function(sub_value, key) { | ||
packed.push(that._pack_models(sub_value)); | ||||
}); | ||||
return packed; | ||||
|
r14583 | } else if (value instanceof Object) { | ||
|
r17199 | packed = {}; | ||
|
r14664 | _.each(value, function(sub_value, key) { | ||
|
r14686 | packed[key] = that._pack_models(sub_value); | ||
|
r14664 | }); | ||
|
r14583 | return packed; | ||
|
r15063 | |||
|
r14583 | } else { | ||
return value; | ||||
} | ||||
}, | ||||
_unpack_models: function(value) { | ||||
|
r14609 | // Replace model ids with models recursively. | ||
|
r17198 | var that = this; | ||
var unpacked; | ||||
|
r15063 | if ($.isArray(value)) { | ||
|
r17198 | unpacked = []; | ||
|
r15063 | _.each(value, function(sub_value, key) { | ||
unpacked.push(that._unpack_models(sub_value)); | ||||
}); | ||||
return unpacked; | ||||
} else if (value instanceof Object) { | ||||
|
r17198 | unpacked = {}; | ||
|
r14664 | _.each(value, function(sub_value, key) { | ||
|
r14686 | unpacked[key] = that._unpack_models(sub_value); | ||
|
r14664 | }); | ||
|
r14583 | return unpacked; | ||
|
r15063 | |||
|
r17262 | } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") { | ||
var model = this.widget_manager.get_model(value.slice(10, value.length)); | ||||
if (model) { | ||||
return model; | ||||
} else { | ||||
return value; | ||||
} | ||||
|
r14583 | } else { | ||
return value; | ||||
} | ||||
}, | ||||
|
r14546 | }); | ||
|
r17202 | widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel); | ||
|
r14546 | |||
|
r14564 | var WidgetView = Backbone.View.extend({ | ||
|
r14565 | initialize: function(parameters) { | ||
|
r14609 | // Public constructor. | ||
|
r14546 | this.model.on('change',this.update,this); | ||
|
r14565 | this.options = parameters.options; | ||
|
r17172 | this.child_model_views = {}; | ||
this.child_views = {}; | ||||
|
r14546 | this.model.views.push(this); | ||
|
r17175 | this.id = this.id || IPython.utils.uuid(); | ||
|
r17270 | this.on('displayed', function() { | ||
this.is_displayed = true; | ||||
}, this); | ||||
|
r14546 | }, | ||
update: function(){ | ||||
|
r14609 | // Triggered on model change. | ||
// | ||||
// Update view to be consistent with this.model | ||||
|
r14546 | }, | ||
|
r14598 | create_child_view: function(child_model, options) { | ||
|
r14609 | // Create and return a child view. | ||
// | ||||
|
r14617 | // -given a model and (optionally) a view name if the view name is | ||
|
r14609 | // not given, it defaults to the model's default view attribute. | ||
|
r14651 | |||
|
r14617 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior | ||
|
r14651 | // it would be great to have the widget manager add the cell metadata | ||
// to the subview without having to add it here. | ||||
|
r17180 | options = $.extend({ parent: this }, options || {}); | ||
|
r17178 | var child_view = this.model.widget_manager.create_view(child_model, options, this); | ||
|
r17172 | |||
// Associate the view id with the model id. | ||||
if (this.child_model_views[child_model.id] === undefined) { | ||||
this.child_model_views[child_model.id] = []; | ||||
} | ||||
this.child_model_views[child_model.id].push(child_view.id); | ||||
// Remember the view by id. | ||||
this.child_views[child_view.id] = child_view; | ||||
|
r14546 | return child_view; | ||
}, | ||||
|
r14598 | |||
|
r17178 | pop_child_view: function(child_model) { | ||
|
r14609 | // Delete a child view that was previously created using create_child_view. | ||
|
r17172 | var view_ids = this.child_model_views[child_model.id]; | ||
if (view_ids !== undefined) { | ||||
|
r17173 | // Only delete the first view in the list. | ||
var view_id = view_ids[0]; | ||||
var view = this.child_views[view_id]; | ||||
delete this.child_views[view_id]; | ||||
|
r17175 | view_ids.splice(0,1); | ||
|
r17173 | child_model.views.pop(view); | ||
// Remove the view list specific to this model if it is empty. | ||||
if (view_ids.length === 0) { | ||||
delete this.child_model_views[child_model.id]; | ||||
|
r17172 | } | ||
|
r17173 | return view; | ||
|
r14667 | } | ||
|
r17173 | return null; | ||
|
r14546 | }, | ||
|
r17176 | do_diff: function(old_list, new_list, removed_callback, added_callback) { | ||
|
r14580 | // Difference a changed list and call remove and add callbacks for | ||
// each removed and added item in the new list. | ||||
// | ||||
// Parameters | ||||
// ---------- | ||||
// old_list : array | ||||
// new_list : array | ||||
// removed_callback : Callback(item) | ||||
// Callback that is called for each item removed. | ||||
// added_callback : Callback(item) | ||||
// Callback that is called for each item added. | ||||
|
r17176 | // Walk the lists until an unequal entry is found. | ||
var i; | ||||
for (i = 0; i < new_list.length; i++) { | ||||
|
r17289 | if (i >= old_list.length || new_list[i] !== old_list[i]) { | ||
|
r17176 | break; | ||
|
r17174 | } | ||
|
r17176 | } | ||
|
r14580 | |||
|
r17176 | // Remove the non-matching items from the old list. | ||
for (var j = i; j < old_list.length; j++) { | ||||
removed_callback(old_list[j]); | ||||
|
r17174 | } | ||
|
r14580 | |||
|
r17176 | // Add the rest of the new list items. | ||
|
r17327 | for (; i < new_list.length; i++) { | ||
|
r17176 | added_callback(new_list[i]); | ||
|
r17172 | } | ||
}, | ||||
|
r14546 | callbacks: function(){ | ||
|
r14609 | // Create msg callbacks for a comm msg. | ||
|
r14640 | return this.model.callbacks(this); | ||
|
r14560 | }, | ||
|
r14546 | |||
render: function(){ | ||||
|
r14609 | // Render the view. | ||
// | ||||
// By default, this is only called the first time the view is created | ||||
|
r14546 | }, | ||
|
r14609 | |||
|
r17227 | show: function(){ | ||
// Show the widget-area | ||||
if (this.options && this.options.cell && | ||||
this.options.cell.widget_area !== undefined) { | ||||
this.options.cell.widget_area.show(); | ||||
} | ||||
}, | ||||
|
r14546 | send: function (content) { | ||
|
r14609 | // Send a custom msg associated with this view. | ||
|
r14546 | this.model.send(content, this.callbacks()); | ||
}, | ||||
touch: function () { | ||||
|
r14639 | this.model.save_changes(this.callbacks()); | ||
|
r14546 | }, | ||
|
r17270 | |||
|
r17329 | after_displayed: function (callback, context) { | ||
|
r17270 | // Calls the callback right away is the view is already displayed | ||
// otherwise, register the callback to the 'displayed' event. | ||||
if (this.is_displayed) { | ||||
callback.apply(context); | ||||
} else { | ||||
this.on('displayed', callback, context); | ||||
} | ||||
}, | ||||
|
r14546 | }); | ||
|
r14609 | |||
|
r14564 | var DOMWidgetView = WidgetView.extend({ | ||
|
r17448 | initialize: function (parameters) { | ||
|
r14609 | // Public constructor | ||
|
r17448 | DOMWidgetView.__super__.initialize.apply(this, [parameters]); | ||
|
r17227 | this.on('displayed', this.show, this); | ||
|
r17386 | this.after_displayed(function() { | ||
this.update_visible(this.model, this.model.get("visible")); | ||||
this.update_css(this.model, this.model.get("_css")); | ||||
}, this); | ||||
|
r17347 | this.model.on('msg:custom', this.on_msg, this); | ||
this.model.on('change:visible', this.update_visible, this); | ||||
this.model.on('change:_css', this.update_css, this); | ||||
|
r14546 | }, | ||
|
r17386 | |||
|
r14546 | on_msg: function(msg) { | ||
|
r14609 | // Handle DOM specific msgs. | ||
|
r14546 | switch(msg.msg_type) { | ||
case 'add_class': | ||||
this.add_class(msg.selector, msg.class_list); | ||||
break; | ||||
case 'remove_class': | ||||
this.remove_class(msg.selector, msg.class_list); | ||||
break; | ||||
} | ||||
}, | ||||
add_class: function (selector, class_list) { | ||||
|
r14609 | // Add a DOM class to an element. | ||
|
r14567 | this._get_selector_element(selector).addClass(class_list); | ||
|
r14546 | }, | ||
|
r17386 | |||
|
r14546 | remove_class: function (selector, class_list) { | ||
|
r14609 | // Remove a DOM class from an element. | ||
|
r14567 | this._get_selector_element(selector).removeClass(class_list); | ||
|
r14546 | }, | ||
|
r17386 | |||
|
r17347 | update_visible: function(model, value) { | ||
// Update visibility | ||||
|
r17386 | this.$el.toggle(value); | ||
|
r17347 | }, | ||
update_css: function (model, css) { | ||||
|
r17353 | // Update the css styling of this view. | ||
|
r14546 | var e = this.$el; | ||
if (css === undefined) {return;} | ||||
|
r17177 | for (var i = 0; i < css.length; i++) { | ||
|
r14664 | // Apply the css traits to all elements that match the selector. | ||
|
r17177 | var selector = css[i][0]; | ||
var elements = this._get_selector_element(selector); | ||||
|
r14664 | if (elements.length > 0) { | ||
|
r17177 | var trait_key = css[i][1]; | ||
var trait_value = css[i][2]; | ||||
elements.css(trait_key ,trait_value); | ||||
|
r14546 | } | ||
|
r17177 | } | ||
|
r14546 | }, | ||
_get_selector_element: function (selector) { | ||||
|
r14792 | // Get the elements via the css selector. | ||
|
r14546 | var elements; | ||
|
r14792 | if (!selector) { | ||
|
r17420 | elements = this.$el; | ||
|
r14546 | } else { | ||
|
r17420 | elements = this.$el.find(selector).addBack(selector); | ||
|
r14546 | } | ||
return elements; | ||||
}, | ||||
}); | ||||
|
r17216 | var widget = { | ||
|
r17198 | 'WidgetModel': WidgetModel, | ||
'WidgetView': WidgetView, | ||||
'DOMWidgetView': DOMWidgetView, | ||||
}; | ||||
|
r17216 | |||
// For backwards compatability. | ||||
$.extend(IPython, widget); | ||||
return widget; | ||||
|
r14546 | }); | ||