summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-10-05 16:47:19 +0200
committerSamy Pessé <samypesse@gmail.com>2015-10-05 16:47:19 +0200
commit3d1f057373836092623766809bbe0551b56b50b6 (patch)
treefa6ccf99f6c7611e0b9ecbdf045a280f68795dd6 /theme/javascript/core
parent0679807517f2b53954b24434bbf7a847fe833c01 (diff)
downloadgitbook-3d1f057373836092623766809bbe0551b56b50b6.zip
gitbook-3d1f057373836092623766809bbe0551b56b50b6.tar.gz
gitbook-3d1f057373836092623766809bbe0551b56b50b6.tar.bz2
Switch to gulp/browserify
Diffstat (limited to 'theme/javascript/core')
-rw-r--r--theme/javascript/core/events.js7
-rwxr-xr-xtheme/javascript/core/keyboard.js39
-rw-r--r--theme/javascript/core/loading.js16
-rwxr-xr-xtheme/javascript/core/navigation.js171
-rwxr-xr-xtheme/javascript/core/progress.js73
-rwxr-xr-xtheme/javascript/core/search.js114
-rwxr-xr-xtheme/javascript/core/sidebar.js51
-rwxr-xr-xtheme/javascript/core/state.js18
8 files changed, 0 insertions, 489 deletions
diff --git a/theme/javascript/core/events.js b/theme/javascript/core/events.js
deleted file mode 100644
index 855755f..0000000
--- a/theme/javascript/core/events.js
+++ /dev/null
@@ -1,7 +0,0 @@
-define([
- "jQuery"
-], function($) {
- var events = $({});
-
- return events;
-}); \ No newline at end of file
diff --git a/theme/javascript/core/keyboard.js b/theme/javascript/core/keyboard.js
deleted file mode 100755
index 27a9247..0000000
--- a/theme/javascript/core/keyboard.js
+++ /dev/null
@@ -1,39 +0,0 @@
-define([
- 'jQuery',
- 'Mousetrap',
- 'core/navigation',
- 'core/sidebar'
-], function($, Mousetrap, navigation, sidebar){
-
- // Bind a keyboard shortcuts
- function bindShortcut(keys, fn) {
- Mousetrap.bind(keys, function(e) {
- fn();
- return false;
- });
- }
-
-
- // Bind keyboard shortcuts
- var init = function() {
- // Next
- bindShortcut(['right'], function(e) {
- navigation.goNext();
- });
-
- // Prev
- bindShortcut(['left'], function(e) {
- navigation.goPrev();
- });
-
- // Toggle Summary
- bindShortcut(['s'], function(e) {
- sidebar.toggle();
- });
- };
-
- return {
- init: init,
- bind: bindShortcut
- };
-}); \ No newline at end of file
diff --git a/theme/javascript/core/loading.js b/theme/javascript/core/loading.js
deleted file mode 100644
index 1ddb213..0000000
--- a/theme/javascript/core/loading.js
+++ /dev/null
@@ -1,16 +0,0 @@
-define([
- "jQuery"
-], function($) {
- var showLoading = function(p) {
- $(".book").addClass("is-loading");
- p.always(function() {
- $(".book").removeClass("is-loading");
- });
-
- return p;
- };
-
- return {
- show: showLoading
- };
-}); \ No newline at end of file
diff --git a/theme/javascript/core/navigation.js b/theme/javascript/core/navigation.js
deleted file mode 100755
index 2e3f9eb..0000000
--- a/theme/javascript/core/navigation.js
+++ /dev/null
@@ -1,171 +0,0 @@
-define([
- "jQuery",
- "utils/url",
- "core/events",
- "core/state",
- "core/progress",
- "core/loading"
-], function($, URL, events, state, progress, loading) {
- var prev, next;
-
- var usePushState = (typeof history.pushState !== "undefined");
-
- var handleNavigation = function(relativeUrl, push) {
- var url = URL.join(window.location.pathname, relativeUrl);
- console.log("navigate to ", url, "baseurl="+relativeUrl, "current="+window.location.pathname);
-
- if (!usePushState) {
- // Refresh the page to the new URL if pushState not supported
- location.href = relativeUrl;
- return
- }
-
- return loading.show($.get(url)
- .done(function (html) {
- // Push url to history
- if (push) history.pushState({ path: url }, null, url);
-
- // Replace html content
- html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
- return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
- });
-
- var $page = $(html);
- var $pageHead = $page.find("[data-element=head]");
- var $pageBody = $page.find('.book');
-
- ////
- // Merge heads
- // !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...)
- ////
-
- // Update title
- document.title = $pageHead.find("title").text();
-
- // Reference to $("head");
- var $head = $("head");
-
- // Update next & prev <link> tags
- // Remove old
- $head.find("link[rel=prev]").remove();
- $head.find("link[rel=next]").remove();
-
- // Add new next * prev <link> tags
- $head.append($pageHead.find("link[rel=prev]"));
- $head.append($pageHead.find("link[rel=next]"));
-
- // Merge body
- var bodyClass = $(".book").attr("class");
- var scrollPosition = $('.book-summary .summary').scrollTop();
- $pageBody.toggleClass("with-summary", $(".book").hasClass("with-summary"))
-
- $(".book").replaceWith($pageBody);
- $(".book").attr("class", bodyClass);
- $('.book-summary .summary').scrollTop(scrollPosition);
-
- // Update state
- state.update($("html"));
- preparePage();
- })
- .fail(function (e) {
- location.href = relativeUrl;
- }));
- };
-
- var updateNavigationPosition = function() {
- var bodyInnerWidth, pageWrapperWidth;
-
- bodyInnerWidth = parseInt($('.body-inner').css('width'), 10);
- pageWrapperWidth = parseInt($('.page-wrapper').css('width'), 10);
- $('.navigation-next').css('margin-right', (bodyInnerWidth - pageWrapperWidth) + 'px');
- };
-
- var preparePage = function() {
- var $bookBody = $(".book-body");
- var $bookInner = $bookBody.find(".body-inner");
- var $pageWrapper = $bookInner.find(".page-wrapper");
-
- // Show progress
- progress.show();
-
- // Update navigation position
- updateNavigationPosition();
-
- // Focus on content
- $pageWrapper.focus();
-
- // Reset scroll
- $bookInner.scrollTop(0);
- $bookBody.scrollTop(0);
-
- // Notify
- events.trigger("page.change");
- };
-
- var isLeftClickEvent = function (e) {
- return e.button === 0;
- };
-
- var isModifiedEvent = function (e) {
- return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
- };
-
- var handlePagination = function (e) {
- if (isModifiedEvent(e) || !isLeftClickEvent(e)) {
- return;
- }
-
- e.stopPropagation();
- e.preventDefault();
-
- var url = $(this).attr('href');
- if (url) handleNavigation(url, true);
- };
-
- var goNext = function() {
- var url = $(".navigation-next").attr("href");
- if (url) handleNavigation(url, true);
- };
-
- var goPrev = function() {
- var url = $(".navigation-prev").attr("href");
- if (url) handleNavigation(url, true);
- };
-
-
-
- var init = function() {
- // Prevent cache so that using the back button works
- // See: http://stackoverflow.com/a/15805399/983070
- $.ajaxSetup({
- cache: false
- });
-
- // Recreate first page when the page loads.
- history.replaceState({ path: window.location.href }, '');
-
- // Back Button Hijacking :(
- window.onpopstate = function (event) {
- if (event.state === null) {
- return;
- }
- return handleNavigation(event.state.path, false);
- };
-
- $(document).on('click', ".navigation-prev", handlePagination);
- $(document).on('click', ".navigation-next", handlePagination);
- $(document).on('click', ".summary [data-path] a", handlePagination);
-
- $(window).resize(updateNavigationPosition);
-
- // Prepare current page
- preparePage();
- };
-
- return {
- init: init,
- goNext: goNext,
- goPrev: goPrev
- };
-});
-
diff --git a/theme/javascript/core/progress.js b/theme/javascript/core/progress.js
deleted file mode 100755
index a409a11..0000000
--- a/theme/javascript/core/progress.js
+++ /dev/null
@@ -1,73 +0,0 @@
-define([
- "lodash",
- "jQuery",
- "utils/storage",
- "core/state"
-], function(_, $, storage, state) {
- // Get current level
- var getCurrentLevel = function() {
- return state.level;
- };
-
- // Return all levels
- var getLevels = function () {
- var levels = $(".book-summary li[data-level]");
-
- return _.map(levels, function(level) {
- return $(level).data("level").toString();
- });
- };
-
- // Return a map chapter -> number (timestamp)
- var getProgress = function () {
- // Current level
- var progress = storage.get("progress", {});
-
- // Levels
- var levels = getLevels();
-
- _.each(levels, function(level) {
- progress[level] = progress[level] || 0;
- });
-
- return progress;
- };
-
- // Change value of progress for a level
- var markProgress = function (level, state) {
- var progress = getProgress();
-
- if (state == null) {
- state = true;
- }
-
- progress[level] = state
- ? Date.now()
- : 0;
-
- storage.set("progress", progress);
- };
-
- // Show progress
- var showProgress = function () {
- var progress = getProgress();
- var $summary = $(".book-summary");
-
- _.each(progress, function (value, level) {
- $summary.find("li[data-level='"+level+"']").toggleClass("done", value > 0);
- });
-
- // Mark current progress if we have not already
- if (!progress[getCurrentLevel()]) {
- markProgress(getCurrentLevel(), true);
- }
- };
-
- return {
- 'current': getCurrentLevel,
- 'levels': getLevels,
- 'get': getProgress,
- 'mark': markProgress,
- 'show': showProgress
- };
-}); \ No newline at end of file
diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js
deleted file mode 100755
index 4cbc3ed..0000000
--- a/theme/javascript/core/search.js
+++ /dev/null
@@ -1,114 +0,0 @@
-define([
- "jQuery",
- "lodash",
- "lunr",
- "utils/storage",
- "core/state",
- "core/sidebar"
-], function($, _, lunr, storage, state, sidebar) {
- var index = null;
-
- // Use a specific idnex
- var useIndex = function(data) {
- index = lunr.Index.load(data);
- };
-
- // Load complete index
- var loadIndex = function() {
- $.getJSON(state.basePath+"/search_index.json")
- .then(useIndex);
- };
-
- // Search for a term
- var search = function(q) {
- if (!index) return;
- var results = _.chain(index.search(q))
- .map(function(result) {
- var parts = result.ref.split("#")
- return {
- path: parts[0],
- hash: parts[1]
- }
- })
- .value();
-
- return results;
- };
-
- // Toggle search bar
- var toggleSearch = function(_state) {
- if (state != null && isSearchOpen() == _state) return;
-
- var $searchInput = $(".book-search input");
- state.$book.toggleClass("with-search", _state);
-
- // If search bar is open: focus input
- if (isSearchOpen()) {
- sidebar.toggle(true);
- $searchInput.focus();
- } else {
- $searchInput.blur();
- $searchInput.val("");
- sidebar.filter(null);
- }
- };
-
- // Return true if search bar is open
- var isSearchOpen = function() {
- return state.$book.hasClass("with-search");
- };
-
-
- var init = function() {
- loadIndex();
-
- // Toggle search
- $(document).on("click", ".book-header .toggle-search", function(e) {
- e.preventDefault();
- toggleSearch();
- });
-
-
- // Type in search bar
- $(document).on("keyup", ".book-search input", function(e) {
- var key = (e.keyCode ? e.keyCode : e.which);
- var q = $(this).val();
-
- if (key == 27) {
- e.preventDefault();
- toggleSearch(false);
- return;
- }
- if (q.length == 0) {
- sidebar.filter(null);
- storage.remove("keyword");
- } else {
- var results = search(q);
- sidebar.filter(
- _.pluck(results, "path")
- );
- storage.set("keyword", q);
- }
- })
-
- };
-
- // filter sidebar menu with current search keyword
- var recoverSearch = function() {
- var keyword = storage.get("keyword", "");
- if(keyword.length > 0) {
- if(!isSearchOpen()){
- toggleSearch();
- }
- sidebar.filter(_.pluck(search(keyword), "path"));
- }
- $(".book-search input").val(keyword);
- };
-
- return {
- init: init,
- search: search,
- toggle: toggleSearch,
- recover:recoverSearch
- };
-});
diff --git a/theme/javascript/core/sidebar.js b/theme/javascript/core/sidebar.js
deleted file mode 100755
index 2d9417e..0000000
--- a/theme/javascript/core/sidebar.js
+++ /dev/null
@@ -1,51 +0,0 @@
-define([
- 'jQuery',
- 'lodash',
- 'utils/storage',
- 'utils/platform',
- 'core/state'
-], function($, _, storage, platform, state) {
- // Toggle sidebar with or withour animation
- function toggleSidebar(_state, animation) {
- if (state != null && isOpen() == _state) return;
- if (animation == null) animation = true;
-
- state.$book.toggleClass('without-animation', !animation);
- state.$book.toggleClass('with-summary', _state);
-
- storage.set('sidebar', isOpen());
- }
-
- // Return true if sidebar is open
- function isOpen() {
- return state.$book.hasClass('with-summary');
- }
-
- // Prepare sidebar: state and toggle button
- function init() {
- // Init last state if not mobile
- if (!platform.isMobile) {
- toggleSidebar(storage.get('sidebar', true), false);
- }
- }
-
- // Filter summary with a list of path
- function filterSummary(paths) {
- var $summary = $('.book-summary');
-
- $summary.find('li').each(function() {
- var path = $(this).data('path');
- var st = paths == null || _.contains(paths, path);
-
- $(this).toggle(st);
- if (st) $(this).parents('li').show();
- });
- }
-
- return {
- init: init,
- isOpen: isOpen,
- toggle: toggleSidebar,
- filter: filterSummary
- };
-}); \ No newline at end of file
diff --git a/theme/javascript/core/state.js b/theme/javascript/core/state.js
deleted file mode 100755
index de5c65c..0000000
--- a/theme/javascript/core/state.js
+++ /dev/null
@@ -1,18 +0,0 @@
-define([
- "jQuery"
-], function() {
- var state = {};
-
- state.update = function(dom) {
- var $book = $(dom.find(".book"));
-
- state.$book = $book;
- state.level = $book.data("level");
- state.basePath = $book.data("basepath");
- state.revision = $book.data("revision");
- };
-
- state.update($);
-
- return state;
-}); \ No newline at end of file