aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/ready.js
blob: 3464ec6ab1becdc444393b04c6007e37724c8b51 (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
/*jslint esversion: 6*/

function ready(callback, top = false) {
    if (top) {
        ready.handlers.unshift({
            type: false,
            callback: callback
        });
    } else {
        ready.handlers.push({
            type: false,
            callback: callback
        });
    }
    return this; // = STUDIP
}
ready.handlers = [];
ready.trigger = function (type, context) {
    ready.handlers.filter(handler => !handler.type || handler.type === type).forEach(handler => {
        handler.callback({
            target: context || document
        });
    });

    let event = $.Event('studip-ready');
    event.target = context || document;
    $(document).trigger(event);
};

function domReady(callback, top = false) {
    if (top) {
        ready.handlers.unshift({
            type: 'dom',
            callback: callback
        });
    } else {
        ready.handlers.push({
            type: 'dom',
            callback: callback
        });
    }
    return this; // = STUDIP
}

function dialogReady(callback, top = false) {
    if (top) {
        ready.handlers.unshift({
            type: 'dialog',
            callback: callback
        });
    } else {
        ready.handlers.push({
            type: 'dialog',
            callback: callback
        });
    }
    return this; // = STUDIP
}

export { ready, domReady, dialogReady };