##// END OF EJS Templates
Add scrollmanager
Jonathan Frederic -
Show More
@@ -0,0 +1,78 b''
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 define([], function(){
4 "use strict";
5
6 var ScrollManager = function (notebook) {
7 // Public constructor.
8 this.notebook = notebook;
9 };
10
11 ScrollManager.prototype.scroll = function (delta) {
12 // Scroll the document.
13 //
14 // Parameters
15 // ----------
16 // delta: integer
17 // direction to scroll the document. Positive is downwards.
18
19 // If one or more slides exist, scroll to the slide.
20 var $slide_cells = $('.slideshow-slide');
21 if ($slide_cells.length > 0) {
22 var i, cell;
23
24 // Get the active slide cell index.
25 var selected_index = this.notebook.find_cell_index(this.notebook.get_selected_cell());
26 var active_slide = -1;
27 var cells = this.notebook.get_cells();
28 for (i = selected_index; i >= 0; i--) {
29 cell = cells[i];
30 var ns = cell.metadata.slideshow;
31 if (ns && ns.slide_type == 'slide') {
32 active_slide = i;
33 break;
34 }
35 }
36
37 // Translate cell index into slide cell index.
38 if (active_slide != -1) {
39 for (i = 0; i < $slide_cells.length; i++) {
40 if (cells[active_slide].element[0] == $slide_cells[i]) {
41 active_slide = i;
42 break;
43 }
44 }
45 }
46
47 // Scroll.
48 if (active_slide != -1 || delta > 0) {
49 active_slide += delta;
50 active_slide = Math.max(0, Math.min($slide_cells.length-1, active_slide));
51
52 var cell_element = $slide_cells[active_slide];
53 cell = $(cell_element).data('cell');
54 this.notebook.select(this.notebook.find_cell_index(cell));
55
56 this.scroll_to(cell_element);
57 //cell_element.scrollIntoView(true);
58 }
59
60 // Cancel browser keyboard scroll.
61 return false;
62
63 // No slides exist, default browser scroll
64 } else {
65 return true;
66 }
67 };
68
69 ScrollManager.prototype.scroll_to = function(destination) {
70 $('html, body').animate({'scrollTop': element.offset().top}, 'slow', 'swing');
71 };
72
73 // For convinience, add the ScrollManager class to the global namespace
74 IPython.ScrollManager = ScrollManager;
75 // Return naemspace for require.js loads
76 return ScrollManager;
77
78 });
@@ -1,12 +1,15 b''
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
8 //============================================================================
9 // Keyboard management
10 //============================================================================
3 11
4 define([
5 'base/js/namespace',
6 'jquery',
7 'base/js/utils',
8 'base/js/keyboard',
9 ], function(IPython, $, utils, keyboard) {
12 var IPython = (function (IPython) {
10 13 "use strict";
11 14
12 15 var browser = utils.browser[0];
@@ -183,6 +186,18 b' define(['
183 186 KeyboardManager.prototype.get_default_command_shortcuts = function() {
184 187 var that = this;
185 188 return {
189 'space': {
190 help: "Scroll down to next H1 cell",
191 handler: function(event) {
192 return that.notebook.scrollmanager.scroll(1);
193 },
194 },
195 'shift-space': {
196 help: "Scroll up to previous H1 cell",
197 handler: function(event) {
198 return that.notebook.scrollmanager.scroll(-1);
199 },
200 },
186 201 'enter' : {
187 202 help : 'edit mode',
188 203 help_index : 'aa',
@@ -19,6 +19,7 b' require(['
19 19 'notebook/js/keyboardmanager',
20 20 'notebook/js/config',
21 21 'notebook/js/kernelselector',
22 'notebook/js/scrollmanager'
22 23 // only loaded, not used:
23 24 'custom/custom',
24 25 ], function(
@@ -38,7 +39,8 b' require(['
38 39 savewidget,
39 40 keyboardmanager,
40 41 config,
41 kernelselector
42 kernelselector,
43 scrollmanager
42 44 ) {
43 45 "use strict";
44 46
@@ -67,6 +69,7 b' require(['
67 69 save_widget: save_widget,
68 70 config: user_config},
69 71 common_options));
72 var scrollmanager = new scrollmanager.ScrollManager(notebook);
70 73 var login_widget = new loginwidget.LoginWidget('span#login_widget', common_options);
71 74 var toolbar = new maintoolbar.MainToolBar('#maintoolbar-container', {
72 75 notebook: notebook,
@@ -132,6 +135,7 b' require(['
132 135 IPython.save_widget = save_widget;
133 136 IPython.config = user_config;
134 137 IPython.tooltip = notebook.tooltip;
138 IPython.scrollmanager = scrollmanager;
135 139
136 140 events.trigger('app_initialized.NotebookApp');
137 141 notebook.load_notebook(common_options.notebook_name, common_options.notebook_path);
General Comments 0
You need to be logged in to leave comments. Login now