Show More
@@ -1,217 +1,217 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 | 27 | |
|
28 | 28 | var raw_edit = function(cell){ |
|
29 | 29 | |
|
30 | 30 | var md = cell.metadata |
|
31 | 31 | var error_div = $('<div/>').css('color','red') |
|
32 | 32 | |
|
33 | 33 | var textarea = $('<textarea/>') |
|
34 | 34 | .attr('rows','13') |
|
35 | 35 | .attr('cols','75') |
|
36 | 36 | .attr('name','metadata') |
|
37 | 37 | .text(JSON.stringify(md, null,4)||''); |
|
38 | 38 | var dialogform = $('<div/>').attr('title','Edit the metadata') |
|
39 | 39 | .append( |
|
40 | 40 | $('<form/>').append( |
|
41 | 41 | $('<fieldset/>').append( |
|
42 | 42 | $('<label/>') |
|
43 | 43 | .attr('for','metadata') |
|
44 | 44 | .text("Metadata (I know what I'm dooing and I won't complain if it breaks my notebook)") |
|
45 | 45 | ) |
|
46 | 46 | .append(error_div) |
|
47 | 47 | .append($('<br/>')) |
|
48 | 48 | .append( |
|
49 | 49 | textarea |
|
50 | 50 | ) |
|
51 | 51 | ) |
|
52 | 52 | ); |
|
53 | 53 | var editor = CodeMirror.fromTextArea(textarea[0], { |
|
54 | 54 | lineNumbers: true, |
|
55 | 55 | matchBrackets: true, |
|
56 | 56 | }); |
|
57 | 57 | $(dialogform).dialog({ |
|
58 | 58 | autoOpen: true, |
|
59 | 59 | height: 300, |
|
60 | 60 | width: 650, |
|
61 | 61 | modal: true, |
|
62 | 62 | buttons: { |
|
63 | 63 | "Ok": function() { |
|
64 | 64 | //validate json and set it |
|
65 | 65 | try { |
|
66 | 66 | var json = JSON.parse(editor.getValue()); |
|
67 | 67 | cell.metadata = json; |
|
68 | 68 | $( this ).dialog( "close" ); |
|
69 | 69 | } |
|
70 | 70 | catch(e) |
|
71 | 71 | { |
|
72 | 72 | error_div.text('Warning, invalid json, not saved'); |
|
73 | 73 | } |
|
74 | 74 | }, |
|
75 | 75 | Cancel: function() { |
|
76 | 76 | $( this ).dialog( "close" ); |
|
77 | 77 | } |
|
78 | 78 | }, |
|
79 | 79 | close: function() { |
|
80 | 80 | //cleanup on close |
|
81 | 81 | $(this).remove(); |
|
82 | 82 | } |
|
83 | 83 | }); |
|
84 | 84 | editor.refresh(); |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | var add_raw_edit_button = function(div, cell) { |
|
88 |
var button_container = |
|
|
88 | var button_container = div | |
|
89 | 89 | var button = $('<div/>').button({label:'Raw Edit'}) |
|
90 | 90 | .click(function(){raw_edit(cell); return false;}) |
|
91 | 91 | button_container.append(button); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | CellToolbar.register_callback('example.rawedit',add_raw_edit_button); |
|
95 | 95 | var example_preset = [] |
|
96 | 96 | example_preset.push('example.rawedit'); |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | var simple_button = function(div, cell) { |
|
100 | 100 | var button_container = $(div); |
|
101 | 101 | var button = $('<div/>').button({icons:{primary:'ui-icon-locked'}}); |
|
102 | 102 | var fun = function(value){ |
|
103 | 103 | try{ |
|
104 | 104 | if(value){ |
|
105 | 105 | cell.code_mirror.setOption('readOnly','nocursor') |
|
106 | 106 | button.button('option','icons',{primary:'ui-icon-locked'}) |
|
107 | 107 | } else { |
|
108 | 108 | cell.code_mirror.setOption('readOnly','false') |
|
109 | 109 | button.button('option','icons',{primary:'ui-icon-unlocked'}) |
|
110 | 110 | } |
|
111 | 111 | } catch(e){} |
|
112 | 112 | |
|
113 | 113 | } |
|
114 | 114 | fun(cell.metadata.ro) |
|
115 | 115 | button.click(function(){ |
|
116 | 116 | var v = cell.metadata.ro; |
|
117 | 117 | var locked = !v; |
|
118 | 118 | cell.metadata.ro = locked; |
|
119 | 119 | fun(locked) |
|
120 | 120 | }) |
|
121 | 121 | .css('height','16px') |
|
122 | 122 | .css('width','35px'); |
|
123 | 123 | button_container.append(button); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | CellToolbar.register_callback('example.lock',simple_button); |
|
127 | 127 | example_preset.push('example.lock'); |
|
128 | 128 | |
|
129 | 129 | var toggle_test = function(div, cell) { |
|
130 | 130 | var button_container = $(div) |
|
131 | 131 | var button = $('<div/>') |
|
132 | 132 | .button({label:String(cell.metadata.foo)}). |
|
133 | 133 | css('width','65px'); |
|
134 | 134 | button.click(function(){ |
|
135 | 135 | var v = cell.metadata.foo; |
|
136 | 136 | cell.metadata.foo = !v; |
|
137 | 137 | button.button("option","label",String(!v)); |
|
138 | 138 | }) |
|
139 | 139 | button_container.append(button); |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | CellToolbar.register_callback('example.toggle',toggle_test); |
|
143 | 143 | example_preset.push('example.toggle'); |
|
144 | 144 | |
|
145 | 145 | var checkbox_test = function(div, cell) { |
|
146 | 146 | var button_container = $(div) |
|
147 | 147 | |
|
148 | 148 | var chkb = $('<input/>').attr('type','checkbox'); |
|
149 | 149 | var lbl = $('<label/>').append($('<span/>').text('bar :').css('font-size','77%')); |
|
150 | 150 | lbl.append(chkb); |
|
151 | 151 | chkb.attr("checked",cell.metadata.bar); |
|
152 | 152 | chkb.click(function(){ |
|
153 | 153 | var v = cell.metadata.bar; |
|
154 | 154 | cell.metadata.bar = !v; |
|
155 | 155 | chkb.attr("checked",!v); |
|
156 | 156 | }) |
|
157 | 157 | button_container.append($('<div/>').append(lbl)); |
|
158 | 158 | |
|
159 | 159 | } |
|
160 | 160 | |
|
161 | 161 | CellToolbar.register_callback('example.checkbox',checkbox_test); |
|
162 | 162 | example_preset.push('example.checkbox'); |
|
163 | 163 | |
|
164 | 164 | var select_test = function(div, cell) { |
|
165 | 165 | var button_container = $(div) |
|
166 | 166 | |
|
167 | 167 | var select = $('<select/>'); |
|
168 | 168 | |
|
169 | 169 | select.append($('<option/>').attr('value','foo').text('foo')); |
|
170 | 170 | select.append($('<option/>').attr('value','bar').text('bar')); |
|
171 | 171 | select.append($('<option/>').attr('value','qux').text('qux')); |
|
172 | 172 | select.append($('<option/>').attr('value','zip').text('zip')); |
|
173 | 173 | select.val(cell.metadata.option); |
|
174 | 174 | select.change(function(){ |
|
175 | 175 | cell.metadata.option = select.val(); |
|
176 | 176 | }); |
|
177 | 177 | button_container.append($('<div/>').append(select)); |
|
178 | 178 | |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | CellToolbar.register_callback('example.select',select_test); |
|
182 | 182 | example_preset.push('example.select'); |
|
183 | 183 | |
|
184 | 184 | var simple_dialog = function(title,text){ |
|
185 | 185 | var dlg = $('<div/>').attr('title',title) |
|
186 | 186 | .append($('<p/>').text(text)) |
|
187 | 187 | $(dlg).dialog({ |
|
188 | 188 | autoOpen: true, |
|
189 | 189 | height: 300, |
|
190 | 190 | width: 650, |
|
191 | 191 | modal: true, |
|
192 | 192 | close: function() { |
|
193 | 193 | //cleanup on close |
|
194 | 194 | $(this).remove(); |
|
195 | 195 | } |
|
196 | 196 | }); |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | var add_simple_dialog_button = function(div, cell) { |
|
200 | 200 | var help_text = ["This is the Metadata editting UI.", |
|
201 | 201 | "It heavily rely on plugin to work ", |
|
202 | 202 | "and is still under developpement. You shouldn't wait too long before", |
|
203 | 203 | " seeing some customisable buttons in those toolbar." |
|
204 | 204 | ].join('\n') |
|
205 | 205 | var button_container = $(div) |
|
206 | 206 | var button = $('<div/>').button({label:'?'}) |
|
207 | 207 | .click(function(){simple_dialog('help',help_text); return false;}) |
|
208 | 208 | button_container.append(button); |
|
209 | 209 | } |
|
210 | 210 | |
|
211 | 211 | CellToolbar.register_callback('example.help',add_simple_dialog_button) |
|
212 | 212 | example_preset.push('example.help') |
|
213 | 213 | |
|
214 | 214 | CellToolbar.register_preset('example',example_preset); |
|
215 | 215 | console.log('Example extension for metadata editting loaded.'); |
|
216 | 216 | |
|
217 | 217 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now