##// END OF EJS Templates
allow cell toolbar presets to apply only to specific cell types
MinRK -
Show More
@@ -91,7 +91,7 b' var IPython = (function (IPython) {'
91
91
92
92
93 /**
93 /**
94 * Class variable that should contains the CellToolbar instances for each
94 * Class variable that should contain the CellToolbar instances for each
95 * cell of the notebook
95 * cell of the notebook
96 *
96 *
97 * @private
97 * @private
@@ -99,17 +99,17 b' var IPython = (function (IPython) {'
99 * @static
99 * @static
100 * @type List
100 * @type List
101 */
101 */
102 CellToolbar._instances =[]
102 CellToolbar._instances = [];
103
103
104
104
105 /**
105 /**
106 * keep a list of all the availlabel presets for the toolbar
106 * keep a list of all the available presets for the toolbar
107 * @private
107 * @private
108 * @property _presets
108 * @property _presets
109 * @static
109 * @static
110 * @type Dict
110 * @type Dict
111 */
111 */
112 CellToolbar._presets ={}
112 CellToolbar._presets = {};
113
113
114
114
115 // this is by design not a prototype.
115 // this is by design not a prototype.
@@ -182,7 +182,7 b' var IPython = (function (IPython) {'
182 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
182 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
183 */
183 */
184 CellToolbar.register_preset = function(name, preset_list) {
184 CellToolbar.register_preset = function(name, preset_list) {
185 CellToolbar._presets[name] = preset_list
185 CellToolbar._presets[name] = preset_list;
186 $([IPython.events]).trigger('preset_added.CellToolbar', {name: name});
186 $([IPython.events]).trigger('preset_added.CellToolbar', {name: name});
187 };
187 };
188
188
@@ -216,14 +216,14 b' var IPython = (function (IPython) {'
216 *
216 *
217 * CellToolbar.activate_preset('foo.foo_preset1');
217 * CellToolbar.activate_preset('foo.foo_preset1');
218 */
218 */
219 CellToolbar.activate_preset= function(preset_name){
219 CellToolbar.activate_preset = function(preset_name){
220 var preset = CellToolbar._presets[preset_name];
220 var preset = CellToolbar._presets[preset_name];
221
221
222 if(preset != undefined){
222 if(preset !== undefined){
223 CellToolbar._ui_controls_list = preset;
223 CellToolbar._ui_controls_list = preset;
224 CellToolbar.rebuild_all();
224 CellToolbar.rebuild_all();
225 }
225 }
226 }
226 };
227
227
228
228
229 /**
229 /**
@@ -237,29 +237,37 b' var IPython = (function (IPython) {'
237 for(var i in CellToolbar._instances){
237 for(var i in CellToolbar._instances){
238 CellToolbar._instances[i].rebuild();
238 CellToolbar._instances[i].rebuild();
239 }
239 }
240 }
240 };
241
241
242 /**
242 /**
243 * Rebuild all the button on the toolbar to update it's state.
243 * Rebuild all the button on the toolbar to update its state.
244 * @method rebuild
244 * @method rebuild
245 */
245 */
246 CellToolbar.prototype.rebuild = function(){
246 CellToolbar.prototype.rebuild = function(){
247 // strip evrything from the div
247 // strip evrything from the div
248 // which is probabli metainner.
248 // which is probably inner_element
249 // or this.element.
249 // or this.element.
250 this.inner_element.empty();
250 this.inner_element.empty();
251
251
252 var cdict = CellToolbar._callback_dict;
252 var callbacks = CellToolbar._callback_dict;
253 var preset = CellToolbar._ui_controls_list;
253 var preset = CellToolbar._ui_controls_list;
254 // Yes we iterate on the class varaible, not the instance one.
254 // Yes we iterate on the class variable, not the instance one.
255 for ( var index in CellToolbar._ui_controls_list){
255 for (var index in preset) {
256 var key = preset[index];
257 var callback = callbacks[key];
258 if (!callback) continue;
259
256 var local_div = $('<div/>').addClass('button_container');
260 var local_div = $('<div/>').addClass('button_container');
257 // Note,
261 try {
258 // do this the other way, wrap in try/catch and don't append if any errors.
262 callback(local_div, this.cell, this);
259 this.inner_element.append(local_div)
263 } catch (e) {
260 cdict[preset[index]](local_div, this.cell)
264 console.log("Error in cell toolbar callback " + key, e);
265 continue;
266 }
267 // only append if callback succeeded.
268 this.inner_element.append(local_div);
261 }
269 }
262 }
270 };
263
271
264
272
265 /**
273 /**
@@ -305,8 +313,8 b' var IPython = (function (IPython) {'
305 *
313 *
306 */
314 */
307 CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
315 CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
308 return function(div, cell) {
316 return function(div, cell, celltoolbar) {
309 var button_container = $(div)
317 var button_container = $(div);
310
318
311 var chkb = $('<input/>').attr('type', 'checkbox');
319 var chkb = $('<input/>').attr('type', 'checkbox');
312 var lbl = $('<label/>').append($('<span/>').text(name));
320 var lbl = $('<label/>').append($('<span/>').text(name));
@@ -317,11 +325,10 b' var IPython = (function (IPython) {'
317 var v = getter(cell);
325 var v = getter(cell);
318 setter(cell, !v);
326 setter(cell, !v);
319 chkb.attr("checked", !v);
327 chkb.attr("checked", !v);
320 })
328 });
321 button_container.append($('<div/>').append(lbl));
329 button_container.append($('<div/>').append(lbl));
322
330 };
323 }
331 };
324 }
325
332
326
333
327 /**
334 /**
@@ -367,16 +374,16 b' var IPython = (function (IPython) {'
367 * CellToolbar.register_callback('slideshow.select', select_type);
374 * CellToolbar.register_callback('slideshow.select', select_type);
368 *
375 *
369 */
376 */
370 CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label){
377 CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label, cell_types){
371 label= label? label: "";
378 label = label || "";
372 return function(div, cell) {
379 return function(div, cell, celltoolbar) {
373 var button_container = $(div)
380 var button_container = $(div);
374 var lbl = $("<label/>").append($('<span/>').text(label));
381 var lbl = $("<label/>").append($('<span/>').text(label));
375 var select = $('<select/>').addClass('ui-widget ui-widget-content');
382 var select = $('<select/>').addClass('ui-widget ui-widget-content');
376 for(var itemn in list_list){
383 for(var itemn in list_list){
377 var opt = $('<option/>');
384 var opt = $('<option/>')
378 opt.attr('value', list_list[itemn][1])
385 .attr('value', list_list[itemn][1])
379 opt.text(list_list[itemn][0])
386 .text(list_list[itemn][0]);
380 select.append(opt);
387 select.append(opt);
381 }
388 }
382 select.val(getter(cell));
389 select.val(getter(cell));
@@ -384,8 +391,13 b' var IPython = (function (IPython) {'
384 setter(cell, select.val());
391 setter(cell, select.val());
385 });
392 });
386 button_container.append($('<div/>').append(lbl).append(select));
393 button_container.append($('<div/>').append(lbl).append(select));
394 if (cell_types && cell_types.indexOf(cell.cell_type) == -1) {
395 celltoolbar.hide();
396 } else {
397 celltoolbar.show();
398 }
387
399
388 }
400 };
389 };
401 };
390
402
391
403
General Comments 0
You need to be logged in to leave comments. Login now