##// END OF EJS Templates
Add selection widget
Jonathan Frederic -
Show More
@@ -0,0 +1,1 b''
1 from widget import SelectionWidget No newline at end of file
@@ -0,0 +1,2 b''
1 var SelectionWidgetModel = IPython.WidgetModel.extend({});
2 IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
@@ -0,0 +1,62 b''
1 var DropdownView = IPython.WidgetView.extend({
2
3 // Called when view is rendered.
4 render : function(){
5
6 this.$el
7 .html('')
8 .addClass(this.model.comm.comm_id);
9 this.$buttongroup = $('<div />')
10 .addClass('btn-group')
11 .appendTo(this.$el);
12 this.$droplabel = $('<button />')
13 .addClass('btn')
14 .appendTo(this.$buttongroup);
15 this.$dropbutton = $('<button />')
16 .addClass('btn')
17 .addClass('dropdown-toggle')
18 .attr('data-toggle', 'dropdown')
19 .html('<span class="caret"></span>')
20 .appendTo(this.$buttongroup);
21 this.$droplist = $('<ul />')
22 .addClass('dropdown-menu')
23 .appendTo(this.$buttongroup);
24
25 // Set defaults.
26 this.update();
27 },
28
29 // Handles: Backend -> Frontend Sync
30 // Frontent -> Frontend Sync
31 update : function(){
32 this.$droplabel.html(this.model.get('value'));
33
34 var items = this.model.get('values');
35 this.$droplist.html('');
36 for (var index in items) {
37 var that = this;
38 var item_button = $('<a href="#"/>')
39 .html(items[index])
40 .on('click', function(e){
41 that.model.set('value', $(e.target).html(), this );
42 })
43
44 this.$droplist.append($('<li />').append(item_button))
45 }
46
47 if (this.model.get('disabled')) {
48 this.$buttongroup.attr('disabled','disabled');
49 this.$droplabel.attr('disabled','disabled');
50 this.$dropbutton.attr('disabled','disabled');
51 this.$droplist.attr('disabled','disabled');
52 } else {
53 this.$buttongroup.removeAttr('disabled');
54 this.$droplabel.removeAttr('disabled');
55 this.$dropbutton.removeAttr('disabled');
56 this.$droplist.removeAttr('disabled');
57 }
58 },
59
60 });
61
62 IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView);
@@ -0,0 +1,63 b''
1 var RadioButtonView = IPython.WidgetView.extend({
2
3 // Called when view is rendered.
4 render : function(){
5 this.$el
6 .html('')
7 .addClass(this.model.comm.comm_id);
8 this.update();
9 },
10
11 // Handles: Backend -> Frontend Sync
12 // Frontent -> Frontend Sync
13 update : function(){
14
15 // Add missing items to the DOM.
16 var items = this.model.get('values');
17 for (var index in items) {
18 var item_query = ' :input[value="' + items[index] + '"]';
19 if (this.$el.find(item_query).length == 0) {
20 var $label = $('<label />')
21 .addClass('radio')
22 .html(items[index])
23 .appendTo(this.$el);
24
25 var that = this;
26 $('<input />')
27 .attr('type', 'radio')
28 .addClass(this.model)
29 .val(items[index])
30 .prependTo($label)
31 .on('click', function(e){
32 that.model.set('value', $(e.target).val(), this);
33 that.model.apply();
34 });
35 }
36
37 if (this.model.get('value') == items[index]) {
38 this.$el.find(item_query).prop('checked', true);
39 } else {
40 this.$el.find(item_query).prop('checked', false);
41 }
42 }
43
44 // Remove items that no longer exist.
45 this.$el.find('input').each(function(i, obj) {
46 var value = $(obj).val();
47 var found = false;
48 for (var index in items) {
49 if (items[index] == value) {
50 found = true;
51 break;
52 }
53 }
54
55 if (!found) {
56 $(obj).parent().remove();
57 }
58 });
59 },
60
61 });
62
63 IPython.notebook.widget_manager.register_widget_view('RadioButtonView', RadioButtonView);
@@ -0,0 +1,15 b''
1 import os
2
3 from ..widget import Widget
4 from IPython.utils.traitlets import Unicode, List, Bool
5 from IPython.utils.javascript import display_all_js
6
7 class SelectionWidget(Widget):
8 target_name = Unicode('SelectionWidgetModel')
9 default_view_name = Unicode('DropdownView')
10 _keys = ['value', 'values', 'disabled']
11
12 value = Unicode()
13 values = List() # List of values the user can select
14 disabled = Bool(False) # Enable or disable user changes
15 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now