11/** @jsx React.DOM */
2+ var Route = ReactRouter . Route ;
3+ var Routes = ReactRouter . Routes ;
4+ var Link = ReactRouter . Link ;
25
36IMAGE_DIFF_MODES = [ 'side-by-side' , 'blink' , 'onion-skin' , 'swipe' ] ;
47
58// Webdiff application root.
69var 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' } ) ;
0 commit comments