File: ElementContext.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 (383 lines) | stat: -rw-r--r-- 15,387 bytes parent folder | download
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*****************************************************************************
 *
 * ElementContext.js - This class realizes the context menus
 *
 * 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 g_context_templates = {};
var g_context_open      = null;

// Hides the currently open context menu
function contextHide() {
    if (g_context_open !== null)
        g_context_open.hide();
}

function context_handle_global_mousedown(event) {
    event = event ? event : window.event; // IE FIX
    if (usesSource('worldmap'))
        event = event.originalEvent;
    var target = getTargetRaw(event);

    while (target && !has_class(target, 'context'))
        target = target.parentNode;

    if (target === null) {
        // when not clicked on the context menu, hide it
        contextHide();
    }
    else {
        // Check for click on the context menu and do nothing in this case
        var object_id = target.id.split('-')[0];
        if (g_context_open && object_id == g_context_open.obj.conf.object_id)
            return preventDefaultEvents(event);
    }
}

var ElementContext = Element.extend({
    template_html : null,
    spacing       : 5,    // px from screen border
    coords        : null, // list of x, y coordinates of the hover menu top left corner

    update: function() {
        this.getTemplate();
    },

    render: function() {
        this.getTemplate();
        if (this.template_html === null || this.template_html === true)
            return false; // template not available yet, skip rendering

        this.renderMenu();
    },

    draw: function() {
        // Not rendered yet, e.g. because template was not fetched yet during
        // initial rendering. Re-try rendering now
        if (this.dom_obj === null)
            this.render();

        this.base();

        addEvent(this.obj.trigger_obj, 'contextmenu', function(element_obj) {
            return function(event) {
                // During render/drawing calls the template was not ready, so the menu
                // has not been drawn yet. Do it now.
                if (element_obj.dom_obj === null) {
                    element_obj.draw();
                }
                element_obj.coords = [event.clientX, event.clientY];
                element_obj.show();
                return preventDefaultEvents(event);
            };
        }(this));
    },

    erase: function() {
        removeEvent(this.obj.trigger_obj, 'mousedown');
        removeEvent(this.obj.trigger_obj, 'contextmenu');
        this.base();
    },

    lock: function() {
        // FIXME: Re-render for new content
        this.erase();
        this.render();
        this.draw();
    },

    unlock: function() {
        // FIXME: Re-render for new content
        this.erase();
        this.render();
        this.draw();
    },

    //
    // END OF PUBLIC METHODS
    //

    getTemplate: function() {
        var name = this.obj.conf.context_template;
        if (!isset(g_context_templates[name])) {
            this.requestTemplate();
        }
        else if (g_context_templates[name] !== true) {
            this.template_html = g_context_templates[name];
            this.replaceStaticMacros();
        }
    },

    handleTemplate: function (templates) {
        var name = templates[0]['name'];
        var code = templates[0]['code'];

        g_context_templates[name] = code;

        this.getTemplate(); // assign template to the object

        // Load css file is one is available
        if (isset(templates[0]['css_file'])) {
            // This is needed for some old browsers which do no load css files
            // which are included in such fetched html code
            var oLink = document.createElement('link');
            oLink.href = templates[0]['css_file'];
            oLink.rel = 'stylesheet';
            oLink.type = 'text/css';
            document.getElementsByTagName("head")[0].appendChild(oLink);
        }
    },

    requestTemplate: function () {
        call_ajax(oGeneralProperties.path_server+'?mod=General&act=getContextTemplate'
                  +'&name[]='+escapeUrlValues(this.obj.conf.context_template), {
            response_handler: this.handleTemplate.bind(this)
        });
        g_context_templates[this.obj.conf.context_template] = true; // mark as already requested
    },

    show: function() {
        hoverHide();

        // document.body.scrollTop does not work in IE
        var scrollTop = document.body.scrollTop ? document.body.scrollTop :
                                                  document.documentElement.scrollTop;
        var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft :
                                                    document.documentElement.scrollLeft;
    
        // hide the menu first to avoid an "up-then-over" visual effect
        this.dom_obj.style.display = 'none';
        this.dom_obj.style.left = this.coords[0] + this.spacing + scrollLeft - getSidebarWidth() + 'px';
        this.dom_obj.style.top = this.coords[1] + this.spacing + scrollTop - getHeaderHeight() + 'px';
        this.dom_obj.style.display = '';
    
        // Check if the context menu is "in screen".
        // When not: reposition
        var contextLeft = parseInt(this.dom_obj.style.left.replace('px', ''));
        if(contextLeft+this.dom_obj.clientWidth > pageWidth()) {
            // move the context menu to the left
            this.dom_obj.style.left = contextLeft - this.dom_obj.clientWidth + 'px';
        }
    
        var contextTop = parseInt(this.dom_obj.style.top.replace('px', ''));
        if(contextTop+this.dom_obj.clientHeight > pageHeight()) {
            // Only move the context menu to the top when the new top will not be
            // out of sight
            if(contextTop - this.dom_obj.clientHeight >= 0) {
                this.dom_obj.style.top = contextTop - this.dom_obj.clientHeight + 'px';
            }
        }

        g_context_open = this;
    },

    hide: function() {
        if (this.dom_obj) {
            this.dom_obj.style.display = 'none';
        }
        if(g_context_open) {
            const context_obj = document.getElementById(g_context_open.obj.conf.object_id+'-context');
            if (context_obj)
                context_obj.style.display = 'none';
            g_context_open = null;
        }
    },

    renderMenu: function () {
        // Only create a new div when the context menu does not exist
        // Create context menu div
        var contextMenu = document.createElement('div');
        this.dom_obj = contextMenu;
        contextMenu.setAttribute('id', this.obj.conf.object_id+'-context');
        contextMenu.className = 'context';
        contextMenu.style.display = 'none';

        // Append template code to context menu div
        contextMenu.innerHTML = this.template_html;
    },

    // Replaces object configuration specific macros in the template code
    replaceStaticMacros: function() {
        var oSectionMacros = {};

        // Break when no template code found
        if(!this.template_html || this.template_html === '') {
            return false;
        }

        var oMacros = {
            'obj_id':      this.obj.conf.object_id,
            'type':        this.obj.conf.type,
            'name':        this.obj.conf.name,
            'alias':       this.obj.conf.alias,
            'address':     this.obj.conf.address,
            'html_cgi':    this.obj.conf.htmlcgi,
            'backend_id':  this.obj.conf.backend_id,
            'custom_1':    this.obj.conf.custom_1,
            'custom_2':    this.obj.conf.custom_2,
            'custom_3':    this.obj.conf.custom_3
        };

        if (g_view.type === 'map')
            oMacros.map_name = g_view.id;

        if(this.obj.conf.type === 'service') {
            oMacros.service_description = escapeUrlValues(this.obj.conf.service_description);

            oMacros.pnp_hostname = this.obj.conf.name.replace(/\s/g,'%20');
            oMacros.pnp_service_description = this.obj.conf.service_description.replace(/\s/g,'%20');
        } else
            oSectionMacros.service = '<!--\\sBEGIN\\sservice\\s-->.+?<!--\\sEND\\sservice\\s-->';

        // Macros which are only for hosts
        if(this.obj.conf.type === 'host')
            oMacros.pnp_hostname = this.obj.conf.name.replace(/\s/g,'%20');
        else
            oSectionMacros.host = '<!--\\sBEGIN\\shost\\s-->.+?<!--\\sEND\\shost\\s-->';

        if(this.obj.conf.type !== 'host' && this.obj.conf.type !== 'shape')
            oSectionMacros.host_or_shape = '<!--\\sBEGIN\\shost_or_shape\\s-->.+?<!--\\sEND\\shost_or_shape\\s-->';

        if(this.obj.conf.type === 'line' || this.obj.conf.type == 'shape'
           || this.obj.conf.type == 'textbox' || this.obj.conf.type === 'container')
            oSectionMacros.stateful = '<!--\\sBEGIN\\sstateful\\s-->.+?<!--\\sEND\\sstateful\\s-->';

        // Remove unlocked section for locked objects
        if(this.obj.bIsLocked)
            oSectionMacros.unlocked = '<!--\\sBEGIN\\sunlocked\\s-->.+?<!--\\sEND\\sunlocked\\s-->';
        else
            oSectionMacros.locked = '<!--\\sBEGIN\\slocked\\s-->.+?<!--\\sEND\\slocked\\s-->';

        if(!oViewProperties || !oViewProperties.permitted_edit)
            oSectionMacros.permitted_edit = '<!--\\sBEGIN\\spermitted_edit\\s-->.+?<!--\\sEND\\spermitted_edit\\s-->';

        if(!oViewProperties || !oViewProperties.permitted_perform)
            oSectionMacros.permitted_perform = '<!--\\sBEGIN\\spermitted_perform\\s-->.+?<!--\\sEND\\spermitted_perform\\s-->';

        if(usesSource('automap')) {
            oSectionMacros.not_automap = '<!--\\sBEGIN\\snot_automap\\s-->.+?<!--\\sEND\\snot_automap\\s-->';
	    // Skip the root change link for the root host
            if(this.obj.conf.name === getUrlParam('root'))
		oSectionMacros.automap_not_root = '<!--\\sBEGIN\\sautomap_not_root\\s-->.+?<!--\\sEND\\sautomap_not_root\\s-->';
        } else {
	    oSectionMacros.automap_not_root = '<!--\\sBEGIN\\sautomap_not_root\\s-->.+?<!--\\sEND\\sautomap_not_root\\s-->';
            oSectionMacros.automap = '<!--\\sBEGIN\\sautomap\\s-->.+?<!--\\sEND\\sautomap\\s-->';
        }
        if(this.obj.conf.view_type !== 'line')
            oSectionMacros.line = '<!--\\sBEGIN\\sline\\s-->.+?<!--\\sEND\\sline\\s-->';
        if(this.obj.conf.view_type !== 'line'
           || (this.obj.conf.line_type == 11 || this.obj.conf.line_type == 12))
            oSectionMacros.line_type = '<!--\\sBEGIN\\sline_two_parts\\s-->.+?<!--\\sEND\\sline_two_parts\\s-->';

        // Replace hostgroup range macros when not in a hostgroup
        if(this.obj.conf.type !== 'hostgroup')
            oSectionMacros.hostgroup = '<!--\\sBEGIN\\shostgroup\\s-->.+?<!--\\sEND\\shostgroup\\s-->';

        // Replace servicegroup range macros when not in a servicegroup
        if(this.obj.conf.type !== 'servicegroup' && !(this.obj.conf.type === 'dyngroup' && this.obj.conf.object_types == 'service'))
            oSectionMacros.servicegroup = '<!--\\sBEGIN\\sservicegroup\\s-->.+?<!--\\sEND\\sservicegroup\\s-->';

        // Replace map range macros when not in a hostgroup
        if(this.obj.conf.type !== 'map')
            oSectionMacros.map = '<!--\\sBEGIN\\smap\\s-->.+?<!--\\sEND\\smap\\s-->';

        // Loop all registered actions, check wether or not this action should be shown for this object
        // and either add the replacement section or not
        for (var key in oGeneralProperties.actions) {
            if(key == "indexOf")
                continue; // skip indexOf prototype (seems to be looped in IE)
            var action = oGeneralProperties.actions[key];
            var hide = false;

            // Check object type
            hide = action.obj_type.indexOf(this.obj.conf.type) == -1;

            // Only check the condition when not already hidden by another check before
            if(!hide && isset(action.client_os) && action.client_os.length > 0) {
                // Check the client os
                var os = navigator.platform.toLowerCase();
                if (os.indexOf('win') !== -1)
                    os = 'win';
                else if (os.indexOf('linux') !== -1)
                    os = 'lnx';
                else if (os.indexOf('mac') !== -1)
                    os = 'mac';

                hide = action.client_os.indexOf(os) == -1;
            }

            // Only check the condition when not already hidden by another check before
            if(!hide && isset(action.condition) && action.condition !== '') {
                var cond = action.condition;

                var op = '';
                if (cond.indexOf('~') != -1) {
                    op = '~';
                } else if (cond.indexOf('=') != -1) {
                    op = '=';
                }

                var parts = cond.split(op);
                var attr  = parts[0];
                var val   = parts[1];
                var to_be_checked;
                if (isset(this.obj.conf.custom_variables) && isset(this.obj.conf.custom_variables[attr])) {
                    to_be_checked = this.obj.conf.custom_variables[attr];
                } else if(isset(this.obj.conf[attr])) {
                    to_be_checked = this.obj.conf[attr];
                }

                if (to_be_checked) {
                    if (op == '=' && to_be_checked != val) {
                        hide = true;
                    } else if (op == '~' && to_be_checked.indexOf(val) == -1) {
                        hide = true;
                    }
                } else {
                    hide = true;
                }
            }

            // Remove the section macros of not hidden actions
            if(!hide) {
                oSectionMacros['action_'+key] = '<!--\\s(BEGIN|END)\\saction_'+key+'\\s-->';
            }
            cond = null;
            action = null;
        }

        // Remove all not hidden actions
        oSectionMacros['actions'] = '<!--\\sBEGIN\\saction_.+?\\s-->.+?<!--\\sEND\\saction_.+?\\s-->';

        // Loop and replace all unwanted section macros
        for (var key in oSectionMacros) {
            var regex = getRegEx('section-'+key, oSectionMacros[key], 'gm');
            this.template_html = this.template_html.replace(regex, '');
            regex = null;
        }
        oSectionMacros = null;

        // Loop and replace all normal macros
        this.template_html = this.template_html.replace(/\[(\w*)\]/g,
                                     function(){ return oMacros[ arguments[1] ] || '';});
        oMacros = null;
    }

});