forked from tanmaykm/jquery-gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-gdrive.js
159 lines (149 loc) · 4.45 KB
/
jquery-gdrive.js
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
(function($) {
var result_target = '';
var picker = null;
var gopts = {
'devkey': null, //api key
'appid': null,
'user': null,
'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/drive',
'authtok': null,
'init_stage': 0,
'width': 800,
'height': 400,
'callback': set_results,
'debug': false
};
var dlgopts = {};
function do_auth(callback) {
google_api_load();
if((gopts.authtok != null) && (gopts.user != null)) {
if(callback) callback();
return;
}
if(gopts.debug) console.log("invoking autorization");
gapi.auth.authorize({
'client_id': gopts.appid,
'scope': gopts.scope,
'immediate': false
},
function(auth_result){
if (auth_result && !auth_result.error) {
gopts.authtok = auth_result.access_token;
get_user_id(callback);
}
else {
console.log("Could not authenticate you with Google");
}
});
};
function get_user_id(callback) {
gapi.client.setApiKey(gopts.devkey);
gapi.client.load('oauth2', 'v2', function(){
gapi.client.oauth2.userinfo.get().execute(function(resp) {
gopts.user = resp.email;
if(callback) callback();
});
});
};
function google_api_load() {
if((gopts.init_stage & 3) == 3) return;
if(!(gopts.init_stage & 1)) gapi.load('auth', {'callback': function(){
gopts.init_stage |= 1;
}});
if(!(gopts.init_stage & 2)) gapi.load('picker', {'callback': function(){
gopts.init_stage |= 2;
}});
};
function set_results(target_id, data) {
if (data.action == google.picker.Action.PICKED) {
var doc = data.docs[0];
var file_id = doc.id;
var url = doc[google.picker.Document.URL];
var filename = doc[google.picker.Document.NAME];
var fullpath = "gdrive://" + filename + "/" + file_id;
$('#'+target_id).val(fullpath);
$('#'+target_id).change();
}
};
function show_dlg(target_id) {
$('#'+target_id).val('');
var elem_opts = dlgopts[target_id];
var view = new google.picker.DocsView(google.picker.ViewId.FOLDERS);
view.setMode(google.picker.DocsViewMode.LIST);
if(elem_opts.filter == 'application/vnd.google-apps.folder') {
view.setSelectFolderEnabled(true);
view.setMimeTypes('application/vnd.google-apps.folder');
}
var picker_builder = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.disableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setAppId(gopts.appid)
.addView(view)
//.addView(new google.picker.View(google.picker.ViewId.FOLDERS))
.setOAuthToken(gopts.authtok)
.setDeveloperKey(gopts.devkey)
.setCallback(function(data){
gopts.callback(target_id, data);
})
.setSize(gopts.width, gopts.height)
.setTitle(elem_opts.header);
if(gopts.user != null) picker_builder.setAuthUser(gopts.user);
picker = picker_builder.build();
picker.setVisible(true);
return picker;
};
$.fn.gdrive = function(action, options) {
if(action === 'init') {
gopts = $.extend(gopts, options);
google_api_load();
return this;
}
if(action === 'set') {
return this.each(function(){
var elemid = $(this).attr('id');
var elem_opts = $.extend($.extend(dlgopts[elemid], {'trigger': null, 'header': '', 'filter': ''}), options);
dlgopts[elemid] = elem_opts;
var trig_elem = (elem_opts.trigger == null) ? elemid : elem_opts.trigger;
$('#'+trig_elem).click(function(){
do_auth(function() {
picker = show_dlg(elemid);
});
});
});
}
if(action === 'show') {
var elemid = this.attr('id');
do_auth(function() {
picker = show_dlg(elemid);
});
return this;
}
if(action === 'hide') {
if(null != picker) {
picker.setVisible(false);
picker = null;
}
return this;
}
if(action == 'token') {
return gopts.authtok;
}
if(action == 'user') {
return gopts.user;
}
if(action == 'setauth') {
var elem_opts = $.extend({'uid': null, 'token': null, 'callback': null}, options);
var target_uid = elem_opts.uid;
var target_tok = elem_opts.token;
var callback = elem_opts.callback;
do_auth(function() {
if(null != target_uid) $('#'+target_uid).val(gopts.user);
if(null != target_tok) $('#'+target_tok).val(gopts.authtok);
if(null != callback) callback();
});
}
if(action == 'debug') {
return gopts;
}
};
}(jQuery));