maintoolbar.js
164 lines
| 5.7 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17192 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
Matthias Bussonnier
|
r19423 | 'require', | ||
Jonathan Frederic
|
r17192 | 'base/js/namespace', | ||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Matthias Bussonnier
|
r19423 | './toolbar', | ||
Bussonnier Matthias
|
r19481 | './celltoolbar' | ||
Matthias Bussonnier
|
r19423 | ], function(require, IPython, $, toolbar, celltoolbar) { | ||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
Matthias BUSSONNIER
|
r7833 | |||
jon
|
r17210 | var MainToolBar = function (selector, options) { | ||
Bussonnier Matthias
|
r19424 | /** | ||
* Constructor | ||||
* | ||||
* Parameters: | ||||
* selector: string | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* events: $(Events) instance | ||||
* notebook: Notebook instance | ||||
**/ | ||||
Matthias Bussonnier
|
r19445 | toolbar.ToolBar.apply(this, [selector, options] ); | ||
jon
|
r17210 | this.events = options.events; | ||
this.notebook = options.notebook; | ||||
Matthias Bussonnier
|
r19423 | this._make(); | ||
Min RK
|
r19896 | this.notebook.keyboard_manager.register_events(this.element); | ||
Matthias Bussonnier
|
r19423 | Object.seal(this); | ||
Matthias BUSSONNIER
|
r7833 | }; | ||
Matthias Bussonnier
|
r18377 | MainToolBar.prototype = Object.create(toolbar.ToolBar.prototype); | ||
Matthias BUSSONNIER
|
r7833 | |||
Matthias Bussonnier
|
r19423 | MainToolBar.prototype._make = function () { | ||
var grps = [ | ||||
[ | ||||
['ipython.save-notebook'], | ||||
'save-notbook' | ||||
], | ||||
[ | ||||
['ipython.insert-cell-after'], | ||||
'insert_above_below'], | ||||
[ | ||||
['ipython.cut-selected-cell', | ||||
'ipython.copy-selected-cell', | ||||
'ipython.paste-cell-after' | ||||
] , | ||||
'cut_copy_paste'], | ||||
[ | ||||
['ipython.move-selected-cell-up', | ||||
'ipython.move-selected-cell-down' | ||||
], | ||||
'move_up_down'], | ||||
[ ['ipython.run-select-next', | ||||
'ipython.interrupt-kernel', | ||||
Bussonnier Matthias
|
r19481 | 'ipython.restart-kernel' | ||
Matthias Bussonnier
|
r19423 | ], | ||
'run_int'], | ||||
['<add_celltype_list>'], | ||||
Bussonnier Matthias
|
r19481 | ['<add_celltoolbar_list>'] | ||
Matthias Bussonnier
|
r19423 | ]; | ||
this.construct(grps); | ||||
Matthias BUSSONNIER
|
r8209 | }; | ||
jon
|
r16934 | |||
Matthias Bussonnier
|
r19423 | // add a cell type drop down to the maintoolbar. | ||
Matthias Bussonnier
|
r19427 | // triggered when the pseudo action `<add_celltype_list>` is | ||
Matthias Bussonnier
|
r19423 | // encountered when building a toolbar. | ||
Bussonnier Matthias
|
r19447 | MainToolBar.prototype._pseudo_actions.add_celltype_list = function () { | ||
Matthias Bussonnier
|
r19423 | var that = this; | ||
var sel = $('<select/>') | ||||
.attr('id','cell_type') | ||||
.addClass('form-control select-xs') | ||||
.append($('<option/>').attr('value','code').text('Code')) | ||||
.append($('<option/>').attr('value','markdown').text('Markdown')) | ||||
.append($('<option/>').attr('value','raw').text('Raw NBConvert')) | ||||
.append($('<option/>').attr('value','heading').text('Heading')); | ||||
this.events.on('selected_cell_type_changed.Notebook', function (event, data) { | ||||
if (data.cell_type === 'heading') { | ||||
sel.val('Markdown'); | ||||
} else { | ||||
sel.val(data.cell_type); | ||||
} | ||||
}); | ||||
sel.change(function () { | ||||
var cell_type = $(this).val(); | ||||
switch (cell_type) { | ||||
case 'code': | ||||
that.notebook.to_code(); | ||||
break; | ||||
case 'markdown': | ||||
that.notebook.to_markdown(); | ||||
break; | ||||
case 'raw': | ||||
that.notebook.to_raw(); | ||||
break; | ||||
case 'heading': | ||||
that.notebook._warn_heading(); | ||||
that.notebook.to_heading(); | ||||
Bussonnier Matthias
|
r19481 | sel.val('markdown'); | ||
Matthias Bussonnier
|
r19423 | break; | ||
default: | ||||
console.log("unrecognized cell type:", cell_type); | ||||
} | ||||
Bussonnier Matthias
|
r19951 | that.notebook.focus_cell(); | ||
Matthias Bussonnier
|
r19423 | }); | ||
Bussonnier Matthias
|
r19481 | return sel; | ||
Matthias Bussonnier
|
r19423 | |||
Matthias BUSSONNIER
|
r8212 | }; | ||
Bussonnier Matthias
|
r19447 | MainToolBar.prototype._pseudo_actions.add_celltoolbar_list = function () { | ||
MinRK
|
r10889 | var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:'); | ||
Brian Granger
|
r9146 | var select = $('<select/>') | ||
.attr('id', 'ctb_select') | ||||
Jonathan Frederic
|
r16948 | .addClass('form-control select-xs') | ||
Brian Granger
|
r9146 | .append($('<option/>').attr('value', '').text('None')); | ||
Jonathan Frederic
|
r17217 | var that = this; | ||
Brian Granger
|
r9146 | select.change(function() { | ||
Jonathan Frederic
|
r17192 | var val = $(this).val(); | ||
if (val ==='') { | ||||
Jonathan Frederic
|
r17202 | celltoolbar.CellToolbar.global_hide(); | ||
Jonathan Frederic
|
r17203 | delete that.notebook.metadata.celltoolbar; | ||
Brian Granger
|
r9146 | } else { | ||
Jonathan Frederic
|
r17202 | celltoolbar.CellToolbar.global_show(); | ||
Jonathan Frederic
|
r17217 | celltoolbar.CellToolbar.activate_preset(val, that.events); | ||
Jonathan Frederic
|
r17203 | that.notebook.metadata.celltoolbar = val; | ||
Brian Granger
|
r9146 | } | ||
Bussonnier Matthias
|
r19951 | that.notebook.focus_cell(); | ||
Brian Granger
|
r9146 | }); | ||
// Setup the currently registered presets. | ||||
Jonathan Frederic
|
r17202 | var presets = celltoolbar.CellToolbar.list_presets(); | ||
Brian Granger
|
r9146 | for (var i=0; i<presets.length; i++) { | ||
var name = presets[i]; | ||||
select.append($('<option/>').attr('value', name).text(name)); | ||||
} | ||||
// Setup future preset registrations. | ||||
Jonathan Frederic
|
r17196 | this.events.on('preset_added.CellToolbar', function (event, data) { | ||
Brian Granger
|
r9146 | var name = data.name; | ||
select.append($('<option/>').attr('value', name).text(name)); | ||||
}); | ||||
Bussonnier Matthias
|
r19586 | this.events.on('unregistered_preset.CellToolbar', function (event, data) { | ||
if (select.val() === data.name){ | ||||
select.val(''); | ||||
celltoolbar.CellToolbar.global_hide(); | ||||
delete that.notebook.metadata.celltoolbar; | ||||
} | ||||
select.find("option[value='"+name+"']" ).remove(); | ||||
}); | ||||
Raffaele De Feo
|
r16533 | // Update select value when a preset is activated. | ||
Jonathan Frederic
|
r17196 | this.events.on('preset_activated.CellToolbar', function (event, data) { | ||
Bussonnier Matthias
|
r19586 | if (select.val() !== data.name){ | ||
Raffaele De Feo
|
r16533 | select.val(data.name); | ||
Bussonnier Matthias
|
r19586 | } | ||
Raffaele De Feo
|
r16533 | }); | ||
Bussonnier Matthias
|
r19481 | |||
var wrapper = $('<div/>').addClass('btn-group'); | ||||
wrapper.append(label).append(select); | ||||
return wrapper; | ||||
Brian Granger
|
r9146 | }; | ||
Matthias Bussonnier
|
r19423 | // Backwards compatibility. | ||
Matthias BUSSONNIER
|
r7833 | IPython.MainToolBar = MainToolBar; | ||
Jonathan Frederic
|
r17201 | return {'MainToolBar': MainToolBar}; | ||
Jonathan Frederic
|
r17192 | }); | ||