forked from GoogleWebComponents/google-apis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
google-client-loader.js
204 lines (176 loc) · 5 KB
/
google-client-loader.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
import { Polymer } from '../polymer/lib/legacy/polymer-fn.js';
import './google-js-api.js';
// Stores whether the API client is done loading.
let _clientLoaded = false;
// Loaders and loading statuses for all APIs, indexed by API name.
// This helps prevent multiple loading requests being fired at the same time
// by multiple google-api-loader elements.
const _statuses = {};
const _loaders = {};
Polymer({
is: 'google-client-loader',
/**
* Fired when the requested API is loaded. Override this name
* by setting `successEventName`.
* @event google-api-load
*/
/**
* Fired if an error occurs while loading the requested API. Override this name
* by setting `errorEventName`.
* @event google-api-load-error
*/
properties: {
/**
* Name of the API to load, e.g. 'urlshortener'.
*
* You can find the full list of APIs on the
* <a href="https://developers.google.com/apis-explorer"> Google APIs
* Explorer</a>.
*/
name: String,
/**
* Version of the API to load, e.g. 'v1'.
*/
version: String,
/**
* App Engine application ID for loading a Google Cloud Endpoints API.
*/
appId: String,
/**
* Root URL where to load the API from, e.g. 'http://host/apis'.
* For App Engine dev server this would be something like:
* 'http://localhost:8080/_ah/api'.
* Overrides 'appId' if both are specified.
*/
apiRoot: String,
/**
* Name of the event fired when API library is loaded.
*/
successEventName: {
type: String,
value: 'google-api-load',
},
/**
* Name of the event fired when there is an error loading the library.
*/
errorEventName: {
type: String,
value: 'google-api-load-error',
},
},
hostAttributes: {
hidden: true, // remove from rendering tree.
},
// Used to fix events potentially being fired multiple times by
// iron-jsonp-library.
_waiting: false,
/**
* Returns the loaded API.
*/
get api() {
if (window.gapi && window.gapi.client &&
window.gapi.client[this.name]) {
return window.gapi.client[this.name];
}
return undefined;
},
/**
* Wrapper for `gapi.auth`.
*/
get auth() {
return gapi.auth;
},
ready() {
this._loader = document.createElement('google-js-api');
if (!this.shadowRoot) { this.attachShadow({ mode: 'open' }); }
this.shadowRoot.appendChild(this._loader);
this.listen(this._loader, 'js-api-load', '_loadClient');
},
detached() {
this.unlisten(this._loader, 'js-api-load', '_loadClient');
},
_loadClient() {
gapi.load('client', this._doneLoadingClient.bind(this));
},
_handleLoadResponse(response) {
if (response && response.error) {
_statuses[this.name] = 'error';
this._fireError(response);
} else {
_statuses[this.name] = 'loaded';
this._fireSuccess();
}
},
_fireSuccess() {
this.fire(
this.successEventName,
{ name: this.name, version: this.version },
);
},
_fireError(response) {
if (response && response.error) {
this.fire(this.errorEventName, {
name: this.name,
version: this.version,
error: response.error,
});
} else {
this.fire(this.errorEventName, {
name: this.name,
version: this.version,
});
}
},
_doneLoadingClient() {
_clientLoaded = true;
// Fix for API client load event being fired multiple times by
// iron-jsonp-library.
if (!this._waiting) {
this._loadApi();
}
},
_createSelfRemovingListener(eventName) {
var handler = function () {
_loaders[this.name].removeEventListener(eventName, handler);
this._loadApi();
}.bind(this);
return handler;
},
_loadApi() {
if (_clientLoaded && this.name && this.version) {
this._waiting = false;
// Is this API already loaded?
if (_statuses[this.name] == 'loaded') {
this._fireSuccess();
// Is a different google-api-loader already loading this API?
} else if (_statuses[this.name] == 'loading') {
this._waiting = true;
_loaders[this.name].addEventListener(
this.successEventName,
this._createSelfRemovingListener(this.successEventName),
);
_loaders[this.name].addEventListener(
this.errorEventName,
this._createSelfRemovingListener(this.errorEventName),
);
// Did we get an error when we tried to load this API before?
} else if (_statuses[this.name] == 'error') {
this._fireError(null);
// Otherwise, looks like we're loading a new API.
} else {
let root;
if (this.apiRoot) {
root = this.apiRoot;
} else if (this.appId) {
root = `https://${this.appId}.appspot.com/_ah/api`;
}
_statuses[this.name] = 'loading';
_loaders[this.name] = this;
gapi.client.load(
this.name, this.version,
this._handleLoadResponse.bind(this), root,
);
}
}
},
});