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