Skip to content

Commit

Permalink
drive open
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Dec 3, 2013
1 parent 08b5266 commit 7d12214
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 91 deletions.
35 changes: 0 additions & 35 deletions jzip.css
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,3 @@ span.statusrow {
font-family: monospace;
white-space: pre;
}

.dialog {
position: fixed;
top: 1em;
border: 1px solid black;
box-shadow: 3px 3px 15px rgba(0,0,0,0.3);
padding: 0.5em;
background-color: white;
}
.dialog>h1 {
font-size:15pt;
margin: 0;
}
.dialog>div>h2 {
font-size:13pt;
margin: 0.5em 0 0 0;
clear: both
}
.option {
position: relative;
margin-left: 2em;
min-height: 2em;
clear: both;
}
.option>br {
clear: both
}
.option input[type=radio] {
position: relative;
left: -2em;
}
.option>button {
float: right;
}
.option.drive { display: none }
1 change: 1 addition & 0 deletions jzip.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<script src="jzip.js"></script>
<script src="map.js"></script>
<link href="jzip.css" rel="stylesheet" type="text/css">
<link href="storage.css" rel="stylesheet" type="text/css">
</head>
<body>

Expand Down
2 changes: 1 addition & 1 deletion storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h2 class="open">&hellip;from server.</h2>
</div>

<script>
s=Storage({ local: 'jszipSaves' });
s=Storage({ local: 'jszipSaves', data: '0000' });
s.save({});
</script>
</body>
Expand Down
95 changes: 40 additions & 55 deletions storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ function Storage(defaults) {
}

// hook up buttons
$('.local button',$dialog).on('click', function() { if(handler.local) handler.local() });
$('.file button',$dialog).on('click', function() { if(handler.file) handler.file() });
$('.drive button',$dialog).on('click', function() { if(handler.drive) handler.drive() });
$('.server button',$dialog).on('click', function() { if(handler.server) handler.server() });
$('.local button',$dialog).on('click', function(e) { if(handler.local) handler.local(e) });
$('.file button',$dialog).on('click', function(e) { if(handler.file) handler.file(e) });
$('.drive button',$dialog).on('click', function(e) { if(handler.drive) handler.drive(e) });
$('.server button',$dialog).on('click', function(e) { if(handler.server) handler.server(e) });
$('.file a.downloadlink',$dialog).on('click', function(e) { if(handler.download) handler.download(e) });

// select/input behaviour
$('.local select',$dialog).on('change', function() { $('.local input',$dialog).val('')});
Expand Down Expand Up @@ -102,24 +103,21 @@ function Storage(defaults) {
}

function drive() {
var id=null, name, mode = $('#savedialog input[type=radio]:checked').val(), method;
if (mode=='drivenew') {
name = $('#savedrivenewname').val();
var id = $('.drive select',$dialog).val() || null
, name = $('.drive input',$dialog).val();
if (id !== null) {
if (name=='') return;
} else if (mode=='driveold') {
id = $('#savedriveoldname').val();
} else {
if (dsave===null || !(id in dsave)) return;
name = dsave[id].title;
} else {
return;
}
}

// save to Google Drive
var boundary = '-------314159265358979323846'
, delimiter = "\r\n--" + boundary + "\r\n"
, close_delim = "\r\n--" + boundary + "--"
, metadate
, base64Data = btoa(saveGame);
, base64Data = btoa(options.data||'');

// specify original metadata and use method 'PUT' to allow update of existing file
if (id) {
Expand Down Expand Up @@ -168,15 +166,15 @@ function Storage(defaults) {
$dialog.fadeIn();

// prepare downloadlink
handler.download = function() {
var a = window.btoa(options.data)
$(this).attr('href','data:application/octet-stream;base64,'+a);
handler.download = function(e) {
var a = window.btoa(options.data);
$('.file a.downloadlink',$dialog).attr('href','data:application/octet-stream;base64,'+a);
}

// handlers
handler.local = local;
handler.drive = drive;
//handler.file = file;
handler.file = null;
}

function open(options) {
Expand Down Expand Up @@ -220,6 +218,31 @@ function Storage(defaults) {
reader.readAsBinaryString(f);
}

function drive() {
var id = $('.drive select').val()
, accessToken = gapi.auth.getToken().access_token
, url = dsave[id].downloadUrl
, e=restoreOpts
, xhr = new XMLHttpRequest();

if (dsave===null || !(id in dsave)) { return; }

xhr.open('GET', url);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (xhr.status == 200) {
callback(new Uint8Array(xhr.response));
} else {
callback(null));
}
};
xhr.onerror = function() {
callback(null);
};
xhr.send();
}

// condition dialog
common(options);
$('.open',$dialog).show();
Expand Down Expand Up @@ -320,44 +343,6 @@ function Storage(defaults) {
}


function restoreDrive() {
var e=restoreOpts, id = $('#restoredriveoldname').val();
if (e==null) {
throw Error('No z_restore options available!');
}
if (dsave===null || !(id in dsave)) { return; }
downloadDriveFile(id);
}

function downloadDriveFile(id) {
var accessToken = gapi.auth.getToken().access_token
, url = dsave[id].downloadUrl
, e=restoreOpts
, xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (xhr.status == 200) {
FS.createDataFile('/', 'save.sav', new Uint8Array(xhr.response), true, true);
restoreOpts=null;
$restore.fadeOut();
zRestore(e.count,e.o0,e.o1,e.o2);
FS.unlink('save.sav');
} else {
error("Google Drive returned an error: " + xhr.statusText );
}
step();
};
xhr.onerror = function() {
// should probably report an error first
error("Error fetching file from Google Drive.");
$restore.fadeOut();
step();
};
xhr.send();
}

$('.signedout',$dialog).append($('<div id="google_signin_button"></div>'));
$('.open',$dialog).hide();

Expand Down

0 comments on commit 7d12214

Please sign in to comment.