From 5c0495d14e2ac6d289b79b09fb47ae3e19e1ed74 Mon Sep 17 00:00:00 2001 From: Grant Nestor Date: Wed, 31 May 2017 14:22:41 -0700 Subject: [PATCH] Inspect mimetype in addition to file extension to determine if a file is editable/viewable --- notebook/static/tree/js/notebooklist.js | 65 +++++++++++++------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index e626a86f3..80ddabf74 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -22,14 +22,16 @@ define([ return parts[parts.length-1]; }; - var extension_in = function(extension, extensionslist){ - var res = extensionslist.indexOf(extension) != -1; - return res; - + var item_in = function(item, list) { + return list.indexOf(item) != -1; }; - var filepath_of_extension = function(filepath, extensionslist){ - return extension_in(extension(filepath), extensionslist); + var includes_extension = function(filepath, extensionslist) { + return item_in(extension(filepath), extensionslist); + }; + + var includes_mimetype = function(str, mimetype) { + return item_in(str, mimetype || ''); }; var NotebookList = function (selector, options) { @@ -549,16 +551,18 @@ define([ NotebookList.editable_extensions = editable_extensions.concat(['geojson', 'plotly', 'plotly.json', 'vg', 'vg.json', 'vl', 'vl.json']); NotebookList.viewable_extensions = ['htm', 'html', 'xhtml', 'mht', 'mhtml']; - NotebookList.prototype._is_editable = function(filepath){ - return filepath_of_extension(filepath, NotebookList.editable_extensions); + NotebookList.prototype._is_notebook = function(model) { + return includes_extension(model.path, NotebookList.ipynb_extensions); }; - - NotebookList.prototype._is_notebook = function(filepath){ - return filepath_of_extension(filepath, NotebookList.ipynb_extensions) + + NotebookList.prototype._is_editable = function(model) { + return (includes_mimetype('text/', model.mimetype) || includes_mimetype('application/', model.mimetype)) + || includes_extension(model.path, NotebookList.editable_extensions); }; - NotebookList.prototype._is_viewable = function(filepath){ - return filepath_of_extension(filepath, NotebookList.viewable_extensions) + NotebookList.prototype._is_viewable = function(model) { + return model.mimetype === 'text/html' + || includes_extension(model.path, NotebookList.viewable_extensions); }; /** @@ -669,7 +673,7 @@ define([ // And non editable files should not show edit button. // for unknown we'll assume users know what they are doing. if (selected.length >= 1 && !has_directory && selected.every(function(el) { - return that._is_editable(el.path); + return that._is_editable(el); })) { $('.edit-button').css('display', 'inline-block'); } else { @@ -715,31 +719,30 @@ define([ }; NotebookList.prototype.add_link = function (model, item) { - var path = model.path, - name = model.name, - modified = model.last_modified; - var running = (model.type === 'notebook' && this.sessions[path] !== undefined); + var running = (model.type === 'notebook' && this.sessions[model.path] !== undefined); - item.data('name', name); - item.data('path', path); - item.data('modified', modified); + item.data('name', model.name); + item.data('path', model.path); + item.data('modified', model.modified); item.data('type', model.type); - item.find(".item_name").text(name); + item.find(".item_name").text(model.name); var icon = NotebookList.icons[model.type]; if (running) { icon = 'running_' + icon; } var uri_prefix = NotebookList.uri_prefixes[model.type]; - if (model.type === 'file' - && !this._is_editable(path)) + if (model.type === 'file' && !this._is_editable(model)) { uri_prefix = 'files'; } - if (model.type === 'file' - && this._is_viewable(path)) + if (model.type === 'file' && this._is_viewable(model)) { uri_prefix = 'view'; } + if (model.type === 'file' && this._is_notebook(model)) + { + uri_prefix = 'notebooks'; + } item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); var link = item.find("a.item_link") @@ -747,7 +750,7 @@ define([ utils.url_path_join( this.base_url, uri_prefix, - utils.encode_uri_components(path) + utils.encode_uri_components(model.path) ) ); @@ -760,8 +763,8 @@ define([ } // Add in the date that the file was last modified - item.find(".item_modified").text(utils.format_datetime(modified)); - item.find(".item_modified").attr("title", moment(modified).format("YYYY-MM-DD HH:mm")); + item.find(".item_modified").text(utils.format_datetime(model.modified)); + item.find(".item_modified").attr("title", moment(model.modified).format("YYYY-MM-DD HH:mm")); }; @@ -1040,9 +1043,7 @@ define([ var that = this; that.selected.forEach(function(item) { var item_path = utils.encode_uri_components(item.path); - // Handle ipynb files differently - var item_type = item_path.endsWith('.ipynb') ? 'notebooks' : 'edit'; - window.open(utils.url_path_join(that.base_url, item_type, item_path), IPython._target); + window.open(utils.url_path_join(that.base_url, 'edit', item_path), IPython._target); }); };