Skip to content

Commit 81ef997

Browse files
committed
Merge pull request #64 from danvk/router
Use react-router to fully support /123 URLs
2 parents 76cbdb1 + c0eb484 commit 81ef997

File tree

5 files changed

+60
-20
lines changed

5 files changed

+60
-20
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"jquery": "~2.1.1",
1919
"highlightjs": "~8.0.0",
2020
"underscore": "~1.6.0",
21-
"react": "~0.11.1"
21+
"react": "~0.11.1",
22+
"react-router": "~0.10.2"
2223
}
2324
}

webdiff/app.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,12 @@ def index():
143143
return file_diff('0')
144144

145145

146-
@app.route("/<idx>")
146+
@app.route("/<int:idx>")
147147
def file_diff(idx):
148148
idx = int(idx)
149149
return render_template('file_diff.html',
150150
idx=idx,
151-
pairs=DIFF,
152-
this_pair=DIFF[idx],
153-
is_image_diff=util.is_image_diff(DIFF[idx]),
154-
num_pairs=len(DIFF))
151+
pairs=DIFF)
155152

156153

157154
@app.route('/favicon.ico')
@@ -172,7 +169,8 @@ def seriouslykill():
172169

173170
@app.route('/kill', methods=['POST'])
174171
def kill():
175-
if 'STAY_RUNNING' in app.config: return
172+
if 'STAY_RUNNING' in app.config:
173+
return 'Will stay running.'
176174

177175
last_ms = LAST_REQUEST_MS
178176
def shutdown():
@@ -183,7 +181,7 @@ def shutdown():
183181

184182
Timer(0.5, shutdown).start()
185183

186-
return "Shutting down..."
184+
return 'Shutting down...'
187185

188186

189187
def open_browser():

webdiff/static/js/components.jsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
/** @jsx React.DOM */
2+
var Route = ReactRouter.Route;
3+
var Routes = ReactRouter.Routes;
4+
var Link = ReactRouter.Link;
25

36
IMAGE_DIFF_MODES = ['side-by-side', 'blink', 'onion-skin', 'swipe'];
47

58
// Webdiff application root.
69
var Root = React.createClass({
710
propTypes: {
8-
filePairs: React.PropTypes.array.isRequired
11+
filePairs: React.PropTypes.array.isRequired,
12+
initiallySelectedIndex: React.PropTypes.number,
13+
params: React.PropTypes.object
914
},
15+
mixins: [ReactRouter.Navigation],
1016
getInitialState: () => ({
11-
selectedFileIndex: 0,
1217
imageDiffMode: 'side-by-side'
1318
}),
14-
fileChangeHandler: function(idx) {
15-
this.setState({selectedFileIndex: idx});
19+
selectIndex: function(idx) {
20+
this.transitionTo('pair', {index:idx});
21+
},
22+
getIndex: function() {
23+
var idx = this.props.params.index;
24+
if (idx == null) idx = this.props.initiallySelectedIndex;
25+
return Number(idx);
1626
},
1727
changeImageDiffModeHandler: function(mode) {
1828
this.setState({imageDiffMode: mode});
1929
},
2030
render: function() {
21-
var filePair = this.props.filePairs[this.state.selectedFileIndex];
31+
var idx = this.getIndex(),
32+
filePair = this.props.filePairs[idx];
2233

2334
return (
2435
<div>
25-
<FileSelector selectedFileIndex={this.state.selectedFileIndex}
36+
<FileSelector selectedFileIndex={idx}
2637
filePairs={this.props.filePairs}
27-
fileChangeHandler={this.fileChangeHandler} />
38+
fileChangeHandler={this.selectIndex} />
2839
<DiffView filePair={filePair}
2940
imageDiffMode={this.state.imageDiffMode}
3041
changeImageDiffModeHandler={this.changeImageDiffModeHandler} />
@@ -34,14 +45,14 @@ var Root = React.createClass({
3445
componentDidMount: function() {
3546
$(document).on('keydown', (e) => {
3647
if (!isLegitKeypress(e)) return;
37-
var idx = this.state.selectedFileIndex;
48+
var idx = this.getIndex();
3849
if (e.keyCode == 75) { // j
3950
if (idx > 0) {
40-
this.setState({selectedFileIndex: idx - 1});
51+
this.selectIndex(idx - 1);
4152
}
4253
} else if (e.keyCode == 74) { // k
4354
if (idx < this.props.filePairs.length - 1) {
44-
this.setState({selectedFileIndex: idx + 1});
55+
this.selectIndex(idx + 1);
4556
}
4657
} else if (e.keyCode == 83) { // s
4758
this.setState({imageDiffMode: 'side-by-side'});

webdiff/templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<script src="/static/components/react/react.js"></script>
1313
<script src="/static/components/react/JSXTransformer.js"></script>
14+
<script src="/static/components/react-router/dist/react-router.js"></script>
1415

1516
<script src="/static/codediff.js/difflib.js"></script>
1617
<script src="/static/codediff.js/codediff.js"></script>

webdiff/templates/file_diff.html

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,42 @@
1111

1212
<script src="/static/js/file_diff.js"></script>
1313

14-
<script>var pairs = {{pairs|tojson}};</script>
14+
<script>
15+
var pairs = {{pairs|tojson}};
16+
var initialIdx = {{idx|tojson}};
17+
</script>
1518

1619
<script type="text/jsx;harmony=true" src="/static/js/util.js"></script>
1720
<script type="text/jsx;harmony=true" src="/static/js/components.jsx"></script>
1821
<script type="text/jsx;harmony=true">
1922
/** @jsx React.DOM */
20-
React.renderComponent(<Root filePairs={pairs} />, $('#application').get(0));
23+
24+
var Route = ReactRouter.Route;
25+
var Routes = ReactRouter.Routes;
26+
27+
var App = React.createClass({
28+
render: function() {
29+
return this.props.activeRouteHandler();
30+
}
31+
});
32+
33+
var routes = (
34+
<Routes location="history">
35+
<Route handler={App}>
36+
<Route name="pair" path="/:index?" handler={Root}
37+
filePairs={pairs}
38+
initiallySelectedIndex={initialIdx} />
39+
</Route>
40+
</Routes>
41+
);
42+
43+
React.renderComponent(routes, $('#application').get(0));
44+
</script>
45+
46+
<script>
47+
window.addEventListener('beforeunload', function(e) {
48+
$.post('/kill');
49+
});
2150
</script>
2251

2352
{% endblock %}

0 commit comments

Comments
 (0)