##// END OF EJS Templates
slightly generalize utils generator
Matthias BUSSONNIER -
Show More
@@ -219,23 +219,20 b' var IPython = (function (IPython) {'
219 }
219 }
220
220
221
221
222
223
224
225 /**
222 /**
226 */
223 */
227 CellToolbar.utils = {};
224 CellToolbar.utils = {};
228
225
229 /**
226 /**
230 * A utility function to generate bindings between a checkbox and metadata
227 * A utility function to generate bindings between a checkbox and cell/metadata
231 * @method utils.checkbox_ui_generator
228 * @method utils.checkbox_ui_generator
232 * @static
229 * @static
233 *
230 *
234 * @param name {string} Label in front of the checkbox
231 * @param name {string} Label in front of the checkbox
235 * @param setter {function( metadata_dict, newValue )}
232 * @param setter {function( cell, newValue )}
236 * A setter method to set the newValue of the metadata dictionnary
233 * A setter method to set the newValue
237 * @param getter {function( metadata )}
234 * @param getter {function( cell )}
238 * A getter methods which return the current value of the metadata.
235 * A getter methods which return the current value.
239 *
236 *
240 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
237 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
241 *
238 *
@@ -245,14 +242,14 b' var IPython = (function (IPython) {'
245 *
242 *
246 * var newSlide = CellToolbar.utils.checkbox_ui_generator('New Slide',
243 * var newSlide = CellToolbar.utils.checkbox_ui_generator('New Slide',
247 * // setter
244 * // setter
248 * function(metadata,value){
245 * function(cell, value){
249 * // we check that the slideshow namespace exist and create it if needed
246 * // we check that the slideshow namespace exist and create it if needed
250 * if (metadata.slideshow == undefined){metadata.slideshow = {}}
247 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
251 * // set the value
248 * // set the value
252 * metadata.slideshow.isSectionStart = value
249 * cell.metadata.slideshow.isSectionStart = value
253 * },
250 * },
254 * //geter
251 * //geter
255 * function(metadata){ var ns = metadata.slideshow;
252 * function(cell){ var ns = cell.metadata.slideshow;
256 * // if the slideshow namespace does not exist return `undefined`
253 * // if the slideshow namespace does not exist return `undefined`
257 * // (will be interpreted as `false` by checkbox) otherwise
254 * // (will be interpreted as `false` by checkbox) otherwise
258 * // return the value
255 * // return the value
@@ -270,11 +267,11 b' var IPython = (function (IPython) {'
270 var chkb = $('<input/>').attr('type','checkbox');
267 var chkb = $('<input/>').attr('type', 'checkbox');
271 var lbl = $('<label/>').append($('<span/>').text(name).css('font-size','77%'));
268 var lbl = $('<label/>').append($('<span/>').text(name).css('font-size', '77%'));
272 lbl.append(chkb);
269 lbl.append(chkb);
273 chkb.attr("checked",getter(cell.metadata));
270 chkb.attr("checked", getter(cell));
274
271
275 chkb.click(function(){
272 chkb.click(function(){
276 var v = getter(cell.metadata);
273 var v = getter(cell);
277 setter(cell.metadata,!v);
274 setter(cell, !v);
278 chkb.attr("checked",!v);
275 chkb.attr("checked", !v);
279 })
276 })
280 button_container.append($('<div/>').append(lbl));
277 button_container.append($('<div/>').append(lbl));
@@ -283,16 +280,16 b' var IPython = (function (IPython) {'
283 }
280 }
284
281
285 /**
282 /**
286 * A utility function to generate bindings between a dropdown list and metadata
283 * A utility function to generate bindings between a dropdown list cell
287 * @method utils.select_ui_generator
284 * @method utils.select_ui_generator
288 * @static
285 * @static
289 *
286 *
290 * @param list_list {list of sublist} List of sublist of metadata value and name in the dropdown list.
287 * @param list_list {list of sublist} List of sublist of metadata value and name in the dropdown list.
291 * subslit shoud contain 2 element each, first a string that woul be displayed in the dropdown list,
288 * subslit shoud contain 2 element each, first a string that woul be displayed in the dropdown list,
292 * and second the corresponding value for the metadata to be passed to setter/return by getter.
289 * and second the corresponding value to be passed to setter/return by getter.
293 * @param setter {function( metadata_dict, newValue )}
290 * @param setter {function( cell, newValue )}
294 * A setter method to set the newValue of the metadata dictionnary
291 * A setter method to set the newValue
295 * @param getter {function( metadata )}
292 * @param getter {function( cell )}
296 * A getter methods which return the current value of the metadata.
293 * A getter methods which return the current value of the metadata.
297 * @param [label=""] {String} optionnal label for the dropdown menu
294 * @param [label=""] {String} optionnal label for the dropdown menu
298 *
295 *
@@ -308,14 +305,14 b' var IPython = (function (IPython) {'
308 * ["Skip" ,"skip" ],
305 * ["Skip" , "skip" ],
309 * ],
306 * ],
310 * // setter
307 * // setter
311 * function(metadata,value){
308 * function(cell, value){
312 * // we check that the slideshow namespace exist and create it if needed
309 * // we check that the slideshow namespace exist and create it if needed
313 * if (metadata.slideshow == undefined){metadata.slideshow = {}}
310 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
314 * // set the value
311 * // set the value
315 * metadata.slideshow.slide_type = value
312 * cell.metadata.slideshow.slide_type = value
316 * },
313 * },
317 * //geter
314 * //geter
318 * function(metadata){ var ns = metadata.slideshow;
315 * function(cell){ var ns = cell.metadata.slideshow;
319 * // if the slideshow namespace does not exist return `undefined`
316 * // if the slideshow namespace does not exist return `undefined`
320 * // (will be interpreted as `false` by checkbox) otherwise
317 * // (will be interpreted as `false` by checkbox) otherwise
321 * // return the value
318 * // return the value
@@ -336,10 +333,9 b' var IPython = (function (IPython) {'
336 opt.text(list_list[itemn][0])
333 opt.text(list_list[itemn][0])
337 select.append(opt);
334 select.append(opt);
338 }
335 }
339 select.val(getter(cell.metadata));
336 select.val(getter(cell));
340
341 select.change(function(){
337 select.change(function(){
342 setter(cell.metadata,select.val());
338 setter(cell, select.val());
343 });
339 });
344 button_container.append($('<div/>').append(lbl).append(select));
340 button_container.append($('<div/>').append(lbl).append(select));
345
341
@@ -142,41 +142,48 b''
142 CellToolbar.register_callback('example.toggle',toggle_test);
142 CellToolbar.register_callback('example.toggle',toggle_test);
143 example_preset.push('example.toggle');
143 example_preset.push('example.toggle');
144
144
145 var checkbox_test = function(div, cell) {
145 var checkbox_test = CellToolbar.utils.checkbox_ui_generator('Yes/No',
146 var button_container = $(div)
146 // setter
147
147 function(cell, value){
148 var chkb = $('<input/>').attr('type','checkbox');
148 // we check that the slideshow namespace exist and create it if needed
149 var lbl = $('<label/>').append($('<span/>').text('bar :').css('font-size','77%'));
149 if (cell.metadata.yn_test == undefined){cell.metadata.yn_test = {}}
150 lbl.append(chkb);
150 // set the value
151 chkb.attr("checked",cell.metadata.bar);
151 cell.metadata.yn_test.value = value
152 chkb.click(function(){
152 },
153 var v = cell.metadata.bar;
153 //geter
154 cell.metadata.bar = !v;
154 function(cell){ var ns = cell.metadata.yn_test;
155 chkb.attr("checked",!v);
155 // if the slideshow namespace does not exist return `undefined`
156 })
156 // (will be interpreted as `false` by checkbox) otherwise
157 button_container.append($('<div/>').append(lbl));
157 // return the value
158
158 return (ns == undefined)? undefined: ns.value
159 }
159 }
160 );
161
160
162
161 CellToolbar.register_callback('example.checkbox',checkbox_test);
163 CellToolbar.register_callback('example.checkbox',checkbox_test);
162 example_preset.push('example.checkbox');
164 example_preset.push('example.checkbox');
163
165
164 var select_test = function(div, cell) {
166 var select_test = CellToolbar.utils.select_ui_generator([
165 var button_container = $(div)
167 ["-" ,undefined ],
166
168 ["Header Slide" ,"header_slide" ],
167 var select = $('<select/>');
169 ["Slide" ,"slide" ],
168
170 ["Fragment" ,"fragment" ],
169 select.append($('<option/>').attr('value','foo').text('foo'));
171 ["Skip" ,"skip" ],
170 select.append($('<option/>').attr('value','bar').text('bar'));
172 ],
171 select.append($('<option/>').attr('value','qux').text('qux'));
173 // setter
172 select.append($('<option/>').attr('value','zip').text('zip'));
174 function(cell,value){
173 select.val(cell.metadata.option);
175 // we check that the slideshow namespace exist and create it if needed
174 select.change(function(){
176 if (cell.metadata.test == undefined){cell.metadata.test = {}}
175 cell.metadata.option = select.val();
177 // set the value
178 cell.metadata.test.slide_type = value
179 },
180 //geter
181 function(cell){ var ns = cell.metadata.test;
182 // if the slideshow namespace does not exist return `undefined`
183 // (will be interpreted as `false` by checkbox) otherwise
184 // return the value
185 return (ns == undefined)? undefined: ns.slide_type
176 });
186 });
177 button_container.append($('<div/>').append(select));
178
179 }
180
187
181 CellToolbar.register_callback('example.select',select_test);
188 CellToolbar.register_callback('example.select',select_test);
182 example_preset.push('example.select');
189 example_preset.push('example.select');
General Comments 0
You need to be logged in to leave comments. Login now