google-client-loader.html
6.58 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at https://polymer.github.io/LICENSE.txt
The complete set of authors may be found at https://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at https://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at https://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="google-js-api.html">
<!--
Element for loading a specific client Google API with the JavaScript client library.
For loading `gapi.client` libraries
##### Example
<google-client-loader id="shortener"
name="urlshortener"
version="v1"></google-client-loader>
<script>
var shortener = document.getElementById('shortener');
shortener.addEventListener('google-api-load', function(event) {
var request = shortener.api.url.get({
shortUrl: 'http://goo.gl/fbsS'
});
request.execute(function(resp) {
console.log(resp);
});
});
</script>
@demo
-->
<dom-module id="google-client-loader">
<template>
<google-js-api on-js-api-load="_loadClient"></google-js-api>
</template>
</dom-module>
<script>
(function() {
'use strict';
// Stores whether the API client is done loading.
var _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.
var _statuses = {};
var _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>.
* @required
*/
name: String,
/**
* Version of the API to load, e.g. 'v1'.
* @required
*/
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'
}
},
// Used to fix events potentially being fired multiple times by
// iron-jsonp-library.
_waiting: false,
/**
* Returns the loaded API.
* @type gapi.client
*/
get api() {
if (window.gapi && window.gapi.client &&
window.gapi.client[this.name]) {
return window.gapi.client[this.name];
} else {
return undefined;
}
},
/**
* Wrapper for `gapi.auth`.
*/
get auth() {
return gapi.auth;
},
_loadClient: function() {
gapi.load('client', this._doneLoadingClient.bind(this));
},
_handleLoadResponse: function(response) {
if (response && response.error) {
_statuses[this.name] = 'error';
this._fireError(response);
} else {
_statuses[this.name] = 'loaded';
this._fireSuccess();
}
},
_fireSuccess: function() {
this.fire(this.successEventName,
{ 'name': this.name, 'version': this.version });
},
_fireError: function(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: function() {
_clientLoaded = true;
// Fix for API client load event being fired multiple times by
// iron-jsonp-library.
if (!this._waiting) {
this._loadApi();
}
},
_createSelfRemovingListener: function(eventName) {
var handler = function () {
_loaders[this.name].removeEventListener(eventName, handler);
this._loadApi();
}.bind(this);
return handler;
},
_loadApi: function() {
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();
// Otherwise, looks like we're loading a new API.
} else {
var 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);
}
}
}
});
})();
</script>