3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
Code distributed by Google as part of the polymer project is also
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10
<link rel="import" href="../polymer/polymer.html">
16
`Polymer.IronJsonpLibraryBehavior` loads a jsonp library.
17
Multiple components can request same library, only one copy will load.
19
Some libraries require a specific global function be defined.
20
If this is the case, specify the `callbackName` property.
22
You should use an HTML Import to load library dependencies
23
when possible instead of using this element.
29
Polymer.IronJsonpLibraryBehavior = {
33
* True if library has been successfully loaded
42
* Not null if library has failed to load
44
libraryErrorMessage: {
50
// Following properties are to be set by behavior users
52
* Library url. Must contain string `%%callback%%`.
54
* `%%callback%%` is a placeholder for jsonp wrapper function name
56
* Ex: https://maps.googleapis.com/maps/api/js?callback=%%callback%%
57
* @property libraryUrl
60
* Set if library requires specific callback name.
61
* Name will be automatically generated if not set.
62
* @property callbackName
65
* name of event to be emitted when library loads. Standard is `api-load`
66
* @property notifyEvent
69
* event with name specified in `notifyEvent` attribute
70
* will fire upon successful load2
71
* @event `notifyEvent`
76
'_libraryUrlChanged(libraryUrl)'
79
_libraryUrlChanged: function(libraryUrl) {
80
// can't load before ready because notifyEvent might not be set
81
if (this._isReady && this.libraryUrl)
85
_libraryLoadCallback: function(err, result) {
87
console.warn("Library load failed:", err.message);
88
this._setLibraryErrorMessage(err.message);
91
this._setLibraryErrorMessage(null);
92
this._setLibraryLoaded(true);
94
this.fire(this.notifyEvent, result);
98
/** loads the library, and fires this.notifyEvent upon completion */
99
_loadLibrary: function() {
102
this._libraryLoadCallback.bind(this),
108
this._isReady = true;
115
* LoaderMap keeps track of all Loaders
118
apiMap: {}, // { hash -> Loader }
121
* @param {Function} notifyCallback loaded callback fn(result)
122
* @param {string} jsonpCallbackName name of jsonpcallback. If API does not provide it, leave empty. Optional.
124
require: function(url, notifyCallback, jsonpCallbackName) {
126
// make hashable string form url
127
var name = this.nameFromUrl(url);
129
// create a loader as needed
130
if (!this.apiMap[name])
131
this.apiMap[name] = new Loader(name, url, jsonpCallbackName);
133
// ask for notification
134
this.apiMap[name].requestNotify(notifyCallback);
137
nameFromUrl: function(url) {
138
return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api';
143
var Loader = function(name, url, callbackName) {
144
this.notifiers = []; // array of notifyFn [ notifyFn* ]
146
// callback is specified either as callback name
147
// or computed dynamically if url has callbackMacro in it
149
if (url.indexOf(this.callbackMacro) >= 0) {
150
callbackName = name + '_loaded';
151
url = url.replace(this.callbackMacro, callbackName);
153
this.error = new Error('IronJsonpLibraryBehavior a %%callback%% parameter is required in libraryUrl');
154
// TODO(sjmiles): we should probably fallback to listening to script.load
158
this.callbackName = callbackName;
159
window[this.callbackName] = this.success.bind(this);
165
callbackMacro: '%%callback%%',
168
addScript: function(src) {
169
var script = document.createElement('script');
171
script.onerror = this.handleError.bind(this);
172
var s = document.querySelector('script') || document.body;
173
s.parentNode.insertBefore(script, s);
174
this.script = script;
177
removeScript: function() {
178
if (this.script.parentNode) {
179
this.script.parentNode.removeChild(this.script);
184
handleError: function(ev) {
185
this.error = new Error("Library failed to load");
190
success: function() {
192
this.result = Array.prototype.slice.call(arguments);
197
cleanup: function() {
198
delete window[this.callbackName];
201
notifyAll: function() {
202
this.notifiers.forEach( function(notifyCallback) {
203
notifyCallback(this.error, this.result);
208
requestNotify: function(notifyCallback) {
209
if (this.loaded || this.error) {
210
notifyCallback( this.error, this.result);
212
this.notifiers.push(notifyCallback);
220
Loads specified jsonp library.
225
library-url="https://apis.google.com/js/plusone.js?onload=%%callback%%"
226
notify-event="api-load"
227
library-loaded="{{loaded}}"></iron-jsonp-library>
229
Will emit 'api-load' event when loaded, and set 'loaded' to true
231
Implemented by Polymer.IronJsonpLibraryBehavior. Use it
232
to create specific library loader elements.
239
is: 'iron-jsonp-library',
241
behaviors: [ Polymer.IronJsonpLibraryBehavior ],
245
* Library url. Must contain string `%%callback%%`.
247
* `%%callback%%` is a placeholder for jsonp wrapper function name
249
* Ex: https://maps.googleapis.com/maps/api/js?callback=%%callback%%
253
* Set if library requires specific callback name.
254
* Name will be automatically generated if not set.
256
callbackName: String,
258
* event with name specified in 'notifyEvent' attribute
259
* will fire upon successful load
263
* event with name specified in 'notifyEvent' attribute
264
* will fire upon successful load
265
* @event `notifyEvent`