##// END OF EJS Templates
add default celltoolbar UI
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,94 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 // CellToolbar Default
10 //============================================================================
11
12 /**
13 * Example Use for the CellToolbar library
14 */
15 // IIFE without asignement, we don't modifiy the IPython namespace
16 (function (IPython) {
17 "use strict";
18
19 var CellToolbar = IPython.CellToolbar;
20
21 var raw_edit = function(cell){
22
23 var md = cell.metadata
24 var error_div = $('<div/>').css('color','red')
25
26 var textarea = $('<textarea/>')
27 .attr('rows','13')
28 .attr('cols','75')
29 .attr('name','metadata')
30 .text(JSON.stringify(md, null,4)||'');
31 var dialogform = $('<div/>').attr('title','Edit the metadata')
32 .append(
33 $('<form/>').append(
34 $('<fieldset/>').append(
35 $('<label/>')
36 .attr('for','metadata')
37 .text("Metadata (I know what I'm dooing and I won't complain if it breaks my notebook)")
38 )
39 .append(error_div)
40 .append($('<br/>'))
41 .append(
42 textarea
43 )
44 )
45 );
46 var editor = CodeMirror.fromTextArea(textarea[0], {
47 lineNumbers: true,
48 matchBrackets: true,
49 });
50 $(dialogform).dialog({
51 autoOpen: true,
52 height: 300,
53 width: 650,
54 modal: true,
55 buttons: {
56 "Ok": function() {
57 //validate json and set it
58 try {
59 var json = JSON.parse(editor.getValue());
60 cell.metadata = json;
61 $( this ).dialog( "close" );
62 }
63 catch(e)
64 {
65 error_div.text('Warning, invalid json, not saved');
66 }
67 },
68 Cancel: function() {
69 $( this ).dialog( "close" );
70 }
71 },
72 close: function() {
73 //cleanup on close
74 $(this).remove();
75 }
76 });
77 editor.refresh();
78 }
79
80 var add_raw_edit_button = function(div, cell) {
81 var button_container = div
82 var button = $('<div/>').button({label:'Raw Edit'})
83 .click(function(){raw_edit(cell); return false;})
84 button_container.append(button);
85 }
86
87 CellToolbar.register_callback('default.rawedit',add_raw_edit_button);
88 var example_preset = []
89 example_preset.push('default.rawedit');
90
91 CellToolbar.register_preset('default',example_preset);
92 console.log('Default extension for metadata editting loaded.');
93
94 }(IPython));
@@ -1,224 +1,152 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 // CellToolbar Example
9 // CellToolbar Example
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * Example Use for the CellToolbar library
13 * Example Use for the CellToolbar library
14 * add the following to your custom.js to load
14 * add the following to your custom.js to load
15 * Celltoolbar UI for slideshow
15 * Celltoolbar UI for slideshow
16 *
16 *
17 * ```
17 * ```
18 * $.getScript('/static/js/examples/celltoolbar.example.js');
18 * $.getScript('/static/js/examples/celltoolbar.example.js');
19 * ```
19 * ```
20 */
20 */
21 // IIFE without asignement, we don't modifiy the IPython namespace
21 // IIFE without asignement, we don't modifiy the IPython namespace
22 (function (IPython) {
22 (function (IPython) {
23 "use strict";
23 "use strict";
24
24
25 var CellToolbar = IPython.CellToolbar;
25 var CellToolbar = IPython.CellToolbar;
26
26
27
28 var raw_edit = function(cell){
29
30 var md = cell.metadata
31 var error_div = $('<div/>').css('color','red')
32
33 var textarea = $('<textarea/>')
34 .attr('rows','13')
35 .attr('cols','75')
36 .attr('name','metadata')
37 .text(JSON.stringify(md, null,4)||'');
38 var dialogform = $('<div/>').attr('title','Edit the metadata')
39 .append(
40 $('<form/>').append(
41 $('<fieldset/>').append(
42 $('<label/>')
43 .attr('for','metadata')
44 .text("Metadata (I know what I'm dooing and I won't complain if it breaks my notebook)")
45 )
46 .append(error_div)
47 .append($('<br/>'))
48 .append(
49 textarea
50 )
51 )
52 );
53 var editor = CodeMirror.fromTextArea(textarea[0], {
54 lineNumbers: true,
55 matchBrackets: true,
56 });
57 $(dialogform).dialog({
58 autoOpen: true,
59 height: 300,
60 width: 650,
61 modal: true,
62 buttons: {
63 "Ok": function() {
64 //validate json and set it
65 try {
66 var json = JSON.parse(editor.getValue());
67 cell.metadata = json;
68 $( this ).dialog( "close" );
69 }
70 catch(e)
71 {
72 error_div.text('Warning, invalid json, not saved');
73 }
74 },
75 Cancel: function() {
76 $( this ).dialog( "close" );
77 }
78 },
79 close: function() {
80 //cleanup on close
81 $(this).remove();
82 }
83 });
84 editor.refresh();
85 }
86
87 var add_raw_edit_button = function(div, cell) {
88 var button_container = div
89 var button = $('<div/>').button({label:'Raw Edit'})
90 .click(function(){raw_edit(cell); return false;})
91 button_container.append(button);
92 }
93
94 CellToolbar.register_callback('example.rawedit',add_raw_edit_button);
95 var example_preset = []
96 example_preset.push('example.rawedit');
97
98
99 var simple_button = function(div, cell) {
27 var simple_button = function(div, cell) {
100 var button_container = $(div);
28 var button_container = $(div);
101 var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}});
29 var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}});
102 var fun = function(value){
30 var fun = function(value){
103 try{
31 try{
104 if(value){
32 if(value){
105 cell.code_mirror.setOption('readOnly','nocursor')
33 cell.code_mirror.setOption('readOnly','nocursor')
106 button.button('option','icons',{primary:'ui-icon-locked'})
34 button.button('option','icons',{primary:'ui-icon-locked'})
107 } else {
35 } else {
108 cell.code_mirror.setOption('readOnly',false)
36 cell.code_mirror.setOption('readOnly',false)
109 button.button('option','icons',{primary:'ui-icon-unlocked'})
37 button.button('option','icons',{primary:'ui-icon-unlocked'})
110 }
38 }
111 } catch(e){}
39 } catch(e){}
112
40
113 }
41 }
114 fun(cell.metadata.ro)
42 fun(cell.metadata.ro)
115 button.click(function(){
43 button.click(function(){
116 var v = cell.metadata.ro;
44 var v = cell.metadata.ro;
117 var locked = !v;
45 var locked = !v;
118 cell.metadata.ro = locked;
46 cell.metadata.ro = locked;
119 fun(locked)
47 fun(locked)
120 })
48 })
121 .css('height','16px')
49 .css('height','16px')
122 .css('width','35px');
50 .css('width','35px');
123 button_container.append(button);
51 button_container.append(button);
124 }
52 }
125
53
126 CellToolbar.register_callback('example.lock',simple_button);
54 CellToolbar.register_callback('example.lock',simple_button);
127 example_preset.push('example.lock');
55 example_preset.push('example.lock');
128
56
129 var toggle_test = function(div, cell) {
57 var toggle_test = function(div, cell) {
130 var button_container = $(div)
58 var button_container = $(div)
131 var button = $('<div/>')
59 var button = $('<div/>')
132 .button({label:String(cell.metadata.foo)}).
60 .button({label:String(cell.metadata.foo)}).
133 css('width','65px');
61 css('width','65px');
134 button.click(function(){
62 button.click(function(){
135 var v = cell.metadata.foo;
63 var v = cell.metadata.foo;
136 cell.metadata.foo = !v;
64 cell.metadata.foo = !v;
137 button.button("option","label",String(!v));
65 button.button("option","label",String(!v));
138 })
66 })
139 button_container.append(button);
67 button_container.append(button);
140 }
68 }
141
69
142 CellToolbar.register_callback('example.toggle',toggle_test);
70 CellToolbar.register_callback('example.toggle',toggle_test);
143 example_preset.push('example.toggle');
71 example_preset.push('example.toggle');
144
72
145 var checkbox_test = CellToolbar.utils.checkbox_ui_generator('Yes/No',
73 var checkbox_test = CellToolbar.utils.checkbox_ui_generator('Yes/No',
146 // setter
74 // setter
147 function(cell, value){
75 function(cell, value){
148 // we check that the slideshow namespace exist and create it if needed
76 // we check that the slideshow namespace exist and create it if needed
149 if (cell.metadata.yn_test == undefined){cell.metadata.yn_test = {}}
77 if (cell.metadata.yn_test == undefined){cell.metadata.yn_test = {}}
150 // set the value
78 // set the value
151 cell.metadata.yn_test.value = value
79 cell.metadata.yn_test.value = value
152 },
80 },
153 //geter
81 //geter
154 function(cell){ var ns = cell.metadata.yn_test;
82 function(cell){ var ns = cell.metadata.yn_test;
155 // if the slideshow namespace does not exist return `undefined`
83 // if the slideshow namespace does not exist return `undefined`
156 // (will be interpreted as `false` by checkbox) otherwise
84 // (will be interpreted as `false` by checkbox) otherwise
157 // return the value
85 // return the value
158 return (ns == undefined)? undefined: ns.value
86 return (ns == undefined)? undefined: ns.value
159 }
87 }
160 );
88 );
161
89
162
90
163 CellToolbar.register_callback('example.checkbox',checkbox_test);
91 CellToolbar.register_callback('example.checkbox',checkbox_test);
164 example_preset.push('example.checkbox');
92 example_preset.push('example.checkbox');
165
93
166 var select_test = CellToolbar.utils.select_ui_generator([
94 var select_test = CellToolbar.utils.select_ui_generator([
167 ["-" ,undefined ],
95 ["-" ,undefined ],
168 ["Header Slide" ,"header_slide" ],
96 ["Header Slide" ,"header_slide" ],
169 ["Slide" ,"slide" ],
97 ["Slide" ,"slide" ],
170 ["Fragment" ,"fragment" ],
98 ["Fragment" ,"fragment" ],
171 ["Skip" ,"skip" ],
99 ["Skip" ,"skip" ],
172 ],
100 ],
173 // setter
101 // setter
174 function(cell,value){
102 function(cell,value){
175 // we check that the slideshow namespace exist and create it if needed
103 // we check that the slideshow namespace exist and create it if needed
176 if (cell.metadata.test == undefined){cell.metadata.test = {}}
104 if (cell.metadata.test == undefined){cell.metadata.test = {}}
177 // set the value
105 // set the value
178 cell.metadata.test.slide_type = value
106 cell.metadata.test.slide_type = value
179 },
107 },
180 //geter
108 //geter
181 function(cell){ var ns = cell.metadata.test;
109 function(cell){ var ns = cell.metadata.test;
182 // if the slideshow namespace does not exist return `undefined`
110 // if the slideshow namespace does not exist return `undefined`
183 // (will be interpreted as `false` by checkbox) otherwise
111 // (will be interpreted as `false` by checkbox) otherwise
184 // return the value
112 // return the value
185 return (ns == undefined)? undefined: ns.slide_type
113 return (ns == undefined)? undefined: ns.slide_type
186 });
114 });
187
115
188 CellToolbar.register_callback('example.select',select_test);
116 CellToolbar.register_callback('example.select',select_test);
189 example_preset.push('example.select');
117 example_preset.push('example.select');
190
118
191 var simple_dialog = function(title,text){
119 var simple_dialog = function(title,text){
192 var dlg = $('<div/>').attr('title',title)
120 var dlg = $('<div/>').attr('title',title)
193 .append($('<p/>').text(text))
121 .append($('<p/>').text(text))
194 $(dlg).dialog({
122 $(dlg).dialog({
195 autoOpen: true,
123 autoOpen: true,
196 height: 300,
124 height: 300,
197 width: 650,
125 width: 650,
198 modal: true,
126 modal: true,
199 close: function() {
127 close: function() {
200 //cleanup on close
128 //cleanup on close
201 $(this).remove();
129 $(this).remove();
202 }
130 }
203 });
131 });
204 }
132 }
205
133
206 var add_simple_dialog_button = function(div, cell) {
134 var add_simple_dialog_button = function(div, cell) {
207 var help_text = ["This is the Metadata editting UI.",
135 var help_text = ["This is the Metadata editting UI.",
208 "It heavily rely on plugin to work ",
136 "It heavily rely on plugin to work ",
209 "and is still under developpement. You shouldn't wait too long before",
137 "and is still under developpement. You shouldn't wait too long before",
210 " seeing some customisable buttons in those toolbar."
138 " seeing some customisable buttons in those toolbar."
211 ].join('\n')
139 ].join('\n')
212 var button_container = $(div)
140 var button_container = $(div)
213 var button = $('<div/>').button({label:'?'})
141 var button = $('<div/>').button({label:'?'})
214 .click(function(){simple_dialog('help',help_text); return false;})
142 .click(function(){simple_dialog('help',help_text); return false;})
215 button_container.append(button);
143 button_container.append(button);
216 }
144 }
217
145
218 CellToolbar.register_callback('example.help',add_simple_dialog_button)
146 CellToolbar.register_callback('example.help',add_simple_dialog_button)
219 example_preset.push('example.help')
147 example_preset.push('example.help')
220
148
221 CellToolbar.register_preset('example',example_preset);
149 CellToolbar.register_preset('example',example_preset);
222 console.log('Example extension for metadata editting loaded.');
150 console.log('Example extension for metadata editting loaded.');
223
151
224 }(IPython));
152 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now