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
97 lines (85 loc) · 2.72 KB
/
Copy pathindex.js
File metadata and controls
97 lines (85 loc) · 2.72 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
import React from 'react';
import { connect } from 'react-redux';
import { BaseComponent } from 'components';
import { actions } from 'reducers';
import styles from './VisualizationViewer.module.scss';
import * as TracerClasses from 'core/tracers';
import * as LayoutClasses from 'core/layouts';
import { classes } from 'common/util';
class VisualizationViewer extends BaseComponent {
constructor(props) {
super(props);
this.reset();
}
reset() {
this.root = null;
this.objects = {};
}
componentDidMount() {
const { chunks, cursor } = this.props.player;
this.update(chunks, cursor);
}
componentWillReceiveProps(nextProps) {
const { chunks, cursor } = nextProps.player;
const { chunks: oldChunks, cursor: oldCursor } = this.props.player;
if (chunks !== oldChunks || cursor !== oldCursor) {
this.update(chunks, cursor, oldChunks, oldCursor);
}
}
update(chunks, cursor, oldChunks = [], oldCursor = 0) {
let applyingChunks;
if (cursor > oldCursor) {
applyingChunks = chunks.slice(oldCursor, cursor);
} else {
this.reset();
applyingChunks = chunks.slice(0, cursor);
}
applyingChunks.forEach(chunk => this.applyChunk(chunk));
const lastChunk = applyingChunks[applyingChunks.length - 1];
if (lastChunk && lastChunk.lineNumber !== undefined) {
this.props.setLineIndicator({ lineNumber: lastChunk.lineNumber, cursor });
} else {
this.props.setLineIndicator(undefined);
}
}
applyCommand(command) {
const { key, method, args } = command;
try {
if (key === null && method === 'setRoot') {
const [root] = args;
this.root = this.objects[root];
} else if (method === 'destroy') {
delete this.objects[key];
} else if (method in LayoutClasses) {
const [children] = args;
const LayoutClass = LayoutClasses[method];
this.objects[key] = new LayoutClass(key, key => this.objects[key], children);
} else if (method in TracerClasses) {
const className = method;
const [title = className] = args;
const TracerClass = TracerClasses[className];
this.objects[key] = new TracerClass(key, key => this.objects[key], title);
} else {
this.objects[key][method](...args);
}
} catch (error) {
this.handleError(error);
}
}
applyChunk(chunk) {
chunk.commands.forEach(command => this.applyCommand(command));
}
render() {
const { className } = this.props;
return (
<div className={classes(styles.visualization_viewer, className)}>
{
this.root && this.root.render()
}
</div>
);
}
}
export default connect(({ player }) => ({ player }), actions)(
VisualizationViewer,
);