selection.js
202 lines
| 7.4 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r14263 | require(["notebook/js/widget"], function(){ | ||
var SelectionWidgetModel = IPython.WidgetModel.extend({}); | ||||
IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | var DropdownView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
Jonathan Frederic
|
r14253 | |||
Jonathan Frederic
|
r14263 | this.$el | ||
.html('') | ||||
Jonathan Frederic
|
r14268 | .addClass('widget_item') | ||
Jonathan Frederic
|
r14263 | .addClass(this.model.comm.comm_id); | ||
this.$buttongroup = $('<div />') | ||||
Jonathan Frederic
|
r14268 | .addClass('widget_item') | ||
Jonathan Frederic
|
r14263 | .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(); | ||||
}, | ||||
Jonathan Frederic
|
r14257 | |||
Jonathan Frederic
|
r14263 | // 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) { | ||||
Jonathan Frederic
|
r14257 | var that = this; | ||
Jonathan Frederic
|
r14263 | var item_button = $('<a href="#"/>') | ||
.html(items[index]) | ||||
Jonathan Frederic
|
r14257 | .on('click', function(e){ | ||
Jonathan Frederic
|
r14263 | that.model.set('value', $(e.target).html(), this ); | ||
Jonathan Frederic
|
r14268 | that.model.apply(that); | ||
Jonathan Frederic
|
r14263 | }) | ||
this.$droplist.append($('<li />').append(item_button)) | ||||
Jonathan Frederic
|
r14252 | } | ||
Jonathan Frederic
|
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
|
r14252 | } else { | ||
Jonathan Frederic
|
r14263 | this.$buttongroup.removeAttr('disabled'); | ||
this.$droplabel.removeAttr('disabled'); | ||||
this.$dropbutton.removeAttr('disabled'); | ||||
this.$droplist.removeAttr('disabled'); | ||||
Jonathan Frederic
|
r14252 | } | ||
Jonathan Frederic
|
r14263 | }, | ||
}); | ||||
IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView); | ||||
var RadioButtonsView = IPython.WidgetView.extend({ | ||||
// Called when view is rendered. | ||||
render : function(){ | ||||
this.$el | ||||
.html('') | ||||
Jonathan Frederic
|
r14268 | .addClass('widget_item') | ||
Jonathan Frederic
|
r14263 | .addClass(this.model.comm.comm_id); | ||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | // Handles: Backend -> Frontend Sync | ||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
// Add missing items to the DOM. | ||||
var items = this.model.get('values'); | ||||
Jonathan Frederic
|
r14252 | for (var index in items) { | ||
Jonathan Frederic
|
r14263 | 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); | ||||
Jonathan Frederic
|
r14252 | } | ||
} | ||||
Jonathan Frederic
|
r14263 | // 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; | ||||
} | ||||
} | ||||
if (!found) { | ||||
$(obj).parent().remove(); | ||||
} | ||||
}); | ||||
}, | ||||
}); | ||||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14263 | IPython.notebook.widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView); | ||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14263 | var ToggleButtonsView = IPython.WidgetView.extend({ | ||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14263 | // Called when view is rendered. | ||
render : function(){ | ||||
this.$el | ||||
.html('') | ||||
Jonathan Frederic
|
r14268 | .addClass('widget_item') | ||
Jonathan Frederic
|
r14263 | .addClass(this.model.comm.comm_id); | ||
this.$buttongroup = $('<div />') | ||||
Jonathan Frederic
|
r14268 | .addClass('widget_item') | ||
Jonathan Frederic
|
r14263 | .addClass('btn-group') | ||
.attr('data-toggle', 'buttons-radio') | ||||
.appendTo(this.$el); | ||||
this.update(); | ||||
}, | ||||
Jonathan Frederic
|
r14258 | |||
Jonathan Frederic
|
r14263 | // Handles: Backend -> Frontend Sync | ||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
// Add missing items to the DOM. | ||||
var items = this.model.get('values'); | ||||
Jonathan Frederic
|
r14258 | for (var index in items) { | ||
Jonathan Frederic
|
r14263 | 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'); | ||||
Jonathan Frederic
|
r14258 | } | ||
} | ||||
Jonathan Frederic
|
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
|
r14258 | |||
Jonathan Frederic
|
r14263 | IPython.notebook.widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView); | ||
}); | ||||