##// END OF EJS Templates
comment model.set, so we know that it triggers update on other views
comment model.set, so we know that it triggers update on other views

File last commit:

r14566:592da7db
r14569:500d8a37
Show More
widgetmanager.js
269 lines | 10.9 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Added widjet.js...
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
jshint widget.js
r14457 (function () {
"use strict";
// 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
Added require.js shims for underscore and backbone...
r14483 define(["underscore",
"backbone",
Jonathan Frederic
Fixed some spacing in widget.js
r14468 ], function (underscore, backbone) {
Jonathan Frederic
move backbone sync outside the widget manager class
r14554
Jonathan Frederic
Added note in widget manager why Backbone.sync is there rather...
r14557 // Backbone.sync method must be in widgetmanager.js file instead of
// widget.js so it can be overwritten for different contexts.
Jonathan Frederic
move backbone sync outside the widget manager class
r14554 Backbone.sync = function (method, model, options, error) {
var result = model._handle_sync(method, options);
if (options.success) {
Jonathan Frederic
Added note in widget manager why Backbone.sync is there rather...
r14557 options.success(result);
Jonathan Frederic
move backbone sync outside the widget manager class
r14554 }
};
Jonathan Frederic
jshint widget.js
r14457
//--------------------------------------------------------------------
// WidgetManager class
//--------------------------------------------------------------------
Jonathan Frederic
Fixed some spacing in widget.js
r14468 var WidgetManager = function () {
Jonathan Frederic
jshint widget.js
r14457 this.comm_manager = null;
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 this._model_types = {}; /* Dictionary of model type names
(target_name) and model types. */
this._view_types = {}; /* Dictionary of view names and view types. */
this._models = {}; /* Dictionary of model ids and model instances */
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
jshint widget.js
r14457 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
this.comm_manager = comm_manager;
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 // Register already-registered widget model types with the comm manager.
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 for (var widget_model_name in this._model_types) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 }
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
jshint widget.js
r14457 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.
if (this.comm_manager !== null) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
Jonathan Frederic
jshint widget.js
r14457 }
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 this._model_types[widget_model_name] = widget_model_type;
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342
Jonathan Frederic
Handle widget hide/show logic...
r14237
Jonathan Frederic
jshint widget.js
r14457 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 this._view_types[widget_view_name] = widget_view_type;
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512
Jonathan Frederic
handle_msg a display_model method.
r14559 WidgetManager.prototype.display_view = function(msg_id, model) {
var cell = this.get_msg_cell(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);
if (view !== undefined
&& cell.widget_subarea !== undefined
&& cell.widget_subarea !== null) {
view.cell = cell;
cell.widget_area.show();
cell.widget_subarea.append(view.$el);
}
Jonathan Frederic
Converted tabs to spaces
r14506 }
Jonathan Frederic
handle_msg a display_model method.
r14559 },
Jonathan Frederic
Converted tabs to spaces
r14506
Jason Grout
Add widget view options in creating child views
r14528 <<<<<<< HEAD
Jonathan Frederic
Completely remove cell from model and view.
r14534 <<<<<<< HEAD
Jonathan Frederic
remove msg.content.data.view_name and corrosponding create_view param
r14555 <<<<<<< HEAD
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 WidgetManager.prototype.create_view = function(model, view_name, cell) {
Jonathan Frederic
Completely remove cell from model and view.
r14534 =======
WidgetManager.prototype.create_view = function(model, view_name, options) {
>>>>>>> Completely remove cell from model and view.
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 view_name = view_name || model.get('default_view_name');
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 <<<<<<< HEAD
Jason Grout
Add widget view options in creating child views
r14528 =======
WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
view_name = view_name || model.get('default_view_name');
>>>>>>> Add widget view options in creating child views
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 var ViewType = this.widget_view_types[view_name];
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 =======
Jonathan Frederic
remove msg.content.data.view_name and corrosponding create_view param
r14555 =======
WidgetManager.prototype.create_view = function(model, options) {
var view_name = model.get('view_name');
>>>>>>> remove msg.content.data.view_name and corrosponding create_view param
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 var ViewType = this._view_types[view_name];
>>>>>>> _model_types, _view_types, _models - and document what keys and values are
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 if (ViewType !== undefined && ViewType !== null) {
Jonathan Frederic
un-nest options.options
r14565 var parameters = {model: model, options: options};
var view = new ViewType(parameters);
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 view.render();
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 model.views.push(view);
model.on('destroy', view.remove, view);
Jason Grout
Add widget view options in creating child views
r14528 <<<<<<< HEAD
Jonathan Frederic
remove msg.content.data.view_name and corrosponding create_view param
r14555 <<<<<<< HEAD
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 /*
// TODO: handle view deletion. Don't forget to delete child views
var that = this;
view.$el.on("remove", function () {
var index = that.views.indexOf(view);
if (index > -1) {
that.views.splice(index, 1);
Jason Grout
Add widget view options in creating child views
r14528 =======
/*
// TODO: handle view deletion. Don't forget to delete child views
var that = this;
view.$el.on("remove", function () {
var index = that.views.indexOf(view);
if (index > -1) {
that.views.splice(index, 1);
}
view.remove(); // Clean-up view
// Close the comm if there are no views left.
if (that.views.length() === 0) {
//trigger comm close event?
}
if (that.comm !== undefined) {
that.comm.close();
delete that.comm.model; // Delete ref so GC will collect widget model.
delete that.comm;
>>>>>>> Add widget view options in creating child views
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 }
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 view.remove(); // Clean-up view
// Close the comm if there are no views left.
if (that.views.length() === 0) {
//trigger comm close event?
}
if (that.comm !== undefined) {
that.comm.close();
delete that.comm.model; // Delete ref so GC will collect widget model.
delete that.comm;
}
delete that.model_id; // Delete id from model so widget manager cleans up.
});
*/
Jonathan Frederic
remove msg.content.data.view_name and corrosponding create_view param
r14555 =======
>>>>>>> remove msg.content.data.view_name and corrosponding create_view param
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 return view;
}
},
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
Jonathan Frederic
handle_msg a display_model method.
r14559
Jonathan Frederic
jshint widget.js
r14457 WidgetManager.prototype.get_msg_cell = function (msg_id) {
Jonathan Frederic
Completely remove cell from model and view.
r14534 var cell = null;
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 // First, check to see if the msg was triggered by cell execution.
Jonathan Frederic
jshint widget.js
r14457 if (IPython.notebook !== undefined && IPython.notebook !== null) {
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 cell = IPython.notebook.get_msg_cell(msg_id);
}
Jonathan Frederic
Completely remove cell from model and view.
r14534 if (cell !== null) {
return cell
}
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 // Second, check to see if a get_cell callback was defined
// for the message. get_cell callbacks are registered for
// widget messages, so this block is actually checking to see if the
// message was triggered by a widget.
Jonathan Frederic
Removed get_kernel method.
r14566 var kernel = null;
if (this.comm_manager !== null) {
kernel = this.comm_manager.kernel;
}
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 if (kernel !== undefined && kernel !== null) {
var callbacks = kernel.get_callbacks_for_msg(msg_id);
if (callbacks !== undefined &&
callbacks.iopub !== undefined &&
callbacks.iopub.get_cell !== undefined) {
return callbacks.iopub.get_cell();
}
Jonathan Frederic
jshint widget.js
r14457 }
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486
// Not triggered by a cell or widget (no get_cell callback
// exists).
return null;
Jonathan Frederic
jshint widget.js
r14457 };
Jonathan Frederic
Removed comm dependency of widget model and view
r14469
Jonathan Frederic
Completely remove cell from model and view.
r14534 WidgetManager.prototype.callbacks = function (view) {
// callback handlers specific a view
var callbacks = {};
var cell = view.cell;
if (cell !== null) {
// Try to get output handlers
var handle_output = null;
var handle_clear_output = null;
if (cell.output_area !== undefined && cell.output_area !== null) {
handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
}
// Create callback dict using what is known
var that = this;
callbacks = {
iopub : {
output : handle_output,
clear_output : handle_clear_output,
status : function (msg) {
Jonathan Frederic
Missing view argument when recursively calling widgetmanager.callbacks(view)
r14535 view.model._handle_status(msg, that.callbacks(view));
Jonathan Frederic
Completely remove cell from model and view.
r14534 },
// Special function only registered by widget messages.
// Allows us to get the cell for a message so we know
// where to add widgets if the code requires it.
get_cell : function () {
return cell;
},
},
};
}
return callbacks;
};
Jonathan Frederic
Removed comm dependency of widget model and view
r14469
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 WidgetManager.prototype.get_model = function (model_id) {
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 var model = this._models[model_id];
Jonathan Frederic
Re-decoupled comm_id from widget models
r14512 if (model !== undefined && model.id == model_id) {
Jonathan Frederic
Removed comm dependency of widget model and view
r14469 return model;
}
return null;
};
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
Jonathan Frederic
jshint widget.js
r14457 var widget_type_name = msg.content.target_name;
Jonathan Frederic
_model_types, _view_types, _models - and document what keys and values are
r14553 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
this._models[comm.comm_id] = widget_model; // comm_id == model_id
Jonathan Frederic
jshint widget.js
r14457 };
//--------------------------------------------------------------------
// Init code
//--------------------------------------------------------------------
IPython.WidgetManager = WidgetManager;
if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
IPython.widget_manager = new WidgetManager();
}
Jonathan Frederic
Remove some empty space
r14390
Jonathan Frederic
jshint widget.js
r14457 return IPython.widget_manager;
});
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 }());