summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/navigation.js
blob: e6143923da10127c1b4edd0204532dd4cd8c4284 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
define([
    "jQuery",
    "utils/path",
    "core/events",
    "core/state",
    "core/search",
    "core/progress",
    "core/exercise",
    "core/quiz",
    "core/loading"
], function($, path, events, state, search, progress, exercises, quiz, loading) {
    var prev, next;
    var githubCountStars, githubCountWatch;

    var usePushState = (typeof history.pushState !== "undefined");

    var handleNavigation = function(relativeUrl, push) {
        var url = path.isAbsolute(relativeUrl) ? relativeUrl : path.join(path.dirname(window.location.pathname), relativeUrl);
        console.log("navigate to ", url, "baseurl="+relativeUrl);

        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]");

            // Merge heads
            var headContent = $pageHead.html()

            $("head style").each(function() {
                headContent = headContent + this.outerHTML
            });
            $("head").html(headContent);

            // Update header, body
            $('.book-header').html($page.find('.book-header').html());
            $('.book-body').html($page.find('.book-body').html());

            // Update summary
            var scrollPosition = $('.book-summary .summary').scrollTop();
            $('.book-summary').html($page.find('.book-summary').html());
            $('.book-summary .summary').scrollTop(scrollPosition);

            // Update state
            state.update($("html"));
            preparePage();
        })
        .fail(function (e) {
            location.href = relativeUrl;
        }));
    };

    var updateGitHubCounts = function() {
        $(".book-header .count-star span").text(githubCountStars);
        $(".book-header .count-watch span").text(githubCountWatch);
    };

    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 $pageWrapper = $(".book-body .page-wrapper");

        // Bind exercises/quiz
        exercises.init();
        quiz.init();

        // Show progress
        progress.show();

        // Update navigation position
        updateNavigationPosition();

        // Reset scroll
        $pageWrapper.scrollTop(0);

        // Focus on content
        $pageWrapper.focus();

        // Prepare search bar
        search.prepare();

        // Update GitHub count
        if (state.githubId) {
            if (githubCountStars) {
                updateGitHubCounts();
            } else {
                $.getJSON("https://api.github.com/repos/"+state.githubId)
                .done(function(repo) {
                    githubCountStars = repo.stargazers_count;
                    githubCountWatch = repo.subscribers_count;
                    updateGitHubCounts();
                });
            }
        }

        // Send to mixpanel
        events.trigger("page.change");
    };

    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);
        
        $(window).resize(updateNavigationPosition);

        // Prepare current page
        preparePage();
    };

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