##// END OF EJS Templates
create metadata-dropdowm-menu generator
Matthias BUSSONNIER -
Show More
@@ -1,376 +1,443 b''
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 // MetaUI
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 MetaUI
18 18 */
19 19 var IPython = (function (IPython) {
20 20 "use strict";
21 21
22 22
23 23 /**
24 24 * @constructor
25 25 * @class MetaUI
26 26 * @param {The cell to attach the metadata UI to} cell
27 27 */
28 28 var MetaUI = function (cell) {
29 29 MetaUI._instances.push(this);
30 30 this.metainner = $('<div/>');
31 31 this.cell = cell;
32 32 this.element = $('<div/>').addClass('metaedit')
33 33 .append(this.metainner)
34 34 this.rebuild();
35 35 return this;
36 36 };
37 37
38 38 /**
39 39 * Class variable that should contain a dict of all availlable callback
40 40 * we need to think of wether or not we allow nested namespace
41 41 * @property _callback_dict
42 42 * @private
43 43 */
44 44 MetaUI._callback_dict = {};
45 45
46 46 /**
47 47 * Class variable that should contain the reverse order list of the button
48 48 * to add to the toolbar of each cell
49 49 * @property _button_list
50 50 * @private
51 51 */
52 52 MetaUI._button_list = [];
53 53
54 54 /**
55 55 * keep a list of all instances to
56 56 * be able to llop over them...
57 57 * but how to 'destroy' them ?
58 58 * have to think about it...
59 59 * or loop over the cells, and find their MetaUI instances.
60 60 * @private
61 61 * @property _instances
62 62 */
63 63 MetaUI._instances =[]
64 64
65 65 /**
66 66 * keep a list of all the availlabel presets for the toolbar
67 67 * @private
68 68 * @property _presets
69 69 */
70 70 MetaUI._presets ={}
71 71
72 72 // this is by design not a prototype.
73 73 /**
74 74 * Register a callback to create an UI element in a cell toolbar.
75 75 * @method register_callback
76 76 * @param name {String} name to use to refer to the callback. It is advised to use a prefix with the name
77 77 * for easier sorting and avoid collision
78 78 * @param callback {function(div, cell)} callback that will be called to generate the ui element
79 79 *
80 80 *
81 81 * The callback will receive the following element :
82 82 *
83 83 * * a div in which to add element.
84 84 * * the cell it is responsable from
85 85 *
86 86 * @example
87 87 *
88 88 * Example that create callback for a button that toggle between `true` and `false` label,
89 89 * with the metadata under the key 'foo' to reflect the status of the button.
90 90 *
91 91 * // first param reference to a DOM div
92 92 * // second param reference to the cell.
93 93 * var toggle = function(div, cell) {
94 94 * var button_container = $(div)
95 95 *
96 96 * // let's create a button that show the current value of the metadata
97 97 * var button = $('<div/>').button({label:String(cell.metadata.foo)});
98 98 *
99 99 * // On click, change the metadata value and update the button label
100 100 * button.click(function(){
101 101 * var v = cell.metadata.foo;
102 102 * cell.metadata.foo = !v;
103 103 * button.button("option","label",String(!v));
104 104 * })
105 105 *
106 106 * // add the button to the DOM div.
107 107 * button_container.append(button);
108 108 * }
109 109 *
110 110 * // now we register the callback under the name `foo` to give the
111 111 * // user the ability to use it later
112 112 * MetaUI.register_callback('foo',toggle);
113 113 */
114 114 MetaUI.register_callback = function(name, callback){
115 115 // what do we do if name already exist ?
116 116 MetaUI._callback_dict[name] = callback;
117 117 };
118 118
119 119 /**
120 120 * Register a preset of UI element in a cell toolbar.
121 121 * Not supported Yet.
122 122 * @method register_preset
123 123 * @param name {String} name to use to refer to the preset. It is advised to use a prefix with the name
124 124 * for easier sorting and avoid collision
125 125 * @param preset_list {List of String} reverse order of the button in the toolbar. Each String of the list
126 126 * should correspond to a name of a registerd callback.
127 127 *
128 128 * @private
129 129 * @example
130 130 *
131 131 * MetaUI.register_callback('foo.c1',function(div,cell){...});
132 132 * MetaUI.register_callback('foo.c2',function(div,cell){...});
133 133 * MetaUI.register_callback('foo.c3',function(div,cell){...});
134 134 * MetaUI.register_callback('foo.c4',function(div,cell){...});
135 135 * MetaUI.register_callback('foo.c5',function(div,cell){...});
136 136 *
137 137 * MetaUI.register_preset('foo.foo_preset1',['foo.c1','foo.c2','foo.c5'])
138 138 * MetaUI.register_preset('foo.foo_preset2',['foo.c4','foo.c5'])
139 139 */
140 140 MetaUI.register_preset = function(name, preset_list){
141 141 MetaUI._presets[name] = preset_list
142 142 }
143 143 /**
144 144 * set an UI preset from `register_preset`
145 145 * @method set_preset
146 146 * @param preset_name {String} string corresponding to the preset name
147 147 *
148 148 * @static
149 149 * @private
150 150 * @example
151 151 *
152 152 * MetaUI.set_preset('foo.foo_preset1');
153 153 */
154 154 MetaUI.set_preset= function(preset_name){
155 155 var preset = MetaUI._presets[preset_name];
156 156
157 157 if(preset != undefined){
158 158 MetaUI._button_list = preset;
159 159 MetaUI.rebuild_all();
160 160 }
161 161 }
162 162
163 163 // this is by design not a prototype.
164 164 /**
165 165 * This should be called on the class and not on a instance as it will trigger
166 166 * rebuild of all the instances.
167 167 * @method rebuild_all
168 168 * @static
169 169 *
170 170 */
171 171 MetaUI.rebuild_all = function(){
172 172 for(var i in MetaUI._instances){
173 173 MetaUI._instances[i].rebuild();
174 174 }
175 175 }
176 176
177 177 /**
178 178 * Rebuild all the button on the toolbar to update it's state.
179 179 * @method rebuild
180 180 */
181 181 MetaUI.prototype.rebuild = function(){
182 182 // strip evrything from the div
183 183 // which is probabli metainner.
184 184 // or this.element.
185 185 this.metainner.empty();
186 186 //this.add_raw_edit_button()
187 187
188 188
189 189 var cdict = MetaUI._callback_dict;
190 190 var preset = MetaUI._button_list;
191 191 // Yes we iterate on the class varaible, not the instance one.
192 192 for ( var index in MetaUI._button_list){
193 193 var local_div = $('<div/>').addClass('button_container');
194 194 // Note,
195 195 // do this the other way, wrap in try/catch and don't append if any errors.
196 196 this.metainner.append(local_div)
197 197 cdict[preset[index]](local_div,this.cell)
198 198 }
199 199
200 200 }
201 201
202 202 var raw_edit = function(cell){
203 203
204 204 var md = cell.metadata
205 205
206 206 var textarea = $('<textarea/>')
207 207 .attr('rows','13')
208 208 .attr('cols','75')
209 209 .attr('name','metadata')
210 210 .text(JSON.stringify(md, null,4)||'');
211 211 var dialogform = $('<div/>').attr('title','Edit the metadata')
212 212 .append(
213 213 $('<form/>').append(
214 214 $('<fieldset/>').append(
215 215 $('<label/>')
216 216 .attr('for','metadata')
217 217 .text("Metadata (I know what I'm dooing and I won't complain if it breaks my notebook)")
218 218 )
219 219 .append($('<br/>'))
220 220 .append(
221 221 textarea
222 222 )
223 223 )
224 224 );
225 225 var editor = CodeMirror.fromTextArea(textarea[0], {
226 226 lineNumbers: true,
227 227 matchBrackets: true,
228 228 });
229 229 $(dialogform).dialog({
230 230 autoOpen: true,
231 231 height: 300,
232 232 width: 650,
233 233 modal: true,
234 234 buttons: {
235 235 "Ok": function() {
236 236 //validate json and set it
237 237 try {
238 238 var json = JSON.parse(editor.getValue());
239 239 cell.metadata = json;
240 240 $( this ).dialog( "close" );
241 241 }
242 242 catch(e)
243 243 {
244 244 alert('invalid json');
245 245 }
246 246 },
247 247 Cancel: function() {
248 248 $( this ).dialog( "close" );
249 249 }
250 250 },
251 251 close: function() {
252 252 //cleanup on close
253 253 $(this).remove();
254 254 }
255 255 });
256 256 editor.refresh();
257 257 }
258 258
259 259 var add_raw_edit_button = function(div, cell) {
260 260 var button_container = $(div)
261 261 var button = $('<div/>').button({label:'Raw Edit'})
262 262 .click(function(){raw_edit(cell); return false;})
263 263 button_container.append(button);
264 264 }
265 265
266 266 MetaUI.register_callback('example.rawedit',add_raw_edit_button);
267 267 var example_preset = []
268 268 example_preset.push('example.rawedit');
269 269
270 270
271 271 var simple_button = function(div, cell) {
272 272 var button_container = $(div);
273 273 var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}});
274 274 var fun = function(value){
275 275 try{
276 276 if(value){
277 277 cell.code_mirror.setOption('readOnly','nocursor')
278 278 button.button('option','icons',{primary:'ui-icon-locked'})
279 279 } else {
280 280 cell.code_mirror.setOption('readOnly','false')
281 281 button.button('option','icons',{primary:'ui-icon-unlocked'})
282 282 }
283 283 } catch(e){}
284 284
285 285 }
286 286 fun(cell.metadata.ro)
287 287 button.click(function(){
288 288 var v = cell.metadata.ro;
289 289 var locked = !v;
290 290 cell.metadata.ro = locked;
291 291 fun(locked)
292 292 })
293 293 .css('height','16px')
294 294 .css('width','35px');
295 295 button_container.append(button);
296 296 }
297 297
298 298 MetaUI.register_callback('example.lock',simple_button);
299 299 example_preset.push('example.lock');
300 300
301 301 var toggle_test = function(div, cell) {
302 302 var button_container = $(div)
303 303 var button = $('<div/>').button({label:String(cell.metadata.foo)});
304 304 button.click(function(){
305 305 var v = cell.metadata.foo;
306 306 cell.metadata.foo = !v;
307 307 button.button("option","label",String(!v));
308 308 })
309 309 button_container.append(button);
310 310 }
311 311
312 312 MetaUI.register_callback('example.toggle',toggle_test);
313 313 example_preset.push('example.toggle');
314 314
315 315 /**
316 316 */
317 317 MetaUI.utils = {};
318 318
319 319 /**
320 320 * A utility function to generate bindings between a checkbox and metadata
321 321 * @method utils.checkbox_ui_generator
322 322 * @static
323 323 *
324 324 * @param name {string} Label in front of the checkbox
325 * @param setter {function( metadata_dict, newValue )} A setter method to set the newValue of the metadata dictionnary
326 * @param getter {function( metadata )} A getter methods which return the current value of the metadata.
325 * @param setter {function( metadata_dict, newValue )}
326 * A setter method to set the newValue of the metadata dictionnary
327 * @param getter {function( metadata )}
328 * A getter methods which return the current value of the metadata.
327 329 *
328 330 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
329 331 *
330 332 * @example
331 333 *
332 334 * An exmple that bind the subkey `slideshow.isSectionStart` to a checkbox with a `New Slide` label
333 335 *
334 336 * var newSlide = MetaUI.utils.checkbox_ui_generator('New Slide',
335 337 * // setter
336 338 * function(metadata,value){
337 339 * // we check that the slideshow namespace exist and create it if needed
338 340 * if (metadata.slideshow == undefined){metadata.slideshow = {}}
339 341 * // set the value
340 342 * metadata.slideshow.isSectionStart = value
341 343 * },
342 344 * //geter
343 345 * function(metadata){ var ns = metadata.slideshow;
344 346 * // if the slideshow namespace does not exist return `undefined`
345 347 * // (will be interpreted as `false` by checkbox) otherwise
346 348 * // return the value
347 349 * return (ns == undefined)? undefined: ns.isSectionStart
348 350 * }
349 351 * );
350 352 *
351 353 * MetaUI.register_callback('newSlide', newSlide);
352 354 *
353 355 */
354 356 MetaUI.utils.checkbox_ui_generator = function(name,setter,getter){
355 357 return function(div, cell) {
356 358 var button_container = $(div)
357 359
358 360 var chkb = $('<input/>').attr('type','checkbox');
359 361 var lbl = $('<label/>').append($('<span/>').text(name).css('font-size','77%'));
360 362 lbl.append(chkb);
361 363 chkb.attr("checked",getter(cell.metadata));
362 364
363 365 chkb.click(function(){
364 366 var v = getter(cell.metadata);
365 367 setter(cell.metadata,!v);
366 368 chkb.attr("checked",!v);
367 369 })
368 370 button_container.append($('<div/>').append(lbl));
369 371
370 372 }
371 373 }
372 374
375 /**
376 * A utility function to generate bindings between a dropdown list and metadata
377 * @method utils.select_ui_generator
378 * @static
379 *
380 * @param list_list {list of sublist} List of sublist of metadata value and name in the dropdown list.
381 * subslit shoud contain 2 element each, first a string that woul be displayed in the dropdown list,
382 * and second the corresponding value for the metadata to be passed to setter/return by getter.
383 * @param setter {function( metadata_dict, newValue )}
384 * A setter method to set the newValue of the metadata dictionnary
385 * @param getter {function( metadata )}
386 * A getter methods which return the current value of the metadata.
387 * @param [label=""] {String} optionnal label for the dropdown menu
388 *
389 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
390 *
391 * @example
392 *
393 * var select_type = MetaUI.utils.select_ui_generator([
394 * ["-" ,undefined ],
395 * ["Header Slide" ,"header_slide" ],
396 * ["Slide" ,"slide" ],
397 * ["Fragment" ,"fragment" ],
398 * ["Skip" ,"skip" ],
399 * ],
400 * // setter
401 * function(metadata,value){
402 * // we check that the slideshow namespace exist and create it if needed
403 * if (metadata.slideshow == undefined){metadata.slideshow = {}}
404 * // set the value
405 * metadata.slideshow.slide_type = value
406 * },
407 * //geter
408 * function(metadata){ var ns = metadata.slideshow;
409 * // if the slideshow namespace does not exist return `undefined`
410 * // (will be interpreted as `false` by checkbox) otherwise
411 * // return the value
412 * return (ns == undefined)? undefined: ns.slide_type
413 * }
414 * MetaUI.register_callback('slideshow.select',select_type);
415 *
416 */
417 MetaUI.utils.select_ui_generator = function(list_list,setter, getter, label){
418 label= label? label: "";
419 return function(div, cell) {
420 var button_container = $(div)
421 var lbl = $("<label/>").append($('<span/>').text(label).css('font-size','77%'));
422 var select = $('<select/>');
423 for(var itemn in list_list){
424 var opt = $('<option/>');
425 opt.attr('value',list_list[itemn][1])
426 opt.text(list_list[itemn][0])
427 select.append(opt);
428 }
429 select.val(getter(cell.metadata));
430
431 select.change(function(){
432 setter(cell.metadata,select.val());
433 });
434 button_container.append($('<div/>').append(lbl).append(select));
435
436 }
437 };
438
439
373 440 IPython.MetaUI = MetaUI;
374 441
375 442 return IPython;
376 443 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now