This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
google-drive.html
182 lines (146 loc) · 4.69 KB
/
google-drive.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-signin/google-signin-aware.html">
<link rel="import" href="google-drive-upload.html">
<!--
Element enabling you to upload files to Google Drive.
##### Examples
Manual upload with an Upload button once files are selected:
<google-drive></google-drive>
Automatic upload on file select, without Upload button:
<google-drive autoUpload></google-drive>
@demo
-->
<dom-module id="google-drive">
<link rel="import" type="css" href="google-drive.css">
<template>
<google-signin-aware
scopes="https://www.googleapis.com/auth/drive"
on-google-signin-aware-success="_signedIn"
on-google-signin-aware-signed-out="_signedOut">
</google-signin-aware>
<span style$="{{_computeSpanStyle(authenticated)}}">
<input tabindex="0" type="file" id="files" class="custom-file-input" name="files[]" multiple on-change="_handleFilePick"/>
<button on-click="_manualUpload" id="upload" style$="{{_computeButtonStyle(autoUpload)}}">Upload files</button>
</span>
</template>
</dom-module>
<script>
Polymer({
is: 'google-drive',
/**
* Fired when the upload status changes
*
* @event google-drive-upload-status
*/
/**
* Fired when files have been selected
*
* @event google-drive-files-selected
*/
/**
* Fired when the element attempts to upload files
*
* @event google-drive-upload-started
*/
/**
* Fired when file uploads have successfully completed
*
* @event google-drive-upload-completed
*/
/**
* Fired when file uploads have failed
*
* @event google-drive-upload-failed
*/
/**
* Fired when the upload lists are cleared
*
* @event google-drive-upload-cleared
*/
/**
* The queue of files being uploaded
*/
_uploadList: [],
/**
* The queue of files that have been uploaded
*/
_uploadedList: [],
properties: {
/**
* Whether the user has authenticated or not. Element is hidden until authentication?
*/
authenticated: {
type: Boolean,
value: false
},
/**
* Whether files should be automatically uploaded
*/
autoUpload: {
type: Boolean,
value: false
}
},
ready: function () {
this.fire('google-drive-upload-status', { status: 'Authentication required' });
},
_computeSpanStyle: function(authenticated) {
return "display:" + (authenticated ? 'inline-block' : 'none');
},
_computeButtonStyle: function(autoUpload) {
return "display:" + (autoUpload ? 'none' : 'block');
},
_signedIn: function(e, result) {
this.accessToken = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;
this.authenticated = true;
this.fire('google-drive-upload-status', { status: 'Ready' });
},
_signedOut: function(e) {
this.authenticated = false;
this.fire('google-drive-upload-status', { status: 'Please sign-in to continue.' });
},
_selectFiles: function (files) {
for (var i = 0, f; f = files[i]; i++) {
this._uploadList.push(f);
}
if (this.autoUpload) {
this._uploadFiles(files);
}
this.fire('google-drive-files-selected', { files: this._uploadList });
this.fire('google-drive-upload-status', { status: 'Files selected'});
},
_manualUpload: function () {
this._uploadFiles(this._uploadList);
},
_uploadFiles: function (files) {
this.fire('google-drive-upload-started', { files: this._uploadList });
this.fire('google-drive-upload-status', { status: 'Uploading' });
for (var i = 0, f; f = files[i]; i++) {
var uploader = new MediaUploader({
file: f,
token: this.accessToken,
onError: function (data) {
this.fire('google-drive-upload-failed', { data: data })
}.bind(this),
onComplete: function (data) {
this._uploadedList.push(JSON.parse(data));
this.fire('google-drive-upload-status', { status: 'Upload successful' });
this._uploadList = [];
this.fire('google-drive-upload-completed', { files: this._uploadedList, data: data });
}.bind(this)
});
uploader.upload();
}
},
_handleFilePick: function (evt) {
evt.stopPropagation();
evt.preventDefault();
this._selectFiles(evt.target.files);
},
_clearUploadList: function () {
this._uploadedList = [];
this._uploadList = [];
this.fire('google-drive-upload-cleared', { status: 'Upload lists cleared' });
}
});
</script>