summaryrefslogtreecommitdiffstats
path: root/public/js/icinga/eventlistener.js
blob: 678e775bcd71214a5dfb1472a9a585f39f0d79ad (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
/*! Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

/**
 * EventListener contains event handlers and can bind / and unbind them from
 * event emitting objects
 */
(function(Icinga, $) {

    "use strict";

    var EventListener = function (icinga) {
        this.icinga = icinga;
        this.handlers = [];
    };

    /**
     * Add an handler to this EventLister
     *
     * @param evt   {String}    The name of the triggering event
     * @param cond  {String}    The filter condition
     * @param fn    {Function}  The event handler to execute
     * @param scope {Object}    The optional 'this' of the called function
     */
    EventListener.prototype.on = function(evt, cond, fn, scope) {
        if (typeof cond === 'function') {
            scope = fn;
            fn = cond;
            cond = 'body';
        }
        this.icinga.logger.debug('on: ' + evt + '(' + cond + ')');
        this.handlers.push({ evt: evt, cond: cond, fn: fn, scope: scope });
    };

    /**
     * Bind all listeners to the given event emitter
     *
     * All event handlers will be executed when the associated event is
     * triggered on the given Emitter.
     *
     * @param emitter   {String}   An event emitter that supports the function
     *                             'on' to register listeners
     */
    EventListener.prototype.bind = function (emitter) {
        var _this = this;

        if (typeof emitter.jquery === 'undefined') {
            emitter = $(emitter);
        }

        $.each(this.handlers, function(i, handler) {
            _this.icinga.logger.debug('bind: ' + handler.evt + '(' + handler.cond + ')');
            emitter.on(
                handler.evt, handler.cond,
                {
                    self: handler.scope || emitter,
                    icinga: _this.icinga
                }, handler.fn
            );
        });
    };

    /**
     * Unbind all listeners from the given event emitter
     *
     * @param emitter   {String}    An event emitter that supports the function
     *                              'off' to un-register listeners.
     */
    EventListener.prototype.unbind = function (emitter) {
        var _this = this;
        $.each(this.handlers, function(i, handler) {
            _this.icinga.logger.debug('unbind: ' + handler.evt + '(' + handler.cond + ')');
            emitter.off(handler.evt, handler.cond, handler.fn);
        });
    };

    Icinga.EventListener = EventListener;

}) (Icinga, jQuery);