{"_id":"54d995534c49f40d00dff98a","project":"54d53c7b23010a0d001aca0c","parentDoc":null,"user":"54cfa8e1a8a4fd0d00b7fd1d","__v":5,"category":{"_id":"54dd1315ca1e5219007e9daa","pages":["54dd1324ca1e5219007e9dac","54e26fe6dd4c990d00479bef","5516e2d97f5d0919007d0701"],"version":"54d5635532d98b0d00384afb","project":"54d53c7b23010a0d001aca0c","__v":3,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-12T20:54:45.914Z","from_sync":false,"order":8,"slug":"user-interface","title":"User Interface"},"version":{"_id":"54d5635532d98b0d00384afb","project":"54d53c7b23010a0d001aca0c","__v":10,"forked_from":"54d53c7c23010a0d001aca0f","createdAt":"2015-02-07T00:59:01.934Z","releaseDate":"2015-02-07T00:59:01.934Z","categories":["54d5635632d98b0d00384afc","54d5635632d98b0d00384afd","54d5635632d98b0d00384afe","54d5635632d98b0d00384aff","54d5635632d98b0d00384b00","54d5635632d98b0d00384b01","54d5635632d98b0d00384b02","54d652097e05890d006f153e","54dd1315ca1e5219007e9daa","54e21e2b22de1c230094b147","54e68e62a43fe13500db3879","54fa1d3fe7a0ba2f00306309","551c453a23a1ee190034d19a","551df586e52a0b23000c62b6","551f39be6886f8230055f02a","55a6720751457325000e4d97"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.1.0","version":"0.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-02-10T05:21:23.834Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"There are two different ways to add menus to Cloud9. The application menu has a different api from the context menu, for practical reasons.\n\n## Application Menu\n\nYou are free to place extra menu items anywhere in the menu structure of Cloud9. You can use an integer to specify the position of your menu items relative to others. The next example shows how to add a menu item to the File menu.\n\n```\nmenus.addItemByPath(\"File/My Menu Item\", new ui.item({\n    command: \"mycommand\"\n}), 300, plugin);\n```\n\nBased on the result of isAvailable() of the command, this menu will either show as enabled or disabled. \n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Positioning using an index\",\n  \"body\": \"Throughout Cloud9 you'll see APIs that allow you to sort your items using integers. By using an int to set the position it is possible to determine the approximate position of, for instance an item in a menu without having any knowledge about other plugins that are loaded. Usually all plugins are a-like and there are no special ranges. Simply choose a position that makes sense to you and use it. \\n\\nTo see what the position ints are simply load Cloud9 with `?menus=1` in the url.\"\n}\n[/block]\nAdding a divider works in a similar way:\n\n```\nmenus.addItemByPath(\"File/~\", new ui.divider(), 310, plugin);\n```\n\n## Context Menu\n\nCommands that are linked to specific parts of the interface should be triggered by a menu item in a context menu. The context menu api is slightly different and requires the `Menu`, `MenuItem` and `Divider` plugins in your list of consumed plugins.\n\nThis example shows a straightforward way to create a menu:\n\n```\nvar menu = new Menu({ items: [\n    new MenuItem({ command: \"open\", caption: \"Open...\" }),\n    new MenuItem({ command: \"save\", caption: \"Save\" }),\n    new MenuItem({ command: \"saveas\", caption: \"Save As...\" }),\n]}, plugin);\n\nmenu.show(10, 10);\n```\n\nIt's easy to manipulate menus when needed:\n\n```\nvar item = new MenuItem({ \n    caption : \"Alert\" \n    icon    : \"alert.png\" \n    onclick : function(){\n        alert(\"Alert!\");\n    }\n});\nmenu.append(item);\n\nitem.disabled = true;\nitem.caption = \"No alert\";\n```\n\nSubmenus are normal context menus that are referenced by a menu item:\n\n```\nvar listMenu = new Menu({}, plugin);\nvar item = new MenuItem({ caption: \"List...\", submenu: listMenu });\n\n```","excerpt":"","slug":"menus","type":"basic","title":"Menus"}
There are two different ways to add menus to Cloud9. The application menu has a different api from the context menu, for practical reasons. ## Application Menu You are free to place extra menu items anywhere in the menu structure of Cloud9. You can use an integer to specify the position of your menu items relative to others. The next example shows how to add a menu item to the File menu. ``` menus.addItemByPath("File/My Menu Item", new ui.item({ command: "mycommand" }), 300, plugin); ``` Based on the result of isAvailable() of the command, this menu will either show as enabled or disabled. [block:callout] { "type": "warning", "title": "Positioning using an index", "body": "Throughout Cloud9 you'll see APIs that allow you to sort your items using integers. By using an int to set the position it is possible to determine the approximate position of, for instance an item in a menu without having any knowledge about other plugins that are loaded. Usually all plugins are a-like and there are no special ranges. Simply choose a position that makes sense to you and use it. \n\nTo see what the position ints are simply load Cloud9 with `?menus=1` in the url." } [/block] Adding a divider works in a similar way: ``` menus.addItemByPath("File/~", new ui.divider(), 310, plugin); ``` ## Context Menu Commands that are linked to specific parts of the interface should be triggered by a menu item in a context menu. The context menu api is slightly different and requires the `Menu`, `MenuItem` and `Divider` plugins in your list of consumed plugins. This example shows a straightforward way to create a menu: ``` var menu = new Menu({ items: [ new MenuItem({ command: "open", caption: "Open..." }), new MenuItem({ command: "save", caption: "Save" }), new MenuItem({ command: "saveas", caption: "Save As..." }), ]}, plugin); menu.show(10, 10); ``` It's easy to manipulate menus when needed: ``` var item = new MenuItem({ caption : "Alert" icon : "alert.png" onclick : function(){ alert("Alert!"); } }); menu.append(item); item.disabled = true; item.caption = "No alert"; ``` Submenus are normal context menus that are referenced by a menu item: ``` var listMenu = new Menu({}, plugin); var item = new MenuItem({ caption: "List...", submenu: listMenu }); ```