MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / third_party / polymer / components-chromium / core-ajax / core-ajax-extracted.js
blob299251b90f61360bde7faca69792706c232b8abc
3 Polymer('core-ajax', {
4 /**
5 * Fired when a response is received.
6 *
7 * @event core-response
8 */
10 /**
11 * Fired when an error is received.
13 * @event core-error
16 /**
17 * Fired whenever a response or an error is received.
19 * @event core-complete
22 /**
23 * The URL target of the request.
25 * @attribute url
26 * @type string
27 * @default ''
29 url: '',
31 /**
32 * Specifies what data to store in the `response` property, and
33 * to deliver as `event.response` in `response` events.
35 * One of:
37 * `text`: uses `XHR.responseText`.
39 * `xml`: uses `XHR.responseXML`.
41 * `json`: uses `XHR.responseText` parsed as JSON.
43 * `arraybuffer`: uses `XHR.response`.
45 * `blob`: uses `XHR.response`.
47 * `document`: uses `XHR.response`.
49 * @attribute handleAs
50 * @type string
51 * @default 'text'
53 handleAs: '',
55 /**
56 * If true, automatically performs an Ajax request when either `url` or `params` changes.
58 * @attribute auto
59 * @type boolean
60 * @default false
62 auto: false,
64 /**
65 * Parameters to send to the specified URL, as JSON.
67 * @attribute params
68 * @type string (JSON)
69 * @default ''
71 params: '',
73 /**
74 * Returns the response object.
76 * @attribute response
77 * @type Object
78 * @default null
80 response: null,
82 /**
83 * The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'.
84 * Default is 'GET'.
86 * @attribute method
87 * @type string
88 * @default ''
90 method: '',
92 /**
93 * HTTP request headers to send.
95 * Example:
97 * <core-ajax
98 * auto
99 * url="http://somesite.com"
100 * headers='{"X-Requested-With": "XMLHttpRequest"}'
101 * handleAs="json"
102 * on-core-response="{{handleResponse}}"></core-ajax>
104 * @attribute headers
105 * @type Object
106 * @default null
108 headers: null,
111 * Optional raw body content to send when method === "POST".
113 * Example:
115 * <core-ajax method="POST" auto url="http://somesite.com"
116 * body='{"foo":1, "bar":2}'>
117 * </core-ajax>
119 * @attribute body
120 * @type Object
121 * @default null
123 body: null,
126 * Content type to use when sending data.
128 * @attribute contentType
129 * @type string
130 * @default 'application/x-www-form-urlencoded'
132 contentType: 'application/x-www-form-urlencoded',
135 * Set the withCredentials flag on the request.
137 * @attribute withCredentials
138 * @type boolean
139 * @default false
141 withCredentials: false,
144 * Additional properties to send to core-xhr.
146 * Can be set to an object containing default properties
147 * to send as arguments to the `core-xhr.request()` method
148 * which implements the low-level communication.
150 * @property xhrArgs
151 * @type Object
152 * @default null
154 xhrArgs: null,
156 ready: function() {
157 this.xhr = document.createElement('core-xhr');
160 receive: function(response, xhr) {
161 if (this.isSuccess(xhr)) {
162 this.processResponse(xhr);
163 } else {
164 this.error(xhr);
166 this.complete(xhr);
169 isSuccess: function(xhr) {
170 var status = xhr.status || 0;
171 return !status || (status >= 200 && status < 300);
174 processResponse: function(xhr) {
175 var response = this.evalResponse(xhr);
176 this.response = response;
177 this.fire('core-response', {response: response, xhr: xhr});
180 error: function(xhr) {
181 var response = xhr.status + ': ' + xhr.responseText;
182 this.fire('core-error', {response: response, xhr: xhr});
185 complete: function(xhr) {
186 this.fire('core-complete', {response: xhr.status, xhr: xhr});
189 evalResponse: function(xhr) {
190 return this[(this.handleAs || 'text') + 'Handler'](xhr);
193 xmlHandler: function(xhr) {
194 return xhr.responseXML;
197 textHandler: function(xhr) {
198 return xhr.responseText;
201 jsonHandler: function(xhr) {
202 var r = xhr.responseText;
203 try {
204 return JSON.parse(r);
205 } catch (x) {
206 console.warn('core-ajax caught an exception trying to parse reponse as JSON:');
207 console.warn('url:', this.url);
208 console.warn(x);
209 return r;
213 documentHandler: function(xhr) {
214 return xhr.response;
217 blobHandler: function(xhr) {
218 return xhr.response;
221 arraybufferHandler: function(xhr) {
222 return xhr.response;
225 urlChanged: function() {
226 if (!this.handleAs) {
227 var ext = String(this.url).split('.').pop();
228 switch (ext) {
229 case 'json':
230 this.handleAs = 'json';
231 break;
234 this.autoGo();
237 paramsChanged: function() {
238 this.autoGo();
241 autoChanged: function() {
242 this.autoGo();
245 // TODO(sorvell): multiple side-effects could call autoGo
246 // during one micro-task, use a job to have only one action
247 // occur
248 autoGo: function() {
249 if (this.auto) {
250 this.goJob = this.job(this.goJob, this.go, 0);
255 * Performs an Ajax request to the specified URL.
257 * @method go
259 go: function() {
260 var args = this.xhrArgs || {};
261 // TODO(sjmiles): we may want XHR to default to POST if body is set
262 args.body = this.body || args.body;
263 args.params = this.params || args.params;
264 if (args.params && typeof(args.params) == 'string') {
265 args.params = JSON.parse(args.params);
267 args.headers = this.headers || args.headers || {};
268 if (args.headers && typeof(args.headers) == 'string') {
269 args.headers = JSON.parse(args.headers);
271 if (this.contentType) {
272 args.headers['content-type'] = this.contentType;
274 if (this.handleAs === 'arraybuffer' || this.handleAs === 'blob' ||
275 this.handleAs === 'document') {
276 args.responseType = this.handleAs;
278 args.withCredentials = this.withCredentials;
279 args.callback = this.receive.bind(this);
280 args.url = this.url;
281 args.method = this.method;
282 return args.url && this.xhr.request(args);