forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (124 loc) · 5.19 KB
/
index.js
File metadata and controls
136 lines (124 loc) · 5.19 KB
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
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 { ExpandableListItem, ListItem } from 'components';
import { classes } from 'common/util';
import { actions } from 'reducers';
import styles from './Navigator.module.scss';
class Navigator extends React.Component {
constructor(props) {
super(props);
this.state = {
categoriesOpened: {},
scratchPaperOpened: true,
query: '',
};
}
componentDidMount() {
const { algorithm } = this.props.current;
if (algorithm) {
this.toggleCategory(algorithm.categoryKey, true);
}
}
componentWillReceiveProps(nextProps) {
const { algorithm } = nextProps.current;
if (algorithm) {
this.toggleCategory(algorithm.categoryKey, true);
}
}
toggleCategory(key, categoryOpened = !this.state.categoriesOpened[key]) {
const categoriesOpened = {
...this.state.categoriesOpened,
[key]: categoryOpened,
};
this.setState({ categoriesOpened });
}
toggleScratchPaper(scratchPaperOpened = !this.state.scratchPaperOpened) {
this.setState({ scratchPaperOpened });
}
handleChangeQuery(e) {
const { categories } = this.props.directory;
const categoriesOpened = {};
const query = e.target.value;
categories.forEach(category => {
if (this.testQuery(category.name) || category.algorithms.find(algorithm => this.testQuery(algorithm.name))) {
categoriesOpened[category.key] = true;
}
});
this.setState({ categoriesOpened, query });
}
testQuery(value) {
const { query } = this.state;
const refine = string => string.replace(/-/g, ' ').replace(/[^\w ]/g, '');
const refinedQuery = refine(query);
const refinedValue = refine(value);
return new RegExp(`(^| )${refinedQuery}`, 'i').test(refinedValue) ||
new RegExp(refinedQuery, 'i').test(refinedValue.split(' ').map(v => v && v[0]).join(''));
}
render() {
const { categoriesOpened, scratchPaperOpened, query } = this.state;
const { className } = this.props;
const { categories, scratchPapers } = this.props.directory;
const { algorithm, scratchPaper } = this.props.current;
const categoryKey = algorithm && algorithm.categoryKey;
const algorithmKey = algorithm && algorithm.algorithmKey;
const gistId = scratchPaper && scratchPaper.gistId;
return (
<nav className={classes(styles.navigator, className)}>
<div className={styles.search_bar_container}>
<FontAwesomeIcon fixedWidth icon={faSearch} className={styles.search_icon}/>
<input type="text" className={styles.search_bar} aria-label="Search" placeholder="Search ..." autoFocus
value={query} onChange={e => this.handleChangeQuery(e)}/>
</div>
<div className={styles.algorithm_list}>
{
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;
}
return (
<ExpandableListItem key={category.key} onClick={() => this.toggleCategory(category.key)}
label={category.name}
opened={categoryOpened}>
{
algorithms.map(algorithm => (
<ListItem indent key={algorithm.key}
selected={category.key === categoryKey && algorithm.key === algorithmKey}
to={`/${category.key}/${algorithm.key}`} label={algorithm.name}/>
))
}
</ExpandableListItem>
);
})
}
</div>
<div className={styles.footer}>
<ExpandableListItem icon={faCode} label="Scratch Paper" onClick={() => this.toggleScratchPaper()}
opened={scratchPaperOpened}>
<ListItem indent label="New ..." to="/scratch-paper/new"/>
{
scratchPapers.map(scratchPaper => (
<ListItem indent key={scratchPaper.key} selected={scratchPaper.key === gistId}
to={`/scratch-paper/${scratchPaper.key}`} label={scratchPaper.name}/>
))
}
</ExpandableListItem>
<ListItem icon={faBook} label="API Reference"
href="https://github.com/algorithm-visualizer/algorithm-visualizer/wiki"/>
<ListItem icon={faGithub} label="Fork me on GitHub"
href="https://github.com/algorithm-visualizer/algorithm-visualizer"/>
</div>
</nav>
);
}
}
export default connect(({ current, directory, env }) => ({ current, directory, env }), actions)(
Navigator,
);