1
1
var React = require ( 'react' ) ;
2
2
var warning = require ( 'react/lib/warning' ) ;
3
+ var invariant = require ( 'react/lib/invariant' ) ;
3
4
var canUseDOM = require ( 'react/lib/ExecutionEnvironment' ) . canUseDOM ;
4
5
var copyProperties = require ( 'react/lib/copyProperties' ) ;
5
6
var PathStore = require ( '../stores/PathStore' ) ;
7
+ var HashLocation = require ( '../locations/HashLocation' ) ;
6
8
var reversedArray = require ( '../utils/reversedArray' ) ;
7
9
var Transition = require ( '../utils/Transition' ) ;
8
10
var Redirect = require ( '../utils/Redirect' ) ;
@@ -262,36 +264,30 @@ function computeHandlerProps(matches, query) {
262
264
263
265
var BrowserTransitionHandling = {
264
266
265
- handleTransitionError : function ( error ) {
267
+ handleTransitionError : function ( component , error ) {
266
268
throw error ; // This error probably originated in a transition hook.
267
269
} ,
268
270
269
- handleAbortedTransition : function ( transition ) {
271
+ handleAbortedTransition : function ( component , transition ) {
270
272
var reason = transition . abortReason ;
271
273
272
274
if ( reason instanceof Redirect ) {
273
- this . replaceWith ( reason . to , reason . params , reason . query ) ;
275
+ component . replaceWith ( reason . to , reason . params , reason . query ) ;
274
276
} else {
275
- this . goBack ( ) ;
277
+ component . goBack ( ) ;
276
278
}
277
279
}
278
280
279
281
} ;
280
282
281
283
var ServerTransitionHandling = {
282
284
283
- handleTransitionError : function ( error ) {
285
+ handleTransitionError : function ( component , error ) {
284
286
// TODO
285
287
} ,
286
288
287
- handleAbortedTransition : function ( transition ) {
288
- var reason = transition . abortReason ;
289
-
290
- if ( reason instanceof Redirect ) {
291
- // TODO
292
- } else {
293
- // TODO
294
- }
289
+ handleAbortedTransition : function ( component , transition ) {
290
+ // TODO
295
291
}
296
292
297
293
} ;
@@ -351,9 +347,9 @@ var Routes = React.createClass({
351
347
352
348
this . dispatch ( path , function ( error , transition ) {
353
349
if ( error ) {
354
- TransitionHandling . handleTransitionError . call ( self , error ) ;
350
+ TransitionHandling . handleTransitionError ( self , error ) ;
355
351
} else if ( transition . isAborted ) {
356
- TransitionHandling . handleAbortedTransition . call ( self , transition ) ;
352
+ TransitionHandling . handleAbortedTransition ( self , transition ) ;
357
353
} else {
358
354
self . updateScroll ( path , actionType ) ;
359
355
}
@@ -417,6 +413,13 @@ var Routes = React.createClass({
417
413
return computeHandlerProps ( this . state . matches , this . state . activeQuery ) ;
418
414
} ,
419
415
416
+ /**
417
+ * Returns a reference to the active route handler's component instance.
418
+ */
419
+ getActiveComponent : function ( ) {
420
+ return this . refs . __activeRoute__ ;
421
+ } ,
422
+
420
423
/**
421
424
* Returns the current URL path.
422
425
*/
@@ -425,10 +428,84 @@ var Routes = React.createClass({
425
428
} ,
426
429
427
430
/**
428
- * Returns a reference to the active route handler's component instance.
431
+ * Returns an absolute URL path created from the given route
432
+ * name, URL parameters, and query values.
429
433
*/
430
- getActiveComponent : function ( ) {
431
- return this . refs . __activeRoute__ ;
434
+ makePath : function ( to , params , query ) {
435
+ var path ;
436
+ if ( Path . isAbsolute ( to ) ) {
437
+ path = Path . normalize ( to ) ;
438
+ } else {
439
+ var namedRoutes = this . getNamedRoutes ( ) ;
440
+ var route = namedRoutes [ to ] ;
441
+
442
+ invariant (
443
+ route ,
444
+ 'Unable to find a route named "' + to + '". Make sure you have ' +
445
+ 'a <Route name="' + to + '"> defined somewhere in your <Routes>'
446
+ ) ;
447
+
448
+ path = route . props . path ;
449
+ }
450
+
451
+ return Path . withQuery ( Path . injectParams ( path , params ) , query ) ;
452
+ } ,
453
+
454
+ /**
455
+ * Returns a string that may safely be used as the href of a
456
+ * link to the route with the given name.
457
+ */
458
+ makeHref : function ( to , params , query ) {
459
+ var path = this . makePath ( to , params , query ) ;
460
+
461
+ if ( this . getLocation ( ) === HashLocation )
462
+ return '#' + path ;
463
+
464
+ return path ;
465
+ } ,
466
+
467
+ /**
468
+ * Transitions to the URL specified in the arguments by pushing
469
+ * a new URL onto the history stack.
470
+ */
471
+ transitionTo : function ( to , params , query ) {
472
+ var location = this . getLocation ( ) ;
473
+
474
+ invariant (
475
+ location ,
476
+ 'You cannot use transitionTo without a location'
477
+ ) ;
478
+
479
+ location . push ( this . makePath ( to , params , query ) ) ;
480
+ } ,
481
+
482
+ /**
483
+ * Transitions to the URL specified in the arguments by replacing
484
+ * the current URL in the history stack.
485
+ */
486
+ replaceWith : function ( to , params , query ) {
487
+ var location = this . getLocation ( ) ;
488
+
489
+ invariant (
490
+ location ,
491
+ 'You cannot use replaceWith without a location'
492
+ ) ;
493
+
494
+ location . replace ( this . makePath ( to , params , query ) ) ;
495
+ } ,
496
+
497
+ /**
498
+ * Transitions to the previous URL.
499
+ */
500
+ goBack : function ( ) {
501
+ var location = this . getLocation ( ) ;
502
+
503
+ invariant (
504
+ location ,
505
+ 'You cannot use goBack without a location'
506
+ ) ;
507
+
508
+ location . pop ( ) ;
432
509
} ,
433
510
434
511
render : function ( ) {
@@ -443,12 +520,22 @@ var Routes = React.createClass({
443
520
} ,
444
521
445
522
childContextTypes : {
446
- currentPath : React . PropTypes . string
523
+ currentPath : React . PropTypes . string ,
524
+ makePath : React . PropTypes . func . isRequired ,
525
+ makeHref : React . PropTypes . func . isRequired ,
526
+ transitionTo : React . PropTypes . func . isRequired ,
527
+ replaceWith : React . PropTypes . func . isRequired ,
528
+ goBack : React . PropTypes . func . isRequired
447
529
} ,
448
530
449
531
getChildContext : function ( ) {
450
532
return {
451
- currentPath : this . getCurrentPath ( )
533
+ currentPath : this . getCurrentPath ( ) ,
534
+ makePath : this . makePath ,
535
+ makeHref : this . makeHref ,
536
+ transitionTo : this . transitionTo ,
537
+ replaceWith : this . replaceWith ,
538
+ goBack : this . goBack
452
539
} ;
453
540
}
454
541
0 commit comments