##// END OF EJS Templates
slightly generalize utils generator
Matthias BUSSONNIER -
Show More
@@ -37,8 +37,8 b' var IPython = (function (IPython) {'
37
37
38 CellToolbar.dropdown_preset_element = $('<select/>')
38 CellToolbar.dropdown_preset_element = $('<select/>')
39 .addClass('ui-widget ui-widget-content')
39 .addClass('ui-widget ui-widget-content')
40 .attr('id','celltoolbar_selector')
40 .attr('id', 'celltoolbar_selector')
41 .append($('<option/>').attr('value','').text('-'))
41 .append($('<option/>').attr('value', '').text('-'))
42
42
43 CellToolbar.dropdown_preset_element.change(function(){
43 CellToolbar.dropdown_preset_element.change(function(){
44 var val = CellToolbar.dropdown_preset_element.val()
44 var val = CellToolbar.dropdown_preset_element.val()
@@ -117,7 +117,7 b' var IPython = (function (IPython) {'
117 * button.click(function(){
117 * button.click(function(){
118 * var v = cell.metadata.foo;
118 * var v = cell.metadata.foo;
119 * cell.metadata.foo = !v;
119 * cell.metadata.foo = !v;
120 * button.button("option","label",String(!v));
120 * button.button("option", "label", String(!v));
121 * })
121 * })
122 *
122 *
123 * // add the button to the DOM div.
123 * // add the button to the DOM div.
@@ -126,7 +126,7 b' var IPython = (function (IPython) {'
126 *
126 *
127 * // now we register the callback under the name `foo` to give the
127 * // now we register the callback under the name `foo` to give the
128 * // user the ability to use it later
128 * // user the ability to use it later
129 * CellToolbar.register_callback('foo',toggle);
129 * CellToolbar.register_callback('foo', toggle);
130 */
130 */
131 CellToolbar.register_callback = function(name, callback){
131 CellToolbar.register_callback = function(name, callback){
132 // what do we do if name already exist ?
132 // what do we do if name already exist ?
@@ -145,19 +145,19 b' var IPython = (function (IPython) {'
145 * @private
145 * @private
146 * @example
146 * @example
147 *
147 *
148 * CellToolbar.register_callback('foo.c1',function(div,cell){...});
148 * CellToolbar.register_callback('foo.c1', function(div, cell){...});
149 * CellToolbar.register_callback('foo.c2',function(div,cell){...});
149 * CellToolbar.register_callback('foo.c2', function(div, cell){...});
150 * CellToolbar.register_callback('foo.c3',function(div,cell){...});
150 * CellToolbar.register_callback('foo.c3', function(div, cell){...});
151 * CellToolbar.register_callback('foo.c4',function(div,cell){...});
151 * CellToolbar.register_callback('foo.c4', function(div, cell){...});
152 * CellToolbar.register_callback('foo.c5',function(div,cell){...});
152 * CellToolbar.register_callback('foo.c5', function(div, cell){...});
153 *
153 *
154 * CellToolbar.register_preset('foo.foo_preset1',['foo.c1','foo.c2','foo.c5'])
154 * CellToolbar.register_preset('foo.foo_preset1', ['foo.c1', 'foo.c2', 'foo.c5'])
155 * CellToolbar.register_preset('foo.foo_preset2',['foo.c4','foo.c5'])
155 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
156 */
156 */
157 CellToolbar.register_preset = function(name, preset_list){
157 CellToolbar.register_preset = function(name, preset_list){
158 CellToolbar._presets[name] = preset_list
158 CellToolbar._presets[name] = preset_list
159 CellToolbar.dropdown_preset_element.append(
159 CellToolbar.dropdown_preset_element.append(
160 $('<option/>').attr('value',name).text(name)
160 $('<option/>').attr('value', name).text(name)
161 )
161 )
162 }
162 }
163 /**
163 /**
@@ -213,29 +213,26 b' var IPython = (function (IPython) {'
213 // Note,
213 // Note,
214 // do this the other way, wrap in try/catch and don't append if any errors.
214 // do this the other way, wrap in try/catch and don't append if any errors.
215 this.inner_element.append(local_div)
215 this.inner_element.append(local_div)
216 cdict[preset[index]](local_div,this.cell)
216 cdict[preset[index]](local_div, this.cell)
217 }
217 }
218
218
219 }
219 }
220
220
221
222
223
224
221
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
@@ -263,19 +260,19 b' var IPython = (function (IPython) {'
263 * CellToolbar.register_callback('newSlide', newSlide);
260 * CellToolbar.register_callback('newSlide', newSlide);
264 *
261 *
265 */
262 */
266 CellToolbar.utils.checkbox_ui_generator = function(name,setter,getter){
263 CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
267 return function(div, cell) {
264 return function(div, cell) {
268 var button_container = $(div)
265 var button_container = $(div)
269
266
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));
281
278
@@ -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 *
@@ -301,45 +298,44 b' var IPython = (function (IPython) {'
301 * @example
298 * @example
302 *
299 *
303 * var select_type = CellToolbar.utils.select_ui_generator([
300 * var select_type = CellToolbar.utils.select_ui_generator([
304 * ["-" ,undefined ],
301 * ["-" , undefined ],
305 * ["Header Slide" ,"header_slide" ],
302 * ["Header Slide" , "header_slide" ],
306 * ["Slide" ,"slide" ],
303 * ["Slide" , "slide" ],
307 * ["Fragment" ,"fragment" ],
304 * ["Fragment" , "fragment" ],
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
322 * return (ns == undefined)? undefined: ns.slide_type
319 * return (ns == undefined)? undefined: ns.slide_type
323 * }
320 * }
324 * CellToolbar.register_callback('slideshow.select',select_type);
321 * CellToolbar.register_callback('slideshow.select', select_type);
325 *
322 *
326 */
323 */
327 CellToolbar.utils.select_ui_generator = function(list_list,setter, getter, label){
324 CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label){
328 label= label? label: "";
325 label= label? label: "";
329 return function(div, cell) {
326 return function(div, cell) {
330 var button_container = $(div)
327 var button_container = $(div)
331 var lbl = $("<label/>").append($('<span/>').text(label).css('font-size','77%'));
328 var lbl = $("<label/>").append($('<span/>').text(label).css('font-size', '77%'));
332 var select = $('<select/>');
329 var select = $('<select/>');
333 for(var itemn in list_list){
330 for(var itemn in list_list){
334 var opt = $('<option/>');
331 var opt = $('<option/>');
335 opt.attr('value',list_list[itemn][1])
332 opt.attr('value', list_list[itemn][1])
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 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 }
160 );
147
161
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
159 }
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