const checkQueryString = (url) => {
if (/\?url=/.test(url)) {
return decodeURIComponent(url.split('=')[1]);
}
return url;
};
function formatItem(item) {
const openInNew = item.isNewWindow === 'True';
const url = openInNew ? checkQueryString(item.AbsolutePortalURL) : item.AbsolutePortalURL;
return {
label: item.Label,
description: item.Description,
url,
openInNew,
imageUrl: item.ImageURL,
};
}
export default class NavigationCollection {
constructor(obj) {
this.name = obj.NavCollection.Name;
this.label = obj.NavCollection.Label;
this.contents = obj.NavCollection.Contents.NavItem;
this.parseContents();
}
parseContents() {
if (!Array.isArray(this.contents)) {
this.contents = [this.contents];
}
this.contents = this.contents.map((content) => {
if (content.NavItem) {
return { label: content.Label, contents: content.NavItem.map(item => formatItem(item)) };
}
return formatItem(content);
});
}
}
|