summaryrefslogtreecommitdiff
path: root/elm/global.js
blob: 367507c5d3f201916a4a852aed581fc6fc2a592a (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
/* Add the X-CSRF-Token header to every POST request. Based on:
 * https://stackoverflow.com/questions/24196140/adding-x-csrf-token-header-globally-to-all-instances-of-xmlhttprequest/24196317#24196317
 */
(function() {
    var open = XMLHttpRequest.prototype.open,
        token = document.querySelector('meta[name=csrf-token]').content;

    XMLHttpRequest.prototype.open = function(method, url) {
        var ret = open.apply(this, arguments);
        this.dataUrl = url;
        if(method.toLowerCase() == 'post' && /^\//.test(url))
            this.setRequestHeader('X-CSRF-Token', token);
        return ret;
    };
})();


/* Find all divs with a data-elm-module and embed the given Elm module in the div */
document.querySelectorAll('div[data-elm-module]').forEach(function(el) {
    var mod = el.getAttribute('data-elm-module').split('.').reduce(function(p, c) { return p[c] }, window.Elm);
    var flags = el.getAttribute('data-elm-flags');
    if(flags)
        mod.init({ node: el, flags: JSON.parse(flags)});
    else
        mod.init({ node: el });
});


/* "checkall" checkbox, usage:
 *
 *    <input type="checkbox" class="checkall" name="$somename">
 *
 *  Checking that will synchronize all other checkboxes with name="$somename".
 */
document.querySelectorAll('input[type=checkbox].checkall').forEach(function(el) {
    el.onclick = function() {
        document.querySelectorAll('input[type=checkbox][name="'+el.name+'"]').forEach(function(el2) {
            if(!el2.classList.contains('hidden')) {
                if(el2.checked != el.checked)
                    el2.click();
            }
        });
    };
});


/* "checkhidden" checkbox, usage:
 *
 *    <input type="checkbox" class="checkhidden" value="$somename">
 *
 * Checking that will toggle the 'hidden' class of all elements with the "$somename" class.
 */
document.querySelectorAll('input[type=checkbox].checkhidden').forEach(function(el) {
    el.onclick = function() {
        document.querySelectorAll('.'+el.value).forEach(function(el2) {
            el2.classList.toggle('hidden', !el.checked);
        });
    };
});