OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 @license |
| 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.g
ithub.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/CONTRI
BUTORS.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/PATEN
TS.txt |
| 9 --> |
| 10 <link rel="import" href="../polymer/polymer.html"> |
| 11 |
| 12 <script> |
| 13 (function() { |
| 14 "use strict"; |
| 15 /** |
| 16 `Polymer.IronJsonpLibraryBehavior` loads a jsonp library. |
| 17 Multiple components can request same library, only one copy will load. |
| 18 |
| 19 Some libraries require a specific global function be defined. |
| 20 If this is the case, specify the `callbackName` property. |
| 21 |
| 22 You should use an HTML Import to load library dependencies |
| 23 when possible instead of using this element. |
| 24 |
| 25 @hero hero.svg |
| 26 @demo demo/index.html |
| 27 @polymerBehavior |
| 28 */ |
| 29 Polymer.IronJsonpLibraryBehavior = { |
| 30 |
| 31 properties: { |
| 32 /** |
| 33 * True if library has been successfully loaded |
| 34 */ |
| 35 libraryLoaded: { |
| 36 type: Boolean, |
| 37 value: false, |
| 38 notify: true, |
| 39 readOnly: true |
| 40 }, |
| 41 /** |
| 42 * Not null if library has failed to load |
| 43 */ |
| 44 libraryErrorMessage: { |
| 45 type: String, |
| 46 value: null, |
| 47 notify: true, |
| 48 readOnly: true |
| 49 } |
| 50 // Following properties are to be set by behavior users |
| 51 /** |
| 52 * Library url. Must contain string `%%callback_name%%`. |
| 53 * |
| 54 * `%%callback_name%%` is a placeholder for jsonp wrapper function name |
| 55 * |
| 56 * Ex: https://maps.googleapis.com/maps/api/js?callback=%%callback%% |
| 57 * @property libraryUrl |
| 58 */ |
| 59 /** |
| 60 * Set if library requires specific callback name. |
| 61 * Name will be automatically generated if not set. |
| 62 * @property callbackName |
| 63 */ |
| 64 /** |
| 65 * name of event to be emitted when library loads. Standard is `api-load` |
| 66 * @property notifyEvent |
| 67 */ |
| 68 /** |
| 69 * event with name specified in `notifyEvent` attribute |
| 70 * will fire upon successful load2 |
| 71 * @event `notifyEvent` |
| 72 */ |
| 73 }, |
| 74 |
| 75 _libraryLoadCallback: function(err, result) { |
| 76 if (err) { |
| 77 console.warn("Library load failed:", err.message); |
| 78 this._setLibraryErrorMessage(err.message); |
| 79 } |
| 80 else { |
| 81 this._setLibraryErrorMessage(null); |
| 82 this._setLibraryLoaded(true); |
| 83 if (this.notifyEvent) |
| 84 this.fire( this.notifyEvent, result); |
| 85 } |
| 86 }, |
| 87 |
| 88 /** loads the library, and fires this.notifyEvent upon completion */ |
| 89 _loadLibrary: function() { |
| 90 LoaderMap.require( |
| 91 this.libraryUrl, |
| 92 this._libraryLoadCallback.bind(this), |
| 93 this.callbackName |
| 94 ); |
| 95 }, |
| 96 |
| 97 ready: function() { |
| 98 this._loadLibrary(); |
| 99 } |
| 100 }; |
| 101 |
| 102 /** |
| 103 * LoaderMap keeps track of all Loaders |
| 104 */ |
| 105 var LoaderMap = { |
| 106 apiMap: {}, // { hash -> Loader } |
| 107 |
| 108 /** |
| 109 * @param {Function} notifyCallback loaded callback fn(result) |
| 110 * @param {string} jsonpCallbackName name of jsonpcallback. If API does not
provide it, leave empty. Optional. |
| 111 */ |
| 112 require: function(url, notifyCallback, jsonpCallbackName) { |
| 113 |
| 114 // make hashable string form url |
| 115 var name = this.nameFromUrl(url); |
| 116 |
| 117 // create a loader as needed |
| 118 if (!this.apiMap[name]) |
| 119 this.apiMap[name] = new Loader(name, url, jsonpCallbackName); |
| 120 |
| 121 // ask for notification |
| 122 this.apiMap[name].requestNotify(notifyCallback); |
| 123 }, |
| 124 |
| 125 nameFromUrl: function(url) { |
| 126 return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api'; |
| 127 } |
| 128 }; |
| 129 |
| 130 /** @constructor */ |
| 131 var Loader = function(name, url, callbackName) { |
| 132 this.notifiers = []; // array of notifyFn [ notifyFn* ] |
| 133 |
| 134 // callback is specified either as callback name |
| 135 // or computed dynamically if url has callbackMacro in it |
| 136 if (!callbackName) { |
| 137 if (url.indexOf(this.callbackMacro) >= 0) { |
| 138 callbackName = name + '_loaded'; |
| 139 url = url.replace(this.callbackMacro, callbackName); |
| 140 } else { |
| 141 this.error = new Error('IronJsonpLibraryBehavior a %%callback_name%% par
ameter is required in libraryUrl'); |
| 142 // TODO(sjmiles): we should probably fallback to listening to script.loa
d |
| 143 return; |
| 144 } |
| 145 } |
| 146 this.callbackName = callbackName; |
| 147 window[this.callbackName] = this.success.bind(this); |
| 148 this.addScript(url); |
| 149 }; |
| 150 |
| 151 Loader.prototype = { |
| 152 |
| 153 callbackMacro: '%%callback%%', |
| 154 loaded: false, |
| 155 |
| 156 addScript: function(src) { |
| 157 var script = document.createElement('script'); |
| 158 script.src = src; |
| 159 script.onerror = this.handleError.bind(this); |
| 160 var s = document.querySelector('script') || document.body; |
| 161 s.parentNode.insertBefore(script, s); |
| 162 this.script = script; |
| 163 }, |
| 164 |
| 165 removeScript: function() { |
| 166 if (this.script.parentNode) { |
| 167 this.script.parentNode.removeChild(this.script); |
| 168 } |
| 169 this.script = null; |
| 170 }, |
| 171 |
| 172 handleError: function(ev) { |
| 173 this.error = new Error("Library failed to load"); |
| 174 this.notifyAll(); |
| 175 this.cleanup(); |
| 176 }, |
| 177 |
| 178 success: function() { |
| 179 this.loaded = true; |
| 180 this.result = Array.prototype.slice.call(arguments); |
| 181 this.notifyAll(); |
| 182 this.cleanup(); |
| 183 }, |
| 184 |
| 185 cleanup: function() { |
| 186 delete window[this.callbackName]; |
| 187 }, |
| 188 |
| 189 notifyAll: function() { |
| 190 this.notifiers.forEach( function(notifyCallback) { |
| 191 notifyCallback(this.error, this.result); |
| 192 }.bind(this)); |
| 193 this.notifiers = []; |
| 194 }, |
| 195 |
| 196 requestNotify: function(notifyCallback) { |
| 197 if (this.loaded || this.error) { |
| 198 notifyCallback( this.error, this.result); |
| 199 } else { |
| 200 this.notifiers.push(notifyCallback); |
| 201 } |
| 202 } |
| 203 }; |
| 204 })(); |
| 205 </script> |
| 206 |
| 207 <!-- |
| 208 Loads specified jsonp library. |
| 209 |
| 210 Example: |
| 211 |
| 212 <iron-jsonp-library |
| 213 library-url="https://apis.google.com/js/plusone.js?onload=%%callback%%" |
| 214 notify-event="api-load" |
| 215 library-loaded="{{loaded}}"></iron-jsonp-library> |
| 216 |
| 217 Will emit 'api-load' event when loaded, and set 'loaded' to true |
| 218 |
| 219 Implemented by Polymer.IronJsonpLibraryBehavior. Use it |
| 220 to create specific library loader elements. |
| 221 |
| 222 @demo |
| 223 --> |
| 224 <script> |
| 225 Polymer({ |
| 226 |
| 227 is: 'iron-jsonp-library', |
| 228 |
| 229 behaviors: [ Polymer.IronJsonpLibraryBehavior ], |
| 230 |
| 231 properties: { |
| 232 /** |
| 233 * Library url. Must contain string `%%callback_name%%`. |
| 234 * |
| 235 * `%%callback_name%%` is a placeholder for jsonp wrapper function name |
| 236 * |
| 237 * Ex: https://maps.googleapis.com/maps/api/js?callback=%%callback%% |
| 238 */ |
| 239 libraryUrl: String, |
| 240 /** |
| 241 * Set if library requires specific callback name. |
| 242 * Name will be automatically generated if not set. |
| 243 */ |
| 244 callbackName: String, |
| 245 /** |
| 246 * event with name specified in 'notifyEvent' attribute |
| 247 * will fire upon successful load |
| 248 */ |
| 249 notifyEvent: String |
| 250 /** |
| 251 * event with name specified in 'notifyEvent' attribute |
| 252 * will fire upon successful load |
| 253 * @event `notifyEvent` |
| 254 */ |
| 255 |
| 256 } |
| 257 }); |
| 258 |
| 259 </script> |
OLD | NEW |