This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Adds support for menus sections, fixes #1210 #1247
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5aa8c7b
add support for menu sections
tvoliter 0d6568a
Merge remote-tracking branch 'origin/master' into tvoliter/issue-1210
tvoliter 73a7f8a
Tests for menu section insertion
tvoliter b937f7e
remove empty menu section test
tvoliter 83dc288
Adding function comment blocks to Menus.js
tvoliter 81b835d
Additional comments in Menus.js for sections
tvoliter eb3bbd5
code review fixes
tvoliter e2357bd
Merge remote-tracking branch 'origin/master' into tvoliter/issue-1210
tvoliter d1216f8
Code review fixes
tvoliter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,20 +67,27 @@ define(function (require, exports, module) { | |
* more semantic. | ||
* Use these constants as the "relativeID" parameter when calling addMenuItem() and | ||
* specify a position of FIRST or LAST. | ||
* | ||
* Menu sections are denoted by dividers or the beginning/end of a menu | ||
*/ | ||
var MenuSection = { | ||
FILE_OPEN_CLOSE_MENU: "file-open-close-menu-section", | ||
FILE_SAVE_MENU: "file-save-menu-section", | ||
FILE_LIVE_MENU: "file-live-menu-section", | ||
// Menu Section Command ID to mark the section | ||
FILE_OPEN_CLOSE_COMMANDS: {sectionMarker: Commands.FILE_NEW}, | ||
FILE_SAVE_COMMANDS: {sectionMarker: Commands.FILE_SAVE}, | ||
FILE_LIVE: {sectionMarker: Commands.FILE_LIVE_FILE_PREVIEW}, | ||
|
||
EDIT_SELECTION_COMMANDS: {sectionMarker: Commands.EDIT_SELECT_ALL}, | ||
EDIT_FIND: {sectionMarker: Commands.EDIT_FIND}, | ||
EDIT_REPLACE_COMMANDS: {sectionMarker: Commands.EDIT_REPLACE}, | ||
EDIT_MODIFY_SELECTION: {sectionMarker: Commands.EDIT_INDENT}, | ||
|
||
EDIT_MODIFY_SELECTION_MENU: "edit-modify-selection-menu-section", | ||
EDIT_FIND_MENU: "find-menu-section", | ||
EDIT_REPLACE_MENU: "replace-menu-section", | ||
EDIT_SELECTED_TEXT_COMMANDS: "selected-text-commands-menu-group", | ||
VIEW_HIDESHOW_COMMANDSkey: {sectionMarker: Commands.VIEW_HIDE_SIDEBAR}, | ||
VIEW_FONTSIZE_COMMANDSkey: {sectionMarker: Commands.VIEW_INCREASE_FONT_SIZE}, | ||
|
||
NAVIGATE_GOTO_MENU: "goto-menu-section", | ||
NAVIGATE_QUICK_EDIT_MENU: "quick-edit-menu-section" | ||
NAVIGATE_GOTO: {sectionMarker: Commands.NAVIGATE_QUICK_OPEN}, | ||
NAVIGATE_QUICK_EDIT: {sectionMarker: Commands.TOGGLE_QUICK_EDIT} | ||
}; | ||
|
||
|
||
/** | ||
* Insertion position constants | ||
|
@@ -275,12 +282,28 @@ define(function (require, exports, module) { | |
this.id = id; | ||
} | ||
|
||
Menu.prototype._getMenuItemForCommand = function (command) { | ||
var foundMenuItem, key, menuItem; | ||
for (key in menuItemMap) { | ||
if (menuItemMap.hasOwnProperty(key)) { | ||
menuItem = menuItemMap[key]; | ||
if (menuItem.getCommand() === command) { | ||
foundMenuItem = menuItem; | ||
break; | ||
} | ||
} | ||
} | ||
return $(_getHTMLMenuItem(foundMenuItem.id)).closest("li"); | ||
}; | ||
|
||
/** | ||
* Determine relative MenuItem | ||
* | ||
* @param {?string} relativeID - id of command (future: also sub-menu, or menu section). | ||
* @param {?string} relativeID - id of command (future: sub-menu). | ||
* @param {?string} position - only needed when relativeID is a MenuSection | ||
* @param {HTMLIElement} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this 3rd parameter can be deleted from documentation |
||
*/ | ||
Menu.prototype._getRelativeMenuItem = function (relativeID) { | ||
Menu.prototype._getRelativeMenuItem = function (relativeID, position) { | ||
var $relativeElement, | ||
key, | ||
menuItem, | ||
|
@@ -293,24 +316,50 @@ define(function (require, exports, module) { | |
|
||
if (command) { | ||
// Find MenuItem that has this command | ||
for (key in menuItemMap) { | ||
if (menuItemMap.hasOwnProperty(key)) { | ||
menuItem = menuItemMap[key]; | ||
if (menuItem.getCommand() === command) { | ||
foundMenuItem = menuItem; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (foundMenuItem) { | ||
$relativeElement = $(_getHTMLMenuItem(foundMenuItem.id)).closest("li"); | ||
} | ||
$relativeElement = this._getMenuItemForCommand(command); | ||
} | ||
} | ||
|
||
return $relativeElement; | ||
}; | ||
|
||
/** | ||
* Returns a menuItem and a relative position for the menuSection/position requested | ||
* @param {{menuSection: String}} | ||
* @param {String} position constant | ||
* @returns {{relativeElement: HTMLIElement, position: String}} | ||
*/ | ||
Menu.prototype._getMenuSectionPosition = function (menuSection, position) { | ||
var $relativeElement; | ||
var $sectionMarker = this._getMenuItemForCommand(CommandManager.get(menuSection.sectionMarker)); | ||
var $sectionItems = $sectionMarker.siblings(); | ||
if (position === FIRST || position === LAST) { | ||
// Determine the $relativeElement by traversing the sibling list and | ||
// stop at the first divider found | ||
var $listElem = $sectionMarker; | ||
$relativeElement = $listElem; | ||
while (true) { | ||
$listElem = (position === FIRST ? $listElem.prev() : $listElem.next()); | ||
if ($listElem.length === 0) { | ||
break; | ||
} else if ($listElem.find(".divider").length === 0) { | ||
$relativeElement = $listElem; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (position === FIRST) { | ||
position = BEFORE; | ||
} else { | ||
position = AFTER; | ||
} | ||
} else { | ||
console.log("Bad Parameter: MenuSection used as relativeID with a position other than FIRST or LAST"); | ||
$relativeElement = null; | ||
} | ||
|
||
return {relativeElement: $relativeElement, position: position}; | ||
}; | ||
|
||
/** | ||
* Adds a new menu item with the specified id and display text. The insertion position is | ||
|
@@ -331,7 +380,7 @@ define(function (require, exports, module) { | |
* one or more key bindings to associate with the supplied command. | ||
* @param {?string} position - constant defining the position of new the MenuItem relative | ||
* to other MenuItems. Default is LAST. (see Insertion position constants). | ||
* @param {?string} relativeID - id of command (future: also sub-menu, or menu section) that | ||
* @param {?string} relativeID - id of command or menu section (future: sub-menu) that | ||
* the new menuItem will be positioned relative to. Required when position is | ||
* AFTER or BEFORE, ignored when position is FIRST or LAST | ||
* | ||
|
@@ -390,7 +439,14 @@ define(function (require, exports, module) { | |
} | ||
|
||
// Insert menu item | ||
var $relativeElement = this._getRelativeMenuItem(relativeID); | ||
var $relativeElement; | ||
if (relativeID && relativeID.hasOwnProperty("sectionMarker")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of overriding position "FIRST" and "LAST" and looking for "sectionMarker", I think it might be better to add new relative position constants something like "FIRST_IN_SECTION" and "LAST_IN_SECTION", and give normal IDs to the dividers. This seems like a simple API and also simpler code. |
||
var sectionPos = this._getMenuSectionPosition(relativeID, position); | ||
$relativeElement = sectionPos.relativeElement; | ||
position = sectionPos.position; | ||
} else { | ||
$relativeElement = this._getRelativeMenuItem(relativeID); | ||
} | ||
_insertInList($("li#" + StringUtils.jQueryIdEscape(this.id) + " > ul.dropdown-menu"), | ||
$menuItem, position, $relativeElement); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to have "key" at the end of these 2 ids? It doesn't seem consistent.