Name: cockpit/lib/contextmenu.js 
1:
/*
2:
  This file is part of CPEE.
3:
 
4:
  CPEE is free software: you can redistribute it and/or modify it under the terms
5:
  of the GNU General Public License as published by the Free Software Foundation,
6:
  either version 3 of the License, or (at your option) any later version.
7:
 
8:
  CPEE is distributed in the hope that it will be useful, but WITHOUT ANY
9:
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10:
  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
11:
 
12:
  You should have received a copy of the GNU General Public License along with
13:
  CPEE (file COPYING in the main directory).  If not, see
14:
  <http://www.gnu.org/licenses/>.
15:
*/
16:
 
17:
function contextmenu(items, e) {
18:
  var x = e.pageX;
19:
  var y = e.pageY;
20:
  if($('div.contextmenu').length > 0) contextmenu_remove();
21:
  var div = $('<div class="contextmenu"><table class="contextmenu"/></div>');
22:
  for(head in items) {
23:
    div.children(':first').append('<tr class="contextmenuheader"><td colspan="2">' + head + '</td></tr>');
24:
    for(item in items[head]) {
25:
      var icon = null;
26:
      if(items[head][item].menu_icon) {
27:
        icon = $X('<svg xmlns="http://www.w3.org/2000/svg" version="1.1">' +
28:
                    '<g transform="translate(1,1) scale(0.5, 0.5)"/>' +
29:
                  '</svg>');
30:
        icon.children('g').append(items[head][item].menu_icon().children());
31:
        icon = icon.serializeXML();
32:
      }
33:
      var row = $('<tr class="contextmenuitem"><td class="contextmenuicon"><div>' + (icon == null ? '' : icon) + '</div></td><td>' + items[head][item].label + '</td></tr>');
34:
      div.children(':first').append(row);
35:
      row.bind('click', items[head][item], function(event){
36:
        event.data.function_call.apply(null, event.data.params);
37:
      });
38:
    }
39:
  }
40:
  div.css({'left':x+5,'top':y+5, 'display':'block'});
41:
  $('body', document).append(div);
42:
  if(($(window).height() < (y + div.height()))) { // contextmenu is position
43:
    div.css({'top':$(window).height()-div.height()-5});
44:
  }
45:
  if((document.body.clientWidth < (x + div.width())) && (x-div.width()-5 >= 0)) { // contextmenu is position
46:
    div.css({'left':x-div.width()-5});
47:
  }
48:
  e.stopPropagation();
49:
  $('body', document).bind('mousedown',contextmenu_remove);
50:
}
51:
 
52:
function contextmenu_remove(event) {
53:
  if (!event) {
54:
    $('.contextmenu:first').remove();
55:
    $('body', document).unbind('mousedown',contextmenu_remove);
56:
    return;
57:
  }  
58:
 
59:
  if($(event.target).parent('tr.contextmenuitem') && (event.button == 0)) { $(event.target).click(); }
60:
  $('.contextmenu:first').remove();
61:
  $('body', document).unbind('mousedown',contextmenu_remove);
62:
}