| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
| 3 This code may only be used under the BSD style license found at https://polymer.
github.io/LICENSE.txt |
| 4 The complete set of authors may be found at https://polymer.github.io/AUTHORS.tx
t |
| 5 The complete set of contributors may be found at https://polymer.github.io/CONTR
IBUTORS.txt |
| 6 Code distributed by Google as part of the polymer project is also |
| 7 subject to an additional IP rights grant found at https://polymer.github.io/PATE
NTS.txt |
| 8 --> |
| 9 |
| 10 <link rel="import" href="../polymer/polymer.html"> |
| 11 <link rel="import" href="google-js-api.html"> |
| 12 |
| 13 <!-- |
| 14 Element for loading a specific client Google API with the JavaScript client libr
ary. |
| 15 |
| 16 For loading `gapi.client` libraries |
| 17 |
| 18 ##### Example |
| 19 |
| 20 <google-client-loader id="shortener" |
| 21 name="urlshortener" |
| 22 version="v1"></google-client-loader> |
| 23 |
| 24 <script> |
| 25 var shortener = document.getElementById('shortener'); |
| 26 shortener.addEventListener('google-api-load', function(event) { |
| 27 var request = shortener.api.url.get({ |
| 28 shortUrl: 'http://goo.gl/fbsS' |
| 29 }); |
| 30 request.execute(function(resp) { |
| 31 console.log(resp); |
| 32 }); |
| 33 }); |
| 34 </script> |
| 35 |
| 36 @demo |
| 37 --> |
| 38 <dom-module id="google-client-loader"> |
| 39 <template> |
| 40 <google-js-api on-js-api-load="_loadClient"></google-js-api> |
| 41 </template> |
| 42 </dom-module> |
| 43 |
| 44 <script> |
| 45 (function() { |
| 46 'use strict'; |
| 47 |
| 48 // Stores whether the API client is done loading. |
| 49 var _clientLoaded = false; |
| 50 |
| 51 // Loaders and loading statuses for all APIs, indexed by API name. |
| 52 // This helps prevent multiple loading requests being fired at the same time |
| 53 // by multiple google-api-loader elements. |
| 54 var _statuses = {}; |
| 55 var _loaders = {}; |
| 56 |
| 57 Polymer({ |
| 58 |
| 59 is: 'google-client-loader', |
| 60 |
| 61 /** |
| 62 * Fired when the requested API is loaded. Override this name |
| 63 * by setting `successEventName`. |
| 64 * @event google-api-load |
| 65 */ |
| 66 |
| 67 /** |
| 68 * Fired if an error occurs while loading the requested API. Override this
name |
| 69 * by setting `errorEventName`. |
| 70 * @event google-api-load-error |
| 71 */ |
| 72 |
| 73 properties: { |
| 74 /** |
| 75 * Name of the API to load, e.g. 'urlshortener'. |
| 76 * |
| 77 * You can find the full list of APIs on the |
| 78 * <a href="https://developers.google.com/apis-explorer"> Google APIs |
| 79 * Explorer</a>. |
| 80 * @required |
| 81 */ |
| 82 name: String, |
| 83 |
| 84 /** |
| 85 * Version of the API to load, e.g. 'v1'. |
| 86 * @required |
| 87 */ |
| 88 version: String, |
| 89 |
| 90 /** |
| 91 * App Engine application ID for loading a Google Cloud Endpoints API. |
| 92 */ |
| 93 appId: String, |
| 94 |
| 95 /** |
| 96 * Root URL where to load the API from, e.g. 'http://host/apis'. |
| 97 * For App Engine dev server this would be something like: |
| 98 * 'http://localhost:8080/_ah/api'. |
| 99 * Overrides 'appId' if both are specified. |
| 100 */ |
| 101 apiRoot: String, |
| 102 |
| 103 /** |
| 104 * Name of the event fired when API library is loaded. |
| 105 */ |
| 106 successEventName: { |
| 107 type: String, |
| 108 value: 'google-api-load' |
| 109 }, |
| 110 |
| 111 /** |
| 112 * Name of the event fired when there is an error loading the library. |
| 113 */ |
| 114 errorEventName: { |
| 115 type: String, |
| 116 value: 'google-api-load-error' |
| 117 } |
| 118 }, |
| 119 |
| 120 // Used to fix events potentially being fired multiple times by |
| 121 // iron-jsonp-library. |
| 122 _waiting: false, |
| 123 |
| 124 /** |
| 125 * Returns the loaded API. |
| 126 * @type gapi.client |
| 127 */ |
| 128 get api() { |
| 129 if (window.gapi && window.gapi.client && |
| 130 window.gapi.client[this.name]) { |
| 131 return window.gapi.client[this.name]; |
| 132 } else { |
| 133 return undefined; |
| 134 } |
| 135 }, |
| 136 |
| 137 /** |
| 138 * Wrapper for `gapi.auth`. |
| 139 */ |
| 140 get auth() { |
| 141 return gapi.auth; |
| 142 }, |
| 143 |
| 144 _loadClient: function() { |
| 145 gapi.load('client', this._doneLoadingClient.bind(this)); |
| 146 }, |
| 147 |
| 148 _handleLoadResponse: function(response) { |
| 149 if (response && response.error) { |
| 150 _statuses[this.name] = 'error'; |
| 151 this._fireError(response); |
| 152 } else { |
| 153 _statuses[this.name] = 'loaded'; |
| 154 this._fireSuccess(); |
| 155 } |
| 156 }, |
| 157 |
| 158 _fireSuccess: function() { |
| 159 this.fire(this.successEventName, |
| 160 { 'name': this.name, 'version': this.version }); |
| 161 }, |
| 162 |
| 163 _fireError: function(response) { |
| 164 if (response && response.error) { |
| 165 this.fire(this.errorEventName, { |
| 166 'name': this.name, |
| 167 'version': this.version, |
| 168 'error': response.error }); |
| 169 } else { |
| 170 this.fire(this.errorEventName, { |
| 171 'name': this.name, |
| 172 'version': this.version }); |
| 173 } |
| 174 }, |
| 175 |
| 176 _doneLoadingClient: function() { |
| 177 _clientLoaded = true; |
| 178 // Fix for API client load event being fired multiple times by |
| 179 // iron-jsonp-library. |
| 180 if (!this._waiting) { |
| 181 this._loadApi(); |
| 182 } |
| 183 }, |
| 184 |
| 185 _createSelfRemovingListener: function(eventName) { |
| 186 var handler = function () { |
| 187 _loaders[this.name].removeEventListener(eventName, handler); |
| 188 this._loadApi(); |
| 189 }.bind(this); |
| 190 |
| 191 return handler; |
| 192 }, |
| 193 |
| 194 _loadApi: function() { |
| 195 if (_clientLoaded && this.name && this.version) { |
| 196 this._waiting = false; |
| 197 // Is this API already loaded? |
| 198 if (_statuses[this.name] == 'loaded') { |
| 199 this._fireSuccess(); |
| 200 // Is a different google-api-loader already loading this API? |
| 201 } else if (_statuses[this.name] == 'loading') { |
| 202 this._waiting = true; |
| 203 _loaders[this.name].addEventListener(this.successEventName, |
| 204 this._createSelfRemovingListener(this.successEventName)); |
| 205 _loaders[this.name].addEventListener(this.errorEventName, |
| 206 this._createSelfRemovingListener(this.errorEventName)); |
| 207 // Did we get an error when we tried to load this API before? |
| 208 } else if (_statuses[this.name] == 'error') { |
| 209 this._fireError(); |
| 210 // Otherwise, looks like we're loading a new API. |
| 211 } else { |
| 212 var root; |
| 213 if (this.apiRoot) { |
| 214 root = this.apiRoot; |
| 215 } else if (this.appId) { |
| 216 root = 'https://' + this.appId + '.appspot.com/_ah/api'; |
| 217 } |
| 218 _statuses[this.name] = 'loading'; |
| 219 _loaders[this.name] = this; |
| 220 gapi.client.load(this.name, this.version, |
| 221 this._handleLoadResponse.bind(this), root); |
| 222 } |
| 223 } |
| 224 } |
| 225 }); |
| 226 })(); |
| 227 </script> |
| OLD | NEW |