summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/actions/navigation.js
blob: f13e878d8dc6492bfed31cbaf71a4bf14fae91a8 (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
const ACTION_TYPES = require('./TYPES');
const getPayload = require('../lib/getPayload');

const SUPPORTED = (
    typeof window !== 'undefined' &&
    window.history && window.history.pushState && window.history.replaceState &&
    // pushState isn't reliable on iOS until 5.
    !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/)
);

let PUSH_ID = 0;

/**
 * Generate a new state to be pushed or replaced
 * @param {Object}
 */
function genState() {
    return {
        id: (PUSH_ID++)
    };
}

/**
 * Push a new url into the navigation
 * @param {String} uri
 * @return {Action} action
 */
function pushURI(uri) {
    return () => {
        const state = genState();

        if (SUPPORTED) {
            window.history.pushState(state, '', uri);
        } else {
            redirect(uri);
        }
    };
}

/**
 * Replace current state in navigation
 * @param {String} uri
 * @return {Action} action
 */
function replaceURI(uri) {
    return () => {
        const state = genState();

        if (SUPPORTED) {
            window.history.replaceState(state, '', uri);
        } else {
            redirect(uri);
        }
    };
}

/**
 * Hard redirection
 * @param {String} uri
 * @return {Action} action
 */
function redirect(uri) {
    return () => {
        window.location.href = uri;
    };
}

/**
 * Fetch a new page and update the store accordingly
 * @param {String} uri
 * @param {Boolean} options.replace
 * @return {Action} action
 */
function fetchPage(uri, options = {}) {
    const { replace } = options;

    return (dispatch, getState) => {
        const prevURI = location.href;
        dispatch({ type: ACTION_TYPES.PAGE_FETCH_START });

        if (replace) {
            dispatch(replaceURI(uri));
        } else {
            dispatch(pushURI(uri));
        }

        window.fetch(uri, {
            credentials: 'include'
        })
        .then(
            response => {
                return response.text();
            }
        )
        .then(
            html => {
                const payload = getPayload(html);

                if (!payload) {
                    throw new Error('No payload found in page');
                }

                dispatch({ type: ACTION_TYPES.PAGE_FETCH_END, payload });
            }
        )
        .catch(
            error => {
                dispatch(replaceURI(prevURI));
                dispatch(redirect(uri));

                dispatch({ type: ACTION_TYPES.PAGE_FETCH_ERROR, error });
            }
        );
    };
}

/**
 * Fetch a new article
 * @param {SummaryArticle} article
 * @return {Action} action
 */
function fetchArticle(article) {
    return fetchPage(article.path);
}

/**
 * Update anchor for current page
 * @param {String} anchor
 * @return {Action} action
 */
function updateAnchor(anchor) {
    return { type: ACTION_TYPES.PAGE_UPDATE_ANCHOR, anchor };
}

/**
 * Update query for current page
 * @param {Object|Map} query
 * @return {Action} action
 */
function updateQuery(query) {
    return { type: ACTION_TYPES.PAGE_UPDATE_QUERY, query };
}

module.exports = {
    pushURI,
    fetchPage,
    fetchArticle,
    updateAnchor,
    updateQuery
};