-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
215 lines (187 loc) · 5.88 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use strict";
var Authorizer = function Authorizer(loader, options) {
this.scope = {
install: 'https://www.googleapis.com/auth/drive.install',
file: 'https://www.googleapis.com/auth/drive.file',
openid: 'openid'
};
this.clientId = options.clientId;
this.userId = loader.params.userId;
this.authButton = document.getElementById(options.authButtonElementId);
};
Authorizer.prototype.start = function (onAuthComplete) {
var _this = this;
gapi.load('auth:client,drive-realtime,drive-share', function () {
_this.authorize(onAuthComplete);
});
};
Authorizer.prototype.authorize = function (onAuthComplete) {
var clientId = this.clientId;
var userId = this.userId;
var _this = this;
var handleAuthResult = function (authResult) {
if (authResult && !authResult.error) {
_this.authButton.disabled = true;
_this.fetchUserId(onAuthComplete);
} else {
_this.authButton.disabled = false;
_this.authButton.onclick = authorizeWithPopup;
}
};
var authorizeWithPopup = function () {
gapi.auth.authorize({
client_id: clientId,
scope: [
_this.scope.install,
_this.scope.file,
_this.scope.openid
],
user_id: userId,
immediate: false
}, handleAuthResult);
};
// Try with no popups first.
gapi.auth.authorize({
client_id: clientId,
scope: [
this.scope.install,
this.scope.file,
this.scope.openid
],
user_id: userId,
immediate: true
}, handleAuthResult);
};
Authorizer.prototype.fetchUserId = function (callback) {
var _this = this;
gapi.client.load('oauth2', 'v2', function () {
gapi.client.oauth2.userinfo.get().execute(function (resp) {
if (resp.id) {
_this.userId = resp.id;
}
if (callback) {
callback();
}
});
});
};
var Loader = function Loader(options) {
options = options || {};
this.params = {};
var queryString = window.location.search;
if (queryString) {
// split up the query string and store in an object
var paramStrs = queryString.slice(1).split("&");
for (var i = 0; i < paramStrs.length; i++) {
var paramStr = paramStrs[i].split("=");
this.params[paramStr[0]] = decodeURI(paramStr[1]);
}
}
this.onFileLoaded = options.onFileLoaded || function () {
console.log("onFileLoaded");
};
this.initializeModel = options.initializeModel || function () {
console.log("initializeModel");
};
this.handleErrors = options.handleErrors || function (e) {
if (e.type == gapi.drive.realtime.ErrorType.TOKEN_REFRESH_REQUIRED) {
authorizer.authorize();
} else if (e.type == gapi.drive.realtime.ErrorType.CLIENT_ERROR) {
alert("An Error happened: " + e.message);
window.location.href = "/";
} else if (e.type == gapi.drive.realtime.ErrorType.NOT_FOUND) {
alert("The file was not found. It does not exist or you do not have read access to the file.");
window.location.href = "/";
}
};
this.autoCreate = options.autoCreate || false;
this.defaultTitle = options.defaultTitle || "新しいリアルタイムドキュメント";
this.authorizer = new Authorizer(this, options);
};
Loader.prototype.start = function (afterAuth) {
var _this = this;
this.authorizer.start(function () {
if (afterAuth) {
afterAuth();
}
_this.load();
});
};
Loader.prototype.load = function () {
var fileId = this.params.fileId;
var userId = this.authorizer.userId;
var state = this.params.state;
// Creating the error callback.
var authorizer = this.authorizer;
if (fileId) {
gapi.drive.realtime.load(fileId, this.onFileLoaded, this.initializeModel, this.handleErrors);
return;
} else if (state) {
var stateObj = (function (stateParam) {
try {
return JSON.parse(stateParam);
} catch (e) {
return {};
}
})(state);
if (stateObj.action == "open") {
fileId = stateObj.ids[0];
userId = stateObj.userId;
history.pushState({fileId: fileId, userId: userId}, null, "?fileId=" + fileId + "&userId=" + userId);
gapi.drive.realtime.load(fileId, this.onFileLoaded, this.initializeModel, this.handleErrors);
return;
}
}
if (this.autoCreate) {
this.createNewFileAndRedirect();
}
};
Loader.prototype.createRealtimeFile = function (title, callback) {
gapi.client.load('drive', 'v2', function () {
gapi.client.drive.files.insert({
'resource': {
mimeType: 'application/vnd.google-apps.drive-sdk',
title: title
}
}).execute(callback);
});
}
Loader.prototype.createNewFileAndRedirect = function () {
var _this = this;
this.createRealtimeFile(this.defaultTitle, function (file) {
if (file.id) {
var userId = _this.authorizer.userId;
var fileId = file.id;
history.pushState({fileId: fileId, userId: userId}, null, "?fileId=" + fileId + "&userId=" + userId);
gapi.drive.realtime.load(fileId, _this.onFileLoaded, _this.initializeModel, _this.handleErrors);
} else {
console.error('Error creating file.');
console.error(file);
}
});
};
$(function () {
var loader = new Loader({
appId: "",
clientId: '',
authButtonElementId: 'authorize',
initializeModel: function (model) {
var string = model.createString('Hello Realtime World!');
model.getRoot().set('text', string);
},
autoCreate: true,
defaultTitle: "ぐらぷす",
onFileLoaded: function (doc) {
var string = doc.getModel().getRoot().get('text');
var textArea1 = document.getElementById('edit');
gapi.drive.realtime.databinding.bindString(string, textArea1);
textArea1.disabled = false;
}
});
loader.start();
document.getElementById("share").addEventListener("click", function () {
var shareClient = new gapi.drive.share.ShareClient("1038254939191");
shareClient.setItemIds([loader.params.fileId]);
shareClient.showSettingsDialog();
});
});