File size: 3,070 Bytes
e5d8d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb35e88
 
 
e5d8d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb35e88
e5d8d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Created by hen on 5/15/17.
 */

export default class URLHandler {

    static basicURL() {
        const url_path = window.location.pathname.split('/').slice(0, -2).join('/');

        return window.location.origin + (url_path.length ? url_path : '');
    }

    /**
     * Read all URL parameters into a map.
     * @returns {Map} the url parameters as a key-value store (ES6 map)
     */
    static get parameters(): object {
        // Adapted from:  http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
        const query = window.location.search.substring(1);
        const vars = query.split('&');

        const urlParameters = {};

        const isInt = x => (/^[0-9]+$/).test(x);
        const isFloat = x => (/^[0-9]+\.[0-9]*$/).test(x);

        const typeCast = val => {
            if (isInt(val)) {
                return Number.parseInt(val, 10);
            } else if (isFloat(val)) {
                return Number.parseFloat(val);
            }
            // else:
            return val;
        }


        vars.forEach(v => {
            if (v.length > 0) {
                const splits = v.split('=');
                const key = decodeURIComponent(splits[0]);
                const raw_value = decodeURIComponent(splits[1]);

                urlParameters[key] = raw_value.length < 1 ? '' : typeCast(raw_value);
            }
        });

        return urlParameters;

    }


    /**
     * Generates an URL string from a map of url parameters
     * @param {{}} urlParameters - the map of parameters
     * @returns {string} - an URI string
     */
    static urlString(urlParameters: object) {
        const attr = [];
        Object.keys(urlParameters).forEach(k => {
            const v = urlParameters[k];
            if (v !== undefined) {
                attr.push(encodeURI(k + '=' + v))
            }
        });


        const url = window.location.pathname;
        let res = url.substring(url.lastIndexOf('/') + 1);
        if (attr.length > 0) {
            res += '?' + attr.join('&')
        }

        return res;
    }

    static updateURLParam(key: string, value: string | any[], addToBrowserHistory = true) {
        const currentParams = URLHandler.parameters;
        currentParams[key] = value;
        URLHandler.updateUrl(currentParams, addToBrowserHistory);
    }

    // /**
    //  * Generates a key-value map of all URL params and replaces replaceKeys
    //  * @param updateKeys
    //  */
    // static updateURLParams(updateKeys) {
    //     const currentParams = URLHandler.parameters;
    //     Object.keys(updateKeys).forEach((k) => currentParams[k] = updateKeys[k])
    //     return currentParams;
    // }


    static updateUrl(urlParameters: object, addToBrowserHistory = true) {
        if (addToBrowserHistory) {
            window.history.pushState(urlParameters, '',
                URLHandler.urlString(urlParameters))
        } else {
            window.history.replaceState(urlParameters, '',
                URLHandler.urlString(urlParameters))
        }
    }

}