Skip to content

Commit

Permalink
added #menu.clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Nov 6, 2012
1 parent 6dfe233 commit c5bac7e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
13 changes: 11 additions & 2 deletions Readme.md
Expand Up @@ -23,6 +23,7 @@ $ npm install menu-component
- `show` when shown
- `hide` when hidden
- `remove` (item) when an item is removed
- `clear` when the menu has been cleared
- `select` (item) when an item is selected
- `*` menu item events are emitted when clicked

Expand Down Expand Up @@ -58,7 +59,7 @@ oncontextmenu = function(e){
```

## API

### Menu()

Create a new `Menu`:
Expand Down Expand Up @@ -123,6 +124,14 @@ menu.add('add-item', 'Add item');
menu.remove('add-item');
```

### Menu#clear()

Clears the menu.

```js
menu.clear();
```

### Menu#has(slug)

Check if a menu item is present.
Expand Down Expand Up @@ -154,4 +163,4 @@ menu.has('Foo');

## License

MIT
MIT
14 changes: 14 additions & 0 deletions index.js
Expand Up @@ -182,6 +182,20 @@ Menu.prototype.remove = function(slug){
return this;
};

/**
* Clear all the items from the menu
*
* @return {Menu}
* @api public
*/

Menu.prototype.clear = function(){
this.el.empty();
this.items = {};
this.emit('clear');
return this;
};

/**
* Check if this menu has an item with the given `slug`.
*
Expand Down
12 changes: 9 additions & 3 deletions test/index.html
Expand Up @@ -10,13 +10,14 @@
<script src="../build/build.js"></script>
<script>
var menu = require('menu');

menu = menu();

menu
.add('add-item', 'Add <em>item</em>')
.add('Edit item', function(){ console.log('edit'); })
.add('Remove item', function(){ console.log('remove'); })
.add('Clear Menu', menu.clear.bind(menu))
.add('Remove "Add item"', function(){
menu.remove('add-item');
menu.remove('Remove "Add item"');
Expand All @@ -30,11 +31,16 @@
console.log('added an item');
});

menu.on('clear', function() {
console.log('clear');
menu.add('born-again', '<em>Born Again</em>');
});

oncontextmenu = function(e){
e.preventDefault();
menu.moveTo(e.pageX, e.pageY);
menu.show();
};
</script>
</body>
</html>
</html>

0 comments on commit c5bac7e

Please sign in to comment.