File: View.js

package info (click to toggle)
nagvis 1%3A1.9.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,556 kB
  • sloc: php: 28,986; javascript: 16,395; sh: 1,696; makefile: 16; xml: 8
file content (310 lines) | stat: -rw-r--r-- 11,937 bytes parent folder | download | duplicates (4)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*****************************************************************************
 *
 * View.js - Base class for all NagVis view classes
 *
 * Copyright (c) 2004-2016 NagVis Project (Contact: info@nagvis.org)
 *
 * License:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *****************************************************************************/

var View = Base.extend({
    id      : null,
    dom_obj : null,
    sum_obj : null,
    objects : null,

    constructor: function(id) {
        this.id = id;
        this.objects = {};
    },

    update: function(args) {
        var show = isset(args.show) ? '&show='+args.show : '';

        var data = [];
        for (var i = 0, len = args.data.length; i < len; i++)
            data.push('i[]='+args.data[i]);

        // Get the updated objects via bulk request
        call_ajax(oGeneralProperties.path_server+'?mod=' + args.mod + '&act=getObjectStates'
                       + show +'&ty=state'+getViewParams() + this.getFileAgeParams(), {
            response_handler : this.handleUpdate.bind(this),
            method           : "POST",
            post_data        : data.join('&')
        });

        // Need to re-raise repeated events?
        this.handleRepeatEvents();
    },

    render: function() {
        addEvent(window, 'resize', function() { g_view.scaleView() });
        addEvent(window, 'scroll', function() { g_view.scaleView() });
        this.scaleView();
    },

    /**
     * This callback function handles the ajax response of bulk object
     * status updates
     */
    handleUpdate: function(o) {
        // Stop processing these informations when the current view should not be
        // updated at the moment e.g. when reparsing the map after a changed mapcfg
        if (this.blockUpdates) {
            eventlog("ajax", "info", "Throwing new object information away since the view is blocked");
            return false;
        }

        if (!o) {
            eventlog("ajax", "info", "handleUpdate: got empty object. Terminating.");
            return false;
        }

        // Procees the "config changed" responses
        if (isset(o['status']) && o['status'] == 'CHANGED') {
            var oChanged = o['data'];

            for (var key in oChanged) {
                if (key == 'maincfg') {
                    eventlog("worker", "info", "Main configuration file was updated. Need to reload the page");
                    // Clear the scheduling timeout to prevent problems with FF4 bugs
                    if (g_worker_id)
                        window.clearTimeout(g_worker_id);
                    window.location.reload(true);
                    return;

                } else {
                    if (g_view.hasUnlocked()) {
                        eventlog("worker", "info", "Map config updated, but having unlocked objects - not reloading.");
                    } else {
                        eventlog("worker", "info", "Map configuration file was updated. Rerendering the map.");
                        g_view.render();
                        g_view.updateFileAges(oChanged);
                        return;
                    }
                }
            }
        }

        // I don't think empty maps make any sense. So when no objects are present:
        // Try to fetch them continously
        if (oLength(g_view.objects) === 0) {
            eventlog("worker", "info", "No objects found, reparsing...");
            g_view.init();
            return;
        }

        /*
         * Now proceed with real actions when everything is OK
         */

        if (o.length > 0)
            this.updateObjects(o, true);
    },

    updateFileAges: function(data) {
        for (var key in data)
            oFileAges[key] = data[key];
    },

    // Bulk update object states (and possibly configs) and then visualize changes
    updateObjects: function(attrs, only_state) {
        var at_least_one_changed = false;

        // Loop all object which have new information
        for (var i = 0, len = attrs.length; i < len; i++) {
            var objectId = attrs[i].object_id;

            // Object not found. This should only be happen for new objects which have
            // just been added to the map by the user. In this case we always have "full"
            // object information, not only state attrs
            if (!isset(this.objects[objectId])) {
                // this is only relevant for ViewMap at the moment
                this.addObject(attrs[i]);
                this.renderObject(objectId);
                at_least_one_changed = true; // always reload summary state
            }
            else {
                at_least_one_changed |= this.objects[objectId].updateAttrs(attrs[i], only_state);
            }
        }

        if (at_least_one_changed)
            this.handleStateChanged();
    },

    handleStateChanged: function() {},

    // Is called by an object to add the rendered dom object to the map
    drawObject: function(obj) {},
    eraseObject: function(obj) {},

    /**
     * Is called by the worker function to check all objects for repeated
     * events to be triggered independent of the state updates.
     */
    handleRepeatEvents: function() {
        eventlog("worker", "debug", "handleRepeatEvents: Start");
        for (var i in this.objects) {
            // Trigger repeated event checking/raising for eacht stateful object which
            // has event_time_first set (means there was an event before which is
            // required to be repeated some time)
            if (this.objects[i].has_state && this.objects[i].event_time_first !== null) {
                this.objects[i].checkRepeatEvents();
            }
        }
        eventlog("worker", "debug", "handleRepeatEvents: End");
    },

    getFileAgeParams: function() {
        var addParams = '';
        if (g_view.type === 'map' && this.id !== false)
            addParams = '&f[]=map,' + this.id + ',' + oFileAges[this.id];
        return '&f[]=maincfg,maincfg,' + oFileAges['maincfg'] + addParams;
    },

    // Detects objects with deprecated state information
    getObjectsToUpdate: function() {
        eventlog("worker", "debug", "getObjectsToUpdate: Start");
        var stateful = [],
            stateless = [];

        // Assign all object which need an update indexes to return Array
        for (var i in this.objects) {
            if (this.objects[i].lastUpdate <= iNow - oWorkerProperties.worker_update_object_states) {
                if (this.objects[i] instanceof NagVisStatefulObject) {
                    stateful.push(i);
                } else if (this.objects[i].conf.enable_refresh
                           && this.objects[i].conf.enable_refresh == '1') {
                    // Do not update stateless objects where enable_refresh=0
                    stateless.push(i);
                }
            }
        }

        eventlog("worker", "debug", "getObjectsToUpdate: Stateful: "+stateful.length
                                   +" Stateless: "+stateless.length);
        return [stateful, stateless];
    },

    // Renders basic information like favicon and page title
    renderPageBasics: function() {
        if (this.sum_obj)
            favicon.change(this.getFaviconImage(this.sum_obj.conf));

        document.title = oPageProperties.page_title;

        if (this.sum_obj)
            document.body.style.backgroundColor = this.getBackgroundColor(this.sum_obj.conf);
    },

    // Gets the background color of the map by the summary state of the map
    getBackgroundColor: function(oObj) {
        if (!oPageProperties.event_background || oPageProperties.event_background != '1')
            return oPageProperties.background_color;

        var sColor;
        // When state is PENDING, OK, UP, set default background color
        if (!oObj.summary_state || oObj.summary_state == 'PENDING'
           || oObj.summary_state == 'OK' || oObj.summary_state == 'UP')
            sColor = oPageProperties.background_color;
        else {
            sColor = oStates[oObj.summary_state].bgcolor;

            // Ack or downtime?
            if (oObj.summary_in_downtime && oObj.summary_in_downtime == 1)
                if (isset(oStates[oObj.summary_state]['downtime_bgcolor'])
                    && oStates[oObj.summary_state]['downtime_bgcolor'] != '')
                    sColor = oStates[oObj.summary_state]['downtime_bgcolor'];
                else
                    sColor = lightenColor(sColor, 100, 100, 100);

            else if (oObj.summary_problem_has_been_acknowledged
                     && oObj.summary_problem_has_been_acknowledged == 1)

                if (isset(oStates[oObj.summary_state]['ack_bgcolor'])
                    && oStates[oObj.summary_state]['ack_bgcolor'] != '')
                    sColor = oStates[oObj.summary_state]['ack_bgcolor'];
                else
                    sColor = lightenColor(sColor, 100, 100, 100);
        }
        return sColor;
    },
    
    // Gets the favicon of the page representating the state of the view
    getFaviconImage: function(oObj) {
        var sFavicon;
    
        if (oObj.summary_in_downtime && oObj.summary_in_downtime == 1)
            sFavicon = 'downtime';
        else if (oObj.summary_problem_has_been_acknowledged
                 && oObj.summary_problem_has_been_acknowledged == 1)
            sFavicon = 'ack';
        else if (oObj.summary_state.toLowerCase() == 'unreachable')
            sFavicon = 'down';
        else
            sFavicon = oObj.summary_state.toLowerCase();
        
        return oGeneralProperties.path_images+'internal/favicon_'+sFavicon+'.png';
    },

    /**
     * Hack to scale elements which should fill 100% of the windows viewport. The
     * problem here is that some map objects might increase the width of the map
     * so that the viewport is not enough to display the whole map. In that case
     * elements with a width of 100% don't scale to the map width. Instead of this
     * the elements are scaled to the viewports width.
     * This changes the behaviour and resizes the 100% elements to the map size.
     *
     * @author  Lars Michelsen <lm@larsmichelsen.com>
     */
    scaleView: function() {
        var header  = document.getElementById('header');
        var content = this.dom_obj;
        if (!content)
            return;

        var headerSpacer = document.getElementById('headerspacer');
        if (header) {
            header.style.width = pageWidth() + 'px';
            if (headerSpacer) {
                headerSpacer.style.height = header.clientHeight + 'px';
                headerSpacer = null;
            }
        }

        content.style.top = getHeaderHeight() + 'px';

        if (typeof sidebarUpdatePosition == "function") {
            sidebarUpdatePosition();
        }
    },

    // Transforms the view specific coordinates to browser x/y coordinates.
    // This is executed when loading object coordinates from persistance.
    project: function(x, y) {
        return [x, y];
    },

    // Transforms the X/Y coordinates to view specific coords, like
    // on worldmaps to lat/long coordinates. This is executed when
    // persisting the object coordinates after a change
    unproject: function(x, y) {
        return [x, y];
    },

});