aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/iron-form/iron-form.html
blob: 258ca997c231ddfcb2e80a67b8e3aea56a53cdeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">

<!--
`<iron-form>` is a wrapper around the HTML `<form>` element, that can
validate and submit both custom and native HTML elements. Note that this
is a breaking change from iron-form 1.0, which was a type extension.

It has two modes: if `allow-redirect` is true, then after the form submission you
will be redirected to the server response. Otherwise, if it is false, it will
use an `iron-ajax` element to submit the form contents to the server.

  Example:

    <iron-form>
      <form method="get" action="/form/handler">
        <input type="text" name="name" value="Batman">
        <input type="checkbox" name="donuts" checked> I like donuts<br>
        <paper-checkbox name="cheese" value="yes" checked></paper-checkbox>
      </form>
    </iron-form>

By default, a native `<button>` element will submit this form. However, if you
want to submit it from a custom element's click handler, you need to explicitly
call the `iron-form`'s `submit` method.

  Example:

    <paper-button raised onclick="submitForm()">Submit</paper-button>

    function submitForm() {
      document.getElementById('iron-form').submit();
    }

If you are not using the `allow-redirect` mode, then you also have the option of
customizing the request sent to the server. To do so, you can listen to the `iron-form-presubmit`
event, and modify the form's [`iron-ajax`](https://elements.polymer-project.org/elements/iron-ajax)
object. However, If you want to not use `iron-ajax` at all, you can cancel the
event and do your own custom submission:

  Example of modifying the request, but still using the build-in form submission:

    form.addEventListener('iron-form-presubmit', function() {
      this.request.method = 'put';
      this.request.params['extraParam'] = 'someValue';
    });

  Example of bypassing the build-in form submission:

    form.addEventListener('iron-form-presubmit', function(event) {
      event.preventDefault();
      var firebase = new Firebase(form.getAttribute('action'));
      firebase.set(form.serializeForm());
    });

Note that if you're dynamically creating this element, it's mandatory that you
first create the contained `<form>` element and all its children, and only then
attach it to the `<iron-form>`:

  var wrapper = document.createElement('iron-form');
  var form = document.createElement('form');
  var input = document.createElement('input');
  form.appendChild(input);
  document.body.appendChild(wrapper);
  wrapper.appendChild(form);

@element iron-form
@hero hero.svg
@demo demo/index.html
-->

<dom-module id="iron-form">
  <template>
    <style>
      :host {
        display: block;
      }
    </style>

    <!-- This form is used to collect the elements that should be submitted -->
    <slot></slot>

    <!-- This form is used for submission -->
    <form id="helper" action$="[[action]]" method$="[[method]]" enctype$="[[enctype]]"></form>
  </template>

  <script>
    Polymer({
      is: 'iron-form',

      properties: {
        /*
         * Set this to true if you don't want the form to be submitted through an
         * ajax request, and you want the page to redirect to the action URL
         * after the form has been submitted.
         */
        allowRedirect: {
          type: Boolean,
          value: false
        },
        /**
        * HTTP request headers to send. See PolymerElements/iron-ajax for
        * more details. Only works when `allowRedirect` is false.
        */
        headers: {
          type: Object,
          value: function() { return {}; }
        },
        /**
        * Set the `withCredentials` flag on the request. See PolymerElements/iron-ajax for
        * more details. Only works when `allowRedirect` is false.
        */
        withCredentials: {
          type: Boolean,
          value: false
        },
      },
      /**
       * Fired if the form cannot be submitted because it's invalid.
       *
       * @event iron-form-invalid
       */

      /**
       * Fired after the form is submitted.
       *
       * @event iron-form-submit
       */

      /**
       * Fired before the form is submitted.
       *
       * @event iron-form-presubmit
       */

      /**
       * Fired after the form is submitted and a response is received. An
       * IronRequestElement is included as the event.detail object.
       *
       * @event iron-form-response
      */

      /**
       * Fired after the form is submitted and an error is received. An
       * error message is included in event.detail.error and an
       * IronRequestElement is included in event.detail.request.
       *
       * @event iron-form-error
      */

      attached: function() {
        this._nodeObserver = Polymer.dom(this).observeNodes(
          function(mutations) {
            for (var i = 0; i < mutations.addedNodes.length; i++) {
              if (mutations.addedNodes[i].tagName === 'FORM' && !this._alreadyCalledInit) {
                this._alreadyCalledInit = true;
                this._form = mutations.addedNodes[i];
                this._init();
              }
            }
          }.bind(this));
      },

      detached: function() {
        if (this._nodeObserver) {
          Polymer.dom(this).unobserveNodes(this._nodeObserver);
          this._nodeObserver = null;
        }
      },

      _init: function() {
        this._form.addEventListener('submit', this.submit.bind(this));
        this._form.addEventListener('reset', this.reset.bind(this));

        // Save the initial values.
        this._defaults = this._defaults || new WeakMap();
        var nodes = this._getSubmittableElements();
        for (var i = 0; i < nodes.length; i++) {
          var node = nodes[i];
          if (!this._defaults.has(node)) {
            this._defaults.set(node, {
              checked: node.checked,
              value: node.value,
            });
          }
        }
      },

      /**
       * Validates all the required elements (custom and native) in the form.
       * @return {boolean} True if all the elements are valid.
       */
      validate: function() {
        if (this._form.getAttribute('novalidate') === '')
          return true;

        // Start by making the form check the native elements it knows about.
        var valid = this._form.checkValidity();
        var elements = this._getValidatableElements();

        // Go through all the elements, and validate the custom ones.
        for (var el, i = 0; el = elements[i], i < elements.length; i++) {
          // This is weird to appease the compiler. We assume the custom element
          // has a validate() method, otherwise we can't check it.
          var validatable = /** @type {{validate: (function() : boolean)}} */ (el);
          if (validatable.validate) {
            valid = !!validatable.validate() && valid;
          }
        }
        return valid;
      },

      /**
       * Submits the form.
       */
      submit: function(event) {
        // We are not using this form for submission, so always cancel its event.
        if (event) {
          event.preventDefault();
        }

        // If you've called this before distribution happened, bail out.
        if (!this._form) {
          return;
        }

        if (!this.validate()) {
          this.fire('iron-form-invalid');
          return;
        }

        // Remove any existing children in the submission form (from a previous submit).
        this.$.helper.textContent = '';

        var json = this.serializeForm();

        // If we want a redirect, submit the form natively.
        if (this.allowRedirect) {
          // If we're submitting the form natively, then create a hidden element for
          // each of the values.
          for (var element in json) {
            this.$.helper.appendChild(this._createHiddenElement(element, json[element]));
          }

          // Copy the original form attributes.
          this.$.helper.action = this._form.getAttribute('action');
          this.$.helper.method = this._form.getAttribute('method') || 'GET';
          this.$.helper.contentType = this._form.getAttribute('enctype') || 'application/x-www-form-urlencoded';

          this.$.helper.submit();
          this.fire('iron-form-submit');
        } else {
          this._makeAjaxRequest(json);
        }
      },

      /**
       * Resets the form to the default values.
       */
      reset: function(event) {
        // We are not using this form for submission, so always cancel its event.
        if (event)
          event.preventDefault();

        // If you've called this before distribution happened, bail out.
        if (!this._form) {
          return;
        }

        // Load the initial values.
        var nodes = this._getSubmittableElements();
        for (var i = 0; i < nodes.length; i++) {
          var node = nodes[i];
          if (this._defaults.has(node)) {
            var defaults = this._defaults.get(node);
            node.value = defaults.value;
            node.checked = defaults.checked;
          }
        }
      },

      /**
       * Serializes the form as will be used in submission. Note that `serialize`
       * is a Polymer reserved keyword, so calling `someIronForm`.serialize()`
       * will give you unexpected results.
       * @return {Object} An object containing name-value pairs for elements that
       *                  would be submitted.
       */
      serializeForm: function() {
        // Only elements that have a `name` and are not disabled are submittable.
        var elements = this._getSubmittableElements();
        var json = {};
        for (var i = 0; i < elements.length; i++) {
          var values = this._serializeElementValues(elements[i]);
          for (var v = 0; v < values.length; v++) {
            this._addSerializedElement(json, elements[i].name, values[v]);
          }
        }
        return json;
      },

      _handleFormResponse: function (event) {
        this.fire('iron-form-response', event.detail);
      },

      _handleFormError: function (event) {
        this.fire('iron-form-error', event.detail);
      },

      _makeAjaxRequest: function(json) {
        // Initialize the iron-ajax element if we haven't already.
        if (!this.request) {
          this.request = document.createElement('iron-ajax');
          this.request.addEventListener('response', this._handleFormResponse.bind(this));
          this.request.addEventListener('error', this._handleFormError.bind(this));
        }

        // Native forms can also index elements magically by their name (can't make
        // this up if I tried) so we need to get the correct attributes, not the
        // elements with those names.
        this.request.url = this._form.getAttribute('action');
        this.request.method = this._form.getAttribute('method') || 'GET';
        this.request.contentType = this._form.getAttribute('enctype') || 'application/x-www-form-urlencoded';
        this.request.withCredentials = this.withCredentials;
        this.request.headers = this.headers;

        if (this._form.method.toUpperCase() === 'POST') {
          this.request.body = json;
        } else {
          this.request.params = json;
        }

        // Allow for a presubmit hook
        var event = this.fire('iron-form-presubmit', {}, {cancelable: true});
        if(!event.defaultPrevented) {
          this.request.generateRequest();
          this.fire('iron-form-submit', json);
        }
      },

      _getValidatableElements: function() {
        return this._findElements(this._form, true);
      },

      _getSubmittableElements: function() {
        return this._findElements(this._form, false);
      },

      _findElements: function(parent, ignoreName) {
        var nodes = Polymer.dom(parent).querySelectorAll('*');
        var submittable = [];

        for (var i = 0; i < nodes.length; i++) {
          var node = nodes[i];
          // An element is submittable if it is not disabled, and if it has a
          // 'name' attribute.
          if(!node.disabled && (ignoreName || node.name)) {
            submittable.push(node);
          }
          else {
            // This element has a root which could contain more submittable elements.
            if(node.root) {
              Array.prototype.push.apply(submittable, this._findElements(node.root, ignoreName));
            }
          }
        }
        return submittable;
      },

      _serializeElementValues: function(element) {
        // We will assume that every custom element that needs to be serialized
        // has a `value` property, and it contains the correct value.
        // The only weird one is an element that implements IronCheckedElementBehaviour,
        // in which case like the native checkbox/radio button, it's only used
        // when checked.
        // For native elements, from https://www.w3.org/TR/html5/forms.html#the-form-element.
        // Native submittable elements: button, input, keygen, object, select, textarea;
        // 1. We will skip `keygen and `object` for this iteration, and deal with
        // them if they're actually required.
        // 2. <button> and <textarea> have a `value` property, so they behave like
        //    the custom elements.
        // 3. <select> can have multiple options selected, in which case its
        //    `value` is incorrect, and we must use the values of each of its
        //    `selectedOptions`
        // 4. <input> can have a whole bunch of behaviours, so it's handled separately.
        // 5. Buttons are hard. The button that was clicked to submit the form
        //    is the one who's name/value gets sent to the server.
        var tag = element.tagName.toLowerCase();
        if (tag === 'button' || (tag === 'input' && (element.type === 'submit' || element.type === 'reset'))) {
          return [];
        }

        if (tag === 'select') {
          return this._serializeSelectValues(element);
        } else if (tag === 'input') {
          return this._serializeInputValues(element);
        } else {
          if (element['_hasIronCheckedElementBehavior'] && !element.checked)
            return [];
          return [element.value];
        }
      },

      _serializeSelectValues: function(element) {
        var values = [];

        // A <select multiple> has an array of options, some of which can be selected.
        for (var i = 0; i < element.options.length; i++) {
          if (element.options[i].selected) {
            values.push(element.options[i].value)
          }
        }
        return values;
      },

      _serializeInputValues: function(element) {
        // Most of the inputs use their 'value' attribute, with the exception
        // of radio buttons, checkboxes and file.
        var type = element.type.toLowerCase();

        // Don't do anything for unchecked checkboxes/radio buttons.
        // Don't do anything for file, since that requires a different request.
        if (((type === 'checkbox' || type === 'radio') && !element.checked) ||
            type === 'file') {
          return [];
        }
        return [element.value];
      },

      _createHiddenElement: function(name, value) {
        var input = document.createElement("input");
        input.setAttribute("type", "hidden");
        input.setAttribute("name", name);
        input.setAttribute("value", value);
        return input;
      },

      _addSerializedElement: function(json, name, value) {
        // If the name doesn't exist, add it. Otherwise, serialize it to
        // an array,
        if (json[name] === undefined) {
          json[name] = value;
        } else {
          if (!Array.isArray(json[name])) {
            json[name] = [json[name]];
          }
          json[name].push(value);
        }
      }
    });
  </script>
</dom-module>