Show More
@@ -1,128 +1,194 | |||
|
1 | 1 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | 2 | IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); |
|
3 | 3 | |
|
4 | 4 | var DropdownView = IPython.WidgetView.extend({ |
|
5 | 5 | |
|
6 | 6 | // Called when view is rendered. |
|
7 | 7 | render : function(){ |
|
8 | 8 | |
|
9 | 9 | this.$el |
|
10 | 10 | .html('') |
|
11 | 11 | .addClass(this.model.comm.comm_id); |
|
12 | 12 | this.$buttongroup = $('<div />') |
|
13 | 13 | .addClass('btn-group') |
|
14 | 14 | .appendTo(this.$el); |
|
15 | 15 | this.$droplabel = $('<button />') |
|
16 | 16 | .addClass('btn') |
|
17 | 17 | .appendTo(this.$buttongroup); |
|
18 | 18 | this.$dropbutton = $('<button />') |
|
19 | 19 | .addClass('btn') |
|
20 | 20 | .addClass('dropdown-toggle') |
|
21 | 21 | .attr('data-toggle', 'dropdown') |
|
22 | 22 | .html('<span class="caret"></span>') |
|
23 | 23 | .appendTo(this.$buttongroup); |
|
24 | 24 | this.$droplist = $('<ul />') |
|
25 | 25 | .addClass('dropdown-menu') |
|
26 | 26 | .appendTo(this.$buttongroup); |
|
27 | 27 | |
|
28 | 28 | // Set defaults. |
|
29 | 29 | this.update(); |
|
30 | 30 | }, |
|
31 | 31 | |
|
32 | 32 | // Handles: Backend -> Frontend Sync |
|
33 | 33 | // Frontent -> Frontend Sync |
|
34 | 34 | update : function(){ |
|
35 | 35 | this.$droplabel.html(this.model.get('value')); |
|
36 | 36 | |
|
37 | 37 | var items = this.model.get('values'); |
|
38 | 38 | this.$droplist.html(''); |
|
39 | 39 | for (var index in items) { |
|
40 | 40 | var that = this; |
|
41 | 41 | var item_button = $('<a href="#"/>') |
|
42 | 42 | .html(items[index]) |
|
43 | 43 | .on('click', function(e){ |
|
44 | 44 | that.model.set('value', $(e.target).html(), this ); |
|
45 | 45 | }) |
|
46 | 46 | |
|
47 | 47 | this.$droplist.append($('<li />').append(item_button)) |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | if (this.model.get('disabled')) { |
|
51 | 51 | this.$buttongroup.attr('disabled','disabled'); |
|
52 | 52 | this.$droplabel.attr('disabled','disabled'); |
|
53 | 53 | this.$dropbutton.attr('disabled','disabled'); |
|
54 | 54 | this.$droplist.attr('disabled','disabled'); |
|
55 | 55 | } else { |
|
56 | 56 | this.$buttongroup.removeAttr('disabled'); |
|
57 | 57 | this.$droplabel.removeAttr('disabled'); |
|
58 | 58 | this.$dropbutton.removeAttr('disabled'); |
|
59 | 59 | this.$droplist.removeAttr('disabled'); |
|
60 | 60 | } |
|
61 | 61 | }, |
|
62 | 62 | |
|
63 | 63 | }); |
|
64 | 64 | |
|
65 | 65 | IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView); |
|
66 | ||
|
66 | 67 | var RadioButtonView = IPython.WidgetView.extend({ |
|
67 | 68 | |
|
68 | 69 | // Called when view is rendered. |
|
69 | 70 | render : function(){ |
|
70 | 71 | this.$el |
|
71 | 72 | .html('') |
|
72 | 73 | .addClass(this.model.comm.comm_id); |
|
73 | 74 | this.update(); |
|
74 | 75 | }, |
|
75 | 76 | |
|
76 | 77 | // Handles: Backend -> Frontend Sync |
|
77 | 78 | // Frontent -> Frontend Sync |
|
78 | 79 | update : function(){ |
|
79 | 80 | |
|
80 | 81 | // Add missing items to the DOM. |
|
81 | 82 | var items = this.model.get('values'); |
|
82 | 83 | for (var index in items) { |
|
83 | 84 | var item_query = ' :input[value="' + items[index] + '"]'; |
|
84 | 85 | if (this.$el.find(item_query).length == 0) { |
|
85 | 86 | var $label = $('<label />') |
|
86 | 87 | .addClass('radio') |
|
87 | 88 | .html(items[index]) |
|
88 | 89 | .appendTo(this.$el); |
|
89 | 90 | |
|
90 | 91 | var that = this; |
|
91 | 92 | $('<input />') |
|
92 | 93 | .attr('type', 'radio') |
|
93 | 94 | .addClass(this.model) |
|
94 | 95 | .val(items[index]) |
|
95 | 96 | .prependTo($label) |
|
96 | 97 | .on('click', function(e){ |
|
97 | 98 | that.model.set('value', $(e.target).val(), this); |
|
98 | 99 | that.model.apply(); |
|
99 | 100 | }); |
|
100 | 101 | } |
|
101 | 102 | |
|
102 | 103 | if (this.model.get('value') == items[index]) { |
|
103 | 104 | this.$el.find(item_query).prop('checked', true); |
|
104 | 105 | } else { |
|
105 | 106 | this.$el.find(item_query).prop('checked', false); |
|
106 | 107 | } |
|
107 | 108 | } |
|
108 | 109 | |
|
109 | 110 | // Remove items that no longer exist. |
|
110 | 111 | this.$el.find('input').each(function(i, obj) { |
|
111 | 112 | var value = $(obj).val(); |
|
112 | 113 | var found = false; |
|
113 | 114 | for (var index in items) { |
|
114 | 115 | if (items[index] == value) { |
|
115 | 116 | found = true; |
|
116 | 117 | break; |
|
117 | 118 | } |
|
118 | 119 | } |
|
119 | 120 | |
|
120 | 121 | if (!found) { |
|
121 | 122 | $(obj).parent().remove(); |
|
122 | 123 | } |
|
123 | 124 | }); |
|
124 | 125 | }, |
|
125 | 126 | |
|
126 | 127 | }); |
|
127 | 128 | |
|
128 | 129 | IPython.notebook.widget_manager.register_widget_view('RadioButtonView', RadioButtonView); |
|
130 | ||
|
131 | ||
|
132 | var ToggleButtonView = IPython.WidgetView.extend({ | |
|
133 | ||
|
134 | // Called when view is rendered. | |
|
135 | render : function(){ | |
|
136 | this.$el | |
|
137 | .html('') | |
|
138 | .addClass(this.model.comm.comm_id); | |
|
139 | this.$buttongroup = $('<div />') | |
|
140 | .addClass('btn-group') | |
|
141 | .attr('data-toggle', 'buttons-radio') | |
|
142 | .appendTo(this.$el); | |
|
143 | this.update(); | |
|
144 | }, | |
|
145 | ||
|
146 | // Handles: Backend -> Frontend Sync | |
|
147 | // Frontent -> Frontend Sync | |
|
148 | update : function(){ | |
|
149 | ||
|
150 | // Add missing items to the DOM. | |
|
151 | var items = this.model.get('values'); | |
|
152 | for (var index in items) { | |
|
153 | var item_query = ' :contains("' + items[index] + '")'; | |
|
154 | if (this.$buttongroup.find(item_query).length == 0) { | |
|
155 | ||
|
156 | var that = this; | |
|
157 | $('<button />') | |
|
158 | .attr('type', 'button') | |
|
159 | .addClass('btn') | |
|
160 | .html(items[index]) | |
|
161 | .appendTo(this.$buttongroup) | |
|
162 | .on('click', function(e){ | |
|
163 | that.model.set('value', $(e.target).html(), this); | |
|
164 | that.model.apply(); | |
|
165 | }); | |
|
166 | } | |
|
167 | ||
|
168 | if (this.model.get('value') == items[index]) { | |
|
169 | this.$buttongroup.find(item_query).addClass('active'); | |
|
170 | } else { | |
|
171 | this.$buttongroup.find(item_query).removeClass('active'); | |
|
172 | } | |
|
173 | } | |
|
174 | ||
|
175 | // Remove items that no longer exist. | |
|
176 | this.$buttongroup.find('button').each(function(i, obj) { | |
|
177 | var value = $(obj).html(); | |
|
178 | var found = false; | |
|
179 | for (var index in items) { | |
|
180 | if (items[index] == value) { | |
|
181 | found = true; | |
|
182 | break; | |
|
183 | } | |
|
184 | } | |
|
185 | ||
|
186 | if (!found) { | |
|
187 | $(obj).remove(); | |
|
188 | } | |
|
189 | }); | |
|
190 | }, | |
|
191 | ||
|
192 | }); | |
|
193 | ||
|
194 | IPython.notebook.widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView); |
General Comments 0
You need to be logged in to leave comments.
Login now