##// END OF EJS Templates
_model_types, _view_types, _models - and document what keys and values are
_model_types, _view_types, _models - and document what keys and values are

File last commit:

r14537:c1bdf445
r14553:cf2d9c45
Show More
widget_selection.js
360 lines | 12.8 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
Updated require.js references
r14537 define(["notebook/js/widgets/widget"], function(widget_manager){
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var SelectionWidgetModel = IPython.WidgetModel.extend({});
Jonathan Frederic
Changed require.js load calls to allow require.js to pass...
r14374 widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
Jonathan Frederic
Moved view code into model files
r14252
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var DropdownView = IPython.WidgetView.extend({
Jonathan Frederic
Moved view code into model files
r14252
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 // Called when view is rendered.
render : function(){
Jonathan Frederic
Use require.js to load widget manager.
r14253
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$el
Jonathan Frederic
MAJOR CSS FIXES...
r14295 .addClass('widget-hbox-single')
Jonathan Frederic
LOTS OF WIDGET CHANGES...
r14278 .html('');
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')
.html('&nbsp;')
.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')
.html('<span class="caret"></span>')
.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
Lots of updates to widget(s) js...
r14263 // Handles: Backend -> Frontend Sync
// Frontent -> Frontend Sync
update : function(){
Jonathan Frederic
Better fix for empty dropdown button alignment...
r14359
var selected_item_text = this.model.get('value');
Jonathan Frederic
Use regular expressions when fixing button-like captions
r14369 selected_item_text = selected_item_text.replace(/ /g, '&nbsp;');
selected_item_text = selected_item_text.replace(/\n/g, '<br>\n');
Jonathan Frederic
jslint /widgets
r14466 if (selected_item_text.length === 0) {
Jonathan Frederic
Better fix for empty dropdown button alignment...
r14359 this.$droplabel.html('&nbsp;');
} else {
Jonathan Frederic
Added support for multiple lines in button-like widgets
r14368 this.$droplabel.html(selected_item_text);
Jonathan Frederic
Better fix for empty dropdown button alignment...
r14359 }
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
var items = this.model.get('values');
this.$droplist.html('');
for (var index in items) {
Jonathan Frederic
Removed require.js scheme since it forces async event driven model,...
r14257 var that = this;
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var item_button = $('<a href="#"/>')
.html(items[index])
Jonathan Frederic
Fixed context of onclick callbacks selection widget views
r14467 .on('click', $.proxy(this.handle_click, this));
Jonathan Frederic
jslint /widgets
r14466 this.$droplist.append($('<li />').append(item_button));
Jonathan Frederic
Moved view code into model files
r14252 }
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 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');
Jonathan Frederic
Moved view code into model files
r14252 } else {
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 this.$buttongroup.removeAttr('disabled');
this.$droplabel.removeAttr('disabled');
this.$dropbutton.removeAttr('disabled');
this.$droplist.removeAttr('disabled');
Jonathan Frederic
Moved view code into model files
r14252 }
Jonathan Frederic
Added labels to basic widgets
r14292
var description = this.model.get('description');
Jonathan Frederic
jslint /widgets
r14466 if (description.length === 0) {
Jonathan Frederic
Added labels to basic widgets
r14292 this.$label.hide();
} else {
this.$label.html(description);
this.$label.show();
}
Jonathan Frederic
Fixes that allow last commit to work.
r14279 return IPython.WidgetView.prototype.update.call(this);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 },
Jonathan Frederic
jslint /widgets
r14466
// Handle when a value is clicked.
handle_click: function (e) {
this.model.set('value', $(e.target).html(), 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
Changed require.js load calls to allow require.js to pass...
r14374 widget_manager.register_widget_view('DropdownView', DropdownView);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
var RadioButtonsView = IPython.WidgetView.extend({
// Called when view is rendered.
render : function(){
this.$el
Jonathan Frederic
MAJOR CSS FIXES...
r14295 .addClass('widget-hbox')
Jonathan Frederic
LOTS OF WIDGET CHANGES...
r14278 .html('');
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
Lots of updates to widget(s) js...
r14263 // Handles: Backend -> Frontend Sync
// Frontent -> Frontend Sync
update : function(){
// Add missing items to the DOM.
var items = this.model.get('values');
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 var disabled = this.model.get('disabled');
Jonathan Frederic
Moved view code into model files
r14252 for (var index in items) {
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var item_query = ' :input[value="' + items[index] + '"]';
Jonathan Frederic
jslint /widgets
r14466 if (this.$el.find(item_query).length === 0) {
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var $label = $('<label />')
.addClass('radio')
.html(items[index])
Jonathan Frederic
MAJOR CSS FIXES...
r14295 .appendTo(this.$container);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263
$('<input />')
.attr('type', 'radio')
.addClass(this.model)
.val(items[index])
.prependTo($label)
Jonathan Frederic
Fixed context of onclick callbacks selection widget views
r14467 .on('click', $.proxy(this.handle_click, this));
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 }
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 var $item_element = this.$container.find(item_query);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 if (this.model.get('value') == items[index]) {
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 $item_element.prop('checked', true);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 } else {
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 $item_element.prop('checked', false);
Jonathan Frederic
Moved view code into model files
r14252 }
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 $item_element.prop('disabled', disabled);
Jonathan Frederic
Moved view code into model files
r14252 }
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 // Remove items that no longer exist.
Jonathan Frederic
MAJOR CSS FIXES...
r14295 this.$container.find('input').each(function(i, obj) {
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 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
Added labels to basic widgets
r14292
var description = this.model.get('description');
Jonathan Frederic
jslint /widgets
r14466 if (description.length === 0) {
Jonathan Frederic
Added labels to basic widgets
r14292 this.$label.hide();
} else {
this.$label.html(description);
this.$label.show();
}
Jonathan Frederic
Fixes that allow last commit to work.
r14279 return IPython.WidgetView.prototype.update.call(this);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 },
Jonathan Frederic
jslint /widgets
r14466
// Handle when a value is clicked.
handle_click: function (e) {
this.model.set('value', $(e.target).val(), 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
Moved view code into model files
r14252
Jonathan Frederic
Changed require.js load calls to allow require.js to pass...
r14374 widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
Jonathan Frederic
Added togglebutton group
r14258
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var ToggleButtonsView = IPython.WidgetView.extend({
Jonathan Frederic
Added togglebutton group
r14258
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 // Called when view is rendered.
render : function(){
this.$el
Jonathan Frederic
MAJOR CSS FIXES...
r14295 .addClass('widget-hbox-single')
Jonathan Frederic
LOTS OF WIDGET CHANGES...
r14278 .html('');
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
Lots of updates to widget(s) js...
r14263 // Handles: Backend -> Frontend Sync
// Frontent -> Frontend Sync
update : function(){
// Add missing items to the DOM.
var items = this.model.get('values');
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 var disabled = this.model.get('disabled');
Jonathan Frederic
Added togglebutton group
r14258 for (var index in items) {
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 var item_query = ' :contains("' + items[index] + '")';
Jonathan Frederic
jslint /widgets
r14466 if (this.$buttongroup.find(item_query).length === 0) {
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 $('<button />')
.attr('type', 'button')
.addClass('btn')
.html(items[index])
.appendTo(this.$buttongroup)
Jonathan Frederic
Fixed context of onclick callbacks selection widget views
r14467 .on('click', $.proxy(this.handle_click, this));
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 }
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 var $item_element = this.$buttongroup.find(item_query);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 if (this.model.get('value') == items[index]) {
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 $item_element.addClass('active');
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 } else {
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 $item_element.removeClass('active');
Jonathan Frederic
Added togglebutton group
r14258 }
Jonathan Frederic
Made RadioButtons and ToggleButtons views compatible with disabled property
r14305 $item_element.prop('disabled', disabled);
Jonathan Frederic
Added togglebutton group
r14258 }
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 // 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
Added labels to basic widgets
r14292
var description = this.model.get('description');
Jonathan Frederic
jslint /widgets
r14466 if (description.length === 0) {
Jonathan Frederic
Added labels to basic widgets
r14292 this.$label.hide();
} else {
this.$label.html(description);
this.$label.show();
}
Jonathan Frederic
Fixes that allow last commit to work.
r14279 return IPython.WidgetView.prototype.update.call(this);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 },
Jonathan Frederic
jslint /widgets
r14466
// Handle when a value is clicked.
handle_click: function (e) {
this.model.set('value', $(e.target).html(), 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
Added togglebutton group
r14258
Jonathan Frederic
Changed require.js load calls to allow require.js to pass...
r14374 widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
Jonathan Frederic
Added ListBoxView
r14375
var ListBoxView = IPython.WidgetView.extend({
// 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();
},
// 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] + '")';
Jonathan Frederic
jslint /widgets
r14466 if (this.$listbox.find(item_query).length === 0) {
Jonathan Frederic
Added ListBoxView
r14375 $('<option />')
.html(items[index])
.attr('value', items[index])
.appendTo(this.$listbox)
Jonathan Frederic
Fixed context of onclick callbacks selection widget views
r14467 .on('click', $.proxy(this.handle_click, this));
Jonathan Frederic
Added ListBoxView
r14375 }
}
// Select the correct element
this.$listbox.val(this.model.get('value'));
// 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();
}
});
var description = this.model.get('description');
Jonathan Frederic
jslint /widgets
r14466 if (description.length === 0) {
Jonathan Frederic
Added ListBoxView
r14375 this.$label.hide();
} else {
this.$label.html(description);
this.$label.show();
}
return IPython.WidgetView.prototype.update.call(this);
},
Jonathan Frederic
jslint /widgets
r14466
// Handle when a value is clicked.
handle_click: function (e) {
this.model.set('value', $(e.target).html(), this);
Jonathan Frederic
Moved touch logic out of model into view....
r14482 this.touch();
Jonathan Frederic
jslint /widgets
r14466 },
Jonathan Frederic
Added ListBoxView
r14375
});
widget_manager.register_widget_view('ListBoxView', ListBoxView);
Jonathan Frederic
Lots of updates to widget(s) js...
r14263 });