@@ -58,37 +58,57 @@ var ScrollContext = {
58
58
} ,
59
59
60
60
componentWillMount : function ( ) {
61
- var behavior = this . getScrollBehavior ( ) ;
62
-
63
61
invariant (
64
- behavior == null || canUseDOM ,
62
+ this . getScrollBehavior ( ) == null || canUseDOM ,
65
63
'Cannot use scroll behavior without a DOM'
66
64
) ;
67
-
68
- if ( behavior != null )
69
- this . _scrollPositions = { } ;
70
65
} ,
71
66
72
67
recordScroll : function ( path ) {
73
- if ( this . _scrollPositions )
74
- this . _scrollPositions [ path ] = getWindowScrollPosition ( ) ;
68
+ var positions = this . getScrollPositions ( ) ;
69
+ positions [ path ] = getWindowScrollPosition ( ) ;
75
70
} ,
76
71
77
72
updateScroll : function ( path , actionType ) {
78
- if ( this . _scrollPositions ) {
79
- var behavior = this . getScrollBehavior ( ) ;
80
- var position = this . _scrollPositions [ path ] ;
73
+ var behavior = this . getScrollBehavior ( ) ;
74
+ var position = this . getScrollPosition ( path ) ;
81
75
82
- if ( behavior && position )
83
- behavior . updateScrollPosition ( position , actionType ) ;
84
- }
76
+ if ( behavior && position )
77
+ behavior . updateScrollPosition ( position , actionType ) ;
85
78
} ,
86
79
87
80
/**
88
81
* Returns the scroll behavior object this component uses.
89
82
*/
90
83
getScrollBehavior : function ( ) {
91
- return this . state . scrollBehavior ;
84
+ if ( this . _scrollBehavior == null ) {
85
+ var behavior = this . props . scrollBehavior ;
86
+
87
+ if ( typeof behavior === 'string' )
88
+ behavior = NAMED_SCROLL_BEHAVIORS [ behavior ] ;
89
+
90
+ this . _scrollBehavior = behavior ;
91
+ }
92
+
93
+ return this . _scrollBehavior ;
94
+ } ,
95
+
96
+ /**
97
+ * Returns a hash of URL paths to their last known scroll positions.
98
+ */
99
+ getScrollPositions : function ( ) {
100
+ if ( this . _scrollPositions == null )
101
+ this . _scrollPositions = { } ;
102
+
103
+ return this . _scrollPositions ;
104
+ } ,
105
+
106
+ /**
107
+ * Returns the last known scroll position for the given URL path.
108
+ */
109
+ getScrollPosition : function ( path ) {
110
+ var positions = this . getScrollPositions ( ) ;
111
+ return positions [ path ] ;
92
112
} ,
93
113
94
114
childContextTypes : {
0 commit comments