|
1 | | -/**! |
| 1 | +/** ! |
2 | 2 | * Google Drive File Picker Example |
3 | 3 | * By Daniel Lo Nigro (http://dan.cx/) |
4 | 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); |
| 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 |
23 | 13 |
|
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 | | - } |
| 14 | + // Elements |
| 15 | + this.buttonEl = options.buttonEl |
29 | 16 |
|
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 | | - view.setOwnedByMe(true); |
56 | | - this.picker = new google.picker.PickerBuilder(). |
57 | | - enableFeature(google.picker.Feature.NAV_HIDDEN). |
58 | | - addView(view). |
59 | | - setAppId(this.clientId). |
60 | | - setOAuthToken(accessToken). |
61 | | - setCallback(this._pickerCallback.bind(this)). |
62 | | - build(). |
63 | | - setVisible(true); |
64 | | - }, |
65 | | - |
66 | | - /** |
67 | | - * Called when a file has been selected in the Google Drive file picker. |
68 | | - * @private |
69 | | - */ |
70 | | - _pickerCallback: function(data) { |
71 | | - if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) { |
72 | | - var file = data[google.picker.Response.DOCUMENTS][0], |
73 | | - id = file[google.picker.Document.ID], |
74 | | - request = gapi.client.drive.files.get({ |
75 | | - fileId: id |
76 | | - }); |
77 | | - |
78 | | - request.execute(this._fileGetCallback.bind(this)); |
79 | | - } |
80 | | - }, |
81 | | - /** |
82 | | - * Called when file details have been retrieved from Google Drive. |
83 | | - * @private |
84 | | - */ |
85 | | - _fileGetCallback: function(file) { |
86 | | - if (this.onSelect) { |
87 | | - this.onSelect(file); |
88 | | - } |
89 | | - }, |
90 | | - |
91 | | - /** |
92 | | - * Called when the Google Drive file picker API has finished loading. |
93 | | - * @private |
94 | | - */ |
95 | | - _pickerApiLoaded: function() { |
96 | | - this.buttonEl.prop('disabled', false); |
97 | | - }, |
98 | | - |
99 | | - /** |
100 | | - * Called when the Google Drive API has finished loading. |
101 | | - * @private |
102 | | - */ |
103 | | - _driveApiLoaded: function() { |
104 | | - this._doAuth(true); |
105 | | - }, |
106 | | - |
107 | | - /** |
108 | | - * Authenticate with Google Drive via the Google JavaScript API. |
109 | | - * @private |
110 | | - */ |
111 | | - _doAuth: function(immediate, callback) { |
112 | | - gapi.auth.authorize({ |
113 | | - client_id: this.clientId, |
114 | | - scope: 'https://www.googleapis.com/auth/drive.readonly', |
115 | | - immediate: immediate |
116 | | - }, callback ? callback : function() {}); |
117 | | - } |
118 | | - }; |
119 | | -}()); |
| 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 | + window.gapi.client.setApiKey(this.apiKey) |
| 26 | + window.gapi.client.load('drive', 'v2', this._driveApiLoaded.bind(this)) |
| 27 | + window.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 = window.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 = window.gapi.auth.getToken().access_token |
| 52 | + var view = new window.google.picker.DocsView() |
| 53 | + view.setMimeTypes('text/markdown,text/html') |
| 54 | + view.setIncludeFolders(true) |
| 55 | + view.setOwnedByMe(true) |
| 56 | + this.picker = new window.google.picker.PickerBuilder() |
| 57 | + .enableFeature(window.google.picker.Feature.NAV_HIDDEN) |
| 58 | + .addView(view) |
| 59 | + .setAppId(this.clientId) |
| 60 | + .setOAuthToken(accessToken) |
| 61 | + .setCallback(this._pickerCallback.bind(this)) |
| 62 | + .build() |
| 63 | + .setVisible(true) |
| 64 | + }, |
| 65 | + |
| 66 | + /** |
| 67 | + * Called when a file has been selected in the Google Drive file picker. |
| 68 | + * @private |
| 69 | + */ |
| 70 | + _pickerCallback: function (data) { |
| 71 | + if (data[window.google.picker.Response.ACTION] === window.google.picker.Action.PICKED) { |
| 72 | + var file = data[window.google.picker.Response.DOCUMENTS][0] |
| 73 | + var id = file[window.google.picker.Document.ID] |
| 74 | + var request = window.gapi.client.drive.files.get({ |
| 75 | + fileId: id |
| 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 | + window.gapi.auth.authorize({ |
| 112 | + client_id: this.clientId, |
| 113 | + scope: 'https://www.googleapis.com/auth/drive.readonly', |
| 114 | + immediate: immediate |
| 115 | + }, callback || function () {}) |
| 116 | + } |
| 117 | + } |
| 118 | +}()) |
0 commit comments