@@ -22,14 +22,16 @@ define([
22
22
return parts [ parts . length - 1 ] ;
23
23
} ;
24
24
25
- var extension_in = function ( extension , extensionslist ) {
26
- var res = extensionslist . indexOf ( extension ) != - 1 ;
27
- return res ;
28
-
25
+ var item_in = function ( item , list ) {
26
+ return list . indexOf ( item ) != - 1 ;
29
27
} ;
30
28
31
- var filepath_of_extension = function ( filepath , extensionslist ) {
32
- return extension_in ( extension ( filepath ) , extensionslist ) ;
29
+ var includes_extension = function ( filepath , extensionslist ) {
30
+ return item_in ( extension ( filepath ) , extensionslist ) ;
31
+ } ;
32
+
33
+ var includes_mimetype = function ( str , mimetype ) {
34
+ return item_in ( str , mimetype || '' ) ;
33
35
} ;
34
36
35
37
var NotebookList = function ( selector , options ) {
@@ -543,19 +545,24 @@ define([
543
545
} ;
544
546
545
547
NotebookList . ipynb_extensions = [ 'ipynb' ] ;
546
- NotebookList . non_editable_extensions = 'jpeg jpeg png zip gif tif tiff bmp ico pdf doc xls xlsx' . split ( ' ' ) ;
547
- NotebookList . editable_extensions = 'txt py cson json yaml html' . split ( ' ' ) ;
548
-
549
- NotebookList . prototype . _is_editable = function ( filepath ) {
550
- return filepath_of_extension ( filepath , NotebookList . editable_extensions ) ;
548
+ // List of text file extensions from
549
+ // https://github.com/sindresorhus/text-extensions/blob/master/text-extensions.json
550
+ var editable_extensions = [ 'applescript' , 'asp' , 'aspx' , 'atom' , 'bashrc' , 'bat' , 'bbcolors' , 'bib' , 'bowerrc' , 'c' , 'cc' , 'cfc' , 'cfg' , 'cfm' , 'cmd' , 'cnf' , 'coffee' , 'conf' , 'cpp' , 'cson' , 'css' , 'csslintrc' , 'csv' , 'curlrc' , 'cxx' , 'diff' , 'eco' , 'editorconfig' , 'ejs' , 'emacs' , 'eml' , 'erb' , 'erl' , 'eslintignore' , 'eslintrc' , 'gemrc' , 'gitattributes' , 'gitconfig' , 'gitignore' , 'go' , 'gvimrc' , 'h' , 'haml' , 'hbs' , 'hgignore' , 'hpp' , 'htaccess' , 'htm' , 'html' , 'iced' , 'ini' , 'ino' , 'irbrc' , 'itermcolors' , 'jade' , 'js' , 'jscsrc' , 'jshintignore' , 'jshintrc' , 'json' , 'jsonld' , 'jsx' , 'less' , 'log' , 'ls' , 'm' , 'markdown' , 'md' , 'mdown' , 'mdwn' , 'mht' , 'mhtml' , 'mkd' , 'mkdn' , 'mkdown' , 'nfo' , 'npmignore' , 'npmrc' , 'nvmrc' , 'patch' , 'pbxproj' , 'pch' , 'php' , 'phtml' , 'pl' , 'pm' , 'properties' , 'py' , 'rb' , 'rdoc' , 'rdoc_options' , 'ron' , 'rss' , 'rst' , 'rtf' , 'rvmrc' , 'sass' , 'scala' , 'scss' , 'seestyle' , 'sh' , 'sls' , 'sql' , 'sss' , 'strings' , 'styl' , 'stylus' , 'sub' , 'sublime-build' , 'sublime-commands' , 'sublime-completions' , 'sublime-keymap' , 'sublime-macro' , 'sublime-menu' , 'sublime-project' , 'sublime-settings' , 'sublime-workspace' , 'svg' , 'terminal' , 'tex' , 'text' , 'textile' , 'tmLanguage' , 'tmTheme' , 'tsv' , 'txt' , 'vbs' , 'vim' , 'viminfo' , 'vimrc' , 'webapp' , 'xht' , 'xhtml' , 'xml' , 'xsl' , 'yaml' , 'yml' , 'zsh' , 'zshrc' ] ;
551
+ NotebookList . editable_extensions = editable_extensions . concat ( [ 'ipynb' , 'geojson' , 'plotly' , 'plotly.json' , 'vg' , 'vg.json' , 'vl' , 'vl.json' ] ) ;
552
+ NotebookList . viewable_extensions = [ 'htm' , 'html' , 'xhtml' , 'mht' , 'mhtml' ] ;
553
+
554
+ NotebookList . prototype . _is_notebook = function ( model ) {
555
+ return includes_extension ( model . path , NotebookList . ipynb_extensions ) ;
551
556
} ;
552
-
553
- NotebookList . prototype . _is_not_editable = function ( filepath ) {
554
- return filepath_of_extension ( filepath , NotebookList . non_editable_extensions ) ;
557
+
558
+ NotebookList . prototype . _is_editable = function ( model ) {
559
+ return ( includes_mimetype ( 'text/' , model . mimetype ) || includes_mimetype ( 'application/' , model . mimetype ) )
560
+ || includes_extension ( model . path , NotebookList . editable_extensions ) ;
555
561
} ;
556
-
557
- NotebookList . prototype . _is_notebook = function ( filepath ) {
558
- return filepath_of_extension ( filepath , NotebookList . ipynb_extensions )
562
+
563
+ NotebookList . prototype . _is_viewable = function ( model ) {
564
+ return model . mimetype === 'text/html'
565
+ || includes_extension ( model . path , NotebookList . viewable_extensions ) ;
559
566
} ;
560
567
561
568
/**
@@ -650,9 +657,7 @@ define([
650
657
// If it's not editable or unknown, the default action should be view
651
658
// already so no need to show the button.
652
659
// That should include things like, html, py, txt, json....
653
- if ( selected . length == 1 && ! has_directory && selected . every ( function ( el ) {
654
- return that . _is_editable ( el . path ) && ! that . _is_notebook ( el . path ) ;
655
- } ) ) {
660
+ if ( selected . length >= 1 && ! has_directory ) {
656
661
$ ( '.view-button' ) . css ( 'display' , 'inline-block' ) ;
657
662
} else {
658
663
$ ( '.view-button' ) . css ( 'display' , 'none' ) ;
@@ -665,10 +670,8 @@ define([
665
670
// Indeed if it's editable the default action is already to edit.
666
671
// And non editable files should not show edit button.
667
672
// for unknown we'll assume users know what they are doing.
668
- if ( selected . length == 1 && ! has_directory && selected . find ( function ( el ) {
669
- return ! that . _is_editable ( el . path )
670
- && ! that . _is_not_editable ( el . path )
671
- && ! that . _is_notebook ( el . path ) ;
673
+ if ( selected . length >= 1 && ! has_directory && selected . every ( function ( el ) {
674
+ return that . _is_editable ( el ) ;
672
675
} ) ) {
673
676
$ ( '.edit-button' ) . css ( 'display' , 'inline-block' ) ;
674
677
} else {
@@ -714,34 +717,42 @@ define([
714
717
} ;
715
718
716
719
NotebookList . prototype . add_link = function ( model , item ) {
717
- var path = model . path ,
718
- name = model . name ,
719
- modified = model . last_modified ;
720
- var running = ( model . type === 'notebook' && this . sessions [ path ] !== undefined ) ;
720
+ var running = ( model . type === 'notebook' && this . sessions [ model . path ] !== undefined ) ;
721
721
722
- item . data ( 'name' , name ) ;
723
- item . data ( 'path' , path ) ;
724
- item . data ( 'modified' , modified ) ;
722
+ item . data ( 'name' , model . name ) ;
723
+ item . data ( 'path' , model . path ) ;
724
+ item . data ( 'modified' , model . modified ) ;
725
725
item . data ( 'type' , model . type ) ;
726
- item . find ( ".item_name" ) . text ( name ) ;
726
+ item . find ( ".item_name" ) . text ( model . name ) ;
727
727
var icon = NotebookList . icons [ model . type ] ;
728
728
if ( running ) {
729
729
icon = 'running_' + icon ;
730
730
}
731
731
var uri_prefix = NotebookList . uri_prefixes [ model . type ] ;
732
- if ( model . type === 'file'
733
- && ! this . _is_editable ( path ) )
732
+ if ( model . type === 'file' && ! this . _is_editable ( model ) )
733
+ {
734
+ uri_prefix = 'files' ;
735
+ }
736
+ if ( model . type === 'file' && this . _is_viewable ( model ) )
734
737
{
735
738
uri_prefix = 'view' ;
736
739
}
740
+ if ( model . type === 'file' && this . _is_editable ( model ) )
741
+ {
742
+ uri_prefix = 'edit' ;
743
+ }
744
+ if ( model . type === 'file' && this . _is_notebook ( model ) )
745
+ {
746
+ uri_prefix = 'notebooks' ;
747
+ }
737
748
738
749
item . find ( ".item_icon" ) . addClass ( icon ) . addClass ( 'icon-fixed-width' ) ;
739
750
var link = item . find ( "a.item_link" )
740
751
. attr ( 'href' ,
741
752
utils . url_path_join (
742
753
this . base_url ,
743
754
uri_prefix ,
744
- utils . encode_uri_components ( path )
755
+ utils . encode_uri_components ( model . path )
745
756
)
746
757
) ;
747
758
@@ -754,8 +765,8 @@ define([
754
765
}
755
766
756
767
// Add in the date that the file was last modified
757
- item . find ( ".item_modified" ) . text ( utils . format_datetime ( modified ) ) ;
758
- item . find ( ".item_modified" ) . attr ( "title" , moment ( modified ) . format ( "YYYY-MM-DD HH:mm" ) ) ;
768
+ item . find ( ".item_modified" ) . text ( utils . format_datetime ( model . modified ) ) ;
769
+ item . find ( ".item_modified" ) . attr ( "title" , moment ( model . modified ) . format ( "YYYY-MM-DD HH:mm" ) ) ;
759
770
} ;
760
771
761
772
@@ -1024,8 +1035,7 @@ define([
1024
1035
var that = this ;
1025
1036
that . selected . forEach ( function ( item ) {
1026
1037
var item_path = utils . encode_uri_components ( item . path ) ;
1027
- // Handle HTML files differently
1028
- var item_type = item_path . endsWith ( '.html' ) ? 'view' : 'files' ;
1038
+ var item_type = that . _is_notebook ( item ) ? 'notebooks' : that . _is_viewable ( item ) ? 'view' : 'files' ;
1029
1039
window . open ( utils . url_path_join ( that . base_url , item_type , item_path ) , IPython . _target ) ;
1030
1040
} ) ;
1031
1041
} ;
@@ -1034,9 +1044,7 @@ define([
1034
1044
var that = this ;
1035
1045
that . selected . forEach ( function ( item ) {
1036
1046
var item_path = utils . encode_uri_components ( item . path ) ;
1037
- // Handle ipynb files differently
1038
- var item_type = item_path . endsWith ( '.ipynb' ) ? 'notebooks' : 'edit' ;
1039
- window . open ( utils . url_path_join ( that . base_url , item_type , utils . encode_uri_components ( item_path ) ) , IPython . _target ) ;
1047
+ window . open ( utils . url_path_join ( that . base_url , 'edit' , item_path ) , IPython . _target ) ;
1040
1048
} ) ;
1041
1049
} ;
1042
1050
0 commit comments