@ -2,149 +2,109 @@
// Distributed under the terms of the Modified BSD License.
define ( [
'require' ,
'base/js/namespace' ,
'jquery' ,
' notebook/js /toolbar',
' notebook/js /celltoolbar',
] , function ( IPython, $ , toolbar , celltoolbar ) {
' . /toolbar',
' . /celltoolbar',
] , function ( require, IPython, $ , toolbar , celltoolbar ) {
"use strict" ;
var MainToolBar = function ( selector , options ) {
/ * *
* Constructor
*
* Parameters :
* selector : string
* options : dictionary
* Dictionary of keyword arguments .
* events : $ ( Events ) instance
* notebook : Notebook instance
* /
toolbar . ToolBar . apply ( this , arguments ) ;
// Constructor
//
// Parameters:
// selector: string
// options: dictionary
// Dictionary of keyword arguments.
// events: $(Events) instance
// notebook: Notebook instance
toolbar . ToolBar . apply ( this , [ selector , undefined , options ] ) ;
this . events = options . events ;
this . notebook = options . notebook ;
this . construct ( ) ;
this . add _celltype _list ( ) ;
this . add _celltoolbar _list ( ) ;
this . bind _events ( ) ;
this . _make ( ) ;
Object . seal ( this ) ;
} ;
MainToolBar . prototype = Object . create ( toolbar . ToolBar . prototype ) ;
MainToolBar . prototype . construct = function ( ) {
var that = this ;
this . add _buttons _group ( [
{
id : 'save_b' ,
label : 'Save and Checkpoint' ,
icon : 'fa-save' ,
callback : function ( ) {
that . notebook . save _checkpoint ( ) ;
}
}
] ) ;
this . add _buttons _group ( [
{
id : 'insert_below_b' ,
label : 'Insert Cell Below' ,
icon : 'fa-plus' ,
callback : function ( ) {
that . notebook . insert _cell _below ( 'code' ) ;
that . notebook . select _next ( ) ;
that . notebook . focus _cell ( ) ;
}
}
] , 'insert_above_below' ) ;
this . add _buttons _group ( [
{
id : 'cut_b' ,
label : 'Cut Cell' ,
icon : 'fa-cut' ,
callback : function ( ) {
that . notebook . cut _cell ( ) ;
}
} ,
{
id : 'copy_b' ,
label : 'Copy Cell' ,
icon : 'fa-copy' ,
callback : function ( ) {
that . notebook . copy _cell ( ) ;
}
} ,
{
id : 'paste_b' ,
label : 'Paste Cell Below' ,
icon : 'fa-paste' ,
callback : function ( ) {
that . notebook . paste _cell _below ( ) ;
}
}
] , 'cut_copy_paste' ) ;
this . add _buttons _group ( [
{
id : 'move_up_b' ,
label : 'Move Cell Up' ,
icon : 'fa-arrow-up' ,
callback : function ( ) {
that . notebook . move _cell _up ( ) ;
}
} ,
{
id : 'move_down_b' ,
label : 'Move Cell Down' ,
icon : 'fa-arrow-down' ,
callback : function ( ) {
that . notebook . move _cell _down ( ) ;
}
}
] , 'move_up_down' ) ;
this . add _buttons _group ( [
{
id : 'run_b' ,
label : 'Run Cell' ,
icon : 'fa-play' ,
callback : function ( ) {
/ * *
* emulate default shift - enter behavior
* /
that . notebook . execute _cell _and _select _below ( ) ;
}
} ,
{
id : 'interrupt_b' ,
label : 'Interrupt' ,
icon : 'fa-stop' ,
callback : function ( ) {
that . notebook . kernel . interrupt ( ) ;
}
} ,
{
id : 'repeat_b' ,
label : 'Restart Kernel' ,
icon : 'fa-repeat' ,
callback : function ( ) {
that . notebook . restart _kernel ( ) ;
}
}
] , 'run_int' ) ;
// 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 ) ;
} ;
// add a cell type drop down to the maintoolbar.
// trigged when the pseudo action `<add_celltype_list>` is
// encountered when building a toolbar.
// there supposed to be only one of these, but many should work in most cases;
MainToolBar . prototype . add _celltype _list = function ( ) {
this . element
. append ( $ ( '<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' ) )
) ;
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 ) ;
} ;
MainToolBar . prototype . add _celltoolbar _list = function ( ) {
@ -184,40 +144,7 @@ define([
} ) ;
} ;
MainToolBar . prototype . bind _events = function ( ) {
var that = this ;
this . element . find ( '#cell_type' ) . 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 ( ) ;
that . element . find ( '#cell_type' ) . val ( "markdown" ) ;
break ;
default :
console . log ( "unrecognized cell type:" , cell _type ) ;
}
} ) ;
this . events . on ( 'selected_cell_type_changed.Notebook' , function ( event , data ) {
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 ) ;
}
} ) ;
} ;
// Backwards compatability.
// Backwards compatibility.
IPython . MainToolBar = MainToolBar ;
return { 'MainToolBar' : MainToolBar } ;