2017 © Pedro Peláez
 

library bootstrap-contexmenu

Bootstrap context menu

image

intelogie/bootstrap-contexmenu

Bootstrap context menu

  • Tuesday, July 19, 2016
  • by bgauthier
  • Repository
  • 1 Watchers
  • 0 Stars
  • 38 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 194 Forks
  • 0 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

Bootstrap Context Menu

A context menu extension of Bootstrap made for everyone's convenience., (*1)

See a demo page., (*2)

Installation

bower install bootstrap-contextmenu, (*3)

Note: Requires bootstrap.css, (*4)

Usage

Via data attributes

Add data-toggle="context" to any element that needs a custom context menu and via CSS set position: relative to the element., (*5)

Point data-target attribute to your custom context menu., (*6)

<div class="context" data-toggle="context" data-target="#context-menu"></div>, (*7)

Via Javascript

Call the context menu via JavaScript:, (*8)

$('.context').contextmenu({
  target:'#context-menu', 
  before: function(e,context) {
    // execute code before context menu if shown
  },
  onItem: function(context,e) {
    // execute on menu item selection
  }
})

Options

target - is the equivalent of the data-target attribute. It identifies the html of the menu that will be displayed., (*9)

before - is a function that is called before the context menu is displayed. If this function returns false, the context menu will not be displayed. It is passed two parameters,, (*10)

  • e - the original event. (You can do an e.preventDefault() to cancel the browser event).
  • context - the DOM element where right click occured.

onItem - is a function that is called when a menu item is clicked. Useful when you want to execute a specific function when an item is clicked. It is passed two parameters,, (*11)

  • context - the DOM element where right click occured.
  • e - the click event of the menu item, $(e.target) is the item element.

scopes - DOM selector for dynamically added context elements. See issue., (*12)

Events

All events are fired at the context's menu. Check out dropdown plugin for a complete description of events., (*13)

  • show.bs.context - This event fires immediately when the menu is opened.
  • shown.bs.context - This event is fired when the dropdown has been made visible to the user.
  • hide.bs.context - This event is fired immediately when the hide instance method has been called.
  • hidden.bs.context - This event is fired when the dropdown has finished being hidden from the user (will wait for CSS transitions, to complete).

Sample, (*14)

$('#myMenu').on('show.bs.context',function () {
  // do something...
});

Example

Activate and specify selector for context menu, (*15)

$('#main').contextmenu({'target':'#context-menu'});

Activate within a div, but not on spans, (*16)

$('#main').contextmenu({
  target: '#context-menu2',
  before: function (e, element, target) {
      e.preventDefault();
      if (e.target.tagName == 'SPAN') {
          e.preventDefault();
          this.closemenu();
          return false;
      }
      return true;
  }
});

Modify the menu dynamically, (*17)

$('#main').contextmenu({
  target: "#myMenu",
  before: function(e) { 
    this.getMenu().find("li").eq(2).find('a').html("This was dynamically changed");
  }
});

Show menu name on selection, (*18)

$('#main').contextmenu({
  onItem: function(context, e) {
    alert($(e.target).text());
  }
});

Nice to have features:

  • Close and open animations
  • Keyboard shortcuts for menus

License

MIT, (*19)

The Versions

19/07 2016

dev-master

9999999-dev

Bootstrap context menu

  Sources   Download