##// END OF EJS Templates
Removed for () loops where necessary. Replaced with _.each
Removed for () loops where necessary. Replaced with _.each

File last commit:

r14664:9d49e0ed
r14664:9d49e0ed
Show More
widget_selection.js
372 lines | 14.6 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Added standard IPY JS header to widget JS files.
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
Widget require.js fix...
r14627 define(["notebook/js/widgets/widget"], function(WidgetManager){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609
Jonathan Frederic
s/BaseWidgetView/WidgetView and s/WidgetView/DOMWidgetView
r14564 var DropdownView = IPython.DOMWidgetView.extend({
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 render : function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Called when view is rendered.
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$el
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 .addClass('widget-hbox-single');
Jonathan Frederic
Added labels to basic widgets
r14292 this.$label = $('<div />')
.appendTo(this.$el)
Jonathan Frederic
Fixed vertical widget labels
r14297 .addClass('widget-hlabel')
Jonathan Frederic
Added labels to basic widgets
r14292 .hide();
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$buttongroup = $('<div />')
Jonathan Frederic
Fixed indentation
r14365 .addClass('widget_item')
.addClass('btn-group')
.appendTo(this.$el);
Jonathan Frederic
Set default element to be styled in built-in views
r14314 this.$el_to_style = this.$buttongroup; // Set default element to style
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$droplabel = $('<button />')
Jonathan Frederic
Fixed indentation
r14365 .addClass('btn')
.addClass('widget-combo-btn')
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 .text(' ')
Jonathan Frederic
Fixed indentation
r14365 .appendTo(this.$buttongroup);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$dropbutton = $('<button />')
Jonathan Frederic
Fixed indentation
r14365 .addClass('btn')
.addClass('dropdown-toggle')
.addClass('widget-combo-carrot-btn')
.attr('data-toggle', 'dropdown')
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 .append($('<span />').addClass("caret"))
Jonathan Frederic
Fixed indentation
r14365 .appendTo(this.$buttongroup);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$droplist = $('<ul />')
Jonathan Frederic
Fixed indentation
r14365 .addClass('dropdown-menu')
.appendTo(this.$buttongroup);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
// Set defaults.
this.update();
},
Jonathan Frederic
Removed require.js scheme since it forces async event driven model,...
r14257
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 update : function(options){
Jonathan Frederic
make JS update comment more descriptive (english)
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
Better fix for empty dropdown button alignment...
r14359
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 if (options === undefined || options.updated_view != this) {
var selected_item_text = this.model.get('value');
if (selected_item_text.length === 0) {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.$droplabel.text(' ');
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 } else {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.$droplabel.text(selected_item_text);
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 }
var items = this.model.get('values');
Jonathan Frederic
Make dropdown view DOM swap elements on update.
r14616 var $replace_droplist = $('<ul />')
.addClass('dropdown-menu');
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, i) {
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var item_button = $('<a href="#"/>')
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 .text(item)
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 .on('click', $.proxy(this.handle_click, this));
Jonathan Frederic
Make dropdown view DOM swap elements on update.
r14616 $replace_droplist.append($('<li />').append(item_button));
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
Make dropdown view DOM swap elements on update.
r14616 this.$droplist.replaceWith($replace_droplist);
this.$droplist.remove();
this.$droplist = $replace_droplist;
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570
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
Added labels to basic widgets
r14292
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var description = this.model.get('description');
if (description.length === 0) {
this.$label.hide();
} else {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.$label.text(description);
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 this.$label.show();
}
Jonathan Frederic
Added labels to basic widgets
r14292 }
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 return DropdownView.__super__.update.apply(this);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 },
Jonathan Frederic
jslint /widgets
r14466
handle_click: function (e) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle when a value is clicked.
Jonathan Frederic
comment model.set, so we know that it triggers update on other views
r14569
// Calling model.set will trigger all of the other views of the
// model to update.
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.model.set('value', $(e.target).text(), {updated_view: this});
Jonathan Frederic
Moved touch logic out of model into view....
r14482 this.touch();
Jonathan Frederic
jslint /widgets
r14466 },
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
});
Jonathan Frederic
Widget require.js fix...
r14627 WidgetManager.register_widget_view('DropdownView', DropdownView);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609
var RadioButtonsView = IPython.DOMWidgetView.extend({
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 render : function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Called when view is rendered.
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$el
Jonathan Frederic
remove .html('');
r14576 .addClass('widget-hbox');
Jonathan Frederic
Added labels to basic widgets
r14292 this.$label = $('<div />')
.appendTo(this.$el)
Jonathan Frederic
Fixed vertical widget labels
r14297 .addClass('widget-hlabel')
Jonathan Frederic
Added labels to basic widgets
r14292 .hide();
Jonathan Frederic
MAJOR CSS FIXES...
r14295 this.$container = $('<div />')
.appendTo(this.$el)
.addClass('widget-container')
.addClass('vbox');
Jonathan Frederic
Set default element to be styled in built-in views
r14314 this.$el_to_style = this.$container; // Set default element to style
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.update();
},
Jonathan Frederic
Moved view code into model files
r14252
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 update : function(options){
Jonathan Frederic
make JS update comment more descriptive (english)
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
add locks to update everywhere by using options to pass this...
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
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, index) {
var item_query = ' :input[value="' + item + '"]';
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 if (this.$el.find(item_query).length === 0) {
var $label = $('<label />')
.addClass('radio')
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 .text(item)
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 .appendTo(this.$container);
$('<input />')
.attr('type', 'radio')
.addClass(this.model)
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 .val(item)
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 .prependTo($label)
.on('click', $.proxy(this.handle_click, this));
}
var $item_element = this.$container.find(item_query);
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 if (this.model.get('value') == item) {
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 $item_element.prop('checked', true);
} else {
$item_element.prop('checked', false);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 }
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 $item_element.prop('disabled', disabled);
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 // Remove items that no longer exist.
this.$container.find('input').each(function(i, obj) {
var value = $(obj).val();
var found = false;
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, index) {
if (item == value) {
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 found = true;
break;
}
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570
if (!found) {
$(obj).parent().remove();
}
});
Jonathan Frederic
Added labels to basic widgets
r14292
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var description = this.model.get('description');
if (description.length === 0) {
this.$label.hide();
} else {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.$label.text(description);
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 this.$label.show();
}
Jonathan Frederic
Added labels to basic widgets
r14292 }
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 return RadioButtonsView.__super__.update.apply(this);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 },
Jonathan Frederic
jslint /widgets
r14466
handle_click: function (e) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle when a value is clicked.
Jonathan Frederic
comment model.set, so we know that it triggers update on other views
r14569
// Calling model.set will trigger all of the other views of the
// model to update.
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 this.model.set('value', $(e.target).val(), {updated_view: this});
Jonathan Frederic
Moved touch logic out of model into view....
r14482 this.touch();
Jonathan Frederic
jslint /widgets
r14466 },
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 });
Jonathan Frederic
Widget require.js fix...
r14627 WidgetManager.register_widget_view('RadioButtonsView', RadioButtonsView);
Jonathan Frederic
Added togglebutton group
r14258
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 var ToggleButtonsView = IPython.DOMWidgetView.extend({
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 render : function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Called when view is rendered.
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$el
Jonathan Frederic
remove .html('');
r14576 .addClass('widget-hbox-single');
Jonathan Frederic
Added labels to basic widgets
r14292 this.$label = $('<div />')
.appendTo(this.$el)
Jonathan Frederic
Fixed vertical widget labels
r14297 .addClass('widget-hlabel')
Jonathan Frederic
Added labels to basic widgets
r14292 .hide();
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$buttongroup = $('<div />')
.addClass('btn-group')
.attr('data-toggle', 'buttons-radio')
.appendTo(this.$el);
Jonathan Frederic
Set default element to be styled in built-in views
r14314 this.$el_to_style = this.$buttongroup; // Set default element to style
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.update();
},
Jonathan Frederic
Added togglebutton group
r14258
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 update : function(options){
Jonathan Frederic
make JS update comment more descriptive (english)
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
add locks to update everywhere by using options to pass this...
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
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, index) {
var item_query = ' :contains("' + item + '")';
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 if (this.$buttongroup.find(item_query).length === 0) {
$('<button />')
.attr('type', 'button')
.addClass('btn')
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 .text(item)
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 .appendTo(this.$buttongroup)
.on('click', $.proxy(this.handle_click, this));
}
var $item_element = this.$buttongroup.find(item_query);
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 if (this.model.get('value') == item) {
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 $item_element.addClass('active');
} else {
$item_element.removeClass('active');
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 }
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 $item_element.prop('disabled', disabled);
});
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 // Remove items that no longer exist.
this.$buttongroup.find('button').each(function(i, obj) {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 var value = $(obj).text();
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var found = false;
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, index) {
if (item == value) {
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 found = true;
break;
}
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 if (!found) {
$(obj).remove();
}
});
Jonathan Frederic
Added labels to basic widgets
r14292
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var description = this.model.get('description');
if (description.length === 0) {
this.$label.hide();
} else {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.$label.text(description);
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 this.$label.show();
}
Jonathan Frederic
Added labels to basic widgets
r14292 }
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 return ToggleButtonsView.__super__.update.apply(this);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 },
Jonathan Frederic
jslint /widgets
r14466
handle_click: function (e) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle when a value is clicked.
Jonathan Frederic
comment model.set, so we know that it triggers update on other views
r14569
// Calling model.set will trigger all of the other views of the
// model to update.
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.model.set('value', $(e.target).text(), {updated_view: this});
Jonathan Frederic
Moved touch logic out of model into view....
r14482 this.touch();
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 },
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 });
Jonathan Frederic
Widget require.js fix...
r14627 WidgetManager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
Jonathan Frederic
Added ListBoxView
r14375
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609
var ListBoxView = IPython.DOMWidgetView.extend({
Jonathan Frederic
Added ListBoxView
r14375 render : function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Called when view is rendered.
Jonathan Frederic
Added ListBoxView
r14375 this.$el
Jonathan Frederic
remove .html('');
r14576 .addClass('widget-hbox');
Jonathan Frederic
Added ListBoxView
r14375 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
add locks to update everywhere by using options to pass this...
r14570 update : function(options){
Jonathan Frederic
make JS update comment more descriptive (english)
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
add locks to update everywhere by using options to pass this...
r14570 if (options === undefined || options.updated_view != this) {
// Add missing items to the DOM.
var items = this.model.get('values');
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, index) {
var item_query = ' :contains("' + item + '")';
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 if (this.$listbox.find(item_query).length === 0) {
$('<option />')
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 .text(item)
.attr('value', item)
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 .appendTo(this.$listbox)
.on('click', $.proxy(this.handle_click, this));
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 }
});
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570
// Select the correct element
this.$listbox.val(this.model.get('value'));
Jonathan Frederic
Added ListBoxView
r14375
Jonathan Frederic
add locks to update everywhere by using options to pass this...
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) {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 var value = $(obj).text();
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var found = false;
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(items, function(item, index) {
if (item == value) {
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 found = true;
break;
}
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570
if (!found) {
$(obj).remove();
}
});
Jonathan Frederic
Added ListBoxView
r14375
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 var description = this.model.get('description');
if (description.length === 0) {
this.$label.hide();
} else {
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.$label.text(description);
Jonathan Frederic
add locks to update everywhere by using options to pass this...
r14570 this.$label.show();
}
Jonathan Frederic
Added ListBoxView
r14375 }
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 return ListBoxView.__super__.update.apply(this);
Jonathan Frederic
Added ListBoxView
r14375 },
Jonathan Frederic
jslint /widgets
r14466
handle_click: function (e) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle when a value is clicked.
Jonathan Frederic
comment model.set, so we know that it triggers update on other views
r14569
// Calling model.set will trigger all of the other views of the
// model to update.
Jonathan Frederic
Replace .html with .text everywhere possible
r14663 this.model.set('value', $(e.target).text(), {updated_view: this});
Jonathan Frederic
Moved touch logic out of model into view....
r14482 this.touch();
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 },
Jonathan Frederic
Added ListBoxView
r14375 });
Jonathan Frederic
Widget require.js fix...
r14627 WidgetManager.register_widget_view('ListBoxView', ListBoxView);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 });