widget_selection.js
385 lines
| 14.9 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r14366 | //---------------------------------------------------------------------------- | ||
// 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. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// SelectionWidget | ||||
//============================================================================ | ||||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
Jonathan Frederic
|
r14537 | define(["notebook/js/widgets/widget"], function(widget_manager){ | ||
Jonathan Frederic
|
r14263 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); | ||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14564 | var DropdownView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
Jonathan Frederic
|
r14253 | |||
Jonathan Frederic
|
r14263 | this.$el | ||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox-single') | ||
Jonathan Frederic
|
r14278 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$buttongroup = $('<div />') | ||
Jonathan Frederic
|
r14365 | .addClass('widget_item') | ||
.addClass('btn-group') | ||||
.appendTo(this.$el); | ||||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$buttongroup; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.$droplabel = $('<button />') | ||
Jonathan Frederic
|
r14365 | .addClass('btn') | ||
.addClass('widget-combo-btn') | ||||
.html(' ') | ||||
.appendTo(this.$buttongroup); | ||||
Jonathan Frederic
|
r14263 | this.$dropbutton = $('<button />') | ||
Jonathan Frederic
|
r14365 | .addClass('btn') | ||
.addClass('dropdown-toggle') | ||||
.addClass('widget-combo-carrot-btn') | ||||
.attr('data-toggle', 'dropdown') | ||||
.html('<span class="caret"></span>') | ||||
.appendTo(this.$buttongroup); | ||||
Jonathan Frederic
|
r14263 | this.$droplist = $('<ul />') | ||
Jonathan Frederic
|
r14365 | .addClass('dropdown-menu') | ||
.appendTo(this.$buttongroup); | ||||
Jonathan Frederic
|
r14263 | |||
// Set defaults. | ||||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r14257 | |||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14359 | |||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
var selected_item_text = this.model.get('value'); | ||||
selected_item_text = selected_item_text.replace(/ /g, ' '); | ||||
selected_item_text = selected_item_text.replace(/\n/g, '<br>\n'); | ||||
if (selected_item_text.length === 0) { | ||||
this.$droplabel.html(' '); | ||||
} else { | ||||
this.$droplabel.html(selected_item_text); | ||||
} | ||||
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', $.proxy(this.handle_click, 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'); | ||||
} | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14564 | return IPython.DOMWidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14466 | |||
// Handle when a value is clicked. | ||||
handle_click: function (e) { | ||||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r14570 | this.model.set('value', $(e.target).html(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14466 | }, | ||
Jonathan Frederic
|
r14263 | |||
}); | ||||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('DropdownView', DropdownView); | ||
Jonathan Frederic
|
r14263 | |||
Jonathan Frederic
|
r14564 | var RadioButtonsView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14263 | |||
// Called when view is rendered. | ||||
render : function(){ | ||||
this.$el | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox') | ||
Jonathan Frederic
|
r14278 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14295 | this.$container = $('<div />') | ||
.appendTo(this.$el) | ||||
.addClass('widget-container') | ||||
.addClass('vbox'); | ||||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$container; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); | ||
}, | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Add missing items to the DOM. | ||||
var items = this.model.get('values'); | ||||
var disabled = this.model.get('disabled'); | ||||
Jonathan Frederic
|
r14263 | for (var index in items) { | ||
Jonathan Frederic
|
r14570 | 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.$container); | ||||
$('<input />') | ||||
.attr('type', 'radio') | ||||
.addClass(this.model) | ||||
.val(items[index]) | ||||
.prependTo($label) | ||||
.on('click', $.proxy(this.handle_click, this)); | ||||
} | ||||
var $item_element = this.$container.find(item_query); | ||||
if (this.model.get('value') == items[index]) { | ||||
$item_element.prop('checked', true); | ||||
} else { | ||||
$item_element.prop('checked', false); | ||||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14570 | $item_element.prop('disabled', disabled); | ||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14570 | // Remove items that no longer exist. | ||
this.$container.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; | ||||
} | ||||
} | ||||
if (!found) { | ||||
$(obj).parent().remove(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14564 | return IPython.DOMWidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14466 | |||
// Handle when a value is clicked. | ||||
handle_click: function (e) { | ||||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r14570 | this.model.set('value', $(e.target).val(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14466 | }, | ||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView); | ||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14564 | var ToggleButtonsView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
this.$el | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-hbox-single') | ||
Jonathan Frederic
|
r14278 | .html(''); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$buttongroup = $('<div />') | ||
.addClass('btn-group') | ||||
.attr('data-toggle', 'buttons-radio') | ||||
.appendTo(this.$el); | ||||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$buttongroup; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); | ||
}, | ||||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Add missing items to the DOM. | ||||
var items = this.model.get('values'); | ||||
var disabled = this.model.get('disabled'); | ||||
Jonathan Frederic
|
r14263 | for (var index in items) { | ||
Jonathan Frederic
|
r14570 | var item_query = ' :contains("' + items[index] + '")'; | ||
if (this.$buttongroup.find(item_query).length === 0) { | ||||
$('<button />') | ||||
.attr('type', 'button') | ||||
.addClass('btn') | ||||
.html(items[index]) | ||||
.appendTo(this.$buttongroup) | ||||
.on('click', $.proxy(this.handle_click, this)); | ||||
} | ||||
var $item_element = this.$buttongroup.find(item_query); | ||||
if (this.model.get('value') == items[index]) { | ||||
$item_element.addClass('active'); | ||||
} else { | ||||
$item_element.removeClass('active'); | ||||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14570 | $item_element.prop('disabled', disabled); | ||
Jonathan Frederic
|
r14263 | } | ||
Jonathan Frederic
|
r14570 | // 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(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14564 | return IPython.DOMWidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14466 | |||
// Handle when a value is clicked. | ||||
handle_click: function (e) { | ||||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r14570 | this.model.set('value', $(e.target).html(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14466 | }, | ||
Jonathan Frederic
|
r14263 | |||
}); | ||||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14374 | widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView); | ||
Jonathan Frederic
|
r14375 | |||
Jonathan Frederic
|
r14564 | var ListBoxView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14375 | |||
// Called when view is rendered. | ||||
render : function(){ | ||||
this.$el | ||||
.addClass('widget-hbox') | ||||
.html(''); | ||||
this.$label = $('<div />') | ||||
.appendTo(this.$el) | ||||
.addClass('widget-hlabel') | ||||
.hide(); | ||||
this.$listbox = $('<select />') | ||||
.addClass('widget-listbox') | ||||
.attr('size', 6) | ||||
.appendTo(this.$el); | ||||
this.$el_to_style = this.$listbox; // Set default element to style | ||||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r14570 | update : function(options){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
// Add missing items to the DOM. | ||||
var items = this.model.get('values'); | ||||
Jonathan Frederic
|
r14375 | for (var index in items) { | ||
Jonathan Frederic
|
r14570 | var item_query = ' :contains("' + items[index] + '")'; | ||
if (this.$listbox.find(item_query).length === 0) { | ||||
$('<option />') | ||||
.html(items[index]) | ||||
.attr('value', items[index]) | ||||
.appendTo(this.$listbox) | ||||
.on('click', $.proxy(this.handle_click, this)); | ||||
Jonathan Frederic
|
r14375 | } | ||
} | ||||
Jonathan Frederic
|
r14570 | |||
// Select the correct element | ||||
this.$listbox.val(this.model.get('value')); | ||||
Jonathan Frederic
|
r14375 | |||
Jonathan Frederic
|
r14570 | // Disable listbox if needed | ||
var disabled = this.model.get('disabled'); | ||||
this.$listbox.prop('disabled', disabled); | ||||
// Remove items that no longer exist. | ||||
this.$listbox.find('option').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(); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r14375 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
Jonathan Frederic
|
r14375 | } | ||
Jonathan Frederic
|
r14564 | return IPython.DOMWidgetView.prototype.update.call(this); | ||
Jonathan Frederic
|
r14375 | }, | ||
Jonathan Frederic
|
r14466 | |||
// Handle when a value is clicked. | ||||
handle_click: function (e) { | ||||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r14570 | this.model.set('value', $(e.target).html(), {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14466 | }, | ||
Jonathan Frederic
|
r14375 | |||
}); | ||||
widget_manager.register_widget_view('ListBoxView', ListBoxView); | ||||
Jonathan Frederic
|
r14263 | }); | ||