widget.js
454 lines
| 18.6 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r14224 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2013 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// WidgetModel, WidgetView, and WidgetManager | ||||
//============================================================================ | ||||
/** | ||||
* Base Widget classes | ||||
* @module IPython | ||||
* @namespace IPython | ||||
* @submodule widget | ||||
*/ | ||||
Jonathan Frederic
|
r14237 | "use strict"; | ||
Jonathan Frederic
|
r14263 | // Use require.js 'define' method so that require.js is intelligent enough to | ||
// syncronously load everything within this file when it is being 'required' | ||||
// elsewhere. | ||||
Jonathan Frederic
|
r14285 | define(["components/underscore/underscore-min", | ||
"components/backbone/backbone-min", | ||||
Jonathan Frederic
|
r14263 | ], function(){ | ||
// Only run once on a notebook. | ||||
if (IPython.notebook.widget_manager == undefined) { | ||||
//-------------------------------------------------------------------- | ||||
// WidgetModel class | ||||
//-------------------------------------------------------------------- | ||||
var WidgetModel = Backbone.Model.extend({ | ||||
Jonathan Frederic
|
r14278 | constructor: function(comm_manager, comm, widget_view_types) { | ||
this.comm_manager = comm_manager; | ||||
this.widget_view_types = widget_view_types; | ||||
this.pending_msgs = 0; | ||||
this.msg_throttle = 3; | ||||
this.msg_buffer = {}; | ||||
this.views = {}; | ||||
// 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); | ||||
}, | ||||
update_other_views: function(caller) { | ||||
this.last_modified_view = caller; | ||||
Jonathan Frederic
|
r14273 | this.save(this.changedAttributes(), {patch: true}); | ||
Jonathan Frederic
|
r14263 | |||
Jonathan Frederic
|
r14317 | for (var output_area in this.views) { | ||
var views = this.views[output_area]; | ||||
Jonathan Frederic
|
r14281 | for (var view_index in views) { | ||
var view = views[view_index]; | ||||
if (view !== caller) { | ||||
view.update(); | ||||
} | ||||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14227 | } | ||
Jonathan Frederic
|
r14278 | }, | ||
handle_status: function (output_area, msg) { | ||||
//execution_state : ('busy', 'idle', 'starting') | ||||
if (msg.content.execution_state=='idle') { | ||||
// Send buffer if this message caused another message to be | ||||
// throttled. | ||||
if (this.msg_throttle == this.pending_msgs && | ||||
this.msg_buffer.length > 0) { | ||||
var output_area = this._get_msg_output_area(msg); | ||||
var callbacks = this._make_callbacks(output_area); | ||||
var data = {sync_method: 'patch', sync_data: this.msg_buffer}; | ||||
comm.send(data, callbacks); | ||||
this.msg_buffer = {}; | ||||
} else { | ||||
// Only decrease the pending message count if the buffer | ||||
// doesn't get flushed (sent). | ||||
--this.pending_msgs; | ||||
} | ||||
} | ||||
}, | ||||
// Custom syncronization logic. | ||||
handle_sync: function (method, options) { | ||||
var model_json = this.toJSON(); | ||||
// Only send updated state if the state hasn't been changed | ||||
// during an update. | ||||
if (!this.updating) { | ||||
if (this.pending_msgs >= this.msg_throttle) { | ||||
// 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. | ||||
if (method=='patch') { | ||||
for (var attr in options.attrs) { | ||||
this.msg_buffer[attr] = options.attrs[attr]; | ||||
} | ||||
} else { | ||||
this.msg_buffer = $.extend({}, model_json); // Copy | ||||
} | ||||
} else { | ||||
// We haven't exceeded the throttle, send the message like | ||||
// normal. If this is a patch operation, just send the | ||||
// changes. | ||||
var send_json = model_json; | ||||
if (method=='patch') { | ||||
send_json = {}; | ||||
for (var attr in options.attrs) { | ||||
send_json[attr] = options.attrs[attr]; | ||||
} | ||||
} | ||||
var data = {sync_method: method, sync_data: send_json}; | ||||
Jonathan Frederic
|
r14317 | var output_area = this.last_modified_view.output_area; | ||
Jonathan Frederic
|
r14279 | var callbacks = this._make_callbacks(output_area); | ||
Jonathan Frederic
|
r14278 | this.comm.send(data, callbacks); | ||
this.pending_msgs++; | ||||
} | ||||
} | ||||
// Since the comm is a one-way communication, assume the message | ||||
// arrived. | ||||
return model_json; | ||||
}, | ||||
// Handle incomming comm msg. | ||||
Jonathan Frederic
|
r14279 | handle_comm_msg: function (msg) { | ||
Jonathan Frederic
|
r14278 | var method = msg.content.data.method; | ||
switch (method){ | ||||
case 'display': | ||||
Jonathan Frederic
|
r14316 | |||
// Try to get the cell index. | ||||
Jonathan Frederic
|
r14317 | var output_area = this._get_output_area(msg.parent_header.msg_id); | ||
if (output_area == null) { | ||||
Jonathan Frederic
|
r14316 | console.log("Could not determine where the display" + | ||
" message was from. Widget will not be displayed") | ||||
} else { | ||||
this.display_view(msg.content.data.view_name, | ||||
Jonathan Frederic
|
r14278 | msg.content.data.parent, | ||
Jonathan Frederic
|
r14317 | output_area); | ||
Jonathan Frederic
|
r14316 | } | ||
Jonathan Frederic
|
r14278 | break; | ||
case 'update': | ||||
this.handle_update(msg.content.data.state); | ||||
break; | ||||
} | ||||
Jonathan Frederic
|
r14279 | }, | ||
Jonathan Frederic
|
r14278 | |||
// Handle when a widget is updated via the python side. | ||||
handle_update: function (state) { | ||||
this.updating = true; | ||||
try { | ||||
for (var key in state) { | ||||
if (state.hasOwnProperty(key)) { | ||||
if (key == "_css"){ | ||||
this.css = state[key]; | ||||
} else { | ||||
this.set(key, state[key]); | ||||
} | ||||
} | ||||
} | ||||
this.id = this.comm.comm_id; | ||||
this.save(); | ||||
} finally { | ||||
this.updating = false; | ||||
} | ||||
Jonathan Frederic
|
r14279 | }, | ||
Jonathan Frederic
|
r14278 | |||
// Handle when a widget is closed. | ||||
handle_comm_closed: function (msg) { | ||||
Jonathan Frederic
|
r14317 | for (var output_area in this.views) { | ||
var views = this.views[output_area]; | ||||
Jonathan Frederic
|
r14281 | for (var view_index in views) { | ||
var view = views[view_index]; | ||||
view.remove(); | ||||
} | ||||
Jonathan Frederic
|
r14278 | } | ||
Jonathan Frederic
|
r14279 | }, | ||
Jonathan Frederic
|
r14278 | |||
// Create view that represents the model. | ||||
Jonathan Frederic
|
r14317 | display_view: function (view_name, parent_comm_id, output_area) { | ||
Jonathan Frederic
|
r14285 | var new_views = []; | ||
var displayed = false; | ||||
if (parent_comm_id != undefined) { | ||||
var parent_comm = this.comm_manager.comms[parent_comm_id]; | ||||
var parent_model = parent_comm.model; | ||||
Jonathan Frederic
|
r14317 | var parent_views = parent_model.views[output_area]; | ||
Jonathan Frederic
|
r14285 | for (var parent_view_index in parent_views) { | ||
var parent_view = parent_views[parent_view_index]; | ||||
if (parent_view.display_child != undefined) { | ||||
Jonathan Frederic
|
r14317 | var view = this._create_view(view_name, output_area); | ||
Jonathan Frederic
|
r14285 | new_views.push(view); | ||
parent_view.display_child(view); | ||||
displayed = true; | ||||
} | ||||
} | ||||
} | ||||
if (!displayed) { | ||||
// No parent view is defined or exists. Add the view's | ||||
// element to cell's widget div. | ||||
Jonathan Frederic
|
r14317 | var view = this._create_view(view_name, output_area); | ||
Jonathan Frederic
|
r14285 | new_views.push(view); | ||
Jonathan Frederic
|
r14317 | output_area.element.find('.widget-area').find('.widget-subarea') | ||
Jonathan Frederic
|
r14285 | .append(view.$el) | ||
.parent().show(); // Show the widget_area (parent of widget_subarea) | ||||
} | ||||
for (var view_index in new_views) { | ||||
var view = new_views[view_index]; | ||||
view.update(); | ||||
} | ||||
}, | ||||
// Create a view | ||||
Jonathan Frederic
|
r14317 | _create_view: function (view_name, output_area) { | ||
Jonathan Frederic
|
r14278 | var view = new this.widget_view_types[view_name]({model: this}); | ||
view.render(); | ||||
Jonathan Frederic
|
r14317 | if (this.views[output_area]==undefined) { | ||
this.views[output_area] = [] | ||||
Jonathan Frederic
|
r14281 | } | ||
Jonathan Frederic
|
r14317 | this.views[output_area].push(view); | ||
view.output_area = output_area; | ||||
Jonathan Frederic
|
r14278 | |||
// Handle when the view element is remove from the page. | ||||
var that = this; | ||||
view.$el.on("remove", function(){ | ||||
Jonathan Frederic
|
r14317 | var index = that.views[output_area].indexOf(view); | ||
Jonathan Frederic
|
r14278 | if (index > -1) { | ||
Jonathan Frederic
|
r14317 | that.views[output_area].splice(index, 1); | ||
Jonathan Frederic
|
r14278 | } | ||
view.remove(); // Clean-up view | ||||
Jonathan Frederic
|
r14317 | if (that.views[output_area].length()==0) { | ||
delete that.views[output_area]; | ||||
Jonathan Frederic
|
r14281 | } | ||
Jonathan Frederic
|
r14278 | |||
// Close the comm if there are no views left. | ||||
if (that.views.length()==0) { | ||||
that.comm.close(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14285 | return view; | ||
Jonathan Frederic
|
r14279 | }, | ||
Jonathan Frederic
|
r14278 | |||
// Build a callback dict. | ||||
_make_callbacks: function (output_area) { | ||||
var callbacks = {}; | ||||
if (output_area != null) { | ||||
var that = this; | ||||
callbacks = { | ||||
iopub : { | ||||
output : $.proxy(output_area.handle_output, output_area), | ||||
clear_output : $.proxy(output_area.handle_clear_output, output_area), | ||||
status : function(msg){ | ||||
that.handle_status(output_area, msg); | ||||
}, | ||||
Jonathan Frederic
|
r14317 | get_output_area : function() { | ||
Jonathan Frederic
|
r14316 | if (that.last_modified_view != undefined && | ||
Jonathan Frederic
|
r14317 | that.last_modified_view.output_area != undefined) { | ||
return that.last_modified_view.output_area; | ||||
Jonathan Frederic
|
r14316 | } else { | ||
Jonathan Frederic
|
r14317 | return null | ||
Jonathan Frederic
|
r14316 | } | ||
}, | ||||
Jonathan Frederic
|
r14278 | }, | ||
}; | ||||
} | ||||
return callbacks; | ||||
}, | ||||
Jonathan Frederic
|
r14282 | // Get the cell index corresponding to the msg_id. | ||
Jonathan Frederic
|
r14317 | // output_area is a JQuery DOM element handle that has widget_area | ||
// and nested widget_subarea elements. | ||||
_get_output_area: function (msg_id) { | ||||
Jonathan Frederic
|
r14316 | |||
// First, guess cell.execute triggered | ||||
Jonathan Frederic
|
r14282 | var cells = IPython.notebook.get_cells(); | ||
Jonathan Frederic
|
r14285 | for (var cell_index in cells) { | ||
Jonathan Frederic
|
r14282 | if (cells[cell_index].last_msg_id == msg_id) { | ||
Jonathan Frederic
|
r14317 | var cell = IPython.notebook.get_cell(cell_index) | ||
return cell.output_area; | ||||
Jonathan Frederic
|
r14282 | } | ||
} | ||||
Jonathan Frederic
|
r14316 | |||
// Second, guess widget triggered | ||||
var callbacks = this.comm_manager.kernel.get_callbacks_for_msg(msg_id) | ||||
Jonathan Frederic
|
r14317 | if (callbacks != undefined && callbacks.iopub != undefined && callbacks.iopub.get_output_area != undefined) { | ||
var output_area = callbacks.iopub.get_output_area(); | ||||
if (output_area != null) { | ||||
return output_area; | ||||
Jonathan Frederic
|
r14316 | } | ||
} | ||||
// Not triggered by a widget or a cell | ||||
Jonathan Frederic
|
r14317 | return null; | ||
Jonathan Frederic
|
r14282 | }, | ||
Jonathan Frederic
|
r14263 | }); | ||
//-------------------------------------------------------------------- | ||||
// WidgetView class | ||||
//-------------------------------------------------------------------- | ||||
var WidgetView = Backbone.View.extend({ | ||||
Jonathan Frederic
|
r14255 | |||
Jonathan Frederic
|
r14263 | initialize: function() { | ||
Jonathan Frederic
|
r14311 | this.visible = true; | ||
Jonathan Frederic
|
r14279 | this.model.on('change',this.update,this); | ||
Jonathan Frederic
|
r14313 | this._add_class_calls = this.model.get('_add_class')[0]; | ||
this._remove_class_calls = this.model.get('_remove_class')[0]; | ||||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14278 | update: function() { | ||
Jonathan Frederic
|
r14311 | if (this.model.get('visible') != undefined) { | ||
if (this.visible != this.model.get('visible')) { | ||||
this.visible = this.model.get('visible'); | ||||
if (this.visible) { | ||||
this.$el.show(); | ||||
} else { | ||||
this.$el.hide(); | ||||
} | ||||
} | ||||
} | ||||
Jonathan Frederic
|
r14313 | |||
Jonathan Frederic
|
r14263 | if (this.model.css != undefined) { | ||
for (var selector in this.model.css) { | ||||
if (this.model.css.hasOwnProperty(selector)) { | ||||
// Apply the css traits to all elements that match the selector. | ||||
Jonathan Frederic
|
r14313 | var elements = this.get_selector_element(selector); | ||
if (elements.length > 0) { | ||||
Jonathan Frederic
|
r14263 | var css_traits = this.model.css[selector]; | ||
for (var css_key in css_traits) { | ||||
if (css_traits.hasOwnProperty(css_key)) { | ||||
elements.css(css_key, css_traits[css_key]); | ||||
} | ||||
Jonathan Frederic
|
r14224 | } | ||
} | ||||
} | ||||
} | ||||
} | ||||
Jonathan Frederic
|
r14313 | |||
var add_class = this.model.get('_add_class'); | ||||
if (add_class != undefined){ | ||||
var add_class_calls = add_class[0]; | ||||
if (add_class_calls > this._add_class_calls) { | ||||
this._add_class_calls = add_class_calls; | ||||
var elements = this.get_selector_element(add_class[1]); | ||||
if (elements.length > 0) { | ||||
elements.addClass(add_class[2]); | ||||
} | ||||
} | ||||
} | ||||
var remove_class = this.model.get('_remove_class'); | ||||
if (remove_class != undefined){ | ||||
var remove_class_calls = remove_class[0]; | ||||
if (remove_class_calls > this._remove_class_calls) { | ||||
this._remove_class_calls = remove_class_calls; | ||||
var elements = this.get_selector_element(remove_class[1]); | ||||
if (elements.length > 0) { | ||||
elements.removeClass(remove_class[2]); | ||||
} | ||||
} | ||||
} | ||||
}, | ||||
get_selector_element: function(selector) { | ||||
// Get the elements via the css selector. If the selector is | ||||
// blank, apply the style to the $el_to_style element. If | ||||
// the $el_to_style element is not defined, use apply the | ||||
// style to the view's element. | ||||
var elements = this.$el.find(selector); | ||||
if (selector=='') { | ||||
if (this.$el_to_style == undefined) { | ||||
elements = this.$el; | ||||
} else { | ||||
elements = this.$el_to_style; | ||||
} | ||||
} | ||||
return elements; | ||||
Jonathan Frederic
|
r14263 | }, | ||
}); | ||||
//-------------------------------------------------------------------- | ||||
// WidgetManager class | ||||
//-------------------------------------------------------------------- | ||||
var WidgetManager = function(comm_manager){ | ||||
this.comm_manager = comm_manager; | ||||
this.widget_model_types = {}; | ||||
this.widget_view_types = {}; | ||||
var that = this; | ||||
Backbone.sync = function(method, model, options, error) { | ||||
Jonathan Frederic
|
r14278 | var result = model.handle_sync(method, options); | ||
Jonathan Frederic
|
r14263 | if (options.success) { | ||
options.success(result); | ||||
} | ||||
}; | ||||
Jonathan Frederic
|
r14257 | } | ||
Jonathan Frederic
|
r14237 | |||
Jonathan Frederic
|
r14278 | |||
Jonathan Frederic
|
r14263 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | ||
// Register the widget with the comm manager. Make sure to pass this object's context | ||||
// in so `this` works in the call back. | ||||
this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); | ||||
this.widget_model_types[widget_model_name] = widget_model_type; | ||||
Jonathan Frederic
|
r14257 | } | ||
Jonathan Frederic
|
r14263 | |||
Jonathan Frederic
|
r14278 | |||
Jonathan Frederic
|
r14263 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | ||
this.widget_view_types[widget_view_name] = widget_view_type; | ||||
} | ||||
Jonathan Frederic
|
r14278 | |||
Jonathan Frederic
|
r14263 | WidgetManager.prototype.handle_com_open = function (comm, msg) { | ||
var widget_type_name = msg.content.target_name; | ||||
Jonathan Frederic
|
r14279 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this.widget_view_types); | ||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14255 | |||
Jonathan Frederic
|
r14224 | |||
Jonathan Frederic
|
r14278 | //-------------------------------------------------------------------- | ||
// Init code | ||||
//-------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r14263 | IPython.WidgetManager = WidgetManager; | ||
IPython.WidgetModel = WidgetModel; | ||||
IPython.WidgetView = WidgetView; | ||||
Jonathan Frederic
|
r14224 | |||
Jonathan Frederic
|
r14263 | IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager); | ||
Jonathan Frederic
|
r14224 | |||
Jonathan Frederic
|
r14263 | }; | ||
}); | ||||