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