##// END OF EJS Templates
Split metadataui into multiple file...
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,184 b''
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2012 The IPython Development Team
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
8 //============================================================================
9 // MetaUI Example
10 //============================================================================
11
12 /**
13 * Example Use for the MetaUI library
14 * add the following to your custom.js to load
15 * metadata UI for slideshow
16 *
17 * ```
18 * $.getScript('/static/js/examples/metaui.example.js');
19 * ```
20 */
21 // IIFE without asignement, we don't modifiy the IPython namespace
22 (function (IPython) {
23 "use strict";
24
25 var MetaUI = IPython.MetaUI;
26
27
28 var raw_edit = function(cell){
29
30 var md = cell.metadata
31
32 var textarea = $('<textarea/>')
33 .attr('rows','13')
34 .attr('cols','75')
35 .attr('name','metadata')
36 .text(JSON.stringify(md, null,4)||'');
37 var dialogform = $('<div/>').attr('title','Edit the metadata')
38 .append(
39 $('<form/>').append(
40 $('<fieldset/>').append(
41 $('<label/>')
42 .attr('for','metadata')
43 .text("Metadata (I know what I'm dooing and I won't complain if it breaks my notebook)")
44 )
45 .append($('<br/>'))
46 .append(
47 textarea
48 )
49 )
50 );
51 var editor = CodeMirror.fromTextArea(textarea[0], {
52 lineNumbers: true,
53 matchBrackets: true,
54 });
55 $(dialogform).dialog({
56 autoOpen: true,
57 height: 300,
58 width: 650,
59 modal: true,
60 buttons: {
61 "Ok": function() {
62 //validate json and set it
63 try {
64 var json = JSON.parse(editor.getValue());
65 cell.metadata = json;
66 $( this ).dialog( "close" );
67 }
68 catch(e)
69 {
70 alert('invalid json');
71 }
72 },
73 Cancel: function() {
74 $( this ).dialog( "close" );
75 }
76 },
77 close: function() {
78 //cleanup on close
79 $(this).remove();
80 }
81 });
82 editor.refresh();
83 }
84
85 var add_raw_edit_button = function(div, cell) {
86 var button_container = $(div)
87 var button = $('<div/>').button({label:'Raw Edit'})
88 .click(function(){raw_edit(cell); return false;})
89 button_container.append(button);
90 }
91
92 MetaUI.register_callback('example.rawedit',add_raw_edit_button);
93 var example_preset = []
94 example_preset.push('example.rawedit');
95
96
97 var simple_button = function(div, cell) {
98 var button_container = $(div);
99 var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}});
100 var fun = function(value){
101 try{
102 if(value){
103 cell.code_mirror.setOption('readOnly','nocursor')
104 button.button('option','icons',{primary:'ui-icon-locked'})
105 } else {
106 cell.code_mirror.setOption('readOnly','false')
107 button.button('option','icons',{primary:'ui-icon-unlocked'})
108 }
109 } catch(e){}
110
111 }
112 fun(cell.metadata.ro)
113 button.click(function(){
114 var v = cell.metadata.ro;
115 var locked = !v;
116 cell.metadata.ro = locked;
117 fun(locked)
118 })
119 .css('height','16px')
120 .css('width','35px');
121 button_container.append(button);
122 }
123
124 MetaUI.register_callback('example.lock',simple_button);
125 example_preset.push('example.lock');
126
127 var toggle_test = function(div, cell) {
128 var button_container = $(div)
129 var button = $('<div/>').button({label:String(cell.metadata.foo)});
130 button.click(function(){
131 var v = cell.metadata.foo;
132 cell.metadata.foo = !v;
133 button.button("option","label",String(!v));
134 })
135 button_container.append(button);
136 }
137
138 MetaUI.register_callback('example.toggle',toggle_test);
139 example_preset.push('example.toggle');
140
141 var checkbox_test = function(div, cell) {
142 var button_container = $(div)
143
144 var chkb = $('<input/>').attr('type','checkbox');
145 var lbl = $('<label/>').append($('<span/>').text('bar :').css('font-size','77%'));
146 lbl.append(chkb);
147 chkb.attr("checked",cell.metadata.bar);
148 chkb.click(function(){
149 var v = cell.metadata.bar;
150 cell.metadata.bar = !v;
151 chkb.attr("checked",!v);
152 })
153 button_container.append($('<div/>').append(lbl));
154
155 }
156
157 MetaUI.register_callback('example.checkbox',checkbox_test);
158 example_preset.push('example.checkbox');
159
160 var select_test = function(div, cell) {
161 var button_container = $(div)
162
163 var select = $('<select/>');
164
165 select.append($('<option/>').attr('value','foo').text('foo'));
166 select.append($('<option/>').attr('value','bar').text('bar'));
167 select.append($('<option/>').attr('value','qux').text('qux'));
168 select.append($('<option/>').attr('value','zip').text('zip'));
169 select.val(cell.metadata.option);
170 select.change(function(){
171 cell.metadata.option = select.val();
172 });
173 button_container.append($('<div/>').append(select));
174
175 }
176
177 MetaUI.register_callback('example.select',select_test);
178 example_preset.push('example.select');
179
180 MetaUI.register_preset('example',example_preset);
181 MetaUI.set_preset('example');
182 console.log('Example extension for metadata editting loaded.');
183
184 }(IPython));
@@ -0,0 +1,63 b''
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2012 The IPython Development Team
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
8 //============================================================================
9 // MetaUI Example
10 //============================================================================
11
12 /**
13 * Example Use for the MetaUI library
14 * add the following to your custom.js to load
15 * metadata UI for slideshow
16 *
17 * ```
18 * $.getScript('/static/js/examples/metaui.slideshow.js');
19 * ```
20 */
21 // IIFE without asignement, we don't modifiy the IPython namespace
22 (function (IPython) {
23 "use strict";
24
25 var MetaUI = IPython.MetaUI;
26 var slideshow_preset = [];
27
28 var hslide = MetaUI.utils.checkbox_ui_generator('New Section',
29 function(md,value){
30 if (md.slideshow == undefined){md.slideshow = {}}
31 return md.slideshow.new_section = value},
32 function(md){ var ns = md.slideshow;
33 return (ns == undefined)? undefined: ns.new_section});
34
35 var vslide = MetaUI.utils.checkbox_ui_generator('New Subsection',
36 function(md,value){
37 if (md.slideshow == undefined){md.slideshow = {}}
38 return md.slideshow.new_subsection = value},
39 function(md){ var ns = md.slideshow;
40 return (ns == undefined)? undefined: ns.new_subsection});
41
42 var fragment = MetaUI.utils.checkbox_ui_generator('New Fragment',
43 function(md,value){
44 if (md.slideshow == undefined){md.slideshow = {}}
45 return md.slideshow.new_fragment = value},
46 function(md){ var ns = md.slideshow;
47 return (ns == undefined)? undefined: ns.new_fragment});
48
49
50 MetaUI.register_callback('slideshow.hslide',hslide);
51 MetaUI.register_callback('slideshow.vslide',vslide);
52 MetaUI.register_callback('slideshow.fragment',fragment);
53
54 slideshow_preset.push('slideshow.fragment');
55 slideshow_preset.push('slideshow.vslide');
56 slideshow_preset.push('slideshow.hslide');
57
58
59 MetaUI.register_preset('slideshow',slideshow_preset);
60 MetaUI.set_preset('slideshow');
61 console.log('Slideshow extension for metadata editting loaded.');
62
63 }(IPython));
@@ -1,35 +1,47 b''
1 // leave at least 2 line with only a star on it below, or doc generation fails
1 // leave at least 2 line with only a star on it below, or doc generation fails
2 /**
2 /**
3 *
3 *
4 *
4 *
5 * Placeholder for custom user javascript
5 * Placeholder for custom user javascript
6 * mainly to be overridden in profile/static/js/custom.js
6 * mainly to be overridden in profile/static/js/custom.js
7 * This will always be an empty file in IPython
7 * This will always be an empty file in IPython
8 *
8 *
9 * User could add any javascript in the `profile/static/js/custom.js` file
9 * User could add any javascript in the `profile/static/js/custom.js` file
10 * (and should create it if it does not exist).
10 * (and should create it if it does not exist).
11 * It will be executed by the ipython notebook at load time.
11 * It will be executed by the ipython notebook at load time.
12 *
12 *
13 * Same thing with `profile/static/css/custom.css` to inject custom css into the notebook.
13 * Same thing with `profile/static/css/custom.css` to inject custom css into the notebook.
14 *
14 *
15 * Example :
15 * Example :
16 *
16 *
17 * Create a custom button in toolbar that execute `%qtconsole` in kernel
17 * Create a custom button in toolbar that execute `%qtconsole` in kernel
18 * and hence open a qtconsole attached to the same kernel as the current notebook
18 * and hence open a qtconsole attached to the same kernel as the current notebook
19 *
19 *
20 * $([IPython.events]).on('notebook_loaded.Notebook', function(){
20 * $([IPython.events]).on('notebook_loaded.Notebook', function(){
21 * IPython.toolbar.add_buttons_group([
21 * IPython.toolbar.add_buttons_group([
22 * {
22 * {
23 * 'label' : 'run qtconsole',
23 * 'label' : 'run qtconsole',
24 * 'icon' : 'ui-icon-calculator', // select your icon from http://jqueryui.com/themeroller/
24 * 'icon' : 'ui-icon-calculator', // select your icon from http://jqueryui.com/themeroller/
25 * 'callback': function(){IPython.notebook.kernel.execute('%qtconsole')}
25 * 'callback': function(){IPython.notebook.kernel.execute('%qtconsole')}
26 * }
26 * }
27 * // add more button here if needed.
27 * // add more button here if needed.
28 * ]);
28 * ]);
29 * });
29 * });
30 *
30 *
31 * Example :
32 *
33 * Use `jQuery.getScript(url [, success(script, textStatus, jqXHR)] );`
34 * to load custom script into the notebook.
35 *
36 * // to load the metadata ui extension example.
37 * $.getScript('/static/js/examples/metaui.example.js');
38 * // or
39 * // to load the metadata ui extension to control slideshow mode / reveal js for nbconvert
40 * $.getScript('/static/js/examples/metaui.slideshow.js');
41 *
42 *
31 * @module IPython
43 * @module IPython
32 * @namespace IPython
44 * @namespace IPython
33 * @class customjs
45 * @class customjs
34 * @static
46 * @static
35 */
47 */
@@ -1,449 +1,376 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 )} A setter method to set the newValue of the metadata dictionnary
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.
326 * @param getter {function( metadata )} A getter methods which return the current value of the metadata.
327 *
327 *
328 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
328 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
329 *
329 *
330 * @example
330 * @example
331 *
331 *
332 * An exmple that bind the subkey `slideshow.isSectionStart` to a checkbox with a `New Slide` label
332 * An exmple that bind the subkey `slideshow.isSectionStart` to a checkbox with a `New Slide` label
333 *
333 *
334 * var newSlide = MetaUI.utils.checkbox_ui_generator('New Slide',
334 * var newSlide = MetaUI.utils.checkbox_ui_generator('New Slide',
335 * // setter
335 * // setter
336 * function(metadata,value){
336 * function(metadata,value){
337 * // we check that the slideshow namespace exist and create it if needed
337 * // we check that the slideshow namespace exist and create it if needed
338 * if (metadata.slideshow == undefined){metadata.slideshow = {}}
338 * if (metadata.slideshow == undefined){metadata.slideshow = {}}
339 * // set the value
339 * // set the value
340 * metadata.slideshow.isSectionStart = value
340 * metadata.slideshow.isSectionStart = value
341 * },
341 * },
342 * //geter
342 * //geter
343 * function(metadata){ var ns = metadata.slideshow;
343 * function(metadata){ var ns = metadata.slideshow;
344 * // if the slideshow namespace does not exist return `undefined`
344 * // if the slideshow namespace does not exist return `undefined`
345 * // (will be interpreted as `false` by checkbox) otherwise
345 * // (will be interpreted as `false` by checkbox) otherwise
346 * // return the value
346 * // return the value
347 * return (ns == undefined)? undefined: ns.isSectionStart
347 * return (ns == undefined)? undefined: ns.isSectionStart
348 * }
348 * }
349 * );
349 * );
350 *
350 *
351 * MetaUI.register_callback('newSlide', newSlide);
351 * MetaUI.register_callback('newSlide', newSlide);
352 *
352 *
353 */
353 */
354 MetaUI.utils.checkbox_ui_generator = function(name,setter,getter){
354 MetaUI.utils.checkbox_ui_generator = function(name,setter,getter){
355 return function(div, cell) {
355 return function(div, cell) {
356 var button_container = $(div)
356 var button_container = $(div)
357
357
358 var chkb = $('<input/>').attr('type','checkbox');
358 var chkb = $('<input/>').attr('type','checkbox');
359 var lbl = $('<label/>').append($('<span/>').text(name).css('font-size','77%'));
359 var lbl = $('<label/>').append($('<span/>').text(name).css('font-size','77%'));
360 lbl.append(chkb);
360 lbl.append(chkb);
361 chkb.attr("checked",getter(cell.metadata));
361 chkb.attr("checked",getter(cell.metadata));
362
362
363 chkb.click(function(){
363 chkb.click(function(){
364 var v = getter(cell.metadata);
364 var v = getter(cell.metadata);
365 setter(cell.metadata,!v);
365 setter(cell.metadata,!v);
366 chkb.attr("checked",!v);
366 chkb.attr("checked",!v);
367 })
367 })
368 button_container.append($('<div/>').append(lbl));
368 button_container.append($('<div/>').append(lbl));
369
369
370 }
370 }
371 }
371 }
372 var hslide = MetaUI.utils.checkbox_ui_generator('New Section',
373 function(md,value){
374 if (md.slideshow == undefined){md.slideshow = {}}
375 return md.slideshow.new_section = value},
376 function(md){ var ns = md.slideshow;
377 return (ns == undefined)? undefined: ns.new_section});
378
379 var vslide = MetaUI.utils.checkbox_ui_generator('New Subsection',
380 function(md,value){
381 if (md.slideshow == undefined){md.slideshow = {}}
382 return md.slideshow.new_subsection = value},
383 function(md){ var ns = md.slideshow;
384 return (ns == undefined)? undefined: ns.new_subsection});
385
386 var fragment = MetaUI.utils.checkbox_ui_generator('New Fragment',
387 function(md,value){
388 if (md.slideshow == undefined){md.slideshow = {}}
389 return md.slideshow.new_fragment = value},
390 function(md){ var ns = md.slideshow;
391 return (ns == undefined)? undefined: ns.new_fragment});
392
393
394 MetaUI.register_callback('slideshow.hslide',hslide);
395 MetaUI.register_callback('slideshow.vslide',vslide);
396 MetaUI.register_callback('slideshow.fragment',fragment);
397
398 example_preset.push('slideshow.fragment');
399 example_preset.push('slideshow.vslide');
400 example_preset.push('slideshow.hslide');
401
402
403 var checkbox_test = function(div, cell) {
404 var button_container = $(div)
405
406 var chkb = $('<input/>').attr('type','checkbox');
407 var lbl = $('<label/>').append($('<span/>').text('bar :').css('font-size','77%'));
408 lbl.append(chkb);
409 chkb.attr("checked",cell.metadata.bar);
410 chkb.click(function(){
411 var v = cell.metadata.bar;
412 cell.metadata.bar = !v;
413 chkb.attr("checked",!v);
414 })
415 button_container.append($('<div/>').append(lbl));
416
417 }
418
419 MetaUI.register_callback('example.checkbox',checkbox_test);
420 example_preset.push('example.checkbox');
421
422 var select_test = function(div, cell) {
423 var button_container = $(div)
424
425 var select = $('<select/>');
426
427 select.append($('<option/>').attr('value','foo').text('foo'));
428 select.append($('<option/>').attr('value','bar').text('bar'));
429 select.append($('<option/>').attr('value','qux').text('qux'));
430 select.append($('<option/>').attr('value','zip').text('zip'));
431 select.val(cell.metadata.option);
432 select.change(function(){
433 cell.metadata.option = select.val();
434 });
435 button_container.append($('<div/>').append(select));
436
437 }
438
439 MetaUI.register_callback('example.select',select_test);
440 example_preset.push('example.select');
441
442 MetaUI.register_preset('example',example_preset);
443 MetaUI.register_preset('foo',['example.select','example.select']);
444 MetaUI.set_preset('example');
445
372
446 IPython.MetaUI = MetaUI;
373 IPython.MetaUI = MetaUI;
447
374
448 return IPython;
375 return IPython;
449 }(IPython));
376 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now