Skip to content

Commit

Permalink
Added optional callback support to Menu items
Browse files Browse the repository at this point in the history
an event is now emitted on click
  • Loading branch information
tj committed Feb 4, 2012
1 parent a7fb1b4 commit ac7a56e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
10 changes: 6 additions & 4 deletions build/ui.js
Expand Up @@ -1148,10 +1148,12 @@ function Menu() {
Menu.prototype = new ui.Emitter;

/**
* Add menu item with the given `text` and callback `fn`.
* Add menu item with the given `text` and optional callback `fn`.
*
* When the item is clicked `fn()` will be invoked
* and the `Menu` is immediately closed.
* and the `Menu` is immediately closed. When clicked
* an event of the name `text` is emitted regardless of
* the callback function being present.
*
* @param {String} text
* @param {Function} fn
Expand All @@ -1160,7 +1162,6 @@ Menu.prototype = new ui.Emitter;
*/

Menu.prototype.add = function(text, fn){
if (1 == arguments.length) return this.items[text];
var self = this
, el = $('<li><a href="#">' + text + '</a></li>')
.addClass(slug(text))
Expand All @@ -1169,7 +1170,8 @@ Menu.prototype.add = function(text, fn){
e.preventDefault();
e.stopPropagation();
self.hide();
fn();
self.emit(text);
fn && fn();
});

this.items[text] = el;
Expand Down
14 changes: 11 additions & 3 deletions docs/index.html
Expand Up @@ -11,14 +11,18 @@
<script>
$(function(){
var menu = ui.menu()
.add('Add item', function(){ console.log('add'); })
.add('Add item')
.add('Edit item', function(){ console.log('edit'); })
.add('Remove item', function(){ console.log('remove'); })
.add('Remove "Add item"', function(){
menu
.remove('Add item')
.remove('Remove "Add item"');
})
});

menu.on('Add item', function(){
console.log('added an item');
});

oncontextmenu = function(e){
e.preventDefault();
Expand Down Expand Up @@ -523,7 +527,7 @@ <h3>Example</h3>
<p>A <code>Menu</code> is comprised of textual menu items and optional arbitrary callback functions. To add an item invoke <code>Menu#add()</code>, and to remove pass the same text to <code>Menu#remove()</code>. Give it a try! right click on this page.</p>

<pre><code>var menu = ui.menu()
.add('Add item', function(){ console.log('add'); })
.add('Add item')
.add('Edit item', function(){ console.log('edit'); })
.add('Remove item', function(){ console.log('remove'); })
.add('Remove "Add item"', function(){
Expand All @@ -532,6 +536,10 @@ <h3>Example</h3>
.remove('Remove "Add item"');
});

menu.on('Add item', function(){
console.log('item added');
});

oncontextmenu = function(e){
e.preventDefault();
menu.moveTo(e.pageX, e.pageY).show();
Expand Down
11 changes: 7 additions & 4 deletions lib/components/menu/menu.js
Expand Up @@ -24,6 +24,7 @@ exports.menu = function(){
* - "show" when shown
* - "hide" when hidden
* - "remove" with the item name when an item is removed
* - * menu item events are emitted when clicked
*
* @api public
*/
Expand All @@ -43,10 +44,12 @@ function Menu() {
Menu.prototype = new ui.Emitter;

/**
* Add menu item with the given `text` and callback `fn`.
* Add menu item with the given `text` and optional callback `fn`.
*
* When the item is clicked `fn()` will be invoked
* and the `Menu` is immediately closed.
* and the `Menu` is immediately closed. When clicked
* an event of the name `text` is emitted regardless of
* the callback function being present.
*
* @param {String} text
* @param {Function} fn
Expand All @@ -55,7 +58,6 @@ Menu.prototype = new ui.Emitter;
*/

Menu.prototype.add = function(text, fn){
if (1 == arguments.length) return this.items[text];
var self = this
, el = $('<li><a href="#">' + text + '</a></li>')
.addClass(slug(text))
Expand All @@ -64,7 +66,8 @@ Menu.prototype.add = function(text, fn){
e.preventDefault();
e.stopPropagation();
self.hide();
fn();
self.emit(text);
fn && fn();
});

this.items[text] = el;
Expand Down

0 comments on commit ac7a56e

Please sign in to comment.