Esempio n. 1
0
 FindBar.prototype._addShortcutToTooltip = function ($elem, commandId) {
     var replaceShortcut = KeyBindingManager.getKeyBindings(commandId)[0];
     if (replaceShortcut) {
         var oldTitle = $elem.attr("title");
         oldTitle = (oldTitle ? oldTitle + " " : "");
         $elem.attr("title", oldTitle + "(" + KeyBindingManager.formatKeyDescriptor(replaceShortcut.displayKey) + ")");
     }
 };
Esempio n. 2
0
 function _addKeyBindingToMenuItem($menuItem, key, displayKey) {
     var $shortcut = $menuItem.find(".menu-shortcut");
     
     if ($shortcut.length === 0) {
         $shortcut = $("<span class='menu-shortcut' />");
         $menuItem.append($shortcut);
     }
     
     $shortcut.data("key", key);
     $shortcut.text(KeyBindingManager.formatKeyDescriptor(displayKey));
 }
Esempio n. 3
0
 MenuItem.prototype._keyBindingAdded = function (event, keyBinding) {
     if (this.isNative) {
         var shortcutKey = keyBinding.displayKey || keyBinding.key;
         brackets.app.setMenuItemShortcut(this.id, shortcutKey, KeyBindingManager.formatKeyDescriptor(shortcutKey), function (err) {
             if (err) {
                 console.error("Error setting menu item shortcut: " + err);
             }
         });
     } else {
         _addKeyBindingToMenuItem($(_getHTMLMenuItem(this.id)), keyBinding.key, keyBinding.displayKey);
     }
 };
Esempio n. 4
0
    Menu.prototype.addMenuItem = function (command, keyBindings, position, relativeID) {
        var menuID = this.id,
            id,
            $menuItem,
            $link,
            menuItem,
            name,
            commandID;

        if (!command) {
            console.error("addMenuItem(): missing required parameters: command");
            return null;
        }

        if (typeof (command) === "string") {
            if (command === DIVIDER) {
                name = DIVIDER;
                commandID = _getNextMenuItemDividerID();
            } else {
                commandID = command;
                command = CommandManager.get(commandID);
                if (!command) {
                    console.error("addMenuItem(): commandID not found: " + commandID);
                    return null;
                }
                name = command.getName();
            }
        } else {
            commandID = command.getID();
            name = command.getName();
        }

        // Internal id is the a composite of the parent menu id and the command id.
        id = this._getMenuItemId(commandID);
        
        if (menuItemMap[id]) {
            console.log("MenuItem added with same id of existing MenuItem: " + id);
            return null;
        }

        // create MenuItem
        menuItem = new MenuItem(id, command);
        menuItemMap[id] = menuItem;

        // create MenuItem DOM
        if (_isHTMLMenu(this.id)) {
            if (name === DIVIDER) {
                $menuItem = $("<li><hr class='divider' /></li>");
            } else {
                // Create the HTML Menu
                $menuItem = $("<li><a href='#' id='" + id + "'> <span class='menu-name'></span></a></li>");
    
                $menuItem.on("click", function () {
                    menuItem._command.execute();
                });
            }
    
            // Insert menu item
            var $relativeElement = this._getRelativeMenuItem(relativeID, position);
            _insertInList($("li#" + StringUtils.jQueryIdEscape(this.id) + " > ul.dropdown-menu"),
                          $menuItem, position, $relativeElement);
        } else {
            var bindings = KeyBindingManager.getKeyBindings(commandID),
                binding,
                bindingStr = "",
                displayStr = "";
            
            if (bindings && bindings.length > 0) {
                binding = bindings[bindings.length - 1];
                bindingStr = binding.displayKey || binding.key;
            }
            
            if (bindingStr.length > 0) {
                displayStr = KeyBindingManager.formatKeyDescriptor(bindingStr);
            }
            
            if (position === FIRST_IN_SECTION || position === LAST_IN_SECTION) {
                if (!relativeID.hasOwnProperty("sectionMarker")) {
                    console.error("Bad Parameter in _getRelativeMenuItem(): relativeID must be a MenuSection when position refers to a menu section");
                    return null;
                }
                
                // For sections, pass in the marker for that section. 
                relativeID = relativeID.sectionMarker;
            }
            
            brackets.app.addMenuItem(this.id, name, commandID, bindingStr, displayStr, position, relativeID, function (err) {
                switch (err) {
                case NO_ERROR:
                    break;
                case ERR_INVALID_PARAMS:
                    console.error("addMenuItem(): Invalid Parameters when adding the command " + commandID);
                    break;
                case ERR_NOT_FOUND:
                    console.error("_getRelativeMenuItem(): MenuItem with Command id " + relativeID + " not found in the Menu " + menuID);
                    break;
                default:
                    console.error("addMenuItem(); Unknown Error (" + err + ") when adding the command " + commandID);
                }
            });
            menuItem.isNative = true;
        }

        // Initialize MenuItem state
        if (!menuItem.isDivider) {
            if (keyBindings) {
                // Add key bindings. The MenuItem listens to the Command object to update MenuItem DOM with shortcuts.
                if (!Array.isArray(keyBindings)) {
                    keyBindings = [keyBindings];
                }
            }
                
            // Note that keyBindings passed during MenuItem creation take precedent over any existing key bindings
            KeyBindingManager.addBinding(commandID, keyBindings);
            
            // Look for existing key bindings
            _addExistingKeyBinding(menuItem, commandID);

            menuItem._checkedChanged();
            menuItem._enabledChanged();
            menuItem._nameChanged();
        }
        
        return menuItem;
    };