diff --git a/Makefile b/Makefile index 00bb7f9..4d6f783 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ build: index.js components - @component build + @component build --dev -components: - @component install +components: component.json + @component install --dev clean: rm -fr build components diff --git a/Readme.md b/Readme.md index d8fb49a..9a2b170 100644 --- a/Readme.md +++ b/Readme.md @@ -57,11 +57,15 @@ oncontextmenu = function(e){ }; ``` +## License + +MIT + ## API - + ### Menu() - Create a new `Menu`: +Initialize a new `Menu`. ```js var Menu = require('menu'); @@ -69,11 +73,40 @@ var menu = new Menu(); var menu = Menu(); ``` -### Menu#add([slug], text, [fn]) +Emits: + +- "show" when shown +- "hide" when hidden +- "remove" with the item name when an item is removed +- "select" (item) when an item is selected +- * menu item events are emitted when clicked + +### Menu.prototype() + +Inherit from `Emitter.prototype`. + +### Menu.move(`direction`:`String`) + +Focus on the next menu item in `direction`. + + +### Menu.get(`slug`:`String`) +> _returns_ MenuItem + +Gets a menu item named `slug`. + + +### Menu.add(`text`:`String`, `fn`:`Function`) +> _returns_ Menu - Add a new menu item with the given `text`, optional `slug` and callback `fn`. +Add a new menu item with the given `text`, optional `slug` and callback `fn`. - Using events to handle selection: +When the item is clicked `fn()` will be invoked +and the `Menu` is immediately closed. When clicked +an event of the name `text` is emitted regardless of +the callback function being present. + +Using events to handle selection: ```js menu.add('Hello'); @@ -83,7 +116,7 @@ menu.on('Hello', function(){ }); ``` - Using callbacks: +Using callbacks: ```js menu.add('Hello', function(){ @@ -91,9 +124,9 @@ menu.add('Hello', function(){ }); ``` - Using a custom slug, otherwise "hello" is generated - from the `text` given, which may conflict with "rich" - styling like icons within menu items, or i18n. +Using a custom slug, otherwise "hello" is generated +from the `text` given, which may conflict with "rich" +styling like icons within menu items, or i18n. ```js menu.add('add-item', 'Add Item'); @@ -107,25 +140,39 @@ menu.add('add-item', 'Add Item', function(){ }); ``` -### Menu#remove(slug) +### Menu.remove(`slug`:`String`) +> _returns_ Menu - Remove an item by the given `slug`: +Remove an item by the given `slug`: ```js menu.add('Add item'); menu.remove('Add item'); ``` - Or with custom slugs: +Or with custom slugs: ```js menu.add('add-item', 'Add item'); menu.remove('add-item'); ``` -### Menu#has(slug) +### Menu.change(`slug`:`String`) +> _returns_ Menu + +Change menu item with `slug`. + + +### Menu.clear() +> _returns_ Menu + +Clear menu. + + +### Menu.has(`item`:`MenuItem`) +> _returns_ Boolean - Check if a menu item is present. +Check if a menu item is present. ```js menu.add('Add item'); @@ -140,18 +187,80 @@ menu.has('Foo'); // => false ``` -### Menu#moveTo(x, y) +### Menu.indexOf(`item`:`MenuItem`) +> _returns_ Number - Move the menu to `(x, y)`. +Find index of menu `item`. -### Menu#show() - Show the menu. +### Menu.moveTo(`x`:`Number`, `y`:`Number`) +> _returns_ Menu -### Menu#hide() +Move context menu to `(x, y)`. - Hide the menu. -## License +### Menu.moveToCenter(`x`:`Number`, `y`:`Number`) +> _returns_ Menu + +Move context menu to `(x, y)`. + + +### Menu.show() +> _returns_ Menu + +Show the menu. + + +### Menu.hide() +> _returns_ Menu + +Hide the menu. + + +### Menu.showItem(`item`:`Element`) +> _returns_ Menu + +Show a menu item. + + +### Menu.hideItem(`item`:`Element`) +> _returns_ Menu + +Hide a menu item. + + +### Menu.unhideAll() +> _returns_ Menu + +Unhide all items. + + +### Menu.toggle() +> _returns_ Menu + +Toggle the menu. + + +### Menu.filter(`fn`:`Function`) +> _returns_ Menu + +Filter menu using `fn`. + + +### Menu.isOpen() +> _returns_ Boolean + +Check if menu is visible. + + +### Menu.isSelecting() +> _returns_ Boolean + +Check if user is selecting. + + +### MenuItem(`item`:`Object`) + +MenuItem class. + - MIT \ No newline at end of file diff --git a/component.json b/component.json index 6042766..afc05c9 100644 --- a/component.json +++ b/component.json @@ -1,14 +1,18 @@ { "name": "menu", "description": "Menu component", - "version": "0.1.1", + "version": "0.2.0", "keywords": [ "menu", "ui" ], "dependencies": { "component/emitter": "*", - "component/jquery": "*" + "component/jquery": "*", + "stagas/viewport": "*" + }, + "development": { + "stagas/watch-js": "*" }, "styles": [ "menu.css" diff --git a/index.js b/index.js index fd19118..d3e407f 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ */ var Emitter = require('emitter') + , viewport = require('viewport') , o = require('jquery'); /** @@ -15,13 +16,19 @@ module.exports = Menu; /** * Initialize a new `Menu`. * + * ```js + * var Menu = require('menu'); + * var menu = new Menu(); + * var menu = Menu(); + * ``` + * * Emits: * - * - "show" when shown - * - "hide" when hidden - * - "remove" with the item name when an item is removed - * - "select" (item) when an item is selected - * - * menu item events are emitted when clicked + * - "show" when shown + * - "hide" when hidden + * - "remove" with the item name when an item is removed + * - "select" (item) when an item is selected + * - * menu item events are emitted when clicked * * @api public */ @@ -29,12 +36,11 @@ module.exports = Menu; function Menu() { if (!(this instanceof Menu)) return new Menu; Emitter.call(this); - this.items = {}; + this.items = []; this.el = o('