Skip to content
Snippets Groups Projects
layout.js 6.43 KiB
Newer Older
Hermann's avatar
Hermann committed
import m from 'mithril';
import {
  List,
  ListTile,
  Icon,
  Toolbar,
  Dialog,
  SVG,
Sandro Lutz's avatar
Sandro Lutz committed
  Button,
} from 'polythene-mithril';
Hermann's avatar
Hermann committed
import { styler } from 'polythene-core-css';
import { icons } from './views/elements';
import { deleteSession, getUserRights, getSchema } from './auth';
import { colors } from './style';
Hermann's avatar
Hermann committed

const layoutStyle = [
  {
    body: { padding: 0, margin: 0, overflow: 'hidden' },
Hermann's avatar
Hermann committed
    '.main-toolbar': {
      'grid-column': '1 / span 2',
      'grid-row': 1,
    },
Hermann's avatar
Hermann committed
    '.wrapper-main': {
      height: '100%',
      width: '100%',
      display: 'grid',
      'grid-template-columns': '200px auto',
Hermann's avatar
Hermann committed
      'grid-template-rows': '64px auto',
Hermann's avatar
Hermann committed
    },
    '.wrapper-sidebar': {
      '@media (min-width:1200px)': { 'grid-column': 1 },
      '@media (max-width:1200px)': {
        position: 'absolute',
        top: '64px',
        left: '-200px',
        width: '200px',
        'z-index': 100000,
      },
Hermann's avatar
Hermann committed
      height: '100%',
      'grid-row': 2,
Hermann's avatar
Hermann committed
      'overflow-y': 'auto',
      background: '#cccccc',
      color: 'white',
    },
    '.wrapper-content': {
      '@media (min-width:1200px)': { 'grid-column': 2 },
      '@media (max-width:1200px)': { 'grid-column': '1 / span 2' },
Hermann's avatar
Hermann committed
      height: 'calc(100vh - 64px)',
Hermann's avatar
Hermann committed
      'grid-row': 2,
Hermann's avatar
Hermann committed
      background: '#eee',
      overflow: 'hidden',
    },
    '.menu-button': {
      '@media (min-width:1200px)': { display: 'none' },
      '@media (max-width:1200px)': { display: 'inline' },
    },
    '.content-hider': {
      display: 'none',
      position: 'absolute',
      top: '64px',
      left: '200px',
      width: '100%',
      height: '100%',
      background: '#000000aa',
Hermann's avatar
Hermann committed
  },
];
styler.add('layout', layoutStyle);

function toggleDrawer() {
  const drawer = document.querySelector('.wrapper-sidebar');
  const shadow = document.querySelector('.content-hider');
  if (drawer.style.left === '0px') {
    drawer.style.left = '-200px';
    shadow.style.display = 'none';
  } else {
    drawer.style.left = '0px';
    shadow.style.display = 'block';
Hermann's avatar
Hermann committed
class Menupoint {
  view({ attrs: { title, href, icon = null } }) {
    return m(ListTile, {
      url: { href, oncreate: m.route.link },
      front: icon ? m(Icon, { svg: m.trust(icon) }) : '',
      ink: true,
Hermann's avatar
Hermann committed
      title,
    });
  }
}

export class loadingScreen {
  view() {
    return m('div', {
      style: {
        height: '100%',
        width: '100%',
        display: 'flex',
        'flex-direction': 'column',
        'justify-content': 'center',
        'align-items': 'center',
        'animation-name': 'popup',
        'animation-duration': '2000ms',
      },
    }, m('div', { style: { height: '5vh', 'font-size': '4em' } }, 'Loading...'), m('div', {
      style: {
        height: '20vh',
        width: '20vh',
        'animation-name': 'spin',
        'animation-duration': '2500ms',
        'animation-iteration-count': 'infinite',
        'animation-timing-function': 'linear',
      },
    }, m('div', {
      style: { height: '20vh', width: '20vh', display: 'inline-block' },
    }, m(SVG, {
      style: { width: 'inherit', height: 'inherit' },
      content: m.trust(icons.amivWheel),
    }))));
  }
}

export class Layout {
Hermann's avatar
Hermann committed
  view({ children }) {
    if (!getSchema()) return m(loadingScreen);
    const userRights = getUserRights();
Hermann's avatar
Hermann committed
    return m('div', [
      m('div.wrapper-main.smooth', [
Hermann's avatar
Hermann committed
        m(Toolbar, {
          className: 'main-toolbar',
Hermann's avatar
Hermann committed
          tone: 'dark',
Hermann's avatar
Hermann committed
          style: { backgroundColor: colors.amiv_blue, color: '#ffffff' },
        }, [
          m('div.menu-button', m(IconButton, {
            className: 'menu-button',
            icon: { svg: { content: m.trust(icons.menu) } },
            events: { onclick: () => { toggleDrawer(); } },
            style: { color: '#ffffff' },
          })),
          m('div', { style: { 'font-size': '18px', 'margin-left': '20px' } }, 'AMIV Admintools'),
Hermann's avatar
Hermann committed
          m('a', {
            href: 'https://gitlab.ethz.ch/amiv/amiv-admintool/issues/new?issuable_template=Bug',
            target: '_blank',
            style: {
              color: '#888888',
              'text-decoration': 'none',
              'text-align': 'right',
              'margin-right': '20px',
              'margin-left': 'auto',
Hermann's avatar
Hermann committed
            },
          }, 'Is something not working? Report a bug!'),
Sandro Lutz's avatar
Sandro Lutz committed
          m(Button, {
            label: 'logout',
            events: { onclick: deleteSession },
          }),
Hermann's avatar
Hermann committed
        ]),
Hermann's avatar
Hermann committed
        m(
          'div.mdc-typography.wrapper-sidebar',
Hermann's avatar
Hermann committed
          { style: { width: '200px' } },
          m(List, {
            header: { title: 'Menu' },
            hoverable: true,
Hermann's avatar
Hermann committed
            tiles: [
              userRights.users.indexOf('POST') > -1 && m(Menupoint, {
Hermann's avatar
Hermann committed
                href: '/users',
                icon: icons.iconUsersSVG,
                title: 'Users',
              }),
              m(Menupoint, {
                href: '/events',
                icon: icons.iconEventSVG,
                title: 'Events',
              }),
              m(Menupoint, {
                href: '/groups',
Hermann's avatar
Hermann committed
                icon: icons.group,
Hermann's avatar
Hermann committed
                title: 'Groups',
              }),
              userRights.joboffers.indexOf('POST') > -1 && m(Menupoint, {
                href: '/joboffers',
                icon: icons.iconJobsSVG,
                title: 'Job offers',
              }),
                href: '/studydocuments',
                icon: icons.studydoc,
                title: 'Studydocs',
Hermann's avatar
Hermann committed
              }),
              m(Menupoint, {
                href: '/blacklist',
                icon: icons.blacklist,
                title: 'Blacklist',
              }),
              m(Menupoint, {
                href: '/infoscreen',
                icon: icons.iconEventSVG,
                title: 'Infoscreen',
              }),
Hermann's avatar
Hermann committed
            ],
          }),
        ),
        m('div.wrapper-content', children),
        // shadow over content in case drawer is out
        m('div.content-hider'),
Hermann's avatar
Hermann committed
      ]),
Hermann's avatar
Hermann committed
      // dialog element will show when Dialog.show() is called, this is only a placeholder
      m(Dialog),
Hermann's avatar
Hermann committed
    ]);
  }
}
Hermann's avatar
Hermann committed
export class Error404 {
  view() {
    return m('div', {
      style: {
        height: '100%',
        width: '100%',
        display: 'flex',
        'flex-direction': 'column',
        'justify-content': 'center',
        'align-items': 'center',
      },
    }, [
      m('div', { style: { height: '5vh', 'font-size': '4em' } }, 'Error 404: Item Not Found!'),
    ]);
  }
}