Show More
@@ -1,87 +1,84 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | define([], function(){ |
|
4 | 4 | "use strict"; |
|
5 | 5 | |
|
6 | 6 | var ScrollManager = function (notebook) { |
|
7 | 7 | // Public constructor. |
|
8 | 8 | this.notebook = notebook; |
|
9 | 9 | this.animation_speed = 250; //ms |
|
10 | 10 | }; |
|
11 | 11 | |
|
12 | 12 | ScrollManager.prototype.scroll = function (delta) { |
|
13 | 13 | // Scroll the document. |
|
14 | 14 | // |
|
15 | 15 | // Parameters |
|
16 | 16 | // ---------- |
|
17 | 17 | // delta: integer |
|
18 | 18 | // direction to scroll the document. Positive is downwards. |
|
19 | 19 | |
|
20 | 20 | // If one or more slides exist, scroll to the slide. |
|
21 | 21 | var $slide_cells = $('.slideshow-slide'); |
|
22 | 22 | if ($slide_cells.length > 0) { |
|
23 | 23 | var i, cell; |
|
24 | 24 | |
|
25 | 25 | // Get the active slide cell index. |
|
26 | 26 | var selected_index = this.notebook.find_cell_index(this.notebook.get_selected_cell()); |
|
27 | 27 | var active_slide = -1; |
|
28 | 28 | var cells = this.notebook.get_cells(); |
|
29 | 29 | for (i = selected_index; i >= 0; i--) { |
|
30 | 30 | cell = cells[i]; |
|
31 | 31 | var ns = cell.metadata.slideshow; |
|
32 | 32 | if (ns && ns.slide_type == 'slide') { |
|
33 | 33 | active_slide = i; |
|
34 | 34 | break; |
|
35 | 35 | } |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | // Translate cell index into slide cell index. |
|
39 | 39 | if (active_slide != -1) { |
|
40 | 40 | for (i = 0; i < $slide_cells.length; i++) { |
|
41 | 41 | if (cells[active_slide].element[0] == $slide_cells[i]) { |
|
42 | 42 | active_slide = i; |
|
43 | 43 | break; |
|
44 | 44 | } |
|
45 | 45 | } |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | // Scroll. |
|
49 | 49 | if (active_slide != -1 || delta > 0) { |
|
50 | 50 | active_slide += delta; |
|
51 | 51 | active_slide = Math.max(0, Math.min($slide_cells.length-1, active_slide)); |
|
52 | 52 | |
|
53 | 53 | var cell_element = $slide_cells[active_slide]; |
|
54 | 54 | cell = $(cell_element).data('cell'); |
|
55 | 55 | this.notebook.select(this.notebook.find_cell_index(cell)); |
|
56 | 56 | |
|
57 | 57 | this.scroll_to(cell_element); |
|
58 | 58 | //cell_element.scrollIntoView(true); |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | // Cancel browser keyboard scroll. |
|
62 | 62 | return false; |
|
63 | 63 | |
|
64 | 64 | // No slides exist, scroll up or down one page height. Instead of using |
|
65 | 65 | // the browser's built in method to do this, animate it using jQuery. |
|
66 | 66 | } else { |
|
67 | 67 | this.scroll_some(delta); |
|
68 | 68 | return false; |
|
69 | 69 | } |
|
70 | 70 | }; |
|
71 | 71 | |
|
72 | 72 | ScrollManager.prototype.scroll_to = function(destination) { |
|
73 | 73 | // Scroll to an element in the notebook. |
|
74 | 74 | $('#notebook').animate({'scrollTop': $(destination).offset().top + $('#notebook').scrollTop() - $('#notebook').offset().top}, this.animation_speed); |
|
75 | 75 | }; |
|
76 | 76 | |
|
77 | 77 | ScrollManager.prototype.scroll_some = function(pages) { |
|
78 | 78 | // Scroll up or down a given number of pages. |
|
79 | 79 | $('#notebook').animate({'scrollTop': $('#notebook').scrollTop() + pages * $('#notebook').height()}, this.animation_speed); |
|
80 | 80 | }; |
|
81 | 81 | |
|
82 | // For convinience, add the ScrollManager class to the global namespace | |
|
83 | IPython.ScrollManager = ScrollManager; | |
|
84 | 82 | // Return naemspace for require.js loads |
|
85 | 83 | return ScrollManager; |
|
86 | ||
|
87 | 84 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now