Browse files

added #menu.clear()

  • Loading branch information...
1 parent 6dfe233 commit c5bac7e2dc504bb8ccc6ac405ee87534b9eca2d9 @matthewmueller matthewmueller committed Nov 6, 2012
Showing with 34 additions and 5 deletions.
  1. +11 −2 Readme.md
  2. +14 −0 index.js
  3. +9 −3 test/index.html
View
13 Readme.md
@@ -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
@@ -58,7 +59,7 @@ oncontextmenu = function(e){
```
## API
-
+
### Menu()
Create a new `Menu`:
@@ -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.
@@ -154,4 +163,4 @@ menu.has('Foo');
## License
- MIT
+ MIT
View
14 index.js
@@ -183,6 +183,20 @@ Menu.prototype.remove = function(slug){
};
/**
+ * 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`.
*
* @param {String} slug
View
12 test/index.html
@@ -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"');
@@ -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.