Permalink
Browse files

added support for accesskeys

  • Loading branch information...
1 parent 196a42d commit 56a595b913c2f0b452d23a57205b8d3d16fe00da @rodneyrehm rodneyrehm committed Sep 2, 2011
Showing with 109 additions and 16 deletions.
  1. +50 −1 README.md
  2. +12 −12 demo.html
  3. +4 −0 jquery.contextMenu.css
  4. +43 −3 jquery.contextMenu.js
View
@@ -48,6 +48,21 @@ import contextMenu from HTML5 <menu>:
$.contextMenu("html5");
```
+## Interaction Principles ##
+
+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.
+
+* ↑ (up) next item in list, will skip disabled elements and wrap around
+* ↓ (down) previous item in, will skip disabled elements and wrap around
+* → (right) dive into sub-menu
+* ← (left) rise from sub-menu
+* ↵ (return) invoke command
+* ➟ (tab) next item or input element, will skip disabled elements and wrap around
+* ⇪ ➟ (shift tab) previous item or input element, will skip disabled elements and wrap around
+
+Besides the obvious, browser also react to alphanumeric key strokes. Hitting <code>r</code> in a context menu will make Firefox (9) 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.
+
+
## Minify ##
use [Google Closure Compiler](http://closure-compiler.appspot.com/home):
@@ -62,12 +77,46 @@ use [Google Closure Compiler](http://closure-compiler.appspot.com/home):
// ==/ClosureCompiler==
</code></pre>
+
## Authors ##
* [Rodney Rehm](https://github.com/rodneyrehm)
* [Christiaan Baartse](https://github.com/christiaan) (single callback per menu)
* [Addy Osmani](https://github.com/addyosmani) (compatibility with native context menu in Firefox 8)
+
## License ##
-$.contextMenu is published under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
+$.contextMenu is published under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
+
+
+## Changelog ##
+
+### 1.3 ###
+
+* Added support for accesskeys
+
+### 1.2.2 ###
+
+* Bug in HTML5 import
+
+### 1.2.1 ###
+
+* Bug in HTML5 detection
+
+### 1.2 ###
+
+* Added compatibility to &lt;menuitem&gt; for Firefox 8
+* Upgraded to jQuery 1.6.2
+
+### 1.1 ###
+
+* Bug #1 TypeError on HTML5 action passthru
+* Bug #2 disbaled callback not invoked properly
+* Feature #3 auto-hide option for hover trigger
+* Feature #4 option to use a single callback for all commands, rather than registering the same function for each item
+* Option to ignore right-click (original "contextmenu" event trigger) for non-right-click triggers
+
+### 1.0 ###
+
+* Initial $.contextMenu handler
View
@@ -31,13 +31,13 @@ <h2 id="commands">Demo: Context Menu triggered by right click</h2>
* Menu 1
**************************************************/
$.contextMenu({selector: '.context-menu-one', items: {
- edit: {name: "Edit", icon: "edit", callback: $.noop},
- cut: {name: "Cut", icon: "cut", callback: $.noop},
- copy: {name: "Copy", icon: "copy", callback: $.noop},
- paste: {name: "Paste", icon: "paste", callback: $.noop},
- "delete": {name: "Delete", icon: "delete", callback: $.noop},
+ edit: {name: "Edit", icon: "edit", callback: $.noop, accesskey:"e d i t"},
+ cut: {name: "Cut", icon: "cut", callback: $.noop, accesskey:"c u t"},
+ copy: {name: "Copy", icon: "copy", callback: $.noop, accesskey:"c o p y"},
+ paste: {name: "Paste", icon: "paste", callback: $.noop, accesskey:"p a s t e"},
+ "delete": {name: "Delete", icon: "delete", callback: $.noop, accesskey:"d e l t"},
sep1: "---------",
- quit: {name: "Quit", icon: "quit", callback: $.noop}
+ quit: {name: "Quit", icon: "quit", callback: $.noop, accesskey:"q u i t"}
}});
/**************************************************
@@ -47,13 +47,13 @@ <h2 id="commands">Demo: Context Menu triggered by right click</h2>
selector: '.context-menu-one-default',
callback: function(key, opt){ alert("Key '" + key + "' clicked"); },
items: {
- edit: {name: "Edit", icon: "edit"},
- cut: {name: "Cut", icon: "cut"},
- copy: {name: "Copy", icon: "copy"},
- paste: {name: "Paste", icon: "paste"},
- "delete": {name: "Delete", icon: "delete"},
+ edit: {name: "Edit", icon: "edit", accesskey:"e d i t"},
+ cut: {name: "Cut", icon: "cut", accesskey:"c u t"},
+ copy: {name: "Copy", icon: "copy", accesskey:"c o p y"},
+ paste: {name: "Paste", icon: "paste", accesskey:"p a s t e"},
+ "delete": {name: "Delete", icon: "delete", accesskey:"d e l t"},
sep1: "---------",
- quit: {name: "Quit", icon: "quit"}
+ quit: {name: "Quit", icon: "quit", accesskey:"q u i t"}
}
});
View
@@ -104,4 +104,8 @@
.context-menu-item.hover > .context-menu-list {
display: block;
+}
+
+.context-menu-accesskey {
+ text-decoration: underline;
}
View
@@ -12,6 +12,7 @@
(function($, undefined){
// TODO
+ // bug: possibility to open 2 submenus simultaneously
// ARIA stuff: menuitem, menuitemcheckbox und menuitemradio
// create <menu> structure if $.support[htmlCommand || htmlMenuitem] and !opt.disableNative
@@ -375,6 +376,15 @@ var // currently active contextMenu trigger
op.hide.call($currentTrigger, opt);
$currentTrigger = null;
break;
+
+ default: // 0-9, a-z
+ var k = (String.fromCharCode(e.keyCode)).toUpperCase();
+ if (opt.accesskeys[k]) {
+ // according to the specs accesskeys must be invoked immediately
+ opt.accesskeys[k].$node.trigger('mouseup');
+ return;
+ }
+ break;
}
},
@@ -692,6 +702,8 @@ var // currently active contextMenu trigger
}
});
+ opt.accesskeys = {};
+
// create contextMenu items
$.each(opt.items, function(key, item){
var $t = $('<li class="context-menu-item ' + (item.className || "") +'"></li>'),
@@ -704,6 +716,19 @@ var // currently active contextMenu trigger
'contextMenuKey': key
});
+ // register accesskey
+ // NOTE: the accesskey attribute should be applicable to any element, but Safari5 and Chrome13 still can't do that
+ if (item.accesskey) {
+ var aks = splitAccesskey(item.accesskey);
+ for (var i=0, ak; ak = aks[i]; i++) {
+ if (!opt.accesskeys[ak]) {
+ opt.accesskeys[ak] = item;
+ item._name = item.name.replace(new RegExp('(' + ak + ')', 'i'), '<span class="context-menu-accesskey">$1</span>');
+ break;
+ }
+ }
+ }
+
if (typeof item == "string") {
$t.addClass('context-menu-separator not-selectable');
} else {
@@ -712,7 +737,7 @@ var // currently active contextMenu trigger
$t.addClass('context-menu-html not-selectable');
} else if (item.type) {
$label = $('<label></label>').appendTo($t);
- $('<span></span>').appendTo($label).text(item.name);
+ $('<span></span>').html(item._name || item.name).appendTo($label);
$t.addClass('context-menu-input');
opt.hasTypes = true;
$.each([opt, root], function(i,k){
@@ -759,7 +784,7 @@ var // currently active contextMenu trigger
break;
case 'sub':
- $('<span></span>').text(item.name).appendTo($t);
+ $('<span></span>').html(item._name || item.name).appendTo($t);
item.appendTo = item.$node;
op.create(item, root);
$t.data('contextMenu', item);
@@ -778,7 +803,7 @@ var // currently active contextMenu trigger
}
});
- $('<span></span>').text(item.name || "").appendTo($t);
+ $('<span></span>').html(item._name || item.name || "").appendTo($t);
break;
}
@@ -868,6 +893,21 @@ var // currently active contextMenu trigger
}
};
+// split accesskey according to http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#assigned-access-key
+function splitAccesskey(val) {
+ var t = val.split(/\s+/),
+ keys = [];
+
+ for (var i=0, k; k = t[i]; i++) {
+ k = k[0].toUpperCase(); // first character only
+ // theoretically non-accessible characters should be ignored, but different systems, different keyboard layouts, … screw it.
+ // a map to look up already used access keys would be nice
+ keys.push(k);
+ }
+
+ return keys;
+}
+
// handle contextMenu triggers
$.fn.contextMenu = function(operation) {
if (operation === undefined) {

0 comments on commit 56a595b

Please sign in to comment.