All files / src/react-components menu.js

100% Statements 55/55
100% Branches 26/26
100% Functions 15/15
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348                                                                                                  4x                                                                                                     122x 122x                       2x               2x                     2x                   126x 126x   124x   126x             2x             2x                               122x       122x 122x           122x   116x     6x                                           6x     6x   6x   3x 3x               3x   2x 1x                   1x           6x               4x                       133x 133x 133x   16x             1x       133x   7x   133x   60x   133x                       135x 135x 135x 135x 135x 135x   1079x 1079x 1079x   146x   1079x 1079x   16x           1063x         135x                              
/*
 * Part of Pleiar.no - a collection of tools for nurses
 *
 * Copyright (C) Eskild Hustvedt 2017-2018
 * Copyright (C) Fagforbundet 2019
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
 
// @flow
import * as React from 'react';
import { Collapse, Container, Row, Col } from 'reactstrap';
import { Link, withRouter } from 'react-router-dom';
import type { RouterHistory } from 'react-router-dom';
import { TopSearch } from './global-search';
import auth from '../auth';
import device from '../helper.device';
import { getAuthURL } from './auth';
import { ExternalLink } from './shared/links';
import IOSBackIcon from 'react-feather/dist/icons/arrow-left';
 
export type appList = Array<{|
    name: string,
    id: string,
    onClick?: (e: Event) => void,
    route?: string,
    exact?: boolean,
    raw?: boolean
|}>;
 
/**
 * This is the list of all apps that should be displayed in the menu
 *
 * The name is the label, id is the path, route is the route if it can not
 * be inferred and exact is a boolean specifying if the exact prop
 * needs to be supplied to react-router
 */
const apps: appList = [
    {
        name: 'Heim',
        id:'base',
        route: '/',
        exact: true
    },
    {
        name: 'Håndbok',
        id: 'handbok',
        exact: false
    },
    {
        name:'Verktøy',
        id:'verktoy',
        exact: false
    },
    {
        name: 'Ordliste',
        id: 'ordliste',
        exact: false
    },
    {
        name:'Hurtigoppslag',
        id: 'oppslag',
        exact: false
    },
    {
        name: 'E-læringskurs',
        id: 'elaering',
        exact: false,
    },
    {
        name:'Andre sider',
        id: 'andresider',
        exact: false
    },
    {
        name: "Flere fagtilbud fra Fagforbundet",
        id: 'om/flerefagtilbud',
    }
];
 
/**
 * The renderer of the top menu
 */
class Menu extends React.PureComponent<{||}, {|collapseOpen: boolean|}>
{
    constructor(props: {||}) // eslint-disable-line require-jsdoc
 
    {
        super(props);
        this.state = {
            collapseOpen: false,
        };
    }
 
    /**
     * Callback called when the user clicks on a nav link, used to
     * auto-close the "collapse" on mobile.
     * This is bound to this instance of `Menu` in the `constructor`.
     */
    onNavLinkClick ()
    {
        this.setNavbarState(false);
    }
 
    /**
     * Callback to toggle the collapse state
     */
    toggleCollapse ()
    {
        this.setState({
            collapseOpen: !this.state.collapseOpen
        });
    }
 
    /**
     * Toggle method for toggling the menu on mobile.
     * This is bound to this instance of `Menu` in the `constructor`.
     */
    setNavbarState(state: boolean)
    {
        this.setState({
            collapseOpen: state
        });
    }
 
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        // This is a hack that allows us to hide the "closing" transition for
        // the collapse
        const collapseStyle = {};
        if (!this.state.collapseOpen)
        {
            collapseStyle.display = 'none';
        }
        return <Container tag="nav">
            <Row>
                <Col className="col-xl-10 offset-xl-1">
                    <Row className="header-menu">
                        <Col className="text-right">
                            <TopSearch />
                            <MenuBranding />
                            <div className="menuButtonWrapper"><span className={"likeLink menuButton "+(this.state.collapseOpen ? "open" : "")} onClick={() => this.toggleCollapse()}>Meny</span></div>
                        </Col>
                    </Row>
                    <Row>
                        <Col>
                            <Collapse isOpen={this.state.collapseOpen} style={collapseStyle}>
                                <div className="col-lg-8 offset-lg-2 mainMenu">
                                    <MenuList onNavLinkClick={() => this.onNavLinkClick()} />
                                </div>
                            </Collapse>
                        </Col>
                    </Row>
                </Col>
            </Row>
        </Container>;
    }
}
 
/**
 * Renders our branding and, on iOS, navigation element
 */
class MenuBranding extends React.PureComponent<{||}>
{
    isIOSApp: boolean = false;
 
    constructor () //eslint-disable-line require-jsdoc
    {
        super();
        this.isIOSApp = device.isIOSApp();
    }
 
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        if(!this.isIOSApp)
        {
            return <Link key="branding" className="float-left BrandLogo" to="/"><img src="/pleiarlogo.svg" height="50" alt="Pleiar.no" /></Link>;
        }
        {
            return <IOSMenuBranding />;
        }
    }
}
 
/**
 * Even though we don't use the actual value of the history prop, we specify it
 * here to illustrate that it's a prop that we realy upon (because it's how
 * we're told about changes to the URL)
 */
type IOSMenuBrandingProps = {|
    history: RouterHistory,
|};
/**
 * This handles rendering the top menu branding in iOS, by appending a
 * back arrow to it, which is used to navigate the app
 */
class IOSMenuBrandingUnconnected extends React.PureComponent<IOSMenuBrandingProps>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const brand = [
            <img key="brand" src="/pleiarlogo.svg" height="45" alt="Pleiar.no" />
        ];
        let iosClass: string = "ios-menu-back";
        // If we're at /, then we return an inactive arrow
        if(location.pathname === "/")
        {
            iosClass += " ios-menu-back-inactive";
            brand.unshift(
                <div key="back" className={iosClass}>
                    <IOSBackIcon size={40} />
                </div>
            );
        }
        // If we're not at / and we have some history, then return an active arrow
        // that uses the history API to navigate back.
        else if (history.state)
        {
            brand.unshift(
                <div key="back" onClick={() => { history.back(); }} className={iosClass}>
                    <IOSBackIcon size={40} />
                </div>
            );
        }
        // Finally, if we're not at / but don't have any history either, just
        // pretend that we do have a history and that history is one back to /,
        // and emulate that behaviour
        else
        {
            brand.unshift(
                <Link to="/" key="back" className={iosClass}>
                    <IOSBackIcon size={40} />
                </Link>
            );
        }
        return <div className="float-left BrandLogo ios-brand">{brand}</div>;
    }
}
 
/**
 * IOSMenuBranding relies upon having its props updated by react-router-dom to
 * trigger re-renders. Thus we connect it to the router.
 */
const IOSMenuBranding = withRouter(IOSMenuBrandingUnconnected);
 
/**
 * A (reusable) list of our main menu items.
 * This is used to render the menu in the collapse, but can also be used
 * anywhere else to render a menu structure.
 */
class MenuList extends React.PureComponent<{|onNavLinkClick?: () => mixed, hideHome?: boolean, extraApps?: appList|}>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const { onNavLinkClick, extraApps, hideHome } = this.props;
        let renderAppList: appList = [].concat(apps);
        if (! auth.isAuthenticated())
        {
            renderAppList.push({
                name: 'Logg inn',
                id: 'login',
                route: getAuthURL(),
                raw: true,
                onClick: () =>
                {
                    window.localStorage.pleiarAuthRedirect = location.pathname;
                }
            });
        }
        if(extraApps)
        {
            renderAppList = renderAppList.concat(extraApps);
        }
        if(hideHome)
        {
            renderAppList = renderAppList.filter( (entry) => entry.id !== "base" );
        }
        return <MenuListRenderer onNavLinkClick={onNavLinkClick} apps={renderAppList} />;
    }
}
 
/**
 * A reusable menu renderer (for instance used by MenuList). 
 */
class MenuListRenderer extends React.PureComponent<{|onNavLinkClick?: () => mixed, apps: appList|}>
{
    // eslint-disable-next-line require-jsdoc
    render(): React.Node
    {
        const { onNavLinkClick, apps } = this.props;
        const numberOfApps: number = apps.length;
        const leftMaxItems = Math.round( numberOfApps / 2 );
        const left = [];
        const right = [];
        for (const app of apps)
        {
            const target = left.length < leftMaxItems ? left : right;
            let path  : string = '/'+app.id;
            if(app.route)
            {
                path = app.route;
            }
            const onClick = (app.onClick ? app.onClick : (onNavLinkClick ? onNavLinkClick : null ) );
            if(app.raw)
            {
                target.push(
                    <ExternalLink sameWindow={true} key={path} href={path} onClick={onClick}>{app.name}</ExternalLink>
                );
            }
            else
            {
                target.push(
                    <Link key={path} to={path} onClick={onClick}>{app.name}</Link>
                );
            }
        }
        return <Row className="appMenu">
            <Col key="left" className="linkList" xs="12" md="6">
                {left}
            </Col>
            <Col key="right" className="linkList" xs="12" md="6">
                {right}
            </Col>
        </Row>;
    }
}
 
export default Menu;
export { MenuList, MenuListRenderer };
// Testing exports
export { apps };