From 87f6b3496714c9a845a5585b6719dd919d164155 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Tue, 24 May 2016 01:51:06 -0500 Subject: [PATCH 1/4] Respect 'readonly' cell metadata - Readonly cells can not be split or merged - Readonly cells can be executed - End users can still modify this by editing the metadata themsevles directly --- notebook/static/notebook/js/cell.js | 25 +++++++++++++++++++++---- notebook/static/notebook/js/codecell.js | 2 ++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/notebook/static/notebook/js/cell.js b/notebook/static/notebook/js/cell.js index d94501389..71b83c35a 100644 --- a/notebook/static/notebook/js/cell.js +++ b/notebook/static/notebook/js/cell.js @@ -498,20 +498,37 @@ define([ /** - * can the cell be split into two cells (false if not deletable) + * can the cell be split into two cells (false if not deletable + * or if readonly) + * * @method is_splittable **/ Cell.prototype.is_splittable = function () { - return this.is_deletable(); + return this.is_deletable() && !this.is_readonly(); }; /** - * can the cell be merged with other cells (false if not deletable) + * can the cell be merged with other cells (false if not deletable + * or if readonly) * @method is_mergeable **/ Cell.prototype.is_mergeable = function () { - return this.is_deletable(); + return this.is_deletable() && !this.is_readonly(); + }; + + /** + * is the cell readonly? only true (readonly) if + * metadata.readonly is explicitly true -- everything else + * counts as false + * + * @method is_readonly + **/ + Cell.prototype.is_readonly = function () { + if (this.metadata.readonly === true) { + return true; + } + return false; }; /** diff --git a/notebook/static/notebook/js/codecell.js b/notebook/static/notebook/js/codecell.js index 6be236899..b7aa71311 100644 --- a/notebook/static/notebook/js/codecell.js +++ b/notebook/static/notebook/js/codecell.js @@ -171,6 +171,8 @@ define([ if (that.keyboard_manager) { that.keyboard_manager.enable(); } + + that.code_mirror.setOption('readOnly', that.is_readonly()); }); this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this)); $(this.code_mirror.getInputField()).attr("spellcheck", "false"); From 70afd0c82db3a7415ec699c63956498e93a34317 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Tue, 24 May 2016 08:17:45 -0500 Subject: [PATCH 2/4] Change is_readonly to is_editable While I prefer things that default to false than true, in this case it is better to be consistent with is_deletable --- notebook/static/notebook/js/cell.js | 32 ++++++++++++------------- notebook/static/notebook/js/codecell.js | 2 +- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/notebook/static/notebook/js/cell.js b/notebook/static/notebook/js/cell.js index 71b83c35a..1dda6167c 100644 --- a/notebook/static/notebook/js/cell.js +++ b/notebook/static/notebook/js/cell.js @@ -498,48 +498,46 @@ define([ /** - * can the cell be split into two cells (false if not deletable - * or if readonly) + * can the cell be split into two cells (false if not deletable) * * @method is_splittable **/ Cell.prototype.is_splittable = function () { - return this.is_deletable() && !this.is_readonly(); + return this.is_deletable(); }; /** - * can the cell be merged with other cells (false if not deletable - * or if readonly) + * can the cell be merged with other cells (false if not deletable) * @method is_mergeable **/ Cell.prototype.is_mergeable = function () { - return this.is_deletable() && !this.is_readonly(); + return this.is_deletable(); }; /** - * is the cell readonly? only true (readonly) if - * metadata.readonly is explicitly true -- everything else - * counts as false + * is the cell edtitable? only false (readonly) if + * metadata.editable is explicitly false -- everything else + * counts as true * - * @method is_readonly + * @method is_editable **/ - Cell.prototype.is_readonly = function () { - if (this.metadata.readonly === true) { - return true; + Cell.prototype.is_editable = function () { + if (this.metadata.editable === false) { + return false; } - return false; + return true; }; /** * is the cell deletable? only false (undeletable) if - * metadata.deletable is explicitly false -- everything else - * counts as true + * metadata.deletable is explicitly false or if the cell is not + * editable -- everything else counts as true * * @method is_deletable **/ Cell.prototype.is_deletable = function () { - if (this.metadata.deletable === false) { + if (this.metadata.deletable === false || !this.is_editable()) { return false; } return true; diff --git a/notebook/static/notebook/js/codecell.js b/notebook/static/notebook/js/codecell.js index b7aa71311..b6b721c83 100644 --- a/notebook/static/notebook/js/codecell.js +++ b/notebook/static/notebook/js/codecell.js @@ -172,7 +172,7 @@ define([ that.keyboard_manager.enable(); } - that.code_mirror.setOption('readOnly', that.is_readonly()); + that.code_mirror.setOption('readOnly', !that.is_editable()); }); this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this)); $(this.code_mirror.getInputField()).attr("spellcheck", "false"); From 66fa05e91969bc1a7a2a066d79a65b75f89eae14 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Tue, 24 May 2016 08:18:22 -0500 Subject: [PATCH 3/4] Implement 'editable' for textcell type too --- notebook/static/notebook/js/textcell.js | 1 + 1 file changed, 1 insertion(+) diff --git a/notebook/static/notebook/js/textcell.js b/notebook/static/notebook/js/textcell.js index 64d613710..1fb8aadf5 100644 --- a/notebook/static/notebook/js/textcell.js +++ b/notebook/static/notebook/js/textcell.js @@ -102,6 +102,7 @@ define([ if (that.keyboard_manager) { that.keyboard_manager.enable(); } + that.code_mirror.setOption('readOnly', !that.is_editable()); }); this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this)) // The tabindex=-1 makes this div focusable. From 767942ccf4d2b1d2e320c916fd68ab0cc97fdca2 Mon Sep 17 00:00:00 2001 From: Grant Nestor Date: Mon, 12 Sep 2016 12:30:20 -0700 Subject: [PATCH 4/4] Upgrade cell's `editable` and `deletable` metadata if not defined --- notebook/static/notebook/js/cell.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/notebook/static/notebook/js/cell.js b/notebook/static/notebook/js/cell.js index 1dda6167c..70134447d 100644 --- a/notebook/static/notebook/js/cell.js +++ b/notebook/static/notebook/js/cell.js @@ -494,6 +494,14 @@ define([ if (data.metadata !== undefined) { this.metadata = data.metadata; } + // upgrade cell's editable metadata if not defined + if (this.metadata.editable === undefined) { + this.metadata.editable = this.is_editable(); + } + // upgrade cell's deletable metadata if not defined + if (this.metadata.deletable === undefined) { + this.metadata.deletable = this.is_deletable(); + } };