jquery.contextmenu.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * jQuery plugin for Pretty looking right click context menu.
  3. *
  4. * Requires popup.js and popup.css to be included in your page. And jQuery, obviously.
  5. *
  6. * Usage:
  7. *
  8. * $('.something').contextPopup({
  9. * title: 'Some title',
  10. * items: [
  11. * {label:'My Item', icon:'/some/icon1.png', action:function() { alert('hi'); }},
  12. * {label:'Item #2', icon:'/some/icon2.png', action:function() { alert('yo'); }},
  13. * null, // divider
  14. * {label:'Blahhhh', icon:'/some/icon3.png', action:function() { alert('bye'); }},
  15. * ]
  16. * });
  17. *
  18. * Icon needs to be 16x16. I recommend the Fugue icon set from: http://p.yusukekamiyamane.com/
  19. *
  20. * - Joe Walnes, 2011 http://joewalnes.com/
  21. * https://github.com/joewalnes/jquery-simple-context-menu
  22. *
  23. * MIT License: https://github.com/joewalnes/jquery-simple-context-menu/blob/master/LICENSE.txt
  24. */
  25. jQuery.fn.contextPopup = function(menuData) {
  26. // Define default settings
  27. var settings = {
  28. contextMenuClass: 'contextMenuPlugin',
  29. gutterLineClass: 'gutterLine',
  30. headerClass: 'header',
  31. seperatorClass: 'divider',
  32. title: '',
  33. items: []
  34. };
  35. // merge them
  36. $.extend(settings, menuData);
  37. // Build popup menu HTML
  38. function createMenu(e) {
  39. var menu = $('<ul class="' + settings.contextMenuClass + '"><div class="' + settings.gutterLineClass + '"></div></ul>')
  40. .appendTo(document.body);
  41. if (settings.title) {
  42. $('<li class="' + settings.headerClass + '"></li>').text(settings.title).appendTo(menu);
  43. }
  44. settings.items.forEach(function(item) {
  45. if (item) {
  46. var rowCode = '<li><a href="#"><span></span></a></li>';
  47. // if(item.icon)
  48. // rowCode += '<img>';
  49. // rowCode += '<span></span></a></li>';
  50. var row = $(rowCode).appendTo(menu);
  51. if(item.icon){
  52. var icon = $('<img>');
  53. icon.attr('src', item.icon);
  54. icon.insertBefore(row.find('span'));
  55. }
  56. row.find('span').text(item.label);
  57. if (item.action) {
  58. row.find('a').click(function(){ item.action(e); });
  59. }
  60. } else {
  61. $('<li class="' + settings.seperatorClass + '"></li>').appendTo(menu);
  62. }
  63. });
  64. menu.find('.' + settings.headerClass ).text(settings.title);
  65. return menu;
  66. }
  67. // On contextmenu event (right click)
  68. this.bind('contextmenu', function(e) {
  69. var menu = createMenu(e)
  70. .show();
  71. var left = e.pageX + 5, /* nudge to the right, so the pointer is covering the title */
  72. top = e.pageY;
  73. if (top + menu.height() >= $(window).height()) {
  74. top -= menu.height();
  75. }
  76. if (left + menu.width() >= $(window).width()) {
  77. left -= menu.width();
  78. }
  79. // Create and show menu
  80. menu.css({zIndex:1000001, left:left, top:top})
  81. .bind('contextmenu', function() { return false; });
  82. // Cover rest of page with invisible div that when clicked will cancel the popup.
  83. var bg = $('<div></div>')
  84. .css({left:0, top:0, width:'100%', height:'100%', position:'absolute', zIndex:1000000})
  85. .appendTo(document.body)
  86. .bind('contextmenu click', function() {
  87. // If click or right click anywhere else on page: remove clean up.
  88. bg.remove();
  89. menu.remove();
  90. return false;
  91. });
  92. // When clicking on a link in menu: clean up (in addition to handlers on link already)
  93. menu.find('a').click(function() {
  94. bg.remove();
  95. menu.remove();
  96. });
  97. // Cancel event, so real browser popup doesn't appear.
  98. return false;
  99. });
  100. return this;
  101. };