|
41 | 41 | onclick='testOpenFile();'/> |
42 | 42 | <input type='button' value='Test saving a file' |
43 | 43 | onclick='testSaveFile();'/> |
| 44 | + <input type='button' value='Test re-saving the same file' |
| 45 | + onclick='testResaveFile();'/> |
44 | 46 | </p> |
45 | 47 |
|
46 | 48 | <!-- |
|
138 | 140 | return ( dialog.style.display == 'block' ) ? dialog : null; |
139 | 141 | } |
140 | 142 |
|
| 143 | + // Global variable used to store the last result of a success |
| 144 | + // callback from any of the functions below. Necessary to |
| 145 | + // implement the "re-save the same file" feature. |
| 146 | + var lastFileObject = null; |
141 | 147 | // Event handler when the user clicks "Test opening a file." |
142 | 148 | // In the various success and failure callbacks, populate one or |
143 | 149 | // both of the two textareas with the results. |
144 | 150 | function testOpenFile () { |
145 | 151 | openFile( function ( chosenFile ) { |
| 152 | + lastFileObject = chosenFile; |
146 | 153 | chosenFile.get( function ( content ) { |
147 | 154 | setMessages( 'User chose: ' + chosenFile.path, null, |
148 | 155 | 'File loaded successfully' ); |
|
154 | 161 | if ( dialogIfVisible() ) toggleDialog(); |
155 | 162 | } ); |
156 | 163 | }, function ( error ) { |
| 164 | + lastFileObject = null; |
157 | 165 | setMessages( null, 'Error', error ); |
158 | 166 | if ( dialogIfVisible() ) toggleDialog(); |
159 | 167 | }, dialogIfVisible() ); |
|
163 | 171 | // both of the two textareas with the results. |
164 | 172 | function testSaveFile () { |
165 | 173 | saveFile( function ( destination ) { |
| 174 | + lastFileObject = destination; |
166 | 175 | destination.update( getEditor(), function ( result ) { |
167 | 176 | setMessages( 'Destination: ' + destination.path, |
168 | 177 | 'Success', result ); |
|
173 | 182 | if ( dialogIfVisible() ) toggleDialog(); |
174 | 183 | } ); |
175 | 184 | }, function ( error ) { |
| 185 | + lastFileObject = null; |
176 | 186 | setMessages( null, 'Error', error ); |
177 | 187 | if ( dialogIfVisible() ) toggleDialog(); |
178 | 188 | }, dialogIfVisible() ); |
179 | 189 | } |
| 190 | + // Event handler when the user clicks "Test re-saving the same |
| 191 | + // file." In the various success and failure callbacks, |
| 192 | + // populate one or both of the two textareas with the results. |
| 193 | + function testResaveFile () { |
| 194 | + if ( !lastFileObject ) { |
| 195 | + setMessages( null, 'Error', |
| 196 | + 'You have not yet successfully opened or saved a ' |
| 197 | + + ' file, and so you cannot re-save to the "same" ' |
| 198 | + + ' location.' ); |
| 199 | + } else { |
| 200 | + lastFileObject.update( getEditor(), |
| 201 | + function ( result ) { |
| 202 | + setMessages( 'Destination: ' |
| 203 | + + lastFileObject.path, |
| 204 | + 'Success', result ); |
| 205 | + if ( dialogIfVisible() ) toggleDialog(); |
| 206 | + }, function ( error ) { |
| 207 | + setMessages( 'Destination: ' |
| 208 | + + lastFileObject.path, |
| 209 | + 'Error', error ); |
| 210 | + if ( dialogIfVisible() ) toggleDialog(); |
| 211 | + } |
| 212 | + ); |
| 213 | + } |
| 214 | + } |
180 | 215 | </script> |
181 | 216 | </body> |
182 | 217 | </html> |
0 commit comments