index.jsx 5.4 KB
Newer Older
J
Jason Park 已提交
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
import React from 'react';
import { connect } from 'react-redux';
import FontAwesomeIcon from '@fortawesome/react-fontawesome'
import faSearch from '@fortawesome/fontawesome-free-solid/faSearch';
import faCode from '@fortawesome/fontawesome-free-solid/faCode';
import faBook from '@fortawesome/fontawesome-free-solid/faBook';
import faGithub from '@fortawesome/fontawesome-free-brands/faGithub';
import { Ellipsis, ExpandableListItem, ListItem } from '/components';
import { classes } from '/common/util';
import { actions as envActions } from '/reducers/env';
import 'github-fork-ribbon-css/gh-fork-ribbon.css';
import styles from './stylesheet.scss';

@connect(
  ({ env }) => ({
    env
  }), {
    ...envActions
  }
)
class Navigator extends React.Component {
  constructor(props) {
    super(props);

    const { categoryKey } = this.props.env;

    this.state = {
      poweredByOpened: false,
      categoriesOpened: {
        [categoryKey]: true,
      },
      query: '',
    }
  }

  togglePoweredBy(poweredByOpened = !this.state.poweredByOpened) {
    this.setState({ poweredByOpened });
  }

  toggleCategory(key, categoryOpened = !this.state.categoriesOpened[key]) {
    const categoriesOpened = {
      ...this.state.categoriesOpened,
      [key]: categoryOpened,
    };
    this.setState({ categoriesOpened });
  }

  handleChangeQuery(e) {
    const { categories } = this.props.env;
    const categoriesOpened = {};
    const query = e.target.value;
52 53 54
    categories.forEach(category => {
      if (this.testQuery(name) || category.algorithms.find(algorithm => this.testQuery(algorithm.name))) {
        categoriesOpened[category.key] = true;
J
Jason Park 已提交
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
      }
    });

    this.setState({ categoriesOpened, query });
  }

  testQuery(value) {
    const { query } = this.state;
    return new RegExp(query, 'i').test(value);
  }

  render() {
    const { poweredByOpened, categoriesOpened, query } = this.state;
    const { className, style } = this.props;
    const { categoryKey: selectedCategoryKey, algorithmKey: selectedAlgorithmKey, categories } = this.props.env;

    return (
      <nav className={classes(styles.navigator, className)} style={style}>
        <div className={styles.search_bar_container}>
          <FontAwesomeIcon fixedWidth icon={faSearch} className={styles.search_icon} />
          <input type="text" className={styles.search_bar} autoFocus placeholder="Search ..." value={query}
                 onChange={e => this.handleChangeQuery(e)} />
        </div>
        <div className={styles.algorithm_list}>
          {
80 81 82 83 84 85
            categories.map(category => {
              const categoryOpened = categoriesOpened[category.key];
              let algorithms = category.algorithms;
              if (!this.testQuery(category.name)) {
                algorithms = algorithms.filter(algorithm => this.testQuery(algorithm.name));
                if (!algorithms.length) return null;
J
Jason Park 已提交
86 87
              }
              return (
88 89
                <ExpandableListItem key={category.key} onClick={() => this.toggleCategory(category.key)}
                                    label={category.name}
J
Jason Park 已提交
90 91
                                    opened={categoryOpened}>
                  {
92 93 94
                    algorithms.map(algorithm => {
                      const selected = category.key === selectedCategoryKey && algorithm.key === selectedAlgorithmKey;
                      const [file] = algorithm.files;
J
Jason Park 已提交
95
                      return (
96
                        <ListItem indent key={algorithm.key} selected={selected}
J
Jason Park 已提交
97
                                  to={`/${category.key}/${algorithm.key}/${file.key}`}>
98
                          <Ellipsis>{algorithm.name}</Ellipsis>
J
Jason Park 已提交
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
                        </ListItem>
                      )
                    })
                  }
                </ExpandableListItem>
              );
            })
          }
        </div>
        <div className={styles.footer}>
          <ListItem className={styles.scratch_paper} icon={faCode}>Scratch Paper</ListItem>
          <ListItem className={styles.documentation} icon={faBook}>Tracer API</ListItem>
          <ExpandableListItem icon={faGithub} onClick={() => this.togglePoweredBy()} label="Powered by ..."
                              opened={poweredByOpened}>
            <ListItem indent href="https://github.com/jquery/jquery">jquery/jquery</ListItem>
            <ListItem indent href="https://github.com/ajaxorg/ace">ajaxorg/ace</ListItem>
            <ListItem indent href="https://github.com/jacomyal/sigma.js">jacomyal/sigma.js</ListItem>
            <ListItem indent href="https://github.com/chartjs/Chart.js">chartjs/Chart.js</ListItem>
            <ListItem indent href="https://github.com/Daniel15/babel-standalone">Daniel15/babel-standalone</ListItem>
            <ListItem indent href="https://github.com/showdownjs/showdown">showdownjs/showdown</ListItem>
            <ListItem indent href="https://github.com/FortAwesome/Font-Awesome">FortAwesome/Font-Awesome</ListItem>
            <ListItem indent href="https://github.com/simonwhitaker/github-fork-ribbon-css">simonwhitaker/github-fork-ribbon-css</ListItem>
            <ListItem indent href="https://github.com/mathjax/MathJax">mathjax/MathJax</ListItem>
          </ExpandableListItem>
        </div>
        <a className={classes('github-fork-ribbon', 'right-bottom')}
           href="http://github.com/parkjs814/AlgorithmVisualizer" data-ribbon="Fork me on GitHub"
           title="Fork me on GitHub">Fork me on GitHub
        </a>
      </nav>
    );
  }
}

export default Navigator;