mirror of
https://github.com/jupyter/notebook.git
synced 2024-12-21 04:10:17 +08:00
Lots of updates to widget(s) js
Use require.js (again) Allow IntTextView and FloatTextView to be used without min/max
This commit is contained in:
parent
f7d96da585
commit
40a923c5b6
@ -17,237 +17,245 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
// Only run once on a notebook.
|
||||
if (IPython.notebook.widget_manager == undefined) {
|
||||
// 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.
|
||||
define(["static/components/underscore/underscore-min.js",
|
||||
"static/components/backbone/backbone-min.js",
|
||||
], function(){
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// WidgetModel class
|
||||
//-----------------------------------------------------------------------
|
||||
var WidgetModel = Backbone.Model.extend({
|
||||
apply: function(sender) {
|
||||
this.save();
|
||||
// Only run once on a notebook.
|
||||
if (IPython.notebook.widget_manager == undefined) {
|
||||
|
||||
for (var index in this.views) {
|
||||
var view = this.views[index];
|
||||
if (view !== sender) {
|
||||
view.refresh();
|
||||
//--------------------------------------------------------------------
|
||||
// WidgetModel class
|
||||
//--------------------------------------------------------------------
|
||||
var WidgetModel = Backbone.Model.extend({
|
||||
apply: function(sender) {
|
||||
this.save();
|
||||
|
||||
for (var index in this.views) {
|
||||
var view = this.views[index];
|
||||
if (view !== sender) {
|
||||
view.refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// WidgetView class
|
||||
//-----------------------------------------------------------------------
|
||||
var WidgetView = Backbone.View.extend({
|
||||
|
||||
initialize: function() {
|
||||
this.model.on('change',this.refresh,this);
|
||||
},
|
||||
|
||||
refresh: function() {
|
||||
this.update();
|
||||
//--------------------------------------------------------------------
|
||||
// WidgetView class
|
||||
//--------------------------------------------------------------------
|
||||
var WidgetView = Backbone.View.extend({
|
||||
|
||||
if (this.model.css != undefined) {
|
||||
for (var selector in this.model.css) {
|
||||
if (this.model.css.hasOwnProperty(selector)) {
|
||||
|
||||
// Get the elements via the css selector. If the selector is
|
||||
// blank, assume the current element is the target.
|
||||
var elements = this.$el.find(selector);
|
||||
if (selector=='') {
|
||||
elements = this.$el;
|
||||
}
|
||||
|
||||
// Apply the css traits to all elements that match the selector.
|
||||
if (elements.length>0){
|
||||
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]);
|
||||
initialize: function() {
|
||||
this.model.on('change',this.refresh,this);
|
||||
},
|
||||
|
||||
refresh: function() {
|
||||
this.update();
|
||||
|
||||
if (this.model.css != undefined) {
|
||||
for (var selector in this.model.css) {
|
||||
if (this.model.css.hasOwnProperty(selector)) {
|
||||
|
||||
// Get the elements via the css selector. If the selector is
|
||||
// blank, assume the current element is the target.
|
||||
var elements = this.$el.find(selector);
|
||||
if (selector=='') {
|
||||
elements = this.$el;
|
||||
}
|
||||
|
||||
// Apply the css traits to all elements that match the selector.
|
||||
if (elements.length>0){
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// WidgetManager class
|
||||
//-----------------------------------------------------------------------
|
||||
// Public constructor
|
||||
var WidgetManager = function(comm_manager){
|
||||
this.comm_manager = comm_manager;
|
||||
this.widget_model_types = {};
|
||||
this.widget_view_types = {};
|
||||
this.model_widget_views = {};
|
||||
|
||||
var that = this;
|
||||
Backbone.sync = function(method, model, options, error) {
|
||||
var result = that.send_sync(method, model);
|
||||
if (options.success) {
|
||||
options.success(result);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Register a widget model type.
|
||||
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));
|
||||
|
||||
// Register the types of the model and view correspong to this widget type. Later
|
||||
// the widget manager will initialize these when the comm is opened.
|
||||
this.widget_model_types[widget_model_name] = widget_model_type;
|
||||
}
|
||||
|
||||
// Register a widget view type.
|
||||
WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
|
||||
this.widget_view_types[widget_view_name] = widget_view_type;
|
||||
}
|
||||
|
||||
// Handle when a comm is opened.
|
||||
WidgetManager.prototype.handle_com_open = function (comm, msg) {
|
||||
var widget_type_name = msg.content.target_name;
|
||||
|
||||
// Create the corresponding widget model.
|
||||
var widget_model = new this.widget_model_types[widget_type_name];
|
||||
|
||||
// Remember comm associated with the model.
|
||||
widget_model.comm = comm;
|
||||
comm.model = widget_model;
|
||||
|
||||
// Create an array to remember the views associated with the model.
|
||||
widget_model.views = [];
|
||||
|
||||
// Add a handle to delete the control when the comm is closed.
|
||||
var that = this;
|
||||
var handle_close = function(msg) {
|
||||
that.handle_comm_closed(comm, msg);
|
||||
}
|
||||
comm.on_close(handle_close);
|
||||
|
||||
// Handle incomming messages.
|
||||
var handle_msg = function(msg) {
|
||||
that.handle_comm_msg(comm, msg);
|
||||
}
|
||||
comm.on_msg(handle_msg);
|
||||
}
|
||||
|
||||
// Create view that represents the model.
|
||||
WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) {
|
||||
var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model});
|
||||
widget_view.render();
|
||||
widget_model.views.push(widget_view);
|
||||
|
||||
// Handle when the view element is remove from the page.
|
||||
widget_view.$el.on("remove", function(){
|
||||
var index = widget_model.views.indexOf(widget_view);
|
||||
if (index > -1) {
|
||||
widget_model.views.splice(index, 1);
|
||||
}
|
||||
widget_view.remove(); // Clean-up view
|
||||
|
||||
// Close the comm if there are no views left.
|
||||
if (widget_model.views.length()==0) {
|
||||
widget_model.comm.close();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Add the view's element to cell's widget div.
|
||||
widget_area
|
||||
.append($("<div />").append(widget_view.$el))
|
||||
.parent().show(); // Show the widget_area (parent of widget_subarea)
|
||||
|
||||
// Update the view based on the model contents.
|
||||
widget_view.refresh();
|
||||
}
|
||||
|
||||
// Handle incomming comm msg.
|
||||
WidgetManager.prototype.handle_comm_msg = function (comm, msg) {
|
||||
// Different logic for different methods.
|
||||
var method = msg.content.data.method;
|
||||
switch (method){
|
||||
case 'show':
|
||||
|
||||
// TODO: Get cell from registered output handler.
|
||||
var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
|
||||
var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea');
|
||||
|
||||
if (msg.content.data.parent != undefined) {
|
||||
var find_results = widget_subarea.find("." + msg.content.data.parent);
|
||||
if (find_results.length > 0) {
|
||||
widget_subarea = find_results;
|
||||
}
|
||||
//--------------------------------------------------------------------
|
||||
// WidgetManager class
|
||||
//--------------------------------------------------------------------
|
||||
// Public constructor
|
||||
var WidgetManager = function(comm_manager){
|
||||
this.comm_manager = comm_manager;
|
||||
this.widget_model_types = {};
|
||||
this.widget_view_types = {};
|
||||
this.model_widget_views = {};
|
||||
|
||||
var that = this;
|
||||
Backbone.sync = function(method, model, options, error) {
|
||||
var result = that.send_sync(method, model);
|
||||
if (options.success) {
|
||||
options.success(result);
|
||||
}
|
||||
|
||||
this.show_view(widget_subarea, comm.model, msg.content.data.view_name);
|
||||
break;
|
||||
case 'update':
|
||||
this.handle_update(comm, msg.content.data.state);
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Handle when a widget is updated via the python side.
|
||||
WidgetManager.prototype.handle_update = function (comm, state) {
|
||||
for (var key in state) {
|
||||
if (state.hasOwnProperty(key)) {
|
||||
if (key=="_css"){
|
||||
comm.model.css = state[key];
|
||||
} else {
|
||||
comm.model.set(key, state[key]);
|
||||
// Register a widget model type.
|
||||
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));
|
||||
|
||||
// Register the types of the model and view correspong to this widget type. Later
|
||||
// the widget manager will initialize these when the comm is opened.
|
||||
this.widget_model_types[widget_model_name] = widget_model_type;
|
||||
}
|
||||
|
||||
// Register a widget view type.
|
||||
WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
|
||||
this.widget_view_types[widget_view_name] = widget_view_type;
|
||||
}
|
||||
|
||||
// Handle when a comm is opened.
|
||||
WidgetManager.prototype.handle_com_open = function (comm, msg) {
|
||||
var widget_type_name = msg.content.target_name;
|
||||
|
||||
// Create the corresponding widget model.
|
||||
var widget_model = new this.widget_model_types[widget_type_name];
|
||||
|
||||
// Remember comm associated with the model.
|
||||
widget_model.comm = comm;
|
||||
comm.model = widget_model;
|
||||
|
||||
// Create an array to remember the views associated with the model.
|
||||
widget_model.views = [];
|
||||
|
||||
// Add a handle to delete the control when the comm is closed.
|
||||
var that = this;
|
||||
var handle_close = function(msg) {
|
||||
that.handle_comm_closed(comm, msg);
|
||||
}
|
||||
comm.on_close(handle_close);
|
||||
|
||||
// Handle incomming messages.
|
||||
var handle_msg = function(msg) {
|
||||
that.handle_comm_msg(comm, msg);
|
||||
}
|
||||
comm.on_msg(handle_msg);
|
||||
}
|
||||
|
||||
// Create view that represents the model.
|
||||
WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) {
|
||||
var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model});
|
||||
widget_view.render();
|
||||
widget_model.views.push(widget_view);
|
||||
|
||||
// Handle when the view element is remove from the page.
|
||||
widget_view.$el.on("remove", function(){
|
||||
var index = widget_model.views.indexOf(widget_view);
|
||||
if (index > -1) {
|
||||
widget_model.views.splice(index, 1);
|
||||
}
|
||||
widget_view.remove(); // Clean-up view
|
||||
|
||||
// Close the comm if there are no views left.
|
||||
if (widget_model.views.length()==0) {
|
||||
widget_model.comm.close();
|
||||
}
|
||||
});
|
||||
|
||||
// Add the view's element to cell's widget div.
|
||||
widget_area
|
||||
.append($("<div />").append(widget_view.$el))
|
||||
.parent().show(); // Show the widget_area (parent of widget_subarea)
|
||||
|
||||
// Update the view based on the model contents.
|
||||
widget_view.refresh();
|
||||
}
|
||||
|
||||
// Handle incomming comm msg.
|
||||
WidgetManager.prototype.handle_comm_msg = function (comm, msg) {
|
||||
// Different logic for different methods.
|
||||
var method = msg.content.data.method;
|
||||
switch (method){
|
||||
case 'show':
|
||||
|
||||
// TODO: Get cell from registered output handler.
|
||||
var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
|
||||
var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea');
|
||||
|
||||
if (msg.content.data.parent != undefined) {
|
||||
var find_results = widget_subarea.find("." + msg.content.data.parent);
|
||||
if (find_results.length > 0) {
|
||||
widget_subarea = find_results;
|
||||
}
|
||||
}
|
||||
|
||||
this.show_view(widget_subarea, comm.model, msg.content.data.view_name);
|
||||
break;
|
||||
case 'update':
|
||||
this.handle_update(comm, msg.content.data.state);
|
||||
break;
|
||||
}
|
||||
}
|
||||
comm.model.save();
|
||||
}
|
||||
|
||||
// Handle when a widget is closed.
|
||||
WidgetManager.prototype.handle_comm_closed = function (comm, msg) {
|
||||
for (var view_index in comm.model.views) {
|
||||
var view = comm.model.views[view_index];
|
||||
view.remove();
|
||||
// Handle when a widget is updated via the python side.
|
||||
WidgetManager.prototype.handle_update = function (comm, state) {
|
||||
for (var key in state) {
|
||||
if (state.hasOwnProperty(key)) {
|
||||
if (key=="_css"){
|
||||
comm.model.css = state[key];
|
||||
} else {
|
||||
comm.model.set(key, state[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
comm.model.save();
|
||||
}
|
||||
}
|
||||
|
||||
// Get the cell output area corresponding to the comm.
|
||||
WidgetManager.prototype._get_comm_outputarea = function (comm) {
|
||||
// TODO: get element from comm instead of guessing
|
||||
var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index())
|
||||
return cell.output_area;
|
||||
}
|
||||
// Handle when a widget is closed.
|
||||
WidgetManager.prototype.handle_comm_closed = function (comm, msg) {
|
||||
for (var view_index in comm.model.views) {
|
||||
var view = comm.model.views[view_index];
|
||||
view.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// Send widget state to python backend.
|
||||
WidgetManager.prototype.send_sync = function (method, model) {
|
||||
|
||||
// Create a callback for the output if the widget has an output area associate with it.
|
||||
var callbacks = {};
|
||||
var comm = model.comm;
|
||||
var outputarea = this._get_comm_outputarea(comm);
|
||||
if (outputarea != null) {
|
||||
callbacks = {
|
||||
iopub : {
|
||||
output : $.proxy(outputarea.handle_output, outputarea),
|
||||
clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
|
||||
// Get the cell output area corresponding to the comm.
|
||||
WidgetManager.prototype._get_comm_outputarea = function (comm) {
|
||||
// TODO: get element from comm instead of guessing
|
||||
var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index())
|
||||
return cell.output_area;
|
||||
}
|
||||
|
||||
// Send widget state to python backend.
|
||||
WidgetManager.prototype.send_sync = function (method, model) {
|
||||
|
||||
// Create a callback for the output if the widget has an output area associate with it.
|
||||
var callbacks = {};
|
||||
var comm = model.comm;
|
||||
var outputarea = this._get_comm_outputarea(comm);
|
||||
if (outputarea != null) {
|
||||
callbacks = {
|
||||
iopub : {
|
||||
output : $.proxy(outputarea.handle_output, outputarea),
|
||||
clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
|
||||
};
|
||||
};
|
||||
};
|
||||
var model_json = model.toJSON();
|
||||
var data = {sync_method: method, sync_data: model_json};
|
||||
comm.send(data, callbacks);
|
||||
return model_json;
|
||||
}
|
||||
var model_json = model.toJSON();
|
||||
var data = {sync_method: method, sync_data: model_json};
|
||||
comm.send(data, callbacks);
|
||||
return model_json;
|
||||
}
|
||||
|
||||
IPython.WidgetManager = WidgetManager;
|
||||
IPython.WidgetModel = WidgetModel;
|
||||
IPython.WidgetView = WidgetView;
|
||||
IPython.WidgetManager = WidgetManager;
|
||||
IPython.WidgetModel = WidgetModel;
|
||||
IPython.WidgetView = WidgetView;
|
||||
|
||||
IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager);
|
||||
IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager);
|
||||
|
||||
};
|
||||
};
|
||||
});
|
||||
|
@ -1,17 +1,19 @@
|
||||
var ContainerModel = IPython.WidgetModel.extend({});
|
||||
IPython.notebook.widget_manager.register_widget_model('container_widget', ContainerModel);
|
||||
require(["notebook/js/widget"], function(){
|
||||
var ContainerModel = IPython.WidgetModel.extend({});
|
||||
IPython.notebook.widget_manager.register_widget_model('container_widget', ContainerModel);
|
||||
|
||||
var ContainerView = IPython.WidgetView.extend({
|
||||
|
||||
render : function(){
|
||||
this.$el.html('');
|
||||
this.$container = $('<div />')
|
||||
.addClass('container')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$el.append(this.$container);
|
||||
},
|
||||
|
||||
update : function(){},
|
||||
});
|
||||
var ContainerView = IPython.WidgetView.extend({
|
||||
|
||||
render : function(){
|
||||
this.$el.html('');
|
||||
this.$container = $('<div />')
|
||||
.addClass('container')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$el.append(this.$container);
|
||||
},
|
||||
|
||||
update : function(){},
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('ContainerView', ContainerView);
|
||||
IPython.notebook.widget_manager.register_widget_view('ContainerView', ContainerView);
|
||||
});
|
@ -1,194 +1,196 @@
|
||||
var SelectionWidgetModel = IPython.WidgetModel.extend({});
|
||||
IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
|
||||
require(["notebook/js/widget"], function(){
|
||||
var SelectionWidgetModel = IPython.WidgetModel.extend({});
|
||||
IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
|
||||
|
||||
var DropdownView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
var DropdownView = IPython.WidgetView.extend({
|
||||
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$buttongroup = $('<div />')
|
||||
.addClass('btn-group')
|
||||
.appendTo(this.$el);
|
||||
this.$droplabel = $('<button />')
|
||||
.addClass('btn')
|
||||
.appendTo(this.$buttongroup);
|
||||
this.$dropbutton = $('<button />')
|
||||
.addClass('btn')
|
||||
.addClass('dropdown-toggle')
|
||||
.attr('data-toggle', 'dropdown')
|
||||
.html('<span class="caret"></span>')
|
||||
.appendTo(this.$buttongroup);
|
||||
this.$droplist = $('<ul />')
|
||||
.addClass('dropdown-menu')
|
||||
.appendTo(this.$buttongroup);
|
||||
|
||||
// Set defaults.
|
||||
this.update();
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
this.$droplabel.html(this.model.get('value'));
|
||||
|
||||
var items = this.model.get('values');
|
||||
this.$droplist.html('');
|
||||
for (var index in items) {
|
||||
var that = this;
|
||||
var item_button = $('<a href="#"/>')
|
||||
.html(items[index])
|
||||
.on('click', function(e){
|
||||
that.model.set('value', $(e.target).html(), this );
|
||||
})
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
|
||||
this.$droplist.append($('<li />').append(item_button))
|
||||
}
|
||||
|
||||
if (this.model.get('disabled')) {
|
||||
this.$buttongroup.attr('disabled','disabled');
|
||||
this.$droplabel.attr('disabled','disabled');
|
||||
this.$dropbutton.attr('disabled','disabled');
|
||||
this.$droplist.attr('disabled','disabled');
|
||||
} else {
|
||||
this.$buttongroup.removeAttr('disabled');
|
||||
this.$droplabel.removeAttr('disabled');
|
||||
this.$dropbutton.removeAttr('disabled');
|
||||
this.$droplist.removeAttr('disabled');
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView);
|
||||
|
||||
var RadioButtonView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.update();
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
|
||||
// Add missing items to the DOM.
|
||||
var items = this.model.get('values');
|
||||
for (var index in items) {
|
||||
var item_query = ' :input[value="' + items[index] + '"]';
|
||||
if (this.$el.find(item_query).length == 0) {
|
||||
var $label = $('<label />')
|
||||
.addClass('radio')
|
||||
.html(items[index])
|
||||
.appendTo(this.$el);
|
||||
|
||||
var that = this;
|
||||
$('<input />')
|
||||
.attr('type', 'radio')
|
||||
.addClass(this.model)
|
||||
.val(items[index])
|
||||
.prependTo($label)
|
||||
.on('click', function(e){
|
||||
that.model.set('value', $(e.target).val(), this);
|
||||
that.model.apply();
|
||||
});
|
||||
}
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$buttongroup = $('<div />')
|
||||
.addClass('btn-group')
|
||||
.appendTo(this.$el);
|
||||
this.$droplabel = $('<button />')
|
||||
.addClass('btn')
|
||||
.appendTo(this.$buttongroup);
|
||||
this.$dropbutton = $('<button />')
|
||||
.addClass('btn')
|
||||
.addClass('dropdown-toggle')
|
||||
.attr('data-toggle', 'dropdown')
|
||||
.html('<span class="caret"></span>')
|
||||
.appendTo(this.$buttongroup);
|
||||
this.$droplist = $('<ul />')
|
||||
.addClass('dropdown-menu')
|
||||
.appendTo(this.$buttongroup);
|
||||
|
||||
if (this.model.get('value') == items[index]) {
|
||||
this.$el.find(item_query).prop('checked', true);
|
||||
} else {
|
||||
this.$el.find(item_query).prop('checked', false);
|
||||
}
|
||||
}
|
||||
// Set defaults.
|
||||
this.update();
|
||||
},
|
||||
|
||||
// Remove items that no longer exist.
|
||||
this.$el.find('input').each(function(i, obj) {
|
||||
var value = $(obj).val();
|
||||
var found = false;
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
this.$droplabel.html(this.model.get('value'));
|
||||
|
||||
var items = this.model.get('values');
|
||||
this.$droplist.html('');
|
||||
for (var index in items) {
|
||||
if (items[index] == value) {
|
||||
found = true;
|
||||
break;
|
||||
var that = this;
|
||||
var item_button = $('<a href="#"/>')
|
||||
.html(items[index])
|
||||
.on('click', function(e){
|
||||
that.model.set('value', $(e.target).html(), this );
|
||||
})
|
||||
|
||||
this.$droplist.append($('<li />').append(item_button))
|
||||
}
|
||||
|
||||
if (this.model.get('disabled')) {
|
||||
this.$buttongroup.attr('disabled','disabled');
|
||||
this.$droplabel.attr('disabled','disabled');
|
||||
this.$dropbutton.attr('disabled','disabled');
|
||||
this.$droplist.attr('disabled','disabled');
|
||||
} else {
|
||||
this.$buttongroup.removeAttr('disabled');
|
||||
this.$droplabel.removeAttr('disabled');
|
||||
this.$dropbutton.removeAttr('disabled');
|
||||
this.$droplist.removeAttr('disabled');
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView);
|
||||
|
||||
var RadioButtonsView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.update();
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
|
||||
// Add missing items to the DOM.
|
||||
var items = this.model.get('values');
|
||||
for (var index in items) {
|
||||
var item_query = ' :input[value="' + items[index] + '"]';
|
||||
if (this.$el.find(item_query).length == 0) {
|
||||
var $label = $('<label />')
|
||||
.addClass('radio')
|
||||
.html(items[index])
|
||||
.appendTo(this.$el);
|
||||
|
||||
var that = this;
|
||||
$('<input />')
|
||||
.attr('type', 'radio')
|
||||
.addClass(this.model)
|
||||
.val(items[index])
|
||||
.prependTo($label)
|
||||
.on('click', function(e){
|
||||
that.model.set('value', $(e.target).val(), this);
|
||||
that.model.apply();
|
||||
});
|
||||
}
|
||||
|
||||
if (this.model.get('value') == items[index]) {
|
||||
this.$el.find(item_query).prop('checked', true);
|
||||
} else {
|
||||
this.$el.find(item_query).prop('checked', false);
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
$(obj).parent().remove();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('RadioButtonView', RadioButtonView);
|
||||
|
||||
|
||||
var ToggleButtonView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$buttongroup = $('<div />')
|
||||
.addClass('btn-group')
|
||||
.attr('data-toggle', 'buttons-radio')
|
||||
.appendTo(this.$el);
|
||||
this.update();
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
|
||||
// Add missing items to the DOM.
|
||||
var items = this.model.get('values');
|
||||
for (var index in items) {
|
||||
var item_query = ' :contains("' + items[index] + '")';
|
||||
if (this.$buttongroup.find(item_query).length == 0) {
|
||||
// Remove items that no longer exist.
|
||||
this.$el.find('input').each(function(i, obj) {
|
||||
var value = $(obj).val();
|
||||
var found = false;
|
||||
for (var index in items) {
|
||||
if (items[index] == value) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var that = this;
|
||||
$('<button />')
|
||||
.attr('type', 'button')
|
||||
.addClass('btn')
|
||||
.html(items[index])
|
||||
.appendTo(this.$buttongroup)
|
||||
.on('click', function(e){
|
||||
that.model.set('value', $(e.target).html(), this);
|
||||
that.model.apply();
|
||||
});
|
||||
}
|
||||
|
||||
if (this.model.get('value') == items[index]) {
|
||||
this.$buttongroup.find(item_query).addClass('active');
|
||||
} else {
|
||||
this.$buttongroup.find(item_query).removeClass('active');
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
$(obj).parent().remove();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Remove items that no longer exist.
|
||||
this.$buttongroup.find('button').each(function(i, obj) {
|
||||
var value = $(obj).html();
|
||||
var found = false;
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
|
||||
|
||||
|
||||
var ToggleButtonsView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$buttongroup = $('<div />')
|
||||
.addClass('btn-group')
|
||||
.attr('data-toggle', 'buttons-radio')
|
||||
.appendTo(this.$el);
|
||||
this.update();
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
|
||||
// Add missing items to the DOM.
|
||||
var items = this.model.get('values');
|
||||
for (var index in items) {
|
||||
if (items[index] == value) {
|
||||
found = true;
|
||||
break;
|
||||
var item_query = ' :contains("' + items[index] + '")';
|
||||
if (this.$buttongroup.find(item_query).length == 0) {
|
||||
|
||||
var that = this;
|
||||
$('<button />')
|
||||
.attr('type', 'button')
|
||||
.addClass('btn')
|
||||
.html(items[index])
|
||||
.appendTo(this.$buttongroup)
|
||||
.on('click', function(e){
|
||||
that.model.set('value', $(e.target).html(), this);
|
||||
that.model.apply();
|
||||
});
|
||||
}
|
||||
|
||||
if (this.model.get('value') == items[index]) {
|
||||
this.$buttongroup.find(item_query).addClass('active');
|
||||
} else {
|
||||
this.$buttongroup.find(item_query).removeClass('active');
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
$(obj).remove();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
});
|
||||
// Remove items that no longer exist.
|
||||
this.$buttongroup.find('button').each(function(i, obj) {
|
||||
var value = $(obj).html();
|
||||
var found = false;
|
||||
for (var index in items) {
|
||||
if (items[index] == value) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
$(obj).remove();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView);
|
||||
IPython.notebook.widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
|
||||
});
|
||||
|
@ -1,74 +1,98 @@
|
||||
var StringWidgetModel = IPython.WidgetModel.extend({});
|
||||
IPython.notebook.widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel);
|
||||
require(["notebook/js/widget"], function(){
|
||||
var StringWidgetModel = IPython.WidgetModel.extend({});
|
||||
IPython.notebook.widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel);
|
||||
|
||||
var TextareaView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$textbox = $('<textarea />')
|
||||
.attr('rows', 5)
|
||||
.appendTo(this.$el);
|
||||
this.update(); // Set defaults.
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
if (!this.user_invoked_update) {
|
||||
this.$textbox.val(this.model.get('value'));
|
||||
}
|
||||
},
|
||||
|
||||
events: {"keyup textarea" : "handleChanging",
|
||||
"paste textarea" : "handleChanging",
|
||||
"cut textarea" : "handleChanging"},
|
||||
|
||||
// Handles and validates user input.
|
||||
handleChanging: function(e) {
|
||||
this.user_invoked_update = true;
|
||||
this.model.set('value', e.target.value);
|
||||
this.model.apply(this);
|
||||
this.user_invoked_update = false;
|
||||
},
|
||||
var LabelView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('');
|
||||
this.$label = $('<div />')
|
||||
.addClass(this.model.comm.comm_id)
|
||||
.appendTo(this.$el);
|
||||
this.update(); // Set defaults.
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
this.$label.html(this.model.get('value'));
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('LabelView', LabelView);
|
||||
|
||||
var TextareaView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$textbox = $('<textarea />')
|
||||
.attr('rows', 5)
|
||||
.appendTo(this.$el);
|
||||
this.update(); // Set defaults.
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
if (!this.user_invoked_update) {
|
||||
this.$textbox.val(this.model.get('value'));
|
||||
}
|
||||
},
|
||||
|
||||
events: {"keyup textarea" : "handleChanging",
|
||||
"paste textarea" : "handleChanging",
|
||||
"cut textarea" : "handleChanging"},
|
||||
|
||||
// Handles and validates user input.
|
||||
handleChanging: function(e) {
|
||||
this.user_invoked_update = true;
|
||||
this.model.set('value', e.target.value);
|
||||
this.model.apply(this);
|
||||
this.user_invoked_update = false;
|
||||
},
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('TextareaView', TextareaView);
|
||||
|
||||
var TextboxView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$textbox = $('<input type="text" />')
|
||||
.addClass('input')
|
||||
.appendTo(this.$el);
|
||||
this.update(); // Set defaults.
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
if (!this.user_invoked_update) {
|
||||
this.$textbox.val(this.model.get('value'));
|
||||
}
|
||||
},
|
||||
|
||||
events: {"keyup input" : "handleChanging",
|
||||
"paste input" : "handleChanging",
|
||||
"cut input" : "handleChanging"},
|
||||
|
||||
// Handles and validates user input.
|
||||
handleChanging: function(e) {
|
||||
this.user_invoked_update = true;
|
||||
this.model.set('value', e.target.value);
|
||||
this.model.apply(this);
|
||||
this.user_invoked_update = false;
|
||||
},
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('TextboxView', TextboxView);
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('TextareaView', TextareaView);
|
||||
|
||||
var TextboxView = IPython.WidgetView.extend({
|
||||
|
||||
// Called when view is rendered.
|
||||
render : function(){
|
||||
this.$el
|
||||
.html('')
|
||||
.addClass(this.model.comm.comm_id);
|
||||
this.$textbox = $('<input type="text" />')
|
||||
.addClass('input')
|
||||
.appendTo(this.$el);
|
||||
this.update(); // Set defaults.
|
||||
},
|
||||
|
||||
// Handles: Backend -> Frontend Sync
|
||||
// Frontent -> Frontend Sync
|
||||
update : function(){
|
||||
if (!this.user_invoked_update) {
|
||||
this.$textbox.val(this.model.get('value'));
|
||||
}
|
||||
},
|
||||
|
||||
events: {"keyup input" : "handleChanging",
|
||||
"paste input" : "handleChanging",
|
||||
"cut input" : "handleChanging"},
|
||||
|
||||
// Handles and validates user input.
|
||||
handleChanging: function(e) {
|
||||
this.user_invoked_update = true;
|
||||
this.model.set('value', e.target.value);
|
||||
this.model.apply(this);
|
||||
this.user_invoked_update = false;
|
||||
},
|
||||
});
|
||||
|
||||
IPython.notebook.widget_manager.register_widget_view('TextboxView', TextboxView);
|
||||
|
@ -71,8 +71,6 @@
|
||||
<script src="{{static_url("components/jquery/jquery.min.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("components/jquery-ui/ui/minified/jquery-ui.min.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("components/bootstrap/bootstrap/js/bootstrap.min.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("components/underscore/underscore-min.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("components/backbone/backbone-min.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("base/js/namespace.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("base/js/page.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
<script src="{{static_url("auth/js/loginwidget.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||
|
Loading…
Reference in New Issue
Block a user