##// END OF EJS Templates
When a preset is activated fire "preset_activated" event....
Raffaele De Feo -
Show More
@@ -1,407 +1,413 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
9 // CellToolbar
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 CellToolbar
17 * @submodule CellToolbar
18 */
18 */
19 var IPython = (function (IPython) {
19 var IPython = (function (IPython) {
20 "use strict";
20 "use strict";
21
21
22 /**
22 /**
23 * @constructor
23 * @constructor
24 * @class CellToolbar
24 * @class CellToolbar
25 * @param {The cell to attach the metadata UI to} cell
25 * @param {The cell to attach the metadata UI to} cell
26 */
26 */
27 var CellToolbar = function (cell) {
27 var CellToolbar = function (cell) {
28 CellToolbar._instances.push(this);
28 CellToolbar._instances.push(this);
29 this.cell = cell;
29 this.cell = cell;
30 this.create_element();
30 this.create_element();
31 this.rebuild();
31 this.rebuild();
32 return this;
32 return this;
33 };
33 };
34
34
35
35
36 CellToolbar.prototype.create_element = function () {
36 CellToolbar.prototype.create_element = function () {
37 this.inner_element = $('<div/>').addClass('celltoolbar')
37 this.inner_element = $('<div/>').addClass('celltoolbar')
38 this.element = $('<div/>').addClass('ctb_hideshow')
38 this.element = $('<div/>').addClass('ctb_hideshow')
39 .append(this.inner_element);
39 .append(this.inner_element);
40 this.show();
40 this.show();
41 };
41 };
42
42
43
43
44 // The default css style for the outer celltoolbar div
44 // The default css style for the outer celltoolbar div
45 // (ctb_hideshow) is display: none.
45 // (ctb_hideshow) is display: none.
46 // To show the cell toolbar, *both* of the following conditions must be met:
46 // To show the cell toolbar, *both* of the following conditions must be met:
47 // - A parent container has class `ctb_global_show`
47 // - A parent container has class `ctb_global_show`
48 // - The celltoolbar has the class `ctb_show`
48 // - The celltoolbar has the class `ctb_show`
49 // This allows global show/hide, as well as per-cell show/hide.
49 // This allows global show/hide, as well as per-cell show/hide.
50
50
51 CellToolbar.global_hide = function () {
51 CellToolbar.global_hide = function () {
52 $('body').removeClass('ctb_global_show');
52 $('body').removeClass('ctb_global_show');
53 };
53 };
54
54
55
55
56 CellToolbar.global_show = function () {
56 CellToolbar.global_show = function () {
57 $('body').addClass('ctb_global_show');
57 $('body').addClass('ctb_global_show');
58 };
58 };
59
59
60
60
61 CellToolbar.prototype.hide = function () {
61 CellToolbar.prototype.hide = function () {
62 this.element.removeClass('ctb_show');
62 this.element.removeClass('ctb_show');
63 };
63 };
64
64
65
65
66 CellToolbar.prototype.show = function () {
66 CellToolbar.prototype.show = function () {
67 this.element.addClass('ctb_show');
67 this.element.addClass('ctb_show');
68 };
68 };
69
69
70
70
71 /**
71 /**
72 * Class variable that should contain a dict of all available callback
72 * Class variable that should contain a dict of all available callback
73 * we need to think of wether or not we allow nested namespace
73 * we need to think of wether or not we allow nested namespace
74 * @property _callback_dict
74 * @property _callback_dict
75 * @private
75 * @private
76 * @static
76 * @static
77 * @type Dict
77 * @type Dict
78 */
78 */
79 CellToolbar._callback_dict = {};
79 CellToolbar._callback_dict = {};
80
80
81
81
82 /**
82 /**
83 * Class variable that should contain the reverse order list of the button
83 * Class variable that should contain the reverse order list of the button
84 * to add to the toolbar of each cell
84 * to add to the toolbar of each cell
85 * @property _ui_controls_list
85 * @property _ui_controls_list
86 * @private
86 * @private
87 * @static
87 * @static
88 * @type List
88 * @type List
89 */
89 */
90 CellToolbar._ui_controls_list = [];
90 CellToolbar._ui_controls_list = [];
91
91
92
92
93 /**
93 /**
94 * Class variable that should contain the CellToolbar instances for each
94 * Class variable that should contain the CellToolbar instances for each
95 * cell of the notebook
95 * cell of the notebook
96 *
96 *
97 * @private
97 * @private
98 * @property _instances
98 * @property _instances
99 * @static
99 * @static
100 * @type List
100 * @type List
101 */
101 */
102 CellToolbar._instances = [];
102 CellToolbar._instances = [];
103
103
104
104
105 /**
105 /**
106 * keep a list of all the available presets for the toolbar
106 * keep a list of all the available presets for the toolbar
107 * @private
107 * @private
108 * @property _presets
108 * @property _presets
109 * @static
109 * @static
110 * @type Dict
110 * @type Dict
111 */
111 */
112 CellToolbar._presets = {};
112 CellToolbar._presets = {};
113
113
114
114
115 // this is by design not a prototype.
115 // this is by design not a prototype.
116 /**
116 /**
117 * Register a callback to create an UI element in a cell toolbar.
117 * Register a callback to create an UI element in a cell toolbar.
118 * @method register_callback
118 * @method register_callback
119 * @param name {String} name to use to refer to the callback. It is advised to use a prefix with the name
119 * @param name {String} name to use to refer to the callback. It is advised to use a prefix with the name
120 * for easier sorting and avoid collision
120 * for easier sorting and avoid collision
121 * @param callback {function(div, cell)} callback that will be called to generate the ui element
121 * @param callback {function(div, cell)} callback that will be called to generate the ui element
122 *
122 *
123 *
123 *
124 * The callback will receive the following element :
124 * The callback will receive the following element :
125 *
125 *
126 * * a div in which to add element.
126 * * a div in which to add element.
127 * * the cell it is responsible from
127 * * the cell it is responsible from
128 *
128 *
129 * @example
129 * @example
130 *
130 *
131 * Example that create callback for a button that toggle between `true` and `false` label,
131 * Example that create callback for a button that toggle between `true` and `false` label,
132 * with the metadata under the key 'foo' to reflect the status of the button.
132 * with the metadata under the key 'foo' to reflect the status of the button.
133 *
133 *
134 * // first param reference to a DOM div
134 * // first param reference to a DOM div
135 * // second param reference to the cell.
135 * // second param reference to the cell.
136 * var toggle = function(div, cell) {
136 * var toggle = function(div, cell) {
137 * var button_container = $(div)
137 * var button_container = $(div)
138 *
138 *
139 * // let's create a button that show the current value of the metadata
139 * // let's create a button that show the current value of the metadata
140 * var button = $('<div/>').button({label:String(cell.metadata.foo)});
140 * var button = $('<div/>').button({label:String(cell.metadata.foo)});
141 *
141 *
142 * // On click, change the metadata value and update the button label
142 * // On click, change the metadata value and update the button label
143 * button.click(function(){
143 * button.click(function(){
144 * var v = cell.metadata.foo;
144 * var v = cell.metadata.foo;
145 * cell.metadata.foo = !v;
145 * cell.metadata.foo = !v;
146 * button.button("option", "label", String(!v));
146 * button.button("option", "label", String(!v));
147 * })
147 * })
148 *
148 *
149 * // add the button to the DOM div.
149 * // add the button to the DOM div.
150 * button_container.append(button);
150 * button_container.append(button);
151 * }
151 * }
152 *
152 *
153 * // now we register the callback under the name `foo` to give the
153 * // now we register the callback under the name `foo` to give the
154 * // user the ability to use it later
154 * // user the ability to use it later
155 * CellToolbar.register_callback('foo', toggle);
155 * CellToolbar.register_callback('foo', toggle);
156 */
156 */
157 CellToolbar.register_callback = function(name, callback){
157 CellToolbar.register_callback = function(name, callback){
158 // Overwrite if it already exists.
158 // Overwrite if it already exists.
159 CellToolbar._callback_dict[name] = callback;
159 CellToolbar._callback_dict[name] = callback;
160 };
160 };
161
161
162
162
163 /**
163 /**
164 * Register a preset of UI element in a cell toolbar.
164 * Register a preset of UI element in a cell toolbar.
165 * Not supported Yet.
165 * Not supported Yet.
166 * @method register_preset
166 * @method register_preset
167 * @param name {String} name to use to refer to the preset. It is advised to use a prefix with the name
167 * @param name {String} name to use to refer to the preset. It is advised to use a prefix with the name
168 * for easier sorting and avoid collision
168 * for easier sorting and avoid collision
169 * @param preset_list {List of String} reverse order of the button in the toolbar. Each String of the list
169 * @param preset_list {List of String} reverse order of the button in the toolbar. Each String of the list
170 * should correspond to a name of a registerd callback.
170 * should correspond to a name of a registerd callback.
171 *
171 *
172 * @private
172 * @private
173 * @example
173 * @example
174 *
174 *
175 * CellToolbar.register_callback('foo.c1', function(div, cell){...});
175 * CellToolbar.register_callback('foo.c1', function(div, cell){...});
176 * CellToolbar.register_callback('foo.c2', function(div, cell){...});
176 * CellToolbar.register_callback('foo.c2', function(div, cell){...});
177 * CellToolbar.register_callback('foo.c3', function(div, cell){...});
177 * CellToolbar.register_callback('foo.c3', function(div, cell){...});
178 * CellToolbar.register_callback('foo.c4', function(div, cell){...});
178 * CellToolbar.register_callback('foo.c4', function(div, cell){...});
179 * CellToolbar.register_callback('foo.c5', function(div, cell){...});
179 * CellToolbar.register_callback('foo.c5', function(div, cell){...});
180 *
180 *
181 * CellToolbar.register_preset('foo.foo_preset1', ['foo.c1', 'foo.c2', 'foo.c5'])
181 * CellToolbar.register_preset('foo.foo_preset1', ['foo.c1', 'foo.c2', 'foo.c5'])
182 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
182 * CellToolbar.register_preset('foo.foo_preset2', ['foo.c4', 'foo.c5'])
183 */
183 */
184 CellToolbar.register_preset = function(name, preset_list) {
184 CellToolbar.register_preset = function(name, preset_list) {
185 CellToolbar._presets[name] = preset_list;
185 CellToolbar._presets[name] = preset_list;
186 $([IPython.events]).trigger('preset_added.CellToolbar', {name: name});
186 $([IPython.events]).trigger('preset_added.CellToolbar', {name: name});
187 // When "register_callback" is called by a custom extension, it may be executed after notebook is loaded.
188 // In that case, activate the preset if needed.
189 if (IPython.notebook && IPython.notebook.metadata && IPython.notebook.metadata.celltoolbar === name)
190 this.activate_preset(name);
187 };
191 };
188
192
189
193
190 /**
194 /**
191 * List the names of the presets that are currently registered.
195 * List the names of the presets that are currently registered.
192 *
196 *
193 * @method list_presets
197 * @method list_presets
194 * @static
198 * @static
195 */
199 */
196 CellToolbar.list_presets = function() {
200 CellToolbar.list_presets = function() {
197 var keys = [];
201 var keys = [];
198 for (var k in CellToolbar._presets) {
202 for (var k in CellToolbar._presets) {
199 keys.push(k);
203 keys.push(k);
200 }
204 }
201 return keys;
205 return keys;
202 };
206 };
203
207
204
208
205 /**
209 /**
206 * Activate an UI preset from `register_preset`
210 * Activate an UI preset from `register_preset`
207 *
211 *
208 * This does not update the selection UI.
212 * This does not update the selection UI.
209 *
213 *
210 * @method activate_preset
214 * @method activate_preset
211 * @param preset_name {String} string corresponding to the preset name
215 * @param preset_name {String} string corresponding to the preset name
212 *
216 *
213 * @static
217 * @static
214 * @private
218 * @private
215 * @example
219 * @example
216 *
220 *
217 * CellToolbar.activate_preset('foo.foo_preset1');
221 * CellToolbar.activate_preset('foo.foo_preset1');
218 */
222 */
219 CellToolbar.activate_preset = function(preset_name){
223 CellToolbar.activate_preset = function(preset_name){
220 var preset = CellToolbar._presets[preset_name];
224 var preset = CellToolbar._presets[preset_name];
221
225
222 if(preset !== undefined){
226 if(preset !== undefined){
223 CellToolbar._ui_controls_list = preset;
227 CellToolbar._ui_controls_list = preset;
224 CellToolbar.rebuild_all();
228 CellToolbar.rebuild_all();
225 }
229 }
230
231 $([IPython.events]).trigger('preset_activated.CellToolbar', {name: preset_name});
226 };
232 };
227
233
228
234
229 /**
235 /**
230 * This should be called on the class and not on a instance as it will trigger
236 * This should be called on the class and not on a instance as it will trigger
231 * rebuild of all the instances.
237 * rebuild of all the instances.
232 * @method rebuild_all
238 * @method rebuild_all
233 * @static
239 * @static
234 *
240 *
235 */
241 */
236 CellToolbar.rebuild_all = function(){
242 CellToolbar.rebuild_all = function(){
237 for(var i=0; i < CellToolbar._instances.length; i++){
243 for(var i=0; i < CellToolbar._instances.length; i++){
238 CellToolbar._instances[i].rebuild();
244 CellToolbar._instances[i].rebuild();
239 }
245 }
240 };
246 };
241
247
242 /**
248 /**
243 * Rebuild all the button on the toolbar to update its state.
249 * Rebuild all the button on the toolbar to update its state.
244 * @method rebuild
250 * @method rebuild
245 */
251 */
246 CellToolbar.prototype.rebuild = function(){
252 CellToolbar.prototype.rebuild = function(){
247 // strip evrything from the div
253 // strip evrything from the div
248 // which is probably inner_element
254 // which is probably inner_element
249 // or this.element.
255 // or this.element.
250 this.inner_element.empty();
256 this.inner_element.empty();
251
257
252 var callbacks = CellToolbar._callback_dict;
258 var callbacks = CellToolbar._callback_dict;
253 var preset = CellToolbar._ui_controls_list;
259 var preset = CellToolbar._ui_controls_list;
254 // Yes we iterate on the class variable, not the instance one.
260 // Yes we iterate on the class variable, not the instance one.
255 for (var i=0; i < preset.length; i++) {
261 for (var i=0; i < preset.length; i++) {
256 var key = preset[i];
262 var key = preset[i];
257 var callback = callbacks[key];
263 var callback = callbacks[key];
258 if (!callback) continue;
264 if (!callback) continue;
259
265
260 var local_div = $('<div/>').addClass('button_container');
266 var local_div = $('<div/>').addClass('button_container');
261 try {
267 try {
262 callback(local_div, this.cell, this);
268 callback(local_div, this.cell, this);
263 } catch (e) {
269 } catch (e) {
264 console.log("Error in cell toolbar callback " + key, e);
270 console.log("Error in cell toolbar callback " + key, e);
265 continue;
271 continue;
266 }
272 }
267 // only append if callback succeeded.
273 // only append if callback succeeded.
268 this.inner_element.append(local_div);
274 this.inner_element.append(local_div);
269 }
275 }
270 };
276 };
271
277
272
278
273 /**
279 /**
274 */
280 */
275 CellToolbar.utils = {};
281 CellToolbar.utils = {};
276
282
277
283
278 /**
284 /**
279 * A utility function to generate bindings between a checkbox and cell/metadata
285 * A utility function to generate bindings between a checkbox and cell/metadata
280 * @method utils.checkbox_ui_generator
286 * @method utils.checkbox_ui_generator
281 * @static
287 * @static
282 *
288 *
283 * @param name {string} Label in front of the checkbox
289 * @param name {string} Label in front of the checkbox
284 * @param setter {function( cell, newValue )}
290 * @param setter {function( cell, newValue )}
285 * A setter method to set the newValue
291 * A setter method to set the newValue
286 * @param getter {function( cell )}
292 * @param getter {function( cell )}
287 * A getter methods which return the current value.
293 * A getter methods which return the current value.
288 *
294 *
289 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
295 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
290 *
296 *
291 * @example
297 * @example
292 *
298 *
293 * An exmple that bind the subkey `slideshow.isSectionStart` to a checkbox with a `New Slide` label
299 * An exmple that bind the subkey `slideshow.isSectionStart` to a checkbox with a `New Slide` label
294 *
300 *
295 * var newSlide = CellToolbar.utils.checkbox_ui_generator('New Slide',
301 * var newSlide = CellToolbar.utils.checkbox_ui_generator('New Slide',
296 * // setter
302 * // setter
297 * function(cell, value){
303 * function(cell, value){
298 * // we check that the slideshow namespace exist and create it if needed
304 * // we check that the slideshow namespace exist and create it if needed
299 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
305 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
300 * // set the value
306 * // set the value
301 * cell.metadata.slideshow.isSectionStart = value
307 * cell.metadata.slideshow.isSectionStart = value
302 * },
308 * },
303 * //geter
309 * //geter
304 * function(cell){ var ns = cell.metadata.slideshow;
310 * function(cell){ var ns = cell.metadata.slideshow;
305 * // if the slideshow namespace does not exist return `undefined`
311 * // if the slideshow namespace does not exist return `undefined`
306 * // (will be interpreted as `false` by checkbox) otherwise
312 * // (will be interpreted as `false` by checkbox) otherwise
307 * // return the value
313 * // return the value
308 * return (ns == undefined)? undefined: ns.isSectionStart
314 * return (ns == undefined)? undefined: ns.isSectionStart
309 * }
315 * }
310 * );
316 * );
311 *
317 *
312 * CellToolbar.register_callback('newSlide', newSlide);
318 * CellToolbar.register_callback('newSlide', newSlide);
313 *
319 *
314 */
320 */
315 CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
321 CellToolbar.utils.checkbox_ui_generator = function(name, setter, getter){
316 return function(div, cell, celltoolbar) {
322 return function(div, cell, celltoolbar) {
317 var button_container = $(div);
323 var button_container = $(div);
318
324
319 var chkb = $('<input/>').attr('type', 'checkbox');
325 var chkb = $('<input/>').attr('type', 'checkbox');
320 var lbl = $('<label/>').append($('<span/>').text(name));
326 var lbl = $('<label/>').append($('<span/>').text(name));
321 lbl.append(chkb);
327 lbl.append(chkb);
322 chkb.attr("checked", getter(cell));
328 chkb.attr("checked", getter(cell));
323
329
324 chkb.click(function(){
330 chkb.click(function(){
325 var v = getter(cell);
331 var v = getter(cell);
326 setter(cell, !v);
332 setter(cell, !v);
327 chkb.attr("checked", !v);
333 chkb.attr("checked", !v);
328 });
334 });
329 button_container.append($('<div/>').append(lbl));
335 button_container.append($('<div/>').append(lbl));
330 };
336 };
331 };
337 };
332
338
333
339
334 /**
340 /**
335 * A utility function to generate bindings between a dropdown list cell
341 * A utility function to generate bindings between a dropdown list cell
336 * @method utils.select_ui_generator
342 * @method utils.select_ui_generator
337 * @static
343 * @static
338 *
344 *
339 * @param list_list {list of sublist} List of sublist of metadata value and name in the dropdown list.
345 * @param list_list {list of sublist} List of sublist of metadata value and name in the dropdown list.
340 * subslit shoud contain 2 element each, first a string that woul be displayed in the dropdown list,
346 * subslit shoud contain 2 element each, first a string that woul be displayed in the dropdown list,
341 * and second the corresponding value to be passed to setter/return by getter. the corresponding value
347 * and second the corresponding value to be passed to setter/return by getter. the corresponding value
342 * should not be "undefined" or behavior can be unexpected.
348 * should not be "undefined" or behavior can be unexpected.
343 * @param setter {function( cell, newValue )}
349 * @param setter {function( cell, newValue )}
344 * A setter method to set the newValue
350 * A setter method to set the newValue
345 * @param getter {function( cell )}
351 * @param getter {function( cell )}
346 * A getter methods which return the current value of the metadata.
352 * A getter methods which return the current value of the metadata.
347 * @param [label=""] {String} optionnal label for the dropdown menu
353 * @param [label=""] {String} optionnal label for the dropdown menu
348 *
354 *
349 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
355 * @return callback {function( div, cell )} Callback to be passed to `register_callback`
350 *
356 *
351 * @example
357 * @example
352 *
358 *
353 * var select_type = CellToolbar.utils.select_ui_generator([
359 * var select_type = CellToolbar.utils.select_ui_generator([
354 * ["<None>" , "None" ],
360 * ["<None>" , "None" ],
355 * ["Header Slide" , "header_slide" ],
361 * ["Header Slide" , "header_slide" ],
356 * ["Slide" , "slide" ],
362 * ["Slide" , "slide" ],
357 * ["Fragment" , "fragment" ],
363 * ["Fragment" , "fragment" ],
358 * ["Skip" , "skip" ],
364 * ["Skip" , "skip" ],
359 * ],
365 * ],
360 * // setter
366 * // setter
361 * function(cell, value){
367 * function(cell, value){
362 * // we check that the slideshow namespace exist and create it if needed
368 * // we check that the slideshow namespace exist and create it if needed
363 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
369 * if (cell.metadata.slideshow == undefined){cell.metadata.slideshow = {}}
364 * // set the value
370 * // set the value
365 * cell.metadata.slideshow.slide_type = value
371 * cell.metadata.slideshow.slide_type = value
366 * },
372 * },
367 * //geter
373 * //geter
368 * function(cell){ var ns = cell.metadata.slideshow;
374 * function(cell){ var ns = cell.metadata.slideshow;
369 * // if the slideshow namespace does not exist return `undefined`
375 * // if the slideshow namespace does not exist return `undefined`
370 * // (will be interpreted as `false` by checkbox) otherwise
376 * // (will be interpreted as `false` by checkbox) otherwise
371 * // return the value
377 * // return the value
372 * return (ns == undefined)? undefined: ns.slide_type
378 * return (ns == undefined)? undefined: ns.slide_type
373 * }
379 * }
374 * CellToolbar.register_callback('slideshow.select', select_type);
380 * CellToolbar.register_callback('slideshow.select', select_type);
375 *
381 *
376 */
382 */
377 CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label, cell_types){
383 CellToolbar.utils.select_ui_generator = function(list_list, setter, getter, label, cell_types){
378 label = label || "";
384 label = label || "";
379 return function(div, cell, celltoolbar) {
385 return function(div, cell, celltoolbar) {
380 var button_container = $(div);
386 var button_container = $(div);
381 var lbl = $("<label/>").append($('<span/>').text(label));
387 var lbl = $("<label/>").append($('<span/>').text(label));
382 var select = $('<select/>').addClass('ui-widget ui-widget-content');
388 var select = $('<select/>').addClass('ui-widget ui-widget-content');
383 for(var i=0; i < list_list.length; i++){
389 for(var i=0; i < list_list.length; i++){
384 var opt = $('<option/>')
390 var opt = $('<option/>')
385 .attr('value', list_list[i][1])
391 .attr('value', list_list[i][1])
386 .text(list_list[i][0]);
392 .text(list_list[i][0]);
387 select.append(opt);
393 select.append(opt);
388 }
394 }
389 select.val(getter(cell));
395 select.val(getter(cell));
390 select.change(function(){
396 select.change(function(){
391 setter(cell, select.val());
397 setter(cell, select.val());
392 });
398 });
393 button_container.append($('<div/>').append(lbl).append(select));
399 button_container.append($('<div/>').append(lbl).append(select));
394 if (cell_types && cell_types.indexOf(cell.cell_type) == -1) {
400 if (cell_types && cell_types.indexOf(cell.cell_type) == -1) {
395 celltoolbar.hide();
401 celltoolbar.hide();
396 } else {
402 } else {
397 celltoolbar.show();
403 celltoolbar.show();
398 }
404 }
399
405
400 };
406 };
401 };
407 };
402
408
403
409
404 IPython.CellToolbar = CellToolbar;
410 IPython.CellToolbar = CellToolbar;
405
411
406 return IPython;
412 return IPython;
407 }(IPython));
413 }(IPython));
@@ -1,214 +1,219 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 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 // ToolBar
9 // ToolBar
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var MainToolBar = function (selector) {
15 var MainToolBar = function (selector) {
16 IPython.ToolBar.apply(this, arguments);
16 IPython.ToolBar.apply(this, arguments);
17 this.construct();
17 this.construct();
18 this.add_celltype_list();
18 this.add_celltype_list();
19 this.add_celltoolbar_list();
19 this.add_celltoolbar_list();
20 this.bind_events();
20 this.bind_events();
21 };
21 };
22
22
23 MainToolBar.prototype = new IPython.ToolBar();
23 MainToolBar.prototype = new IPython.ToolBar();
24
24
25 MainToolBar.prototype.construct = function () {
25 MainToolBar.prototype.construct = function () {
26 this.add_buttons_group([
26 this.add_buttons_group([
27 {
27 {
28 id : 'save_b',
28 id : 'save_b',
29 label : 'Save and Checkpoint',
29 label : 'Save and Checkpoint',
30 icon : 'icon-save',
30 icon : 'icon-save',
31 callback : function () {
31 callback : function () {
32 IPython.notebook.save_checkpoint();
32 IPython.notebook.save_checkpoint();
33 }
33 }
34 }
34 }
35 ]);
35 ]);
36
36
37 this.add_buttons_group([
37 this.add_buttons_group([
38 {
38 {
39 id : 'insert_below_b',
39 id : 'insert_below_b',
40 label : 'Insert Cell Below',
40 label : 'Insert Cell Below',
41 icon : 'icon-plus-sign',
41 icon : 'icon-plus-sign',
42 callback : function () {
42 callback : function () {
43 IPython.notebook.insert_cell_below('code');
43 IPython.notebook.insert_cell_below('code');
44 IPython.notebook.select_next();
44 IPython.notebook.select_next();
45 IPython.notebook.focus_cell();
45 IPython.notebook.focus_cell();
46 }
46 }
47 }
47 }
48 ],'insert_above_below');
48 ],'insert_above_below');
49
49
50 this.add_buttons_group([
50 this.add_buttons_group([
51 {
51 {
52 id : 'cut_b',
52 id : 'cut_b',
53 label : 'Cut Cell',
53 label : 'Cut Cell',
54 icon : 'icon-cut',
54 icon : 'icon-cut',
55 callback : function () {
55 callback : function () {
56 IPython.notebook.cut_cell();
56 IPython.notebook.cut_cell();
57 }
57 }
58 },
58 },
59 {
59 {
60 id : 'copy_b',
60 id : 'copy_b',
61 label : 'Copy Cell',
61 label : 'Copy Cell',
62 icon : 'icon-copy',
62 icon : 'icon-copy',
63 callback : function () {
63 callback : function () {
64 IPython.notebook.copy_cell();
64 IPython.notebook.copy_cell();
65 }
65 }
66 },
66 },
67 {
67 {
68 id : 'paste_b',
68 id : 'paste_b',
69 label : 'Paste Cell Below',
69 label : 'Paste Cell Below',
70 icon : 'icon-paste',
70 icon : 'icon-paste',
71 callback : function () {
71 callback : function () {
72 IPython.notebook.paste_cell_below();
72 IPython.notebook.paste_cell_below();
73 }
73 }
74 }
74 }
75 ],'cut_copy_paste');
75 ],'cut_copy_paste');
76
76
77 this.add_buttons_group([
77 this.add_buttons_group([
78 {
78 {
79 id : 'move_up_b',
79 id : 'move_up_b',
80 label : 'Move Cell Up',
80 label : 'Move Cell Up',
81 icon : 'icon-arrow-up',
81 icon : 'icon-arrow-up',
82 callback : function () {
82 callback : function () {
83 IPython.notebook.move_cell_up();
83 IPython.notebook.move_cell_up();
84 }
84 }
85 },
85 },
86 {
86 {
87 id : 'move_down_b',
87 id : 'move_down_b',
88 label : 'Move Cell Down',
88 label : 'Move Cell Down',
89 icon : 'icon-arrow-down',
89 icon : 'icon-arrow-down',
90 callback : function () {
90 callback : function () {
91 IPython.notebook.move_cell_down();
91 IPython.notebook.move_cell_down();
92 }
92 }
93 }
93 }
94 ],'move_up_down');
94 ],'move_up_down');
95
95
96
96
97 this.add_buttons_group([
97 this.add_buttons_group([
98 {
98 {
99 id : 'run_b',
99 id : 'run_b',
100 label : 'Run Cell',
100 label : 'Run Cell',
101 icon : 'icon-play',
101 icon : 'icon-play',
102 callback : function () {
102 callback : function () {
103 // emulate default shift-enter behavior
103 // emulate default shift-enter behavior
104 IPython.notebook.execute_cell_and_select_below();
104 IPython.notebook.execute_cell_and_select_below();
105 }
105 }
106 },
106 },
107 {
107 {
108 id : 'interrupt_b',
108 id : 'interrupt_b',
109 label : 'Interrupt',
109 label : 'Interrupt',
110 icon : 'icon-stop',
110 icon : 'icon-stop',
111 callback : function () {
111 callback : function () {
112 IPython.notebook.session.interrupt_kernel();
112 IPython.notebook.session.interrupt_kernel();
113 }
113 }
114 },
114 },
115 {
115 {
116 id : 'repeat_b',
116 id : 'repeat_b',
117 label : 'Restart Kernel',
117 label : 'Restart Kernel',
118 icon : 'icon-repeat',
118 icon : 'icon-repeat',
119 callback : function () {
119 callback : function () {
120 IPython.notebook.restart_kernel();
120 IPython.notebook.restart_kernel();
121 }
121 }
122 }
122 }
123 ],'run_int');
123 ],'run_int');
124 };
124 };
125
125
126 MainToolBar.prototype.add_celltype_list = function () {
126 MainToolBar.prototype.add_celltype_list = function () {
127 this.element
127 this.element
128 .append($('<select/>')
128 .append($('<select/>')
129 .attr('id','cell_type')
129 .attr('id','cell_type')
130 // .addClass('ui-widget-content')
130 // .addClass('ui-widget-content')
131 .append($('<option/>').attr('value','code').text('Code'))
131 .append($('<option/>').attr('value','code').text('Code'))
132 .append($('<option/>').attr('value','markdown').text('Markdown'))
132 .append($('<option/>').attr('value','markdown').text('Markdown'))
133 .append($('<option/>').attr('value','raw').text('Raw NBConvert'))
133 .append($('<option/>').attr('value','raw').text('Raw NBConvert'))
134 .append($('<option/>').attr('value','heading1').text('Heading 1'))
134 .append($('<option/>').attr('value','heading1').text('Heading 1'))
135 .append($('<option/>').attr('value','heading2').text('Heading 2'))
135 .append($('<option/>').attr('value','heading2').text('Heading 2'))
136 .append($('<option/>').attr('value','heading3').text('Heading 3'))
136 .append($('<option/>').attr('value','heading3').text('Heading 3'))
137 .append($('<option/>').attr('value','heading4').text('Heading 4'))
137 .append($('<option/>').attr('value','heading4').text('Heading 4'))
138 .append($('<option/>').attr('value','heading5').text('Heading 5'))
138 .append($('<option/>').attr('value','heading5').text('Heading 5'))
139 .append($('<option/>').attr('value','heading6').text('Heading 6'))
139 .append($('<option/>').attr('value','heading6').text('Heading 6'))
140 );
140 );
141 };
141 };
142
142
143
143
144 MainToolBar.prototype.add_celltoolbar_list = function () {
144 MainToolBar.prototype.add_celltoolbar_list = function () {
145 var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:');
145 var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:');
146 var select = $('<select/>')
146 var select = $('<select/>')
147 // .addClass('ui-widget-content')
147 // .addClass('ui-widget-content')
148 .attr('id', 'ctb_select')
148 .attr('id', 'ctb_select')
149 .append($('<option/>').attr('value', '').text('None'));
149 .append($('<option/>').attr('value', '').text('None'));
150 this.element.append(label).append(select);
150 this.element.append(label).append(select);
151 select.change(function() {
151 select.change(function() {
152 var val = $(this).val()
152 var val = $(this).val()
153 if (val =='') {
153 if (val =='') {
154 IPython.CellToolbar.global_hide();
154 IPython.CellToolbar.global_hide();
155 delete IPython.notebook.metadata.celltoolbar;
155 delete IPython.notebook.metadata.celltoolbar;
156 } else {
156 } else {
157 IPython.CellToolbar.global_show();
157 IPython.CellToolbar.global_show();
158 IPython.CellToolbar.activate_preset(val);
158 IPython.CellToolbar.activate_preset(val);
159 IPython.notebook.metadata.celltoolbar = val;
159 IPython.notebook.metadata.celltoolbar = val;
160 }
160 }
161 });
161 });
162 // Setup the currently registered presets.
162 // Setup the currently registered presets.
163 var presets = IPython.CellToolbar.list_presets();
163 var presets = IPython.CellToolbar.list_presets();
164 for (var i=0; i<presets.length; i++) {
164 for (var i=0; i<presets.length; i++) {
165 var name = presets[i];
165 var name = presets[i];
166 select.append($('<option/>').attr('value', name).text(name));
166 select.append($('<option/>').attr('value', name).text(name));
167 }
167 }
168 // Setup future preset registrations.
168 // Setup future preset registrations.
169 $([IPython.events]).on('preset_added.CellToolbar', function (event, data) {
169 $([IPython.events]).on('preset_added.CellToolbar', function (event, data) {
170 var name = data.name;
170 var name = data.name;
171 select.append($('<option/>').attr('value', name).text(name));
171 select.append($('<option/>').attr('value', name).text(name));
172 });
172 });
173 // Update select value when a preset is activated.
174 $([IPython.events]).on('preset_activated.CellToolbar', function (event, data) {
175 if (select.val() !== data.name)
176 select.val(data.name);
177 });
173 };
178 };
174
179
175
180
176 MainToolBar.prototype.bind_events = function () {
181 MainToolBar.prototype.bind_events = function () {
177 var that = this;
182 var that = this;
178
183
179 this.element.find('#cell_type').change(function () {
184 this.element.find('#cell_type').change(function () {
180 var cell_type = $(this).val();
185 var cell_type = $(this).val();
181 if (cell_type === 'code') {
186 if (cell_type === 'code') {
182 IPython.notebook.to_code();
187 IPython.notebook.to_code();
183 } else if (cell_type === 'markdown') {
188 } else if (cell_type === 'markdown') {
184 IPython.notebook.to_markdown();
189 IPython.notebook.to_markdown();
185 } else if (cell_type === 'raw') {
190 } else if (cell_type === 'raw') {
186 IPython.notebook.to_raw();
191 IPython.notebook.to_raw();
187 } else if (cell_type === 'heading1') {
192 } else if (cell_type === 'heading1') {
188 IPython.notebook.to_heading(undefined, 1);
193 IPython.notebook.to_heading(undefined, 1);
189 } else if (cell_type === 'heading2') {
194 } else if (cell_type === 'heading2') {
190 IPython.notebook.to_heading(undefined, 2);
195 IPython.notebook.to_heading(undefined, 2);
191 } else if (cell_type === 'heading3') {
196 } else if (cell_type === 'heading3') {
192 IPython.notebook.to_heading(undefined, 3);
197 IPython.notebook.to_heading(undefined, 3);
193 } else if (cell_type === 'heading4') {
198 } else if (cell_type === 'heading4') {
194 IPython.notebook.to_heading(undefined, 4);
199 IPython.notebook.to_heading(undefined, 4);
195 } else if (cell_type === 'heading5') {
200 } else if (cell_type === 'heading5') {
196 IPython.notebook.to_heading(undefined, 5);
201 IPython.notebook.to_heading(undefined, 5);
197 } else if (cell_type === 'heading6') {
202 } else if (cell_type === 'heading6') {
198 IPython.notebook.to_heading(undefined, 6);
203 IPython.notebook.to_heading(undefined, 6);
199 }
204 }
200 });
205 });
201 $([IPython.events]).on('selected_cell_type_changed.Notebook', function (event, data) {
206 $([IPython.events]).on('selected_cell_type_changed.Notebook', function (event, data) {
202 if (data.cell_type === 'heading') {
207 if (data.cell_type === 'heading') {
203 that.element.find('#cell_type').val(data.cell_type+data.level);
208 that.element.find('#cell_type').val(data.cell_type+data.level);
204 } else {
209 } else {
205 that.element.find('#cell_type').val(data.cell_type);
210 that.element.find('#cell_type').val(data.cell_type);
206 }
211 }
207 });
212 });
208 };
213 };
209
214
210 IPython.MainToolBar = MainToolBar;
215 IPython.MainToolBar = MainToolBar;
211
216
212 return IPython;
217 return IPython;
213
218
214 }(IPython));
219 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now