##// END OF EJS Templates
Make HeadingScrollManager scroll to heading elements, not cells.
Jonathan Frederic -
Show More
@@ -6,7 +6,7 b" define(['jquery'], function($){"
6 var ScrollManager = function(notebook, options) {
6 var ScrollManager = function(notebook, options) {
7 // Public constructor.
7 // Public constructor.
8 this.notebook = notebook;
8 this.notebook = notebook;
9 options = options = {};
9 options = options || {};
10 this.animation_speed = options.animation_speed || 250; //ms
10 this.animation_speed = options.animation_speed || 250; //ms
11 };
11 };
12
12
@@ -127,17 +127,61 b" define(['jquery'], function($){"
127
127
128 var HeadingScrollManager = function(notebook, options) {
128 var HeadingScrollManager = function(notebook, options) {
129 // Public constructor.
129 // Public constructor.
130 TargetScrollManager.apply(this, [notebook, options]);
130 ScrollManager.apply(this, [notebook, options]);
131 options = options = {};
131 options = options || {};
132 this._level = options.heading_level || 1;
132 this._level = options.heading_level || 1;
133 };
133 };
134 HeadingScrollManager.prototype = new TargetScrollManager();
134 HeadingScrollManager.prototype = new ScrollManager();
135
135
136 HeadingScrollManager.prototype.is_target = function (index) {
136 HeadingScrollManager.prototype.scroll = function (delta) {
137 var cell = this.notebook.get_cell(index);
137 // Scroll the document.
138 return cell.cell_type === "heading" && cell.level <= this._level;
138 //
139 };
139 // Parameters
140 // ----------
141 // delta: integer
142 // direction to scroll the document. Positive is downwards.
143 // Units are headers.
144
145 // Get all of the header elements that match the heading level or are of
146 // greater magnitude (a smaller header number).
147 var headers = $();
148 var i;
149 for (i = 1; i <= this._level; i++) {
150 headers = headers.add('#notebook-container h' + i);
151 }
140
152
153 // Find the header the user is on or below.
154 var first_cell_top = this.notebook.get_cell(0).element.offset().top;
155 var notebook = $('#notebook');
156 var current_scroll = notebook.scrollTop();
157 var header_scroll = 0;
158 i = -1;
159 while (current_scroll >= header_scroll && i < headers.length) {
160 if (++i < headers.length) {
161 header_scroll = $(headers[i]).offset().top - first_cell_top;
162 }
163 }
164 i--;
165
166 // Check if the user is below the header.
167 if (i < 0 || current_scroll > $(headers[i]).offset().top - first_cell_top + 30) {
168 // Below the header, count the header as a target.
169 if (delta < 0) {
170 delta += 1;
171 }
172 }
173 i += delta;
174
175 // Scroll!
176 if (0 <= i && i < headers.length) {
177 this.scroll_to(headers[i]);
178 return false;
179 } else {
180 // Default to the base's scroll behavior when target header doesn't
181 // exist.
182 return ScrollManager.prototype.scroll.apply(this, [delta]);
183 }
184 };
141
185
142 // Return naemspace for require.js loads
186 // Return naemspace for require.js loads
143 return {
187 return {
General Comments 0
You need to be logged in to leave comments. Login now