Skip to content

Commit

Permalink
Submenus can be empty to start
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Apr 30, 2020
1 parent d1ef420 commit bf4c875
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/browser/api/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MenuItem = function (options) {
}
this.submenu = this.submenu || roles.getDefaultSubmenu(this.role);
if (this.submenu != null && this.submenu.constructor !== Menu) {
this.submenu = Menu.buildFromTemplate(this.submenu);
this.submenu = Menu.buildFromTemplate(this.submenu, true);
}
if (this.type == null && this.submenu != null) {
this.type = 'submenu';
Expand Down
8 changes: 6 additions & 2 deletions lib/browser/api/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,17 @@ Menu.setApplicationMenu = function (menu) {
}
};

Menu.buildFromTemplate = function (template) {
if (!Array.isArray(template) || template.length === 0) {
Menu.buildFromTemplate = function (template, isSubmenu = false) {
if (!Array.isArray(template)) {
throw new TypeError('Invalid template for Menu: Menu template must be an array');
} else if (!isSubmenu && template.length === 0) {
throw new TypeError('Invalid template for Menu: Menu template must be an non-empty array');
}

if (!areValidTemplateItems(template)) {
throw new TypeError('Invalid template for MenuItem: must have at least one of label, role or type');
}

const filtered = removeExtraSeparators(template);
const sorted = sortTemplate(filtered);

Expand Down
2 changes: 1 addition & 1 deletion spec-main/api-menu-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Menu module', function () {
it('throws when an non-array is passed as a template', () => {
expect(() => {
Menu.buildFromTemplate('hello' as any);
}).to.throw(/Invalid template for Menu: Menu template must be an non-empty array/);
}).to.throw(/Invalid template for Menu: Menu template must be an array/);
});

describe('Menu sorting and building', () => {
Expand Down

0 comments on commit bf4c875

Please sign in to comment.