Advertisement
27GRiS

jquery.contextmenu.js

Jan 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.74 KB | None | 0 0
  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.    
  36.     // merge them
  37.     $.extend(settings, menuData);
  38.  
  39.   // Build popup menu HTML
  40.   function createMenu(e) {
  41.     var menu = $('<ul class="' + settings.contextMenuClass + '"><div class="' + settings.gutterLineClass + '"></div></ul>')
  42.       .appendTo(document.body);
  43.     if (settings.title) {
  44.       $('<li class="' + settings.headerClass + '"></li>').text(settings.title).appendTo(menu);
  45.     }
  46.     settings.items.forEach(function(item) {
  47.       if (item) {
  48.         var rowCode = '<li><a href="#"><span></span></a></li>';
  49.         // if(item.icon)
  50.         //   rowCode += '<img>';
  51.         // rowCode +=  '<span></span></a></li>';
  52.         var row = $(rowCode).appendTo(menu);
  53.         if(item.icon){
  54.           var icon = $('<img>');
  55.           icon.attr('src', item.icon);
  56.           icon.insertBefore(row.find('span'));
  57.         }
  58.         row.find('span').text(item.label);
  59.          
  60.         if (item.isEnabled != undefined && !item.isEnabled()) {
  61.             row.addClass('disabled');
  62.         } else if (item.action) {
  63.             row.find('a').click(function () { item.action(e); });
  64.         }
  65.  
  66.       } else {
  67.         $('<li class="' + settings.seperatorClass + '"></li>').appendTo(menu);
  68.       }
  69.     });
  70.     menu.find('.' + settings.headerClass ).text(settings.title);
  71.     return menu;
  72.   }
  73.  
  74.   // On contextmenu event (right click)
  75.   this.bind('contextmenu', function(e) {   
  76.     var menu = createMenu(e)
  77.       .show();
  78.    
  79.     var left = e.pageX + 5, /* nudge to the right, so the pointer is covering the title */
  80.         top = e.pageY;
  81.     if (top + menu.height() >= $(window).height()) {
  82.         top -= menu.height();
  83.     }
  84.     if (left + menu.width() >= $(window).width()) {
  85.         left -= menu.width();
  86.     }
  87.  
  88.     // Create and show menu
  89.     menu.css({zIndex:1000001, left:left, top:top})
  90.       .bind('contextmenu', function() { return false; });
  91.  
  92.     // Cover rest of page with invisible div that when clicked will cancel the popup.
  93.     var bg = $('<div></div>')
  94.       .css({left:0, top:0, width:'100%', height:'100%', position:'absolute', zIndex:1000000})
  95.       .appendTo(document.body)
  96.       .bind('contextmenu click', function() {
  97.         // If click or right click anywhere else on page: remove clean up.
  98.         bg.remove();
  99.         menu.remove();
  100.         return false;
  101.       });
  102.  
  103.     // When clicking on a link in menu: clean up (in addition to handlers on link already)
  104.     menu.find('a').click(function() {
  105.       bg.remove();
  106.       menu.remove();
  107.     });
  108.  
  109.     // Cancel event, so real browser popup doesn't appear.
  110.     return false;
  111.   });
  112.  
  113.   return this;
  114. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement