maintoolbar.js
226 lines
| 8.4 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17192 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17192 | 'notebook/js/toolbar', | ||
Jonathan Frederic
|
r17196 | 'notebook/js/celltoolbar', | ||
Jonathan Frederic
|
r17872 | ], function(IPython, $, toolbar, celltoolbar) { | ||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
Matthias BUSSONNIER
|
r7833 | |||
jon
|
r17210 | var MainToolBar = function (selector, options) { | ||
jon
|
r17211 | // Constructor | ||
// | ||||
// Parameters: | ||||
// selector: string | ||||
// options: dictionary | ||||
// Dictionary of keyword arguments. | ||||
// events: $(Events) instance | ||||
// notebook: Notebook instance | ||||
Jonathan Frederic
|
r17203 | toolbar.ToolBar.apply(this, arguments); | ||
jon
|
r17210 | this.events = options.events; | ||
this.notebook = options.notebook; | ||||
Matthias BUSSONNIER
|
r8213 | this.construct(); | ||
Brian Granger
|
r9146 | this.add_celltype_list(); | ||
this.add_celltoolbar_list(); | ||||
Matthias BUSSONNIER
|
r8213 | this.bind_events(); | ||
Matthias BUSSONNIER
|
r7833 | }; | ||
Jonathan Frederic
|
r17203 | MainToolBar.prototype = new toolbar.ToolBar(); | ||
Matthias BUSSONNIER
|
r7833 | |||
Matthias BUSSONNIER
|
r8210 | MainToolBar.prototype.construct = function () { | ||
Jonathan Frederic
|
r17203 | var that = this; | ||
Matthias BUSSONNIER
|
r8051 | this.add_buttons_group([ | ||
Matthias BUSSONNIER
|
r7833 | { | ||
Matthias BUSSONNIER
|
r8210 | id : 'save_b', | ||
MinRK
|
r10513 | label : 'Save and Checkpoint', | ||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-save', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.save_checkpoint(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
} | ||||
Matthias BUSSONNIER
|
r7833 | ]); | ||
Susan Tan
|
r13731 | |||
this.add_buttons_group([ | ||||
{ | ||||
id : 'insert_below_b', | ||||
label : 'Insert Cell Below', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-plus', | ||
Susan Tan
|
r13731 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.insert_cell_below('code'); | ||
that.notebook.select_next(); | ||||
that.notebook.focus_cell(); | ||||
Susan Tan
|
r13731 | } | ||
} | ||||
],'insert_above_below'); | ||||
Matthias BUSSONNIER
|
r8051 | this.add_buttons_group([ | ||
Matthias BUSSONNIER
|
r7833 | { | ||
Matthias BUSSONNIER
|
r8210 | id : 'cut_b', | ||
label : 'Cut Cell', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-cut', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.cut_cell(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
Matthias BUSSONNIER
|
r7833 | }, | ||
{ | ||||
Matthias BUSSONNIER
|
r8210 | id : 'copy_b', | ||
label : 'Copy Cell', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-copy', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.copy_cell(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
Matthias BUSSONNIER
|
r7833 | }, | ||
{ | ||||
Matthias BUSSONNIER
|
r8210 | id : 'paste_b', | ||
Matthias BUSSONNIER
|
r8770 | label : 'Paste Cell Below', | ||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-paste', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.paste_cell_below(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
} | ||||
Matthias BUSSONNIER
|
r7833 | ],'cut_copy_paste'); | ||
Matthias BUSSONNIER
|
r8051 | this.add_buttons_group([ | ||
Matthias BUSSONNIER
|
r7833 | { | ||
Matthias BUSSONNIER
|
r8210 | id : 'move_up_b', | ||
label : 'Move Cell Up', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-arrow-up', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.move_cell_up(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
Matthias BUSSONNIER
|
r7833 | }, | ||
{ | ||||
Matthias BUSSONNIER
|
r8210 | id : 'move_down_b', | ||
label : 'Move Cell Down', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-arrow-down', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.move_cell_down(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
} | ||||
Matthias BUSSONNIER
|
r7833 | ],'move_up_down'); | ||
jon
|
r16931 | |||
Matthias BUSSONNIER
|
r7833 | |||
Matthias BUSSONNIER
|
r8051 | this.add_buttons_group([ | ||
Matthias BUSSONNIER
|
r7833 | { | ||
Matthias BUSSONNIER
|
r8210 | id : 'run_b', | ||
label : 'Run Cell', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-play', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
MinRK
|
r15179 | // emulate default shift-enter behavior | ||
Jonathan Frederic
|
r17203 | that.notebook.execute_cell_and_select_below(); | ||
MinRK
|
r15179 | } | ||
Matthias BUSSONNIER
|
r7833 | }, | ||
{ | ||||
Matthias BUSSONNIER
|
r8210 | id : 'interrupt_b', | ||
label : 'Interrupt', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-stop', | ||
Matthias BUSSONNIER
|
r8210 | callback : function () { | ||
Jessica B. Hamrick
|
r18204 | that.notebook.kernel.interrupt(); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
Brian E. Granger
|
r14866 | }, | ||
{ | ||||
id : 'repeat_b', | ||||
label : 'Restart Kernel', | ||||
Matthias BUSSONNIER
|
r17414 | icon : 'fa-repeat', | ||
Brian E. Granger
|
r14866 | callback : function () { | ||
Jonathan Frederic
|
r17203 | that.notebook.restart_kernel(); | ||
Brian E. Granger
|
r14866 | } | ||
Matthias BUSSONNIER
|
r8209 | } | ||
Matthias BUSSONNIER
|
r7833 | ],'run_int'); | ||
Matthias BUSSONNIER
|
r8209 | }; | ||
jon
|
r16934 | |||
Brian Granger
|
r9146 | MainToolBar.prototype.add_celltype_list = function () { | ||
this.element | ||||
Matthias BUSSONNIER
|
r8212 | .append($('<select/>') | ||
.attr('id','cell_type') | ||||
Jonathan Frederic
|
r16948 | .addClass('form-control select-xs') | ||
Brian Granger
|
r9146 | .append($('<option/>').attr('value','code').text('Code')) | ||
.append($('<option/>').attr('value','markdown').text('Markdown')) | ||||
MinRK
|
r13671 | .append($('<option/>').attr('value','raw').text('Raw NBConvert')) | ||
Brian Granger
|
r9146 | .append($('<option/>').attr('value','heading1').text('Heading 1')) | ||
.append($('<option/>').attr('value','heading2').text('Heading 2')) | ||||
.append($('<option/>').attr('value','heading3').text('Heading 3')) | ||||
.append($('<option/>').attr('value','heading4').text('Heading 4')) | ||||
.append($('<option/>').attr('value','heading5').text('Heading 5')) | ||||
.append($('<option/>').attr('value','heading6').text('Heading 6')) | ||||
); | ||||
Matthias BUSSONNIER
|
r8212 | }; | ||
Brian Granger
|
r9146 | MainToolBar.prototype.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')); | ||
this.element.append(label).append(select); | ||||
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 | } | ||
}); | ||||
// 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)); | ||||
}); | ||||
Raffaele De Feo
|
r16533 | // Update select value when a preset is activated. | ||
Jonathan Frederic
|
r17196 | this.events.on('preset_activated.CellToolbar', function (event, data) { | ||
Raffaele De Feo
|
r16533 | if (select.val() !== data.name) | ||
select.val(data.name); | ||||
}); | ||||
Brian Granger
|
r9146 | }; | ||
Matthias BUSSONNIER
|
r7833 | MainToolBar.prototype.bind_events = function () { | ||
var that = this; | ||||
jon
|
r16931 | |||
Matthias BUSSONNIER
|
r7833 | this.element.find('#cell_type').change(function () { | ||
var cell_type = $(this).val(); | ||||
if (cell_type === 'code') { | ||||
Jonathan Frederic
|
r17203 | that.notebook.to_code(); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'markdown') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_markdown(); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'raw') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_raw(); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'heading1') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_heading(undefined, 1); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'heading2') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_heading(undefined, 2); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'heading3') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_heading(undefined, 3); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'heading4') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_heading(undefined, 4); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'heading5') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_heading(undefined, 5); | ||
Matthias BUSSONNIER
|
r7833 | } else if (cell_type === 'heading6') { | ||
Jonathan Frederic
|
r17203 | that.notebook.to_heading(undefined, 6); | ||
Matthias BUSSONNIER
|
r8209 | } | ||
Matthias BUSSONNIER
|
r7833 | }); | ||
Jonathan Frederic
|
r17196 | this.events.on('selected_cell_type_changed.Notebook', function (event, data) { | ||
Matthias BUSSONNIER
|
r7833 | if (data.cell_type === 'heading') { | ||
that.element.find('#cell_type').val(data.cell_type+data.level); | ||||
} else { | ||||
that.element.find('#cell_type').val(data.cell_type); | ||||
} | ||||
}); | ||||
}; | ||||
Jonathan Frederic
|
r17192 | // Backwards compatability. | ||
Matthias BUSSONNIER
|
r7833 | IPython.MainToolBar = MainToolBar; | ||
Jonathan Frederic
|
r17201 | return {'MainToolBar': MainToolBar}; | ||
Jonathan Frederic
|
r17192 | }); | ||