Skip to content

Commit ce56217

Browse files
authored
Merge pull request #2449 from gnestor/issue-2404
Open all files with `/files` path except for `.html` and .svg`
2 parents f75eb99 + 6a1f807 commit ce56217

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

notebook/static/tree/js/notebooklist.js

+50-42
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ define([
2222
return parts[parts.length-1];
2323
};
2424

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;
2927
};
3028

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 || '');
3335
};
3436

3537
var NotebookList = function (selector, options) {
@@ -543,19 +545,24 @@ define([
543545
};
544546

545547
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);
551556
};
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);
555561
};
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);
559566
};
560567

561568
/**
@@ -650,9 +657,7 @@ define([
650657
// If it's not editable or unknown, the default action should be view
651658
// already so no need to show the button.
652659
// 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) {
656661
$('.view-button').css('display', 'inline-block');
657662
} else {
658663
$('.view-button').css('display', 'none');
@@ -665,10 +670,8 @@ define([
665670
// Indeed if it's editable the default action is already to edit.
666671
// And non editable files should not show edit button.
667672
// 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);
672675
})) {
673676
$('.edit-button').css('display', 'inline-block');
674677
} else {
@@ -714,34 +717,42 @@ define([
714717
};
715718

716719
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);
721721

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);
725725
item.data('type', model.type);
726-
item.find(".item_name").text(name);
726+
item.find(".item_name").text(model.name);
727727
var icon = NotebookList.icons[model.type];
728728
if (running) {
729729
icon = 'running_' + icon;
730730
}
731731
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))
734737
{
735738
uri_prefix = 'view';
736739
}
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+
}
737748

738749
item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
739750
var link = item.find("a.item_link")
740751
.attr('href',
741752
utils.url_path_join(
742753
this.base_url,
743754
uri_prefix,
744-
utils.encode_uri_components(path)
755+
utils.encode_uri_components(model.path)
745756
)
746757
);
747758

@@ -754,8 +765,8 @@ define([
754765
}
755766

756767
// 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"));
759770
};
760771

761772

@@ -1024,8 +1035,7 @@ define([
10241035
var that = this;
10251036
that.selected.forEach(function(item) {
10261037
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';
10291039
window.open(utils.url_path_join(that.base_url, item_type, item_path), IPython._target);
10301040
});
10311041
};
@@ -1034,9 +1044,7 @@ define([
10341044
var that = this;
10351045
that.selected.forEach(function(item) {
10361046
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);
10401048
});
10411049
};
10421050

0 commit comments

Comments
 (0)