##// END OF EJS Templates
Actually define the module...
Actually define the module I didn't forget something really obvious and important. Not at all.

File last commit:

r18377:848742ff
r18876:edcee758
Show More
scrollmanager.js
193 lines | 7.0 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Add scrollmanager
r17864 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
Jonathan Frederic
Added scroll mode selector,...
r17869 define(['jquery'], function($){
Jonathan Frederic
Add scrollmanager
r17864 "use strict";
Jonathan Frederic
Address review comments
r17873 var ScrollManager = function(notebook, options) {
Jonathan Frederic
Add scrollmanager
r17864 // Public constructor.
this.notebook = notebook;
Jonathan Frederic
Make HeadingScrollManager scroll to heading elements, not cells.
r17895 options = options || {};
Jonathan Frederic
Address review comments
r17873 this.animation_speed = options.animation_speed || 250; //ms
Jonathan Frederic
Add scrollmanager
r17864 };
ScrollManager.prototype.scroll = function (delta) {
// Scroll the document.
//
// Parameters
// ----------
// delta: integer
Jonathan Frederic
Added comments clarifying scroll units
r17875 // direction to scroll the document. Positive is downwards.
// Unit is one page length.
Jonathan Frederic
Added scroll mode selector,...
r17869 this.scroll_some(delta);
return false;
};
Jonathan Frederic
Address review comments
r17873 ScrollManager.prototype.scroll_to = function(selector) {
Jonathan Frederic
Added scroll mode selector,...
r17869 // Scroll to an element in the notebook.
Jonathan Frederic
Address review comments
r17873 $('#notebook').animate({'scrollTop': $(selector).offset().top + $('#notebook').scrollTop() - $('#notebook').offset().top}, this.animation_speed);
Jonathan Frederic
Added scroll mode selector,...
r17869 };
ScrollManager.prototype.scroll_some = function(pages) {
// Scroll up or down a given number of pages.
Jonathan Frederic
Address review comments
r17873 //
// Parameters
// ----------
// pages: integer
// number of pages to scroll the document, may be positive or negative.
Jonathan Frederic
Added scroll mode selector,...
r17869 $('#notebook').animate({'scrollTop': $('#notebook').scrollTop() + pages * $('#notebook').height()}, this.animation_speed);
};
ScrollManager.prototype.get_first_visible_cell = function() {
// Gets the index of the first visible cell in the document.
// First, attempt to be smart by guessing the index of the cell we are
// scrolled to. Then, walk from there up or down until the right cell
// is found. To guess the index, get the top of the last cell, and
// divide that by the number of cells to get an average cell height.
// Then divide the scroll height by the average cell height.
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 var cell_count = this.notebook.ncells();
var first_cell_top = this.notebook.get_cell(0).element.offset().top;
var last_cell_top = this.notebook.get_cell(cell_count-1).element.offset().top;
Jonathan Frederic
Added scroll mode selector,...
r17869 var avg_cell_height = (last_cell_top - first_cell_top) / cell_count;
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 var notebook = $('#notebook');
var i = Math.ceil(notebook.scrollTop() / avg_cell_height);
i = Math.min(Math.max(i , 0), cell_count - 1);
Jonathan Frederic
Added scroll mode selector,...
r17869
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 while (this.notebook.get_cell(i).element.offset().top - first_cell_top < notebook.scrollTop() && i < cell_count - 1) {
Jonathan Frederic
Added scroll mode selector,...
r17869 i += 1;
}
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 while (this.notebook.get_cell(i).element.offset().top - first_cell_top > notebook.scrollTop() - 50 && i >= 0) {
Jonathan Frederic
Added scroll mode selector,...
r17869 i -= 1;
}
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 return Math.min(i + 1, cell_count - 1);
Jonathan Frederic
Added scroll mode selector,...
r17869 };
Jonathan Frederic
Address review comments
r17873 var TargetScrollManager = function(notebook, options) {
Jonathan Frederic
Added scroll mode selector,...
r17869 // Public constructor.
Jonathan Frederic
Address review comments
r17873 ScrollManager.apply(this, [notebook, options]);
Jonathan Frederic
Added scroll mode selector,...
r17869 };
Matthias Bussonnier
Use low overhead object heritence in Js (Object.create vs new)...
r18377 TargetScrollManager.prototype = Object.create(ScrollManager.prototype);
Jonathan Frederic
Added scroll mode selector,...
r17869
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 TargetScrollManager.prototype.is_target = function (index) {
Jonathan Frederic
Address review comments
r17873 // Check if a cell should be a scroll stop.
//
// Returns `true` if the cell is a cell that the scroll manager
// should scroll to. Otherwise, false is returned.
//
// Parameters
// ----------
// index: integer
// index of the cell to test.
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 return false;
Jonathan Frederic
Added scroll mode selector,...
r17869 };
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 TargetScrollManager.prototype.scroll = function (delta) {
// Scroll the document.
Jonathan Frederic
Added scroll mode selector,...
r17869 //
// Parameters
// ----------
// delta: integer
// direction to scroll the document. Positive is downwards.
Jonathan Frederic
Added comments clarifying scroll units
r17875 // Units are targets.
Jonathan Frederic
Add scrollmanager
r17864
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 // Try to scroll to the next slide.
var cell_count = this.notebook.ncells();
var selected_index = this.get_first_visible_cell() + delta;
while (0 <= selected_index && selected_index < cell_count && !this.is_target(selected_index)) {
selected_index += delta;
}
Jonathan Frederic
Add scrollmanager
r17864
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 if (selected_index < 0 || cell_count <= selected_index) {
return ScrollManager.prototype.scroll.apply(this, [delta]);
Jonathan Frederic
Add scrollmanager
r17864 } else {
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 this.scroll_to(this.notebook.get_cell(selected_index).element);
// Cancel browser keyboard scroll.
Jonathan Frederic
Added smooth scroll to replace std browser behavior.
r17867 return false;
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 }
};
Jonathan Frederic
Address review comments
r17873 var SlideScrollManager = function(notebook, options) {
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 // Public constructor.
Jonathan Frederic
Address review comments
r17873 TargetScrollManager.apply(this, [notebook, options]);
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 };
Matthias Bussonnier
Use low overhead object heritence in Js (Object.create vs new)...
r18377 SlideScrollManager.prototype = Object.create(TargetScrollManager.prototype);
Jonathan Frederic
Added heading and slideshow scroll managers
r17870
SlideScrollManager.prototype.is_target = function (index) {
var cell = this.notebook.get_cell(index);
return cell.metadata && cell.metadata.slideshow &&
cell.metadata.slideshow.slide_type &&
Jonathan Frederic
Address review comments
r17873 (cell.metadata.slideshow.slide_type === "slide" ||
cell.metadata.slideshow.slide_type === "subslide");
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 };
Jonathan Frederic
Address review comments
r17873 var HeadingScrollManager = function(notebook, options) {
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 // Public constructor.
Jonathan Frederic
Make HeadingScrollManager scroll to heading elements, not cells.
r17895 ScrollManager.apply(this, [notebook, options]);
options = options || {};
Jonathan Frederic
Address review comments
r17873 this._level = options.heading_level || 1;
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 };
Matthias Bussonnier
Use low overhead object heritence in Js (Object.create vs new)...
r18377 HeadingScrollManager.prototype = Object.create(ScrollManager.prototype)
Jonathan Frederic
Added heading and slideshow scroll managers
r17870
Jonathan Frederic
Make HeadingScrollManager scroll to heading elements, not cells.
r17895 HeadingScrollManager.prototype.scroll = function (delta) {
// Scroll the document.
//
// Parameters
// ----------
// delta: integer
// direction to scroll the document. Positive is downwards.
// Units are headers.
// Get all of the header elements that match the heading level or are of
// greater magnitude (a smaller header number).
var headers = $();
var i;
for (i = 1; i <= this._level; i++) {
headers = headers.add('#notebook-container h' + i);
}
Jonathan Frederic
Added heading and slideshow scroll managers
r17870
Jonathan Frederic
Make HeadingScrollManager scroll to heading elements, not cells.
r17895 // Find the header the user is on or below.
var first_cell_top = this.notebook.get_cell(0).element.offset().top;
var notebook = $('#notebook');
var current_scroll = notebook.scrollTop();
var header_scroll = 0;
i = -1;
while (current_scroll >= header_scroll && i < headers.length) {
if (++i < headers.length) {
header_scroll = $(headers[i]).offset().top - first_cell_top;
}
}
i--;
// Check if the user is below the header.
if (i < 0 || current_scroll > $(headers[i]).offset().top - first_cell_top + 30) {
// Below the header, count the header as a target.
if (delta < 0) {
delta += 1;
}
}
i += delta;
// Scroll!
if (0 <= i && i < headers.length) {
this.scroll_to(headers[i]);
return false;
} else {
// Default to the base's scroll behavior when target header doesn't
// exist.
return ScrollManager.prototype.scroll.apply(this, [delta]);
}
};
Jonathan Frederic
Add scrollmanager
r17864
// Return naemspace for require.js loads
Jonathan Frederic
Added scroll mode selector,...
r17869 return {
'ScrollManager': ScrollManager,
'SlideScrollManager': SlideScrollManager,
Jonathan Frederic
Added heading and slideshow scroll managers
r17870 'HeadingScrollManager': HeadingScrollManager,
'TargetScrollManager': TargetScrollManager
Jonathan Frederic
Added scroll mode selector,...
r17869 };
Jonathan Frederic
Add scrollmanager
r17864 });