summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/navigation.js
blob: 2a6d1df04ce3c53d464ef783d0e1178ef38188c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
define([
    "jQuery",
    "core/progress",
    "core/exercise",
    "core/quiz"
], function($, progress, exercises, quiz) {
    var prev, next;

    var updateHistory = function(url, title) {
        history.pushState({ path: url }, title, url);
    };

    var handleNavigation = function(url, push) {
        if (typeof history.pushState === "undefined") {
            // Refresh the page to the new URL if pushState not supported
            location.href = url;
            return
        }

        return $.get(url)
        .done(function (html) {
            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]");

            $("head").html($pageHead.html());
            $('.book-header').html($page.find('.book-header').html());
            $('.book-body').html($page.find('.book-body').html());
            $('.book-summary').html($page.find('.book-summary').html());

            if (push) updateHistory(url, null);
            preparePage();
        })
        .fail(function () {
            location.href = url;
        });
    };

    var preparePage = function() {
        // Bind exercises/quiz
        exercises.init();
        quiz.init();

        // Show progress
        progress.show();

        // Reset scroll
        $(".book-body").scrollTop(0);

        // Focus on content
        $(".book-body").focus();
    };

    var handlePagination = function (e) {
        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);

        // Prepare current page
        preparePage();
    };

    return {
        init: init,
        goNext: goNext,
        goPrev: goPrev
    };
});