Show More
maintoolbar.js
152 lines
| 5.2 KiB
| application/javascript
|
JavascriptLexer
|
r17192 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
|
r19423 | 'require', | ||
|
r17192 | 'base/js/namespace', | ||
|
r17200 | 'jquery', | ||
|
r19423 | './toolbar', | ||
'./celltoolbar', | ||||
], function(require, IPython, $, toolbar, celltoolbar) { | ||||
|
r12103 | "use strict"; | ||
|
r7833 | |||
|
r17210 | var MainToolBar = function (selector, options) { | ||
|
r19424 | /** | ||
* Constructor | ||||
* | ||||
* Parameters: | ||||
* selector: string | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* events: $(Events) instance | ||||
* notebook: Notebook instance | ||||
**/ | ||||
|
r19423 | toolbar.ToolBar.apply(this, [selector, undefined ,options] ); | ||
|
r17210 | this.events = options.events; | ||
this.notebook = options.notebook; | ||||
|
r19423 | this._make(); | ||
Object.seal(this); | ||||
|
r7833 | }; | ||
|
r18377 | MainToolBar.prototype = Object.create(toolbar.ToolBar.prototype); | ||
|
r7833 | |||
|
r19423 | // thought, this might not be the best way as dict might not keep the right order. | ||
// Might want to put the group name as second to make it optional | ||||
// | ||||
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', | ||||
'ipython.restart-kernel', | ||||
], | ||||
'run_int'], | ||||
['<add_celltype_list>'], | ||||
['<add_celltoolbar_list>'], | ||||
]; | ||||
this.construct(grps); | ||||
|
r8209 | }; | ||
|
r16934 | |||
|
r19423 | // add a cell type drop down to the maintoolbar. | ||
|
r19427 | // triggered when the pseudo action `<add_celltype_list>` is | ||
|
r19423 | // encountered when building a toolbar. | ||
|
r9146 | MainToolBar.prototype.add_celltype_list = function () { | ||
|
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(); | ||||
sel.val('markdown') | ||||
break; | ||||
default: | ||||
console.log("unrecognized cell type:", cell_type); | ||||
} | ||||
}); | ||||
this.element.append(sel); | ||||
|
r8212 | }; | ||
|
r9146 | MainToolBar.prototype.add_celltoolbar_list = function () { | ||
|
r10889 | var label = $('<span/>').addClass("navbar-text").text('Cell Toolbar:'); | ||
|
r9146 | var select = $('<select/>') | ||
.attr('id', 'ctb_select') | ||||
|
r16948 | .addClass('form-control select-xs') | ||
|
r9146 | .append($('<option/>').attr('value', '').text('None')); | ||
this.element.append(label).append(select); | ||||
|
r17217 | var that = this; | ||
|
r9146 | select.change(function() { | ||
|
r17192 | var val = $(this).val(); | ||
if (val ==='') { | ||||
|
r17202 | celltoolbar.CellToolbar.global_hide(); | ||
|
r17203 | delete that.notebook.metadata.celltoolbar; | ||
|
r9146 | } else { | ||
|
r17202 | celltoolbar.CellToolbar.global_show(); | ||
|
r17217 | celltoolbar.CellToolbar.activate_preset(val, that.events); | ||
|
r17203 | that.notebook.metadata.celltoolbar = val; | ||
|
r9146 | } | ||
}); | ||||
// Setup the currently registered presets. | ||||
|
r17202 | var presets = celltoolbar.CellToolbar.list_presets(); | ||
|
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. | ||||
|
r17196 | this.events.on('preset_added.CellToolbar', function (event, data) { | ||
|
r9146 | var name = data.name; | ||
select.append($('<option/>').attr('value', name).text(name)); | ||||
}); | ||||
|
r16533 | // Update select value when a preset is activated. | ||
|
r17196 | this.events.on('preset_activated.CellToolbar', function (event, data) { | ||
|
r16533 | if (select.val() !== data.name) | ||
select.val(data.name); | ||||
}); | ||||
|
r9146 | }; | ||
|
r19423 | // Backwards compatibility. | ||
|
r7833 | IPython.MainToolBar = MainToolBar; | ||
|
r17201 | return {'MainToolBar': MainToolBar}; | ||
|
r17192 | }); | ||