From 2f266d3183f128a4d6e79cfd2d124c70a1b4c4b5 Mon Sep 17 00:00:00 2001 From: "Brian E. Granger" Date: Sat, 25 Feb 2017 18:33:05 -0800 Subject: [PATCH 1/4] Only show View button for HTML files. --- notebook/static/tree/js/notebooklist.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 94525e6d6..b7d3921e9 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -601,7 +601,7 @@ define([ // View is visible when an item is renderable or downloadable if (selected.length > 0 && !has_directory && selected.every(function(el) { - return el.path.match(/html?|json|jpe?g|png|gif|tiff?|svg|bmp|ico|pdf|doc|xls/); + return el.path.match(/html?/); })) { $('.view-button').css('display', 'inline-block'); } else { From 5974cbe2ad812ff49e7889af2ba0f729aa2b1edc Mon Sep 17 00:00:00 2001 From: "Brian E. Granger" Date: Sat, 25 Feb 2017 18:40:37 -0800 Subject: [PATCH 2/4] Don't show Edit button on files we know we can't edit or ipynb This removes the showing of the Edit button when: * We know we can't edit the file (pdf, doc, xls, jpeg, png, etc.) * Or a notebook - looking at this again though... --- notebook/static/tree/js/notebooklist.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index b7d3921e9..a4d9cf23f 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -609,7 +609,9 @@ define([ } // Edit is visible when an item is editable - if (selected.length > 0 && !has_directory) { + if (selected.length > 0 && !has_directory && !selected.find(function(el) { + return el.path.match(/ipynb||jpe?g|png|gif|tiff?|bmp|ico|pdf|doc|xls/); + })) { $('.edit-button').css('display', 'inline-block'); } else { $('.edit-button').css('display', 'none'); From 6846deec7a0bc3adf7a0cac1cf906f69358b2dc1 Mon Sep 17 00:00:00 2001 From: "Brian E. Granger" Date: Sat, 25 Feb 2017 18:44:02 -0800 Subject: [PATCH 3/4] Fixing notebook behavior of Edit. In the future I would like to show the Edit button for notebooks but have it open them in the text editor. Right now, there isn't any way for a user to do this. Thus hiding Edit on notebooks for now. --- notebook/static/tree/js/notebooklist.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index a4d9cf23f..29192d3c7 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -610,7 +610,7 @@ define([ // Edit is visible when an item is editable if (selected.length > 0 && !has_directory && !selected.find(function(el) { - return el.path.match(/ipynb||jpe?g|png|gif|tiff?|bmp|ico|pdf|doc|xls/); + return el.path.match(/ipynb|jpe?g|png|gif|tiff?|bmp|ico|pdf|doc|xls/); })) { $('.edit-button').css('display', 'inline-block'); } else { From e751e1ca1e69123d4df472c56f91264d6524fc08 Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Mon, 27 Feb 2017 09:54:23 -0800 Subject: [PATCH 4/4] Implement discussed behavior: - replace regexes they were not matching at the end of files. - hide edit/view for multiple, they where not working From the Pr comments that should closely match the following Observations: - "View" is handled by the browser so in most case it's something "safe". I think that (A) all items we don't know what to do with should have the _possibility_ to be viewed. (example `config.yaml`, `cat.gifv`). - "Edit" make sens only on text files only. In general (B) "edit" should not be the default action for most files. - Though (C) "Edit" should be triggerable by advanced users (unless we know we should not). Example `foo.zorblax`, `conf.toml` - (D) "Edit" does not make sens for some filetypes, thus should not be available (`'png`, `.jpeg`) Question (E): - If the default action is X {in edit/view} should the X button be shown ? Proposition: - if ipynb: - default link edit - no button in toolbar. - If known type, editable (txt, json, yaml, py, rb): - default link open edit, - button in toolbar show view - if known type not editable: (, png, gif, zip) - default link open view - no button in toolbar - if unknown type ('.zorblax, toml, cson, ') - default link open "view" - button in the toolbar show "Edit". --- notebook/static/tree/js/notebooklist.js | 73 +++++++++++++++++++++---- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 29192d3c7..ff146f3fe 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -12,6 +12,26 @@ define([ ], function($, IPython, utils, dialog, events, keyboard, moment) { "use strict"; + var extension = function(path){ + /** + * return the last pat after the dot in a filepath + * or the filepath itself if no dots present. + * Empty string if the filepath ends with a dot. + **/ + var parts = path.split('.'); + return parts[parts.length-1]; + }; + + var extension_in = function(extension, extensionslist){ + var res = extensionslist.indexOf(extension) != -1; + return res; + + }; + + var filepath_of_extension = function(filepath, extensionslist){ + return extension_in(extension(filepath), extensionslist); + }; + var NotebookList = function (selector, options) { /** * Constructor @@ -515,6 +535,21 @@ define([ this._selection_changed(); }; + NotebookList.ipynb_extensions = ['ipynb']; + NotebookList.non_editable_extensions = 'jpeg jpeg png zip gif tif tiff bmp ico pdf doc xls xlsx'.split(' '); + NotebookList.editable_extensions = 'txt py cson json yaml html'.split(' '); + + NotebookList.prototype._is_editable = function(filepath){ + return filepath_of_extension(filepath, NotebookList.editable_extensions); + }; + + NotebookList.prototype._is_not_editable = function(filepath){ + return filepath_of_extension(filepath, NotebookList.non_editable_extensions); + }; + + NotebookList.prototype._is_notebook = function(filepath){ + return filepath_of_extension(filepath, NotebookList.ipynb_extensions) + }; /** * Handles when any row selector checkbox is toggled. @@ -523,6 +558,7 @@ define([ // Use a JQuery selector to find each row with a checked checkbox. If // we decide to add more checkboxes in the future, this code will need // to be changed to distinguish which checkbox is the row selector. + var that = this; var selected = []; var has_running_notebook = false; var has_directory = false; @@ -599,18 +635,33 @@ define([ $('.delete-button').css('display', 'none'); } - // View is visible when an item is renderable or downloadable - if (selected.length > 0 && !has_directory && selected.every(function(el) { - return el.path.match(/html?/); + // View is visible in the following case: + // + // - the item is editable + // - it is not a notebook + // + // If it's not editable or unknown, the default action should be view + // already so no need to show the button. + // That should include things like, html, py, txt, json.... + if (selected.length == 1 && !has_directory && selected.every(function(el) { + return that._is_editable(el.path) && ! that._is_notebook(el.path); })) { $('.view-button').css('display', 'inline-block'); } else { $('.view-button').css('display', 'none'); } - // Edit is visible when an item is editable - if (selected.length > 0 && !has_directory && !selected.find(function(el) { - return el.path.match(/ipynb|jpe?g|png|gif|tiff?|bmp|ico|pdf|doc|xls/); + // Edit is visible when an item is unknown, that is to say: + // - not in the editable list + // - not in the known non-editable list. + // - not a notebook. + // Indeed if it's editable the default action is already to edit. + // 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.find(function(el) { + return !that._is_editable(el.path) + && !that._is_not_editable(el.path) + && !that._is_notebook(el.path); })) { $('.edit-button').css('display', 'inline-block'); } else { @@ -671,12 +722,10 @@ define([ icon = 'running_' + icon; } var uri_prefix = NotebookList.uri_prefixes[model.type]; - if (model.type === 'file' && - model.mimetype && - model.mimetype.substr(0, 5) !== 'text/' && - this.EDIT_MIMETYPES.indexOf(model.mimetype) < 0) { - // send text/unidentified files to editor, others go to raw viewer - uri_prefix = 'files'; + if (model.type === 'file' + && !this._is_editable(path)) + { + uri_prefix = 'view'; } item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');