##// END OF EJS Templates
remove one useless element
Matthias BUSSONNIER -
Show More
@@ -1,394 +1,392
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2012 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // CellToolbar
10 10 //============================================================================
11 11
12 12
13 13 /**
14 14 * A Module to control the per-cell toolbar.
15 15 * @module IPython
16 16 * @namespace IPython
17 17 * @submodule CellToolbar
18 18 */
19 19 var IPython = (function (IPython) {
20 20 "use strict";
21 21
22 22 /**
23 23 * @constructor
24 24 * @class CellToolbar
25 25 * @param {The cell to attach the metadata UI to} cell
26 26 */
27 27 var CellToolbar = function (cell) {
28 28 CellToolbar._instances.push(this);
29 29 this.cell = cell;
30 30 this.create_element();
31 31 this.rebuild();
32 32 return this;
33 33 };
34 34
35 35
36 36 CellToolbar.prototype.create_element = function () {
37 this.inner_element = $('<div/>');
38 var ctb_element = $('<div/>').addClass('celltoolbar hbox reverse')
39 .append(this.inner_element);
37 this.inner_element = $('<div/>').addClass('celltoolbar hbox reverse')
40 38 this.element = $('<div/>').addClass('ctb_hideshow')
41 .append(ctb_element);
39 .append(this.inner_element);
42 40 };
43 41
44 42
45 43 // The default css style for the outer celltoolbar div
46 44 // (ctb_hideshow) is display: none. We add the ctb_show
47 45 // class to either 1) the body to show all cell's toolbars
48 46 // or 2) to the individual celltoolbar divs to show just one
49 47 // cell's toolbar.
50 48
51 49 CellToolbar.global_hide = function () {
52 50 $('body').removeClass('ctb_show');
53 51 }
54 52
55 53
56 54 CellToolbar.global_show = function () {
57 55 $('body').addClass('ctb_show');
58 56 }
59 57
60 58
61 59 CellToolbar.prototype.hide = function () {
62 60 this.element.removeClass('ctb_show');
63 61 }
64 62
65 63
66 64 CellToolbar.prototype.show = function () {
67 65 this.element.addClass('ctb_show');
68 66 }
69 67
70 68
71 69 /**
72 70 * Class variable that should contain a dict of all availlable callback
73 71 * we need to think of wether or not we allow nested namespace
74 72 * @property _callback_dict
75 73 * @private
76 74 * @static
77 75 * @type Dict
78 76 */
79 77 CellToolbar._callback_dict = {};
80 78
81 79
82 80 /**
83 81 * Class variable that should contain the reverse order list of the button
84 82 * to add to the toolbar of each cell
85 83 * @property _ui_controls_list
86 84 * @private
87 85 * @static
88 86 * @type List
89 87 */
90 88 CellToolbar._ui_controls_list = [];
91 89
92 90
93 91 /**
94 92 * Class variable that should contains the CellToolbar instances for each
95 93 * cell of the notebook
96 94 *
97 95 * @private
98 96 * @property _instances
99 97 * @static
100 98 * @type List
101 99 */
102 100 CellToolbar._instances =[]
103 101
104 102
105 103 /**
106 104 * keep a list of all the availlabel presets for the toolbar
107 105 * @private
108 106 * @property _presets
109 107 * @static
110 108 * @type Dict
111 109 */
112 110 CellToolbar._presets ={}
113 111
114 112
115 113 // this is by design not a prototype.
116 114 /**
117 115 * Register a callback to create an UI element in a cell toolbar.
118 116 * @method register_callback
119 117 * @param name {String} name to use to refer to the callback. It is advised to use a prefix with the name
120 118 * for easier sorting and avoid collision
121 119 * @param callback {function(div, cell)} callback that will be called to generate the ui element
122 120 *
123 121 *
124 122 * The callback will receive the following element :
125 123 *
126 124 * * a div in which to add element.
127 125 * * the cell it is responsible from
128 126 *
129 127 * @example
130 128 *
131 129 * Example that create callback for a button that toggle between `true` and `false` label,
132 130 * with the metadata under the key 'foo' to reflect the status of the button.
133 131 *
134 132 * // first param reference to a DOM div
135 133 * // second param reference to the cell.
136 134 * var toggle = function(div, cell) {
137 135 * var button_container = $(div)
138 136 *
139 137 * // let's create a button that show the current value of the metadata
140 138 * var button = $('<div/>').button({label:String(cell.metadata.foo)});
141 139 *
142 140 * // On click, change the metadata value and update the button label
143 141 * button.click(function(){
144 142 * var v = cell.metadata.foo;
145 143 * cell.metadata.foo = !v;
146 144 * button.button("option", "label", String(!v));
147 145 * })
148 146 *
149 147 * // add the button to the DOM div.
150 148 * button_container.append(button);
151 149 * }
152 150 *
153 151 * // now we register the callback under the name `foo` to give the
154 152 * // user the ability to use it later
155 153 * CellToolbar.register_callback('foo', toggle);
156 154 */
157 155 CellToolbar.register_callback = function(name, callback){
158 156 // Overwrite if it already exists.
159 157 CellToolbar._callback_dict[name] = callback;
160 158 };
161 159
162 160
163 161 /**
164 162 * Register a preset of UI element in a cell toolbar.
165 163 * Not supported Yet.
166 164 * @method register_preset
167 165 * @param name {String} name to use to refer to the preset. It is advised to use a prefix with the name
168 166 * for easier sorting and avoid collision
169 167 * @param preset_list {List of String} reverse order of the button in the toolbar. Each String of the list
170 168 * should correspond to a name of a registerd callback.
171 169 *
172 170 * @private
173 171 * @example
174 172 *
175 173 * CellToolbar.register_callback('foo.c1', function(div, cell){...});
176 174 * CellToolbar.register_callback('foo.c2', function(div, cell){...});
177 175 * CellToolbar.register_callback('foo.c3', function(div, cell){...});
178 176 * CellToolbar.register_callback('foo.c4', function(div, cell){...});
179 177 * CellToolbar.register_callback('foo.c5', function(div, cell){...});
180 178 *
181 179 * CellToolbar.register_preset('foo.foo_preset1', ['foo.c1', 'foo.c2', 'foo.c5'])
182 180 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
183 181 */
184 182 CellToolbar.register_preset = function(name, preset_list) {
185 183 CellToolbar._presets[name] = preset_list
186 184 $([IPython.events]).trigger('preset_added.CellToolbar', {name: name});
187 185 };
188 186
189 187
190 188 /**
191 189 * List the names of the presets that are currently registered.
192 190 *
193 191 * @method list_presets
194 192 * @static
195 193 */
196 194 CellToolbar.list_presets = function() {
197 195 var keys = [];
198 196 for (var k in CellToolbar._presets) {
199 197 keys.push(k);
200 198 }
201 199 return keys;
202 200 };
203 201
204 202
205 203 /**
206 204 * Activate an UI preset from `register_preset`
207 205 *
208 206 * This does not update the selection UI.
209 207 *
210 208 * @method activate_preset
211 209 * @param preset_name {String} string corresponding to the preset name
212 210 *
213 211 * @static
214 212 * @private
215 213 * @example
216 214 *
217 215 * CellToolbar.activate_preset('foo.foo_preset1');
218 216 */
219 217 CellToolbar.activate_preset= function(preset_name){
220 218 var preset = CellToolbar._presets[preset_name];
221 219
222 220 if(preset != undefined){
223 221 CellToolbar._ui_controls_list = preset;
224 222 CellToolbar.rebuild_all();
225 223 }
226 224 }
227 225
228 226
229 227 /**
230 228 * This should be called on the class and not on a instance as it will trigger
231 229 * rebuild of all the instances.
232 230 * @method rebuild_all
233 231 * @static
234 232 *
235 233 */
236 234 CellToolbar.rebuild_all = function(){
237 235 for(var i in CellToolbar._instances){
238 236 CellToolbar._instances[i].rebuild();
239 237 }
240 238 }
241 239
242 240 /**
243 241 * Rebuild all the button on the toolbar to update it's state.
244 242 * @method rebuild
245 243 */
246 244 CellToolbar.prototype.rebuild = function(){
247 245 // strip evrything from the div
248 246 // which is probabli metainner.
249 247 // or this.element.
250 248 this.inner_element.empty();
251 249
252 250 var cdict = CellToolbar._callback_dict;
253 251 var preset = CellToolbar._ui_controls_list;
254 252 // Yes we iterate on the class varaible, not the instance one.
255 253 for ( var index in CellToolbar._ui_controls_list){
256 254 var local_div = $('<div/>').addClass('button_container');
257 255 // Note,
258 256 // do this the other way, wrap in try/catch and don't append if any errors.
259 257 this.inner_element.append(local_div)
260 258 cdict[preset[index]](local_div, this.cell)
261 259 }
262 260 }
263 261
264 262
265 263 /**
266 264 */
267 265 CellToolbar.utils = {};
268 266
269 267
270 268 /**
271 269 * A utility function to generate bindings between a checkbox and cell/metadata
272 270 * @method utils.checkbox_ui_generator
273 271 * @static
274 272 *
275 273 * @param name {string} Label in front of the checkbox
276 274 * @param setter {function( cell, newValue )}
277 275 * A setter method to set the newValue
278 276 * @param getter {function( cell )}
279 277 * A getter methods which return the current value.
280 278 *
281 279 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
282 280 *
283 281 * @example
284 282 *
285 283 * An exmple that bind the subkey `slideshow.isSectionStart` to a checkbox with a `New Slide` label
286 284 *
287 285 * var newSlide = CellToolbar.utils.checkbox_ui_generator('New Slide',
288 286 * // setter
289 287 * function(cell, value){
290 288 * // we check that the slideshow namespace exist and create it if needed
291 289 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
292 290 * // set the value
293 291 * cell.metadata.slideshow.isSectionStart = value
294 292 * },
295 293 * //geter
296 294 * function(cell){ var ns = cell.metadata.slideshow;
297 295 * // if the slideshow namespace does not exist return `undefined`
298 296 * // (will be interpreted as `false` by checkbox) otherwise
299 297 * // return the value
300 298 * return (ns == undefined)? undefined: ns.isSectionStart
301 299 * }
302 300 * );
303 301 *
304 302 * CellToolbar.register_callback('newSlide', newSlide);
305 303 *
306 304 */
307 305 CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
308 306 return function(div, cell) {
309 307 var button_container = $(div)
310 308
311 309 var chkb = $('<input/>').attr('type', 'checkbox');
312 310 var lbl = $('<label/>').append($('<span/>').text(name));
313 311 lbl.append(chkb);
314 312 chkb.attr("checked", getter(cell));
315 313
316 314 chkb.click(function(){
317 315 var v = getter(cell);
318 316 setter(cell, !v);
319 317 chkb.attr("checked", !v);
320 318 })
321 319 button_container.append($('<div/>').append(lbl));
322 320
323 321 }
324 322 }
325 323
326 324
327 325 /**
328 326 * A utility function to generate bindings between a dropdown list cell
329 327 * @method utils.select_ui_generator
330 328 * @static
331 329 *
332 330 * @param list_list {list of sublist} List of sublist of metadata value and name in the dropdown list.
333 331 * subslit shoud contain 2 element each, first a string that woul be displayed in the dropdown list,
334 332 * and second the corresponding value to be passed to setter/return by getter.
335 333 * @param setter {function( cell, newValue )}
336 334 * A setter method to set the newValue
337 335 * @param getter {function( cell )}
338 336 * A getter methods which return the current value of the metadata.
339 337 * @param [label=""] {String} optionnal label for the dropdown menu
340 338 *
341 339 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
342 340 *
343 341 * @example
344 342 *
345 343 * var select_type = CellToolbar.utils.select_ui_generator([
346 344 * ["<None>" , undefined ],
347 345 * ["Header Slide" , "header_slide" ],
348 346 * ["Slide" , "slide" ],
349 347 * ["Fragment" , "fragment" ],
350 348 * ["Skip" , "skip" ],
351 349 * ],
352 350 * // setter
353 351 * function(cell, value){
354 352 * // we check that the slideshow namespace exist and create it if needed
355 353 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
356 354 * // set the value
357 355 * cell.metadata.slideshow.slide_type = value
358 356 * },
359 357 * //geter
360 358 * function(cell){ var ns = cell.metadata.slideshow;
361 359 * // if the slideshow namespace does not exist return `undefined`
362 360 * // (will be interpreted as `false` by checkbox) otherwise
363 361 * // return the value
364 362 * return (ns == undefined)? undefined: ns.slide_type
365 363 * }
366 364 * CellToolbar.register_callback('slideshow.select', select_type);
367 365 *
368 366 */
369 367 CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label){
370 368 label= label? label: "";
371 369 return function(div, cell) {
372 370 var button_container = $(div)
373 371 var lbl = $("<label/>").append($('<span/>').text(label));
374 372 var select = $('<select/>').addClass('ui-widget ui-widget-content');
375 373 for(var itemn in list_list){
376 374 var opt = $('<option/>');
377 375 opt.attr('value', list_list[itemn][1])
378 376 opt.text(list_list[itemn][0])
379 377 select.append(opt);
380 378 }
381 379 select.val(getter(cell));
382 380 select.change(function(){
383 381 setter(cell, select.val());
384 382 });
385 383 button_container.append($('<div/>').append(lbl).append(select));
386 384
387 385 }
388 386 };
389 387
390 388
391 389 IPython.CellToolbar = CellToolbar;
392 390
393 391 return IPython;
394 392 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now