|
| 1 | +/* |
| 2 | + * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | +/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ |
| 26 | +/*global define, brackets, $ */ |
| 27 | + |
| 28 | +define(function (require, exports, module) { |
| 29 | + 'use strict'; |
| 30 | + |
| 31 | + // Brackets modules |
| 32 | + var CommandManager = brackets.getModule("command/CommandManager"), |
| 33 | + EditorManager = brackets.getModule("editor/EditorManager"), |
| 34 | + DocumentManager = brackets.getModule("document/DocumentManager"), |
| 35 | + Menus = brackets.getModule("command/Menus"); |
| 36 | + |
| 37 | + function TestCommand1() { |
| 38 | + var command1 = CommandManager.get("custom.command1"); |
| 39 | + if (!command1) { |
| 40 | + return; |
| 41 | + } |
| 42 | + var command2 = CommandManager.get("custom.command2"); |
| 43 | + if (!command2) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + var checked = command1.getChecked(); |
| 48 | + if (checked) { |
| 49 | + alert("Unchecking self. Disabling next."); |
| 50 | + command2.setEnabled(false); |
| 51 | + } else { |
| 52 | + alert("Checking self. Enabling next."); |
| 53 | + command2.setEnabled(true); |
| 54 | + } |
| 55 | + command1.setChecked(!checked); |
| 56 | + } |
| 57 | + |
| 58 | + function TestCommand2() { |
| 59 | + alert("Executing command 2"); |
| 60 | + } |
| 61 | + |
| 62 | + function TestCommand3() { |
| 63 | + alert("Executing command 3"); |
| 64 | + } |
| 65 | + |
| 66 | + // Create command |
| 67 | + var command1 = CommandManager.register("Toggle Checkmark", "custom.command1", TestCommand1); |
| 68 | + var command2 = CommandManager.register("Enabled when previous is Checked", "custom.command2", TestCommand2); |
| 69 | + var command3 = CommandManager.register("Enabled when text selected", "custom.command3", TestCommand3); |
| 70 | + |
| 71 | + command1.setChecked(true); |
| 72 | + command2.setEnabled(true); |
| 73 | + command3.setEnabled(false); |
| 74 | + |
| 75 | + |
| 76 | + var handleDocChanged = function () { |
| 77 | + var editor = EditorManager.getFocusedEditor(); |
| 78 | + |
| 79 | + var handleEnableState = function () { |
| 80 | + command3.setEnabled(editor.getSelectedText() !== ""); |
| 81 | + }; |
| 82 | + |
| 83 | + if (editor) { |
| 84 | + $(editor).off("cursorActivity", handleEnableState); |
| 85 | + $(editor).on("cursorActivity", handleEnableState); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + $(DocumentManager).on("currentDocumentChange", handleDocChanged); |
| 90 | + handleDocChanged(); |
| 91 | + |
| 92 | + |
| 93 | + // Get editor context menu |
| 94 | + var editor_cmenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU); |
| 95 | + |
| 96 | + // Add our MenuItem at the end |
| 97 | + if (editor_cmenu) { |
| 98 | + editor_cmenu.addMenuDivider(); |
| 99 | + editor_cmenu.addMenuItem("custom.command1"); |
| 100 | + editor_cmenu.addMenuItem("custom.command2"); |
| 101 | + editor_cmenu.addMenuItem("custom.command3"); |
| 102 | + } |
| 103 | +}); |
0 commit comments