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