44 work in solid-ui by adjusting where imported functions are found.
55 */
66import { IndexedFormula , NamedNode } from 'rdflib'
7- import { icons } from '..'
7+ import { icons } from '../index '
88import { authn } from 'solid-logic'
99import { loginStatusBox } from '../login/login'
1010// import { loginStatusBox, authSession, currentUser } from '../authn/authn'
1111import * as widgets from '../widgets'
1212import { emptyProfile } from './empty-profile'
13- import { addStyleClassToElement , getPod , throttle } from './headerHelpers'
13+ import { addStyleClassToElement , getPod , throttle } from '../utils/headerFooterHelpers'
14+
15+ /**
16+ * menu icons
17+ */
18+ const DEFAULT_HELP_MENU_ICON = icons . iconBase + 'noun_help.svg'
19+ const DEFAUL_SOLID_ICON_URL = 'https://solidproject.org/assets/img/solid-emblem.svg'
1420
1521export type MenuItemLink = {
1622 label : string ,
17- url : string
23+ url : string ,
24+ target ?: string
1825}
1926
2027export type MenuItemButton = {
2128 label : string ,
22- onclick : ( ) => { }
29+ onclick : ( ) => void
2330}
2431
2532export type MenuItems = MenuItemLink | MenuItemButton
2633
2734/*
28- HeaderOptions allow for customizing the logo and menu list. If a logo is not provided the default
29- is solid. Menulist will always show a link to logout and to the users profile.
35+ HeaderOptions allow for customizing the logo and menu list. If a logo is not provided the default
36+ is solid. Menulist will always show a link to logout and to the users profile.
3037 */
3138export type HeaderOptions = {
3239 logo ?: string ,
33- menuList ?: MenuItems [ ]
40+ helpIcon ?: string ,
41+ helpMenuList ?: MenuItems [ ]
3442}
3543
3644/**
3745 * Initialize header component, the header object returned depends on whether the user is authenticated.
3846 * @param store the data store
39- * @param options allow the header to be customized with a personalized logo and a menu list of links or buttons.
47+ * @param userMenuList a list of menu items when the user is logged in
48+ * @param options allow the header to be customized with a personalized logo, help icon and a help menu list of links or buttons.
4049 * @returns a header for an authenticated user with menu items given or a login screen
4150 */
42- export async function initHeader ( store : IndexedFormula , options ?: HeaderOptions ) {
51+ export async function initHeader ( store : IndexedFormula , userMenuList : MenuItems [ ] , options ?: HeaderOptions ) {
4352 const header = document . getElementById ( 'PageHeader' )
4453 if ( ! header ) {
4554 return
4655 }
4756
4857 const pod = getPod ( )
49- rebuildHeader ( header , store , pod , options ) ( )
50- authn . authSession . onLogout ( rebuildHeader ( header , store , pod , options ) )
51- authn . authSession . onLogin ( rebuildHeader ( header , store , pod , options ) )
58+ rebuildHeader ( header , store , pod , userMenuList , options ) ( )
59+ authn . authSession . onLogout ( rebuildHeader ( header , store , pod , userMenuList , options ) )
60+ authn . authSession . onLogin ( rebuildHeader ( header , store , pod , userMenuList , options ) )
5261}
5362/**
5463 * @ignore exporting this only for the unit test
5564 */
56- export function rebuildHeader ( header : HTMLElement , store : IndexedFormula , pod : NamedNode , options ?: HeaderOptions ) {
65+ export function rebuildHeader ( header : HTMLElement , store : IndexedFormula , pod : NamedNode , userMenuList : MenuItems [ ] , options ?: HeaderOptions ) {
5766 return async ( ) => {
5867 const user = authn . currentUser ( )
5968 header . innerHTML = ''
60- header . appendChild ( await createBanner ( store , pod , user , options ) )
69+ header . appendChild ( await createBanner ( store , pod , user , userMenuList , options ) )
6170 }
6271}
6372/**
6473 * @ignore exporting this only for the unit test
6574 */
66- export async function createBanner ( store : IndexedFormula , pod : NamedNode , user : NamedNode | null , options ?: HeaderOptions ) : Promise < HTMLElement > {
75+ export async function createBanner ( store : IndexedFormula , pod : NamedNode , user : NamedNode | null , userMenuList : MenuItems [ ] , options ?: HeaderOptions ) : Promise < HTMLElement > {
6776 const podLink = document . createElement ( 'a' )
6877 podLink . href = pod . uri
6978 addStyleClassToElement ( podLink , [ 'header-banner__link' ] )
7079 const image = document . createElement ( 'img' )
7180 if ( options ) {
72- image . src = options . logo ? options . logo : 'https://solidproject.org/assets/img/solid-emblem.svg'
81+ image . src = options . logo ? options . logo : DEFAUL_SOLID_ICON_URL
7382 }
7483 addStyleClassToElement ( image , [ 'header-banner__icon' ] )
7584 podLink . appendChild ( image )
7685
7786 const userMenu = user
78- ? await createUserMenu ( store , user , options )
87+ ? await createUserMenu ( store , user , userMenuList )
7988 : createLoginSignUpButtons ( )
8089
81- const helpMenu = createHelpMenu ( )
82-
8390 const banner = document . createElement ( 'div' )
8491 addStyleClassToElement ( banner , [ 'header-banner' ] )
8592 banner . appendChild ( podLink )
8693
8794 const leftSideOfHeader = document . createElement ( 'div' )
8895 addStyleClassToElement ( leftSideOfHeader , [ 'header-banner__right-menu' ] )
8996 leftSideOfHeader . appendChild ( userMenu )
90- leftSideOfHeader . appendChild ( helpMenu )
97+
98+ if ( options && options . helpMenuList ) {
99+ const helpMenu = createHelpMenu ( options , options . helpMenuList )
100+ leftSideOfHeader . appendChild ( helpMenu as HTMLDivElement )
101+ }
91102
92103 banner . appendChild ( leftSideOfHeader )
93104
@@ -96,20 +107,25 @@ export async function createBanner (store: IndexedFormula, pod: NamedNode, user:
96107/**
97108 * @ignore exporting this only for the unit test
98109 */
99- export function createHelpMenu ( ) {
110+ export function createHelpMenu ( options : HeaderOptions , helpMenuItems : MenuItems [ ] ) {
111+ if ( ! helpMenuItems ) return
100112 const helpMenuList = document . createElement ( 'ul' )
101113 addStyleClassToElement ( helpMenuList , [ 'header-user-menu__list' ] )
102- helpMenuList . appendChild ( createUserMenuItem ( createUserMenuLink ( 'User guide' , 'https://github.com/solid/userguide' , '_blank' ) ) )
103- helpMenuList . appendChild ( createUserMenuItem ( createUserMenuLink ( 'Report a problem' , 'https://github.com/solid/solidos/issues' , '_blank' ) ) )
114+ helpMenuItems . forEach ( function ( menuItem ) {
115+ const menuItemType : string = ( menuItem as MenuItemLink ) . url ? 'url' : 'onclick'
116+ if ( menuItemType === 'url' ) {
117+ helpMenuList . appendChild ( createUserMenuItem ( createUserMenuLink ( menuItem . label , ( menuItem as MenuItemLink ) . url , ( menuItem as MenuItemLink ) . target ) ) )
118+ } else {
119+ helpMenuList . appendChild ( createUserMenuItem ( createUserMenuButton ( menuItem . label , ( menuItem as MenuItemButton ) . onclick ) ) )
120+ }
121+ } )
104122
105123 const helpMenu = document . createElement ( 'nav' )
106124
107125 addStyleClassToElement ( helpMenu , [ 'header-user-menu__navigation-menu' ] )
108126 helpMenu . setAttribute ( 'aria-hidden' , 'true' )
109127 helpMenu . appendChild ( helpMenuList )
110128
111- const helpIcon = icons . iconBase + 'noun_144.svg'
112-
113129 const helpMenuContainer = document . createElement ( 'div' )
114130 addStyleClassToElement ( helpMenuContainer , [ 'header-banner__user-menu' ] )
115131 addStyleClassToElement ( helpMenuContainer , [ 'header-user-menu' ] )
@@ -120,7 +136,7 @@ export function createHelpMenu () {
120136 helpMenuTrigger . type = 'button'
121137
122138 const helpMenuIcon = document . createElement ( 'img' )
123- helpMenuIcon . src = helpIcon
139+ helpMenuIcon . src = ( options && options . helpIcon ) ? options . helpIcon : icons . iconBase + DEFAULT_HELP_MENU_ICON
124140 addStyleClassToElement ( helpMenuIcon , [ 'header-banner__help-icon' ] )
125141 helpMenuContainer . appendChild ( helpMenuTrigger )
126142 helpMenuTrigger . appendChild ( helpMenuIcon )
@@ -160,7 +176,7 @@ export function createUserMenuButton (label: string, onClick: EventListenerOrEve
160176/**
161177 * @ignore exporting this only for the unit test
162178 */
163- export function createUserMenuLink ( label : string , href : string , target ?:string ) : HTMLElement {
179+ export function createUserMenuLink ( label : string , href : string , target ?: string ) : HTMLElement {
164180 const link = document . createElement ( 'a' )
165181 addStyleClassToElement ( link , [ 'header-user-menu__link' ] )
166182 link . href = href
@@ -172,30 +188,25 @@ export function createUserMenuLink (label: string, href: string, target?:string)
172188/**
173189 * @ignore exporting this only for the unit test
174190 */
175- export async function createUserMenu ( store : IndexedFormula , user : NamedNode , options ?: HeaderOptions ) : Promise < HTMLElement > {
191+ export async function createUserMenu ( store : IndexedFormula , user : NamedNode , userMenuList : MenuItems [ ] ) : Promise < HTMLElement > {
176192 const fetcher = ( < any > store ) . fetcher
177193 if ( fetcher ) {
178194 // Making sure that Profile is loaded before building menu
179195 await fetcher . load ( user )
180196 }
197+
181198 const loggedInMenuList = document . createElement ( 'ul' )
182199 addStyleClassToElement ( loggedInMenuList , [ 'header-user-menu__list' ] )
183- loggedInMenuList . appendChild ( createUserMenuItem ( createUserMenuLink ( 'Show your profile' , user . uri ) ) )
184- if ( options ) {
185- if ( options . menuList ) {
186- options . menuList . forEach ( function ( menuItem ) {
187- const menuItemType : string = ( menuItem as MenuItemLink ) . url ? 'url' : 'onclick'
188- if ( menuItemType === 'url' ) {
189- loggedInMenuList . appendChild ( createUserMenuItem ( createUserMenuLink ( menuItem . label , menuItem [ menuItemType ] ) ) )
190- } else {
191- loggedInMenuList . appendChild ( createUserMenuItem ( createUserMenuButton ( menuItem . label , menuItem [ menuItemType ] ) ) )
192- }
193- } )
194- }
200+ if ( userMenuList ) {
201+ userMenuList . forEach ( function ( menuItem ) {
202+ const menuItemType : string = ( menuItem as MenuItemLink ) . url ? 'url' : 'onclick'
203+ if ( menuItemType === 'url' ) {
204+ loggedInMenuList . appendChild ( createUserMenuItem ( createUserMenuLink ( menuItem . label , ( menuItem as MenuItemLink ) . url , ( menuItem as MenuItemLink ) . target ) ) )
205+ } else {
206+ loggedInMenuList . appendChild ( createUserMenuItem ( createUserMenuButton ( menuItem . label , ( menuItem as MenuItemButton ) . onclick ) ) )
207+ }
208+ } )
195209 }
196-
197- loggedInMenuList . appendChild ( createUserMenuItem ( createUserMenuButton ( 'Log out' , ( ) => authn . authSession . logout ( ) ) ) )
198-
199210 const loggedInMenu = document . createElement ( 'nav' )
200211
201212 addStyleClassToElement ( loggedInMenu , [ 'header-user-menu__navigation-menu' ] )
0 commit comments