Index: trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.js |
— | — | @@ -3,6 +3,19 @@ |
4 | 4 | */ |
5 | 5 | jQuery( function( $ ) { |
6 | 6 | /** |
| 7 | + * Saved form state |
| 8 | + */ |
| 9 | + var formState = { types: [], username: '' }; |
| 10 | + |
| 11 | + /** |
| 12 | + * Save the current form state to formState |
| 13 | + */ |
| 14 | + function saveFormState() { |
| 15 | + formState.types = getSelectedTypes(); |
| 16 | + formState.username = $( '#fbd-filters-username' ).val(); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
7 | 20 | * Figure out which comment type filters have been selected. |
8 | 21 | * @return array of comment types |
9 | 22 | */ |
— | — | @@ -17,11 +30,12 @@ |
18 | 31 | } |
19 | 32 | |
20 | 33 | /** |
21 | | - * Set the moodbar-feedback-types and moodbar-feedback-username cookies based on the form state. |
| 34 | + * Set the moodbar-feedback-types and moodbar-feedback-username cookies based on formState. |
| 35 | + * This function uses the form state saved in formState, so you may want to call saveFormState() first. |
22 | 36 | */ |
23 | 37 | function setCookies() { |
24 | | - $.cookie( 'moodbar-feedback-types', getSelectedTypes().join( '|' ), { 'path': '/', 'expires': 7 } ); |
25 | | - $.cookie( 'moodbar-feedback-username', $( '#fbd-filters-username' ).val(), { 'path': '/', 'expires': 7 } ); |
| 38 | + $.cookie( 'moodbar-feedback-types', formState.types.join( '|' ), { 'path': '/', 'expires': 7 } ); |
| 39 | + $.cookie( 'moodbar-feedback-username', formState.username, { 'path': '/', 'expires': 7 } ); |
26 | 40 | } |
27 | 41 | |
28 | 42 | /** |
— | — | @@ -77,12 +91,13 @@ |
78 | 92 | * Load a set of 20 comments into the list. In 'filter' mode, the list is |
79 | 93 | * blanked before the new comments are loaded. In 'more' mode, the comments are |
80 | 94 | * loaded at the end of the existing set. |
| 95 | + * |
| 96 | + * This function uses the form state saved in formState, so you may want to call saveFormState() first. |
| 97 | + * |
81 | 98 | * @param mode string Either 'filter' or 'more' |
82 | 99 | */ |
83 | 100 | function loadComments( mode ) { |
84 | 101 | var limit = 20, |
85 | | - username = $( '#fbd-filters-username' ).val(), |
86 | | - types = getSelectedTypes(), |
87 | 102 | reqData; |
88 | 103 | |
89 | 104 | if ( mode == 'filter' ) { |
— | — | @@ -111,11 +126,11 @@ |
112 | 127 | if ( mode == 'more' ) { |
113 | 128 | reqData['mbccontinue'] = $( '#fbd-list').find( 'li:last' ).data( 'mbccontinue' ); |
114 | 129 | } |
115 | | - if ( types.length ) { |
116 | | - reqData['mbctype'] = types.join( '|' ); |
| 130 | + if ( formState.types.length ) { |
| 131 | + reqData['mbctype'] = formState.types.join( '|' ); |
117 | 132 | } |
118 | | - if ( username.length ) { |
119 | | - reqData['mbcuser'] = username; |
| 133 | + if ( formState.username.length ) { |
| 134 | + reqData['mbcuser'] = formState.username; |
120 | 135 | } |
121 | 136 | |
122 | 137 | $.ajax( mw.util.wikiScript( 'api' ), { |
— | — | @@ -175,22 +190,30 @@ |
176 | 191 | } ); |
177 | 192 | } |
178 | 193 | |
| 194 | + // On-load stuff |
| 195 | + |
179 | 196 | $( '#fbd-filters' ).children( 'form' ).submit( function( e ) { |
180 | 197 | e.preventDefault(); |
| 198 | + saveFormState(); |
181 | 199 | setCookies(); |
182 | 200 | loadComments( 'filter' ); |
183 | 201 | } ); |
184 | 202 | |
185 | 203 | $( '#fbd-list-more' ).children( 'a' ).click( function( e ) { |
186 | 204 | e.preventDefault(); |
| 205 | + // We don't call saveFormState() here because we want to use the state of the form |
| 206 | + // at the time it was last filtered. Otherwise, you would see strange behavior if |
| 207 | + // you changed the form state then clicked More. |
187 | 208 | loadComments( 'more' ); |
188 | 209 | } ); |
189 | 210 | |
| 211 | + saveFormState(); |
190 | 212 | var filterType = $( '#fbd-filters' ).children( 'form' ).data( 'filtertype' ); |
191 | 213 | // If filtering already happened on the PHP side, don't load the form state from cookies |
192 | 214 | if ( filterType != 'filtered' ) { |
193 | 215 | // Don't do an AJAX filter if we're on an ID view, or if the form is still blank after loadFromCookies() |
194 | 216 | if ( loadFromCookies() && filterType != 'id' ) { |
| 217 | + saveFormState(); |
195 | 218 | loadComments( 'filter' ); |
196 | 219 | } |
197 | 220 | } |