summaryrefslogtreecommitdiffstats
path: root/public/js/icinga.js
blob: e8b8bccc88d799b7f63728c77e663a2fc825599c (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*! Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

/**
 * Icinga starts here.
 *
 * Usage example:
 *
 * <code>
 * var icinga = new Icinga({
 *   baseUrl: '/icinga',
 * });
 * </code>
 */
(function(window, $) {

    'use strict';

    var Icinga = function (config) {

        this.initialized = false;

        /**
         * Our config object
         */
        this.config = config;

        /**
         * Icinga.Logger
         */
        this.logger = null;

        /**
         * Icinga.UI
         */
        this.ui = null;

        /**
         * Icinga.Loader
         */
        this.loader = null;

        /**
         * Icinga.Events
         */
        this.events = null;

        /**
         * Icinga.Timer
         */
        this.timer = null;

        /**
         * Icinga.History
         */
        this.history = null;

        /**
         * Icinga.Utils
         */
        this.utils = null;

        /**
         * Additional site behavior
         */
        this.behaviors = {};

        /**
         * Loaded modules
         */
        this.modules = {};

        var _this = this;
        $(document).ready(function () {
            _this.initialize();
            _this = null;
        });
    };

    Icinga.prototype = {

        /**
         * Icinga startup, will be triggerd once the document is ready
         */
        initialize: function () {
            if (this.initialized) {
                return false;
            }

            this.timezone   = new Icinga.Timezone();
            this.utils      = new Icinga.Utils(this);
            this.logger     = new Icinga.Logger(this);
            this.timer      = new Icinga.Timer(this);
            this.ui         = new Icinga.UI(this);
            this.loader     = new Icinga.Loader(this);
            this.events     = new Icinga.Events(this);
            this.history    = new Icinga.History(this);
            var _this = this;
            $.each(Icinga.Behaviors, function(name, Behavior) {
                _this.behaviors[name.toLowerCase()] = new Behavior(_this);
            });

            this.timezone.initialize();
            this.timer.initialize();
            this.events.initialize();
            this.history.initialize();
            this.ui.initialize();
            this.loader.initialize();

            this.logger.info('Icinga is ready, running on jQuery ', $().jquery);
            this.initialized = true;

            // Trigger our own post-init event, `onLoad` is not reliable enough
            $(document).trigger('icinga-init');
        },

        /**
         * Load a given module by name
         *
         * @param   {string}    name
         *
         * @return  {boolean}
         */
        loadModule: function (name) {

            if (this.isLoadedModule(name)) {
                this.logger.error('Cannot load module ' + name + ' twice');
                return false;
            }

            if (! this.hasModule(name)) {
                this.logger.error('Cannot find module ' + name);
                return false;
            }

            this.modules[name] = new Icinga.Module(
                this,
                name,
                Icinga.availableModules[name]
            );
            return true;
        },

        /**
         * Whether a module matching the given name exists or is loaded
         *
         * @param   {string}    name
         *
         * @return  {boolean}
         */
        hasModule: function (name) {
            return this.isLoadedModule(name) ||
                'undefined' !== typeof Icinga.availableModules[name];
        },

        /**
         * Return whether the given module is loaded
         *
         * @param   {string}    name    The name of the module
         *
         * @returns {Boolean}
         */
        isLoadedModule: function (name) {
            return 'undefined' !==  typeof this.modules[name];
        },

        /**
         * Ensure we have loaded the javascript code for a module
         *
         * @param {string} moduleName
         */
        ensureModule: function(moduleName) {
            if (this.hasModule(moduleName) && ! this.isLoadedModule(moduleName)) {
                this.loadModule(moduleName);
            }
        },

        /**
         * If a container contains sub-containers for other modules,
         * make sure the javascript code for each module is loaded.
         *
         * Containers are identified by "data-icinga-module" which
         * holds the module name.
         *
         * @param container
         */
        ensureSubModules: function (container) {
            var icinga = this;

            $(container).find('[data-icinga-module]').each(function () {
                var moduleName = $(this).data('icingaModule');
                if (moduleName) {
                    icinga.ensureModule(moduleName);
                }
            });
        },

        /**
         * Get a module by name
         *
         * @param   {string}    name
         *
         * @return  {object}
         */
        module: function (name) {

            if (this.hasModule(name) && !this.isLoadedModule(name)) {
                this.modules[name] = new Icinga.Module(
                    this,
                    name,
                    Icinga.availableModules[name]
                );
            }

            return this.modules[name];
        },

        /**
         * Clean up and unload all Icinga components
         */
        destroy: function () {

            $.each(this.modules, function (name, module) {
                module.destroy();
            });

            this.timezone.destroy();
            this.timer.destroy();
            this.events.destroy();
            this.loader.destroy();
            this.ui.destroy();
            this.logger.debug('Icinga has been destroyed');
            this.logger.destroy();
            this.utils.destroy();

            this.modules = [];
            this.timer = this.events = this.loader = this.ui = this.logger =
                this.utils = null;
            this.initialized = false;
        },

        reload: function () {
            setTimeout(function () {
                var oldjQuery = window.jQuery;
                var oldConfig = window.icinga.config;
                var oldIcinga = window.Icinga;
                window.icinga.destroy();
                window.Icinga = undefined;
                window.$ = undefined;
                window.jQuery = undefined;
                jQuery = undefined;
                $ = undefined;

                oldjQuery.getScript(
                    oldConfig.baseUrl.replace(/\/$/, '') + '/js/icinga.min.js'
                ).done(function () {
                    var jQuery = window.jQuery;
                    window.icinga = new window.Icinga(oldConfig);
                    window.icinga.initialize();
                    window.icinga.ui.reloadCss();
                    oldjQuery = undefined;
                    oldConfig = undefined;
                    oldIcinga = undefined;
                }).fail(function () {
                    window.jQuery = oldjQuery;
                    window.$ = window.jQuery;
                    window.Icinga = oldIcinga;
                    window.icinga = new Icinga(oldConfig);
                    window.icinga.ui.reloadCss();
                });
            }, 0);
        }

    };

    window.Icinga = Icinga;

    Icinga.availableModules = {};

})(window, jQuery);