From 7cbc22b052a28a1e5340a0fa58b27500e2495c87 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 2 Mar 2017 11:39:30 -0800 Subject: [PATCH] Management sections: Allow easy sub-section lookup (#10651) * Allow sub-section lookup using `/` separator * Adding assertion to check against the more explicit method * Conforming to JS style guide * Better wording --- src/ui/public/management/__tests__/section.js | 10 ++++++++++ src/ui/public/management/section.js | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ui/public/management/__tests__/section.js b/src/ui/public/management/__tests__/section.js index 97f08491aed69..b41a2e494a652 100644 --- a/src/ui/public/management/__tests__/section.js +++ b/src/ui/public/management/__tests__/section.js @@ -94,6 +94,16 @@ describe('ManagementSection', () => { it('returns undefined if un-registered', () => { expect(section.getSection('unknown')).to.be(undefined); }); + + it('returns sub-sections specified via a /-separated path', () => { + section.getSection('about').register('time'); + expect(section.getSection('about/time')).to.be.a(ManagementSection); + expect(section.getSection('about/time')).to.be(section.getSection('about').getSection('time')); + }); + + it('returns undefined if a sub-section along a /-separated path does not exist', () => { + expect(section.getSection('about/damn/time')).to.be(undefined); + }); }); describe('items', () => { diff --git a/src/ui/public/management/section.js b/src/ui/public/management/section.js index 1a318939c2dcb..fccfc5f24dbd9 100644 --- a/src/ui/public/management/section.js +++ b/src/ui/public/management/section.js @@ -72,6 +72,17 @@ export default class ManagementSection { */ getSection(id) { - return this.items.byId[id]; + if (!id) { + return; + } + + const sectionPath = id.split('/'); + return sectionPath.reduce((currentSection, nextSection) => { + if (!currentSection) { + return; + } + + return currentSection.items.byId[nextSection]; + }, this); } }