forked from railsware/upterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.js
118 lines (106 loc) · 3.42 KB
/
react.js
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
var jQuery = require('jquery');
var Terminal = require('./compiled/terminal');
var React = require('react');
var Board = React.createClass({
getInitialState: function () {
var terminal = new Terminal(getDimensions());
jQuery(window).resize(function() {
terminal.resize(getDimensions());
});
return {terminal: terminal};
},
componentDidMount: function () {
this.state.terminal.on('invocation', function () {
this.forceUpdate();
}.bind(this));
},
render: function () {
var invocations = this.state.terminal.invocations.map(function (invocation, index) {
return (
<Invocation key={index} invocation={invocation}/>
)
});
return (
<div id="board">
<div id="invocations">
{invocations}
</div>
<StatusLine currentWorkingDirectory={this.state.terminal.currentDirectory}/>
</div>
);
}
});
var Invocation = React.createClass({
componentDidMount: function () {
this.props.invocation.on('data', function () {
this.forceUpdate();
}.bind(this));
},
render: function () {
return (
<div className="invocation">
<Prompt prompt={this.props.invocation.getPrompt()}/>
{this.props.invocation.getBuffer().render()}
</div>
);
}
});
var Prompt = React.createClass({
getInputNode: function (event) {
return this.refs.command.getDOMNode()
},
handleKeyDown: function (event) {
if (event.keyCode == 13) {
this.props.prompt.send(this.getInputNode().value);
}
// Ctrl+P, ↑.
if ((event.ctrlKey && event.keyCode === 80) || event.keyCode === 38) {
var prevCommand = this.props.prompt.history.getPrevious();
if (typeof prevCommand != 'undefined') {
this.getInputNode().value = prevCommand;
}
}
// Ctrl+N, ↓.
if ((event.ctrlKey && event.keyCode === 78) || event.keyCode === 40) {
var command = this.props.prompt.history.getNext();
this.getInputNode().value = command || '';
}
event.stopPropagation();
},
render: function () {
return (
<div className="prompt">
<div className="prompt-decoration">
<div className="arrow"></div>
</div>
<input onKeyDown={this.handleKeyDown} type="text" ref="command" autoFocus="autofocus"/>
</div>
)
}
});
var StatusLine = React.createClass({
render: function () {
return (
<div id="status-line">
<CurrentDirectory currentWorkingDirectory={this.props.currentWorkingDirectory}/>
</div>
)
}
});
var CurrentDirectory = React.createClass({
render: function () {
return (
<div id="current-directory">{this.props.currentWorkingDirectory}</div>
)
}
});
function getDimensions() {
var letter = document.getElementById('sizes-calculation');
return {
columns: Math.floor(window.innerWidth / letter.clientWidth * 10),
rows: Math.floor(window.innerHeight / letter.clientHeight)
};
}
jQuery(document).ready(function () {
React.render(<Board />, document.getElementById('black-board'));
});