~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/iron-ajax/iron-ajax.html

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
 
3
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
 
4
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
 
5
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
 
6
Code distributed by Google as part of the polymer project is also
 
7
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
 
8
-->
 
9
 
 
10
<link rel="import" href="../polymer/polymer.html">
 
11
<link rel="import" href="iron-request.html">
 
12
 
 
13
<!--
 
14
The `iron-ajax` element exposes network request functionality.
 
15
 
 
16
    <iron-ajax
 
17
        auto
 
18
        url="http://gdata.youtube.com/feeds/api/videos/"
 
19
        params='{"alt":"json", "q":"chrome"}'
 
20
        handle-as="json"
 
21
        on-response="handleResponse"
 
22
        debounce-duration="300"></iron-ajax>
 
23
 
 
24
With `auto` set to `true`, the element performs a request whenever
 
25
its `url`, `params` or `body` properties are changed. Automatically generated
 
26
requests will be debounced in the case that multiple attributes are changed
 
27
sequentially.
 
28
 
 
29
Note: The `params` attribute must be double quoted JSON.
 
30
 
 
31
You can trigger a request explicitly by calling `generateRequest` on the
 
32
element.
 
33
 
 
34
@demo demo/index.html
 
35
@hero hero.svg
 
36
-->
 
37
 
 
38
<script>
 
39
  'use strict';
 
40
 
 
41
  Polymer({
 
42
 
 
43
    is: 'iron-ajax',
 
44
 
 
45
    /**
 
46
     * Fired when a request is sent.
 
47
     *
 
48
     * @event request
 
49
     */
 
50
 
 
51
    /**
 
52
     * Fired when a response is received.
 
53
     *
 
54
     * @event response
 
55
     */
 
56
 
 
57
    /**
 
58
     * Fired when an error is received.
 
59
     *
 
60
     * @event error
 
61
     */
 
62
 
 
63
    hostAttributes: {
 
64
      hidden: true
 
65
    },
 
66
 
 
67
    properties: {
 
68
      /**
 
69
       * The URL target of the request.
 
70
       */
 
71
      url: {
 
72
        type: String
 
73
      },
 
74
 
 
75
      /**
 
76
       * An object that contains query parameters to be appended to the
 
77
       * specified `url` when generating a request. If you wish to set the body
 
78
       * content when making a POST request, you should use the `body` property
 
79
       * instead.
 
80
       */
 
81
      params: {
 
82
        type: Object,
 
83
        value: function() {
 
84
          return {};
 
85
        }
 
86
      },
 
87
 
 
88
      /**
 
89
       * The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'.
 
90
       * Default is 'GET'.
 
91
       */
 
92
      method: {
 
93
        type: String,
 
94
        value: 'GET'
 
95
      },
 
96
 
 
97
      /**
 
98
       * HTTP request headers to send.
 
99
       *
 
100
       * Example:
 
101
       *
 
102
       *     <iron-ajax
 
103
       *         auto
 
104
       *         url="http://somesite.com"
 
105
       *         headers='{"X-Requested-With": "XMLHttpRequest"}'
 
106
       *         handle-as="json"></iron-ajax>
 
107
       *
 
108
       * Note: setting a `Content-Type` header here will override the value
 
109
       * specified by the `contentType` property of this element.
 
110
       */
 
111
      headers: {
 
112
        type: Object,
 
113
        value: function() {
 
114
          return {};
 
115
        }
 
116
      },
 
117
 
 
118
      /**
 
119
       * Content type to use when sending data. If the `contentType` property
 
120
       * is set and a `Content-Type` header is specified in the `headers`
 
121
       * property, the `headers` property value will take precedence.
 
122
       *
 
123
       * Varies the handling of the `body` param.
 
124
       */
 
125
      contentType: {
 
126
        type: String,
 
127
        value: null
 
128
      },
 
129
 
 
130
      /**
 
131
       * Body content to send with the request, typically used with "POST"
 
132
       * requests.
 
133
       *
 
134
       * If body is a string it will be sent unmodified.
 
135
       *
 
136
       * If Content-Type is set to a value listed below, then
 
137
       * the body will be encoded accordingly.
 
138
       *
 
139
       *    * `content-type="application/json"`
 
140
       *      * body is encoded like `{"foo":"bar baz","x":1}`
 
141
       *    * `content-type="application/x-www-form-urlencoded"`
 
142
       *      * body is encoded like `foo=bar+baz&x=1`
 
143
       *
 
144
       * Otherwise the body will be passed to the browser unmodified, and it
 
145
       * will handle any encoding (e.g. for FormData, Blob, ArrayBuffer).
 
146
       *
 
147
       * @type (ArrayBuffer|ArrayBufferView|Blob|Document|FormData|null|string|undefined|Object)
 
148
       */
 
149
      body: {
 
150
        type: Object,
 
151
        value: null
 
152
      },
 
153
 
 
154
      /**
 
155
       * Toggle whether XHR is synchronous or asynchronous. Don't change this
 
156
       * to true unless You Know What You Are Doing™.
 
157
       */
 
158
      sync: {
 
159
        type: Boolean,
 
160
        value: false
 
161
      },
 
162
 
 
163
      /**
 
164
       * Specifies what data to store in the `response` property, and
 
165
       * to deliver as `event.detail.response` in `response` events.
 
166
       *
 
167
       * One of:
 
168
       *
 
169
       *    `text`: uses `XHR.responseText`.
 
170
       *
 
171
       *    `xml`: uses `XHR.responseXML`.
 
172
       *
 
173
       *    `json`: uses `XHR.responseText` parsed as JSON.
 
174
       *
 
175
       *    `arraybuffer`: uses `XHR.response`.
 
176
       *
 
177
       *    `blob`: uses `XHR.response`.
 
178
       *
 
179
       *    `document`: uses `XHR.response`.
 
180
       */
 
181
      handleAs: {
 
182
        type: String,
 
183
        value: 'json'
 
184
      },
 
185
 
 
186
      /**
 
187
       * Set the withCredentials flag on the request.
 
188
       */
 
189
      withCredentials: {
 
190
        type: Boolean,
 
191
        value: false
 
192
      },
 
193
 
 
194
      /**
 
195
       * Set the timeout flag on the request.
 
196
       */
 
197
      timeout: {
 
198
        type: Number,
 
199
        value: 0
 
200
      },
 
201
 
 
202
      /**
 
203
       * If true, automatically performs an Ajax request when either `url` or
 
204
       * `params` changes.
 
205
       */
 
206
      auto: {
 
207
        type: Boolean,
 
208
        value: false
 
209
      },
 
210
 
 
211
      /**
 
212
       * If true, error messages will automatically be logged to the console.
 
213
       */
 
214
      verbose: {
 
215
        type: Boolean,
 
216
        value: false
 
217
      },
 
218
 
 
219
      /**
 
220
       * The most recent request made by this iron-ajax element.
 
221
       */
 
222
      lastRequest: {
 
223
        type: Object,
 
224
        notify: true,
 
225
        readOnly: true
 
226
      },
 
227
 
 
228
      /**
 
229
       * True while lastRequest is in flight.
 
230
       */
 
231
      loading: {
 
232
        type: Boolean,
 
233
        notify: true,
 
234
        readOnly: true
 
235
      },
 
236
 
 
237
      /**
 
238
       * lastRequest's response.
 
239
       *
 
240
       * Note that lastResponse and lastError are set when lastRequest finishes,
 
241
       * so if loading is true, then lastResponse and lastError will correspond
 
242
       * to the result of the previous request.
 
243
       *
 
244
       * The type of the response is determined by the value of `handleAs` at
 
245
       * the time that the request was generated.
 
246
       *
 
247
       * @type {Object}
 
248
       */
 
249
      lastResponse: {
 
250
        type: Object,
 
251
        notify: true,
 
252
        readOnly: true
 
253
      },
 
254
 
 
255
      /**
 
256
       * lastRequest's error, if any.
 
257
       *
 
258
       * @type {Object}
 
259
       */
 
260
      lastError: {
 
261
        type: Object,
 
262
        notify: true,
 
263
        readOnly: true
 
264
      },
 
265
 
 
266
      /**
 
267
       * An Array of all in-flight requests originating from this iron-ajax
 
268
       * element.
 
269
       */
 
270
      activeRequests: {
 
271
        type: Array,
 
272
        notify: true,
 
273
        readOnly: true,
 
274
        value: function() {
 
275
          return [];
 
276
        }
 
277
      },
 
278
 
 
279
      /**
 
280
       * Length of time in milliseconds to debounce multiple automatically generated requests.
 
281
       */
 
282
      debounceDuration: {
 
283
        type: Number,
 
284
        value: 0,
 
285
        notify: true
 
286
      },
 
287
 
 
288
      /**
 
289
       * Prefix to be stripped from a JSON response before parsing it.
 
290
       *
 
291
       * In order to prevent an attack using CSRF with Array responses
 
292
       * (http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx/)
 
293
       * many backends will mitigate this by prefixing all JSON response bodies
 
294
       * with a string that would be nonsensical to a JavaScript parser.
 
295
       *
 
296
       */
 
297
      jsonPrefix: {
 
298
        type: String,
 
299
        value: ''
 
300
      },
 
301
 
 
302
      /**
 
303
       * By default, these events do not bubble largely because the `error` event has special
 
304
       * meaning in the window object. Setting this attribute will cause iron-ajax's request,
 
305
       * response, and error events to bubble to the window object.
 
306
       */
 
307
      bubbles: {
 
308
        type: Boolean,
 
309
        value: false
 
310
      },
 
311
 
 
312
      _boundHandleResponse: {
 
313
        type: Function,
 
314
        value: function() {
 
315
          return this._handleResponse.bind(this);
 
316
        }
 
317
      }
 
318
    },
 
319
 
 
320
    observers: [
 
321
      '_requestOptionsChanged(url, method, params.*, headers, contentType, ' +
 
322
          'body, sync, handleAs, jsonPrefix, withCredentials, timeout, auto)'
 
323
    ],
 
324
 
 
325
    /**
 
326
     * The query string that should be appended to the `url`, serialized from
 
327
     * the current value of `params`.
 
328
     *
 
329
     * @return {string}
 
330
     */
 
331
    get queryString () {
 
332
      var queryParts = [];
 
333
      var param;
 
334
      var value;
 
335
 
 
336
      for (param in this.params) {
 
337
        value = this.params[param];
 
338
        param = window.encodeURIComponent(param);
 
339
 
 
340
        if (Array.isArray(value)) {
 
341
          for (var i = 0; i < value.length; i++) {
 
342
            queryParts.push(param + '=' + window.encodeURIComponent(value[i]));
 
343
          }
 
344
        } else if (value !== null) {
 
345
          queryParts.push(param + '=' + window.encodeURIComponent(value));
 
346
        } else {
 
347
          queryParts.push(param);
 
348
        }
 
349
      }
 
350
 
 
351
      return queryParts.join('&');
 
352
    },
 
353
 
 
354
    /**
 
355
     * The `url` with query string (if `params` are specified), suitable for
 
356
     * providing to an `iron-request` instance.
 
357
     *
 
358
     * @return {string}
 
359
     */
 
360
    get requestUrl() {
 
361
      var queryString = this.queryString;
 
362
 
 
363
      if (queryString) {
 
364
        var bindingChar = this.url.indexOf('?') >= 0 ? '&' : '?';
 
365
        return this.url + bindingChar + queryString;
 
366
      }
 
367
 
 
368
      return this.url;
 
369
    },
 
370
 
 
371
    /**
 
372
     * An object that maps header names to header values, first applying the
 
373
     * the value of `Content-Type` and then overlaying the headers specified
 
374
     * in the `headers` property.
 
375
     *
 
376
     * @return {Object}
 
377
     */
 
378
    get requestHeaders() {
 
379
      var headers = {};
 
380
      var contentType = this.contentType;
 
381
      if (contentType == null && (typeof this.body === 'string')) {
 
382
        contentType = 'application/x-www-form-urlencoded';
 
383
      }
 
384
      if (contentType) {
 
385
        headers['content-type'] = contentType;
 
386
      }
 
387
      var header;
 
388
 
 
389
      if (this.headers instanceof Object) {
 
390
        for (header in this.headers) {
 
391
          headers[header] = this.headers[header].toString();
 
392
        }
 
393
      }
 
394
 
 
395
      return headers;
 
396
    },
 
397
 
 
398
    /**
 
399
     * Request options suitable for generating an `iron-request` instance based
 
400
     * on the current state of the `iron-ajax` instance's properties.
 
401
     *
 
402
     * @return {{
 
403
     *   url: string,
 
404
     *   method: (string|undefined),
 
405
     *   async: (boolean|undefined),
 
406
     *   body: (ArrayBuffer|ArrayBufferView|Blob|Document|FormData|null|string|undefined|Object),
 
407
     *   headers: (Object|undefined),
 
408
     *   handleAs: (string|undefined),
 
409
     *   jsonPrefix: (string|undefined),
 
410
     *   withCredentials: (boolean|undefined)}}
 
411
     */
 
412
    toRequestOptions: function() {
 
413
      return {
 
414
        url: this.requestUrl || '',
 
415
        method: this.method,
 
416
        headers: this.requestHeaders,
 
417
        body: this.body,
 
418
        async: !this.sync,
 
419
        handleAs: this.handleAs,
 
420
        jsonPrefix: this.jsonPrefix,
 
421
        withCredentials: this.withCredentials,
 
422
        timeout: this.timeout
 
423
      };
 
424
    },
 
425
 
 
426
    /**
 
427
     * Performs an AJAX request to the specified URL.
 
428
     *
 
429
     * @return {!IronRequestElement}
 
430
     */
 
431
    generateRequest: function() {
 
432
      var request = /** @type {!IronRequestElement} */ (document.createElement('iron-request'));
 
433
      var requestOptions = this.toRequestOptions();
 
434
 
 
435
      this.activeRequests.push(request);
 
436
 
 
437
      request.completes.then(
 
438
        this._boundHandleResponse
 
439
      ).catch(
 
440
        this._handleError.bind(this, request)
 
441
      ).then(
 
442
        this._discardRequest.bind(this, request)
 
443
      );
 
444
 
 
445
      request.send(requestOptions);
 
446
 
 
447
      this._setLastRequest(request);
 
448
      this._setLoading(true);
 
449
 
 
450
      this.fire('request', {
 
451
        request: request,
 
452
        options: requestOptions
 
453
      }, {bubbles: this.bubbles});
 
454
 
 
455
      return request;
 
456
    },
 
457
 
 
458
    _handleResponse: function(request) {
 
459
      if (request === this.lastRequest) {
 
460
        this._setLastResponse(request.response);
 
461
        this._setLastError(null);
 
462
        this._setLoading(false);
 
463
      }
 
464
      this.fire('response', request, {bubbles: this.bubbles});
 
465
    },
 
466
 
 
467
    _handleError: function(request, error) {
 
468
      if (this.verbose) {
 
469
        console.error(error);
 
470
      }
 
471
 
 
472
      if (request === this.lastRequest) {
 
473
        this._setLastError({
 
474
          request: request,
 
475
          error: error
 
476
        });
 
477
        this._setLastResponse(null);
 
478
        this._setLoading(false);
 
479
      }
 
480
      this.fire('error', {
 
481
        request: request,
 
482
        error: error
 
483
      }, {bubbles: this.bubbles});
 
484
    },
 
485
 
 
486
    _discardRequest: function(request) {
 
487
      var requestIndex = this.activeRequests.indexOf(request);
 
488
 
 
489
      if (requestIndex > -1) {
 
490
        this.activeRequests.splice(requestIndex, 1);
 
491
      }
 
492
    },
 
493
 
 
494
    _requestOptionsChanged: function() {
 
495
      this.debounce('generate-request', function() {
 
496
        if (this.url == null) {
 
497
          return;
 
498
        }
 
499
 
 
500
        if (this.auto) {
 
501
          this.generateRequest();
 
502
        }
 
503
      }, this.debounceDuration);
 
504
    },
 
505
 
 
506
  });
 
507
</script>