From 11d56a23e883fbd6c22d7246a6a334b3830cf78f Mon Sep 17 00:00:00 2001 From: Grant Nestor Date: Fri, 16 Sep 2016 08:52:28 -0700 Subject: [PATCH 1/4] Define `header` and `toolbar` properties for notebook If `notebook.header` or `notebook.toolbar` are `false`, hide them on load --- notebook/static/notebook/js/actions.js | 2 + notebook/static/notebook/js/notebook.js | 66 +++++++++++++++++++------ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index d61e9ee75..2cd526c86 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -484,6 +484,7 @@ define(function(require){ 'toggle-toolbar':{ help: 'hide/show the toolbar', handler : function(env){ + env.notebook.toolbar = !env.notebook.toolbar; $('div#maintoolbar').toggle(); events.trigger('resize-header.Page'); } @@ -491,6 +492,7 @@ define(function(require){ 'toggle-header':{ help: 'hide/show the header', handler : function(env){ + env.notebook.header = !env.notebook.header; $('#header-container').toggle(); $('.header-bar').toggle(); events.trigger('resize-header.Page'); diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index e10f33798..c154d1890 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -164,19 +164,56 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; var that = this; Object.defineProperty(this, 'line_numbers', { - get: function(){ - var d = that.config.data||{} - var cmc = (d['Cell']||{})['cm_config']||{} - return cmc['lineNumbers'] || false; - }, - set: function(value){ - this.get_cells().map(function(c) { - c.code_mirror.setOption('lineNumbers', value); - }) - that.config.update({'Cell':{'cm_config':{'lineNumbers':value}}}) - } - - }) + get: function() { + var d = that.config.data || {}; + var cmc = (d['Cell'] || {}) ['cm_config'] || {}; + return cmc['lineNumbers'] || false; + }, + set: function(value) { + this.get_cells().map(function(c) { + c.code_mirror.setOption('lineNumbers', value); + }); + that.config.update({ + 'Cell': { + 'cm_config': { + 'lineNumbers':value + } + } + }); + } + }); + + Object.defineProperty(this, 'header', { + get: function() { + var d = that.config.data || {}; + return d['Header'] || true; + }, + set: function(value) { + that.config.update({'Header': value}); + } + }); + + if (!this.header) { + $('#header-container').hide(); + $('.header-bar').hide(); + this.events.trigger('resize-header.Page'); + } + + Object.defineProperty(this, 'toolbar', { + get: function() { + var d = that.config.data || {}; + return d['Toolbar'] || true; + }, + set: function(value) { + that.config.update({'Toolbar': value}); + } + }); + + if (!this.toolbar) { + $('div#maintoolbar').hide(); + this.events.trigger('resize-header.Page'); + } + // prevent assign to miss-typed properties. Object.seal(this); }; @@ -578,7 +615,7 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; */ Notebook.prototype.toggle_all_line_numbers = function () { this.line_numbers = !this.line_numbers; - } + }; /** * Get the cell above a given cell. @@ -3185,4 +3222,3 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; this.events.trigger('checkpoint_deleted.Notebook'); this.load_notebook(this.notebook_path); }; - From 61eec926fe742071d00ed3225ef98545cb8252f7 Mon Sep 17 00:00:00 2001 From: Grant Nestor Date: Mon, 19 Sep 2016 17:08:31 -0700 Subject: [PATCH 2/4] Check header and toolbar toggle status after nbconfig loads --- notebook/static/notebook/js/notebook.js | 31 +++++++++++++------------ 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index c154d1890..09f092122 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -185,34 +185,35 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; Object.defineProperty(this, 'header', { get: function() { - var d = that.config.data || {}; - return d['Header'] || true; + if (that.config.data.Header === undefined) return true; + return that.config.data.Header; }, set: function(value) { that.config.update({'Header': value}); } }); - - if (!this.header) { - $('#header-container').hide(); - $('.header-bar').hide(); - this.events.trigger('resize-header.Page'); - } - + Object.defineProperty(this, 'toolbar', { get: function() { - var d = that.config.data || {}; - return d['Toolbar'] || true; + if (that.config.data.Toolbar === undefined) return true; + return that.config.data.Toolbar; }, set: function(value) { that.config.update({'Toolbar': value}); } }); - if (!this.toolbar) { - $('div#maintoolbar').hide(); - this.events.trigger('resize-header.Page'); - } + this.config.loaded.then(function() { + if (!that.header) { + $('#header-container').hide(); + $('.header-bar').hide(); + that.events.trigger('resize-header.Page'); + } + if (!that.toolbar) { + $('div#maintoolbar').hide(); + that.events.trigger('resize-header.Page'); + } + }); // prevent assign to miss-typed properties. Object.seal(this); From c656fc08469f2771a16f07644987b73954fcee28 Mon Sep 17 00:00:00 2001 From: Grant Nestor Date: Tue, 20 Sep 2016 14:19:34 -0700 Subject: [PATCH 3/4] Access config via `ConfigWithDefaults` vs. `ConfigSection` Move toggle logic to `notebook.js` vs. `actions.js` to be consistent with `notebook.line_numbers` --- notebook/static/notebook/js/actions.js | 29 +++----------- notebook/static/notebook/js/notebook.js | 50 +++++++++++++++---------- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index 2cd526c86..9f3fb52de 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -462,18 +462,6 @@ define(function(require){ env.notebook.show_command_palette(); } }, - 'show-all-line-numbers': { - help : 'show line numbers in all cells, and persist the setting', - handler: function(env) { - env.notebook.line_numbers = true; - } - }, - 'hide-all-line-numbers': { - help : 'hide line numbers in all cells, and persist the setting', - handler: function(env) { - env.notebook.line_numbers = false; - } - }, 'toggle-all-line-numbers': { help : 'toggles line numbers in all cells, and persist the setting', icon: 'fa-list-ol', @@ -481,21 +469,16 @@ define(function(require){ env.notebook.line_numbers = !env.notebook.line_numbers; } }, - 'toggle-toolbar':{ - help: 'hide/show the toolbar', - handler : function(env){ - env.notebook.toolbar = !env.notebook.toolbar; - $('div#maintoolbar').toggle(); - events.trigger('resize-header.Page'); - } - }, 'toggle-header':{ help: 'hide/show the header', handler : function(env){ env.notebook.header = !env.notebook.header; - $('#header-container').toggle(); - $('.header-bar').toggle(); - events.trigger('resize-header.Page'); + } + }, + 'toggle-toolbar':{ + help: 'hide/show the toolbar', + handler : function(env){ + env.notebook.toolbar = !env.notebook.toolbar; } }, 'close-pager': { diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index 09f092122..f56850775 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -185,46 +185,58 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; Object.defineProperty(this, 'header', { get: function() { - if (that.config.data.Header === undefined) return true; - return that.config.data.Header; + return that.class_config.get_sync('Header'); }, set: function(value) { - that.config.update({'Header': value}); + if (value === true) { + $('#header-container').show(); + $('.header-bar').show(); + } else if (value === false) { + $('#header-container').hide(); + $('.header-bar').hide(); + } + that.events.trigger('resize-header.Page'); + that.class_config.set('Header', value); } }); Object.defineProperty(this, 'toolbar', { get: function() { - if (that.config.data.Toolbar === undefined) return true; - return that.config.data.Toolbar; + return that.class_config.get_sync('Toolbar'); }, set: function(value) { - that.config.update({'Toolbar': value}); + if (value === true) { + $('div#maintoolbar').show(); + } else if (value === false) { + $('div#maintoolbar').hide(); + } + that.events.trigger('resize-header.Page'); + that.class_config.set('Toolbar', value); + } + }); + + this.class_config.get('Header').then(function(header) { + if (header === false) { + that.header = false; } }); - this.config.loaded.then(function() { - if (!that.header) { - $('#header-container').hide(); - $('.header-bar').hide(); - that.events.trigger('resize-header.Page'); - } - if (!that.toolbar) { - $('div#maintoolbar').hide(); - that.events.trigger('resize-header.Page'); - } + this.class_config.get('Toolbar').then(function(toolbar) { + if (toolbar === false) { + that.toolbar = false; + } }); // prevent assign to miss-typed properties. Object.seal(this); }; - - Notebook.options_default = { // can be any cell type, or the special values of // 'above', 'below', or 'selected' to get the value from another cell. - default_cell_type: 'code' + default_cell_type: 'code', + Header: true, + Toolbar: true }; /** From 6c1fe1f947221ce21f973d47289b76450913e884 Mon Sep 17 00:00:00 2001 From: Grant Nestor Date: Wed, 21 Sep 2016 08:51:35 -0700 Subject: [PATCH 4/4] Move toggle logic from property setters in to actions --- notebook/static/notebook/js/actions.js | 82 +++++++++++++++++++++++-- notebook/static/notebook/js/notebook.js | 25 ++------ 2 files changed, 81 insertions(+), 26 deletions(-) diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index 9f3fb52de..9cd5ea489 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -466,19 +466,91 @@ define(function(require){ help : 'toggles line numbers in all cells, and persist the setting', icon: 'fa-list-ol', handler: function(env) { - env.notebook.line_numbers = !env.notebook.line_numbers; + var value = !env.notebook.line_numbers; + env.notebook.get_cells().map(function(c) { + c.code_mirror.setOption('lineNumbers', value); + }); + env.notebook.line_numbers = value; + } + }, + 'show-all-line-numbers': { + help : 'show line numbers in all cells, and persist the setting', + handler: function(env) { + env.notebook.get_cells().map(function(c) { + c.code_mirror.setOption('lineNumbers', true); + }); + env.notebook.line_numbers = true; + } + }, + 'hide-all-line-numbers': { + help : 'hide line numbers in all cells, and persist the setting', + handler: function(env) { + env.notebook.get_cells().map(function(c) { + c.code_mirror.setOption('lineNumbers', false); + }); + env.notebook.line_numbers = false; } }, 'toggle-header':{ help: 'hide/show the header', - handler : function(env){ - env.notebook.header = !env.notebook.header; + handler : function(env) { + var value = !env.notebook.header; + if (value === true) { + $('#header-container').show(); + $('.header-bar').show(); + } else if (value === false) { + $('#header-container').hide(); + $('.header-bar').hide(); + } + events.trigger('resize-header.Page'); + env.notebook.header = value; + } + }, + 'show-header':{ + help: 'show the header', + handler : function(env) { + $('#header-container').show(); + $('.header-bar').show(); + events.trigger('resize-header.Page'); + env.notebook.header = true; + } + }, + 'hide-header':{ + help: 'hide the header', + handler : function(env) { + $('#header-container').hide(); + $('.header-bar').hide(); + events.trigger('resize-header.Page'); + env.notebook.header = false; } }, 'toggle-toolbar':{ help: 'hide/show the toolbar', - handler : function(env){ - env.notebook.toolbar = !env.notebook.toolbar; + handler : function(env) { + var value = !env.notebook.toolbar; + if (value === true) { + $('div#maintoolbar').show(); + } else if (value === false) { + $('div#maintoolbar').hide(); + } + events.trigger('resize-header.Page'); + env.notebook.toolbar = value; + } + }, + 'show-toolbar':{ + help: 'show the toolbar', + handler : function(env) { + $('div#maintoolbar').show(); + events.trigger('resize-header.Page'); + env.notebook.toolbar = true; + } + }, + 'hide-toolbar':{ + help: 'hide the toolbar', + handler : function(env) { + $('div#maintoolbar').hide(); + events.trigger('resize-header.Page'); + env.notebook.toolbar = false; } }, 'close-pager': { diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index f56850775..6f0694bf3 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -170,9 +170,6 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; return cmc['lineNumbers'] || false; }, set: function(value) { - this.get_cells().map(function(c) { - c.code_mirror.setOption('lineNumbers', value); - }); that.config.update({ 'Cell': { 'cm_config': { @@ -188,14 +185,6 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; return that.class_config.get_sync('Header'); }, set: function(value) { - if (value === true) { - $('#header-container').show(); - $('.header-bar').show(); - } else if (value === false) { - $('#header-container').hide(); - $('.header-bar').hide(); - } - that.events.trigger('resize-header.Page'); that.class_config.set('Header', value); } }); @@ -205,26 +194,20 @@ import {ShortcutEditor} from 'notebook/js/shortcuteditor'; return that.class_config.get_sync('Toolbar'); }, set: function(value) { - if (value === true) { - $('div#maintoolbar').show(); - } else if (value === false) { - $('div#maintoolbar').hide(); - } - that.events.trigger('resize-header.Page'); that.class_config.set('Toolbar', value); } }); this.class_config.get('Header').then(function(header) { if (header === false) { - that.header = false; + that.keyboard_manager.actions.call('jupyter-notebook:hide-header'); } }); this.class_config.get('Toolbar').then(function(toolbar) { - if (toolbar === false) { - that.toolbar = false; - } + if (toolbar === false) { + that.keyboard_manager.actions.call('jupyter-notebook:hide-toolbar'); + } }); // prevent assign to miss-typed properties.