1+ /**!
2+ * Google Drive File Picker Example
3+ * By Daniel Lo Nigro (http://dan.cx/)
4+ */
5+ ( function ( ) {
6+ /**
7+ * Initialise a Google Driver file picker
8+ */
9+ var FilePicker = window . FilePicker = function ( options ) {
10+ // Config
11+ this . apiKey = options . apiKey ;
12+ this . clientId = options . clientId ;
13+
14+ // Elements
15+ this . buttonEl = options . buttonEl ;
16+
17+ // Events
18+ this . onSelect = options . onSelect ;
19+ this . buttonEl . on ( 'click' , this . open . bind ( this ) ) ;
20+
21+ // Disable the button until the API loads, as it won't work properly until then.
22+ this . buttonEl . prop ( 'disabled' , true ) ;
23+
24+ // Load the drive API
25+ gapi . client . setApiKey ( this . apiKey ) ;
26+ gapi . client . load ( 'drive' , 'v2' , this . _driveApiLoaded . bind ( this ) ) ;
27+ google . load ( 'picker' , '1' , { callback : this . _pickerApiLoaded . bind ( this ) } ) ;
28+ }
29+
30+ FilePicker . prototype = {
31+ /**
32+ * Open the file picker.
33+ */
34+ open : function ( ) {
35+ // Check if the user has already authenticated
36+ var token = gapi . auth . getToken ( ) ;
37+ if ( token ) {
38+ this . _showPicker ( ) ;
39+ } else {
40+ // The user has not yet authenticated with Google
41+ // We need to do the authentication before displaying the Drive picker.
42+ this . _doAuth ( false , function ( ) { this . _showPicker ( ) ; } . bind ( this ) ) ;
43+ }
44+ } ,
45+
46+ /**
47+ * Show the file picker once authentication has been done.
48+ * @private
49+ */
50+ _showPicker : function ( ) {
51+ var accessToken = gapi . auth . getToken ( ) . access_token ;
52+ var view = new google . picker . DocsView ( ) ;
53+ view . setMimeTypes ( "text/markdown,text/html" ) ;
54+ view . setIncludeFolders ( true ) ;
55+ this . picker = new google . picker . PickerBuilder ( ) .
56+ enableFeature ( google . picker . Feature . NAV_HIDDEN ) .
57+ addView ( view ) .
58+ setAppId ( this . clientId ) .
59+ setOAuthToken ( accessToken ) .
60+ setCallback ( this . _pickerCallback . bind ( this ) ) .
61+ build ( ) .
62+ setVisible ( true ) ;
63+ } ,
64+
65+ /**
66+ * Called when a file has been selected in the Google Drive file picker.
67+ * @private
68+ */
69+ _pickerCallback : function ( data ) {
70+ if ( data [ google . picker . Response . ACTION ] == google . picker . Action . PICKED ) {
71+ var file = data [ google . picker . Response . DOCUMENTS ] [ 0 ] ,
72+ id = file [ google . picker . Document . ID ] ,
73+ request = gapi . client . drive . files . get ( {
74+ fileId : id
75+ } ) ;
76+
77+ request . execute ( this . _fileGetCallback . bind ( this ) ) ;
78+ }
79+ } ,
80+ /**
81+ * Called when file details have been retrieved from Google Drive.
82+ * @private
83+ */
84+ _fileGetCallback : function ( file ) {
85+ if ( this . onSelect ) {
86+ this . onSelect ( file ) ;
87+ }
88+ } ,
89+
90+ /**
91+ * Called when the Google Drive file picker API has finished loading.
92+ * @private
93+ */
94+ _pickerApiLoaded : function ( ) {
95+ this . buttonEl . prop ( 'disabled' , false ) ;
96+ } ,
97+
98+ /**
99+ * Called when the Google Drive API has finished loading.
100+ * @private
101+ */
102+ _driveApiLoaded : function ( ) {
103+ this . _doAuth ( true ) ;
104+ } ,
105+
106+ /**
107+ * Authenticate with Google Drive via the Google JavaScript API.
108+ * @private
109+ */
110+ _doAuth : function ( immediate , callback ) {
111+ gapi . auth . authorize ( {
112+ client_id : this . clientId ,
113+ scope : 'https://www.googleapis.com/auth/drive.readonly' ,
114+ immediate : immediate
115+ } , callback ? callback : function ( ) { } ) ;
116+ }
117+ } ;
118+ } ( ) ) ;
0 commit comments