You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

2 years ago
  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'); }, isEnabled: function() { return false; }},
  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. $.each(settings.items, function (index, 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.isEnabled != undefined && !item.isEnabled()) {
  58. row.addClass('disabled');
  59. } else if (item.action) {
  60. row.find('a').click(function () { item.action(e); });
  61. }
  62. } else {
  63. $('<li class="' + settings.seperatorClass + '"></li>').appendTo(menu);
  64. }
  65. });
  66. menu.find('.' + settings.headerClass).text(settings.title);
  67. return menu;
  68. }
  69. // On contextmenu event (right click)
  70. this.bind('contextmenu', function (e) {
  71. var menu = createMenu(e)
  72. .show();
  73. var left = e.pageX + 5, /* nudge to the right, so the pointer is covering the title */
  74. top = e.pageY;
  75. if (top + menu.height() >= $(window).height()) {
  76. top -= menu.height();
  77. }
  78. if (left + menu.width() >= $(window).width()) {
  79. left -= menu.width();
  80. }
  81. // Create and show menu
  82. menu.css({ zIndex: 1000001, left: left, top: top })
  83. .bind('contextmenu', function () { return false; });
  84. // Cover rest of page with invisible div that when clicked will cancel the popup.
  85. var bg = $('<div></div>')
  86. .css({ left: 0, top: 0, width: '100%', height: '100%', position: 'absolute', zIndex: 1000000 })
  87. .appendTo(document.body)
  88. .bind('contextmenu click', function () {
  89. // If click or right click anywhere else on page: remove clean up.
  90. bg.remove();
  91. menu.remove();
  92. return false;
  93. });
  94. // When clicking on a link in menu: clean up (in addition to handlers on link already)
  95. menu.find('a').click(function () {
  96. bg.remove();
  97. menu.remove();
  98. });
  99. // Cancel event, so real browser popup doesn't appear.
  100. return false;
  101. });
  102. return this;
  103. };