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