Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/polymer/v1_0/components-chromium/iron-jsonp-library/iron-jsonp-library-extracted.js

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

Powered by Google App Engine
This is Rietveld 408576698