07cd674d34cef2c1d10d0b513d1ce4dd2120f3925561ed378eb9b5fb5bd184999938e1fb02f763fa6d645e52bd0ca0d271b4636ca59223fa932b1f0a013d69 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # jQuery contextMenu plugin & polyfill #
  2. [![Greenkeeper badge](https://badges.greenkeeper.io/swisnl/jQuery-contextMenu.svg)](https://greenkeeper.io/)
  3. [![Travis](https://img.shields.io/travis/swisnl/jQuery-contextMenu/master.svg?style=flat-square&maxAge=600)](https://travis-ci.org/swisnl/jQuery-contextMenu) [![npm](https://img.shields.io/npm/v/jquery-contextmenu.svg?style=flat-square&maxAge=600)](https://www.npmjs.com/package/jquery-contextmenu) [![npm](https://img.shields.io/npm/dm/jquery-contextmenu.svg?style=flat-square&maxAge=600)](https://www.npmjs.com/package/jquery-contextmenu) [![CDNJS](https://img.shields.io/cdnjs/v/jquery-contextmenu.svg?style=flat-square&maxAge=600)](https://cdnjs.com/libraries/jquery-contextmenu) [![npm](https://img.shields.io/npm/l/jquery-contextmenu.svg?style=flat-square)]()
  4. $.contextMenu is a management facility for - you guessed it - context menus. It was designed for an application where there are hundreds of elements that may show a context menu - so intialization speed and memory usage are kept fairly small. It also allows to register context menus without providing actual markup, as $.contextMenu generates DOMElements as needed.
  5. [features](http://swisnl.github.io/jQuery-contextMenu/index.html) -
  6. [demo](http://swisnl.github.io/jQuery-contextMenu/demo.html) -
  7. [documentation](http://swisnl.github.io/jQuery-contextMenu/docs.html)
  8. [![Sauce Test Status](https://saucelabs.com/browser-matrix/bbrala-contextmenu.svg)](https://saucelabs.com/u/bbrala-contextmenu)
  9. ## Dependencies ##
  10. * jQuery >=1.8.2
  11. * jQuery UI position (optional but recommended)
  12. ## Usage ##
  13. register contextMenu from javascript:
  14. ```javascript
  15. $.contextMenu({
  16. // define which elements trigger this menu
  17. selector: ".with-cool-menu",
  18. // define the elements of the menu
  19. items: {
  20. foo: {name: "Foo", callback: function(key, opt){ alert("Foo!"); }},
  21. bar: {name: "Bar", callback: function(key, opt){ alert("Bar!") }}
  22. }
  23. // there's more, have a look at the demos and docs...
  24. });
  25. ```
  26. have a look at the [demos](http://swisnl.github.io/jQuery-contextMenu/demo.html).
  27. ## Version 3.0 beta
  28. Version 3.0 is a restructure of the javascript into something more sane written in ES6. It consolidates all API's so callbacks are better documented and more concise. The basics are still the same, but all callbacks are structured differently.
  29. The goal of this refactor was mostly to make the ContextMenu easier to maintain, and make the API's more consise. It also adds JSdoc comments so the API documentation is generated from the code and it enables code completion.
  30. Check out the [3.x branch](https://github.com/swisnl/jQuery-contextMenu/tree/3.x), or install with `npm install jquery-contextmenu@next`.
  31. ## HTML5 Compatibility ##
  32. Firefox 8 implemented contextmenu using the <menuitem> tags for menu-structure. The specs however state that <command> tags should be used for this purpose. $.contextMenu accepts both.
  33. Firefox 8 does not yet fully implement the contextmenu specification ([Ticket #617528](https://bugzilla.mozilla.org/show_bug.cgi?id=617528)). The elements
  34. [a](http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-a-element-to-define-a-command),
  35. [button](http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-button-element-to-define-a-command),
  36. [input](http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-input-element-to-define-a-command) and
  37. [option](http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-option-element-to-define-a-command)
  38. usable as commands are being ignored altogether. It also doesn't (optically) distinguish between checkbox/radio and regular commands ([Bug #705292](https://bugzilla.mozilla.org/show_bug.cgi?id=705292)).
  39. * [contextmenu specs](http://www.w3.org/TR/html5/interactive-elements.html#context-menus)
  40. * [command specs](http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html)
  41. * [Browser support according to caniuse.com](http://caniuse.com/#search=context%20menu)
  42. Note: While the specs note <option>s to be renderd as regular commands, $.contextMenu will render an actual <select>. import contextMenu from HTML5 <menu>:
  43. ```javascript
  44. $.contextMenu("html5");
  45. ```
  46. ## Interaction Principles
  47. You're (obviously) able to use the context menu with your mouse. Once it is opened, you can also use the keyboard to (fully) navigate it.
  48. * ↑ (up) previous item in list, will skip disabled elements and wrap around
  49. * ↓ (down) next item in, will skip disabled elements and wrap around
  50. * → (right) dive into sub-menu
  51. * ← (left) rise from sub-menu
  52. * ↵ (return) invoke command
  53. * ⇥ (tab) next item or input element, will skip disabled elements and wrap around
  54. * ⇪ ⇥ (shift tab) previous item or input element, will skip disabled elements and wrap around
  55. * ⎋ (escape) close menu
  56. * ⌴ (space) captured and ignore to avoid page scrolling (for consistency with native menus)
  57. * ⇞ (page up) captured and ignore to avoid page scrolling (for consistency with native menus)
  58. * ⇟ (page down) captured and ignore to avoid page scrolling (for consistency with native menus)
  59. * ↖ (home) first item in list, will skip disabled elements
  60. * ↘ (end) last item in list, will skip disabled elements
  61. Besides the obvious, browser also react to alphanumeric key strokes. Hitting <code>r</code> in a context menu will make Firefox (8) reload the page immediately. Chrome selects the option to see infos on the page, Safari selects the option to print the document. Awesome, right? Until trying the same on Windows I did not realize that the browsers were using the access-key for this. I would've preferred typing the first character of something, say "s" for "save" and then iterate through all the commands beginning with s. But that's me - what do I know about UX? Anyways, $.contextMenu now also supports accesskey handling.
  62. ## Authors
  63. * [Björn Brala](https://github.com/swisnl)
  64. * [Rodney Rehm](https://github.com/rodneyrehm) (original creator)
  65. * [Christiaan Baartse](https://github.com/christiaan) (single callback per menu)
  66. * [Addy Osmani](https://github.com/addyosmani) (compatibility with native context menu in Firefox 8)
  67. ## License
  68. $.contextMenu is published under the [MIT license](http://www.opensource.org/licenses/mit-license)
  69. ## Special thanks
  70. Font-Awesome icons used from [encharm/Font-Awesome-SVG-PNG](https://github.com/encharm/Font-Awesome-SVG-PNG).
  71. ## SWIS :heart: Open Source
  72. [SWIS][link-swis] is a web agency from Leiden, the Netherlands. We love working with open source software.
  73. [link-swis]: https://www.swis.nl