(function () {
"use strict";
define(["underscore",
"backbone",
], function (_, Backbone) {
var WidgetManager = function (comm_manager) {
WidgetManager._managers.push(this);
this.comm_manager = comm_manager;
this._models = {};
var that = this;
_.each(WidgetManager._model_types, function(model_type, model_name) {
that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
});
};
WidgetManager._model_types = {};
WidgetManager._view_types = {};
WidgetManager._managers = [];
WidgetManager.register_widget_model = function (model_name, model_type) {
WidgetManager._model_types[model_name] = model_type;
_.each(WidgetManager._managers, function(instance, i) {
if (instance.comm_manager !== null) {
instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
}
});
};
WidgetManager.register_widget_view = function (view_name, view_type) {
WidgetManager._view_types[view_name] = view_type;
};
WidgetManager.prototype.display_view = function(msg, model) {
var cell = this.get_msg_cell(msg.parent_header.msg_id);
if (cell === null) {
console.log("Could not determine where the display" +
" message was from. Widget will not be displayed");
} else {
var view = this.create_view(model, {cell: cell});
if (view === null) {
console.error("View creation failed", model);
}
if (cell.widget_subarea) {
cell.widget_area.show();
this._handle_display_view(view);
cell.widget_subarea.append(view.$el);
}
}
};
WidgetManager.prototype._handle_display_view = function (view) {
IPython.keyboard_manager.register_events(view.$el);
if (view.additional_elements) {
for (var i = 0; i < view.additional_elements.length; i++) {
IPython.keyboard_manager.register_events(view.additional_elements[i]);
}
}
};
WidgetManager.prototype.create_view = function(model, options, view) {
var view_name = model.get('_view_name');
var ViewType = WidgetManager._view_types[view_name];
if (ViewType) {
options = options || {};
if (view !== undefined) {
options.cell = view.options.cell;
}
var parameters = {model: model, options: options};
view = new ViewType(parameters);
view.render();
model.views.push(view);
model.on('destroy', view.remove, view);
return view;
}
return null;
};
WidgetManager.prototype.get_msg_cell = function (msg_id) {
var cell = null;
if (IPython.notebook) {
cell = IPython.notebook.get_msg_cell(msg_id);
}
if (cell !== null) {
return cell;
}
var kernel = this.comm_manager.kernel;
if (kernel) {
var callbacks = kernel.get_callbacks_for_msg(msg_id);
if (callbacks && callbacks.iopub &&
callbacks.iopub.get_cell !== undefined) {
return callbacks.iopub.get_cell();
}
}
return null;
};
WidgetManager.prototype.callbacks = function (view) {
var callbacks = {};
if (view && view.options.cell) {
var cell = view.options.cell;
var handle_output = null;
var handle_clear_output = null;
if (cell.output_area) {
handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
}
var that = this;
callbacks = {
iopub : {
output : handle_output,
clear_output : handle_clear_output,
get_cell : function () {
return cell;
},
},
};
}
return callbacks;
};
WidgetManager.prototype.get_model = function (model_id) {
var model = this._models[model_id];
if (model !== undefined && model.id == model_id) {
return model;
}
return null;
};
WidgetManager.prototype._handle_comm_open = function (comm, msg) {
var model_id = comm.comm_id;
var widget_type_name = msg.content.target_name;
var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
this._models[model_id] = widget_model;
};
IPython.WidgetManager = WidgetManager;
return IPython.WidgetManager;
});
}());