Skip to content

Commit a86719c

Browse files
committed
Added save-again-to-same-file feature, with tests
1 parent 3ca2cd5 commit a86719c

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

cloud-storage.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ function failureDebug () {
172172
* If the user doesn't log in to the service or cancels the dialog, the
173173
* failure callback will be called.
174174
*
175+
* The object passed to the success callback also contains an `update`
176+
* member, which can be used to save new content over top of the content of
177+
* the file opened using this method. See details in the `saveFile`
178+
* function implemented, after the `openFile` function, further below.
179+
*
175180
* By default, this routine displays an iframe to contain the File > Open
176181
* dialog it shows to the user. If the client already has an iframe to use
177182
* as the dialog, it can be passed as the third parameter, and will be used.
@@ -212,6 +217,12 @@ openFile = window.openFile = function ( successCB, failureCB, iframe )
212217
if ( !succ ) succ = successDebug;
213218
if ( !fail ) fail = failureDebug;
214219
fileSystemBackEnd.readFile( path, succ, fail );
220+
},
221+
update : function ( content, succ, fail ) {
222+
if ( !succ ) succ = successDebug;
223+
if ( !fail ) fail = failureDebug;
224+
fileSystemBackEnd.writeFile( path, content,
225+
succ, fail );
215226
}
216227
} );
217228
} else {
@@ -229,13 +240,6 @@ openFile = window.openFile = function ( successCB, failureCB, iframe )
229240
} );
230241
}
231242

232-
/*
233-
* Failure callback will have reason as a string parameter.
234-
* Success callback will have an object with file metadata, and a method
235-
* for resaving the file, update(newContent,successCB,failureCB), in which
236-
* the first callback will yield a new object to replace the old, and the
237-
* second will have an error message.
238-
*/
239243
/*
240244
* Show a "File > Save" dialog box.
241245
*

test.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
onclick='testOpenFile();'/>
4242
<input type='button' value='Test saving a file'
4343
onclick='testSaveFile();'/>
44+
<input type='button' value='Test re-saving the same file'
45+
onclick='testResaveFile();'/>
4446
</p>
4547

4648
<!--
@@ -138,11 +140,16 @@
138140
return ( dialog.style.display == 'block' ) ? dialog : null;
139141
}
140142

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;
141147
// Event handler when the user clicks "Test opening a file."
142148
// In the various success and failure callbacks, populate one or
143149
// both of the two textareas with the results.
144150
function testOpenFile () {
145151
openFile( function ( chosenFile ) {
152+
lastFileObject = chosenFile;
146153
chosenFile.get( function ( content ) {
147154
setMessages( 'User chose: ' + chosenFile.path, null,
148155
'File loaded successfully' );
@@ -154,6 +161,7 @@
154161
if ( dialogIfVisible() ) toggleDialog();
155162
} );
156163
}, function ( error ) {
164+
lastFileObject = null;
157165
setMessages( null, 'Error', error );
158166
if ( dialogIfVisible() ) toggleDialog();
159167
}, dialogIfVisible() );
@@ -163,6 +171,7 @@
163171
// both of the two textareas with the results.
164172
function testSaveFile () {
165173
saveFile( function ( destination ) {
174+
lastFileObject = destination;
166175
destination.update( getEditor(), function ( result ) {
167176
setMessages( 'Destination: ' + destination.path,
168177
'Success', result );
@@ -173,10 +182,36 @@
173182
if ( dialogIfVisible() ) toggleDialog();
174183
} );
175184
}, function ( error ) {
185+
lastFileObject = null;
176186
setMessages( null, 'Error', error );
177187
if ( dialogIfVisible() ) toggleDialog();
178188
}, dialogIfVisible() );
179189
}
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+
}
180215
</script>
181216
</body>
182217
</html>

0 commit comments

Comments
 (0)