Skip to content
This repository was archived by the owner on Nov 19, 2018. It is now read-only.

Commit a42386d

Browse files
author
Ray Nicholus
committed
fix(totally-broken): doesn't load in all browsers other than Chrome
Also removed call to `unwrap(form)` since this is not needed as of Polymer/ShadowDOM 0.3.4. fixes #21
1 parent fcfd59e commit a42386d

File tree

2 files changed

+63
-63
lines changed

2 files changed

+63
-63
lines changed

ajax-form.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
## HTML forms on performance-enhancing drugs
77
88
The `ajax-form` element adds some more power and features to traditional HTML forms.
9-
Also allows the `<file-input>` custom element (and any other custom element form fields
9+
Also allows the `<file-input>` custom element (and any other custom element form fields
1010
that have a `name` attribute and a `value`) to be submitted along with other traditional
1111
form elements.
1212
@@ -62,7 +62,7 @@
6262
@status beta
6363
@homepage index.html
6464
-->
65-
<polymer-element name="ajax-form" extends="form" attributes="action cookies enctype headers method">
65+
<polymer-element name="ajax-form" extends="form" attributes="cookies headers">
6666

6767
<template>
6868

@@ -175,7 +175,7 @@
175175
*
176176
* The above form will be submitted as a multipart encoded POST request.
177177
* By default, without an enctype specified, "application/x-www-form-urlencoded"
178-
* is assumed. GET requests will not have a body, and the form fields
178+
* is assumed. GET requests will not have a body, and the form fields
179179
* will be URL encoded as URI query parameters.
180180
*
181181
* @attribute enctype

ajax-form.js

+60-60
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// and returning false doesn't seem to reliably work.
1919
event.preventDefault();
2020
event.stopPropagation();
21-
21+
2222
// respect any field validation attributes
2323
if (this.checkValidity()) {
2424
this.fire('submitting');
@@ -35,7 +35,7 @@
3535
}
3636
}
3737
}.bind(this));
38-
38+
3939
// Intercept native form submit function.
4040
// In order to force the browser to highlight the invalid fields,
4141
// we need to create a hidden submit button and click it if the form is invalid.
@@ -52,56 +52,56 @@
5252
}
5353
};
5454
},
55-
55+
5656
listenForAjaxComplete = function() {
5757
var sender = this.shadowRoot.getElementsByTagName('core-ajax')[0];
58-
58+
5959
sender.addEventListener('core-complete', function(event) {
6060
this.fire('submitted', event.detail.xhr);
6161
}.bind(this));
6262
},
63-
63+
6464
parseCustomElements = function(form, parseFileInputs) {
6565
var data = {};
66-
66+
6767
Array.prototype.slice.call(form.querySelectorAll('*[name]')).forEach(function(el) {
6868
if (parseFileInputs && el.tagName.toLowerCase() === 'file-input') {
6969
var fileInputName = el.getAttribute('name') || 'files';
70-
70+
7171
if (el.files.length > 1) {
7272
fileInputName += '[]';
7373
}
74-
74+
7575
el.files.forEach(function(file) {
7676
data[fileInputName] = file;
7777
});
7878
}
7979
else if (el.tagName.indexOf('-') >= 0 && el.value != null) {
80-
data[el.getAttribute('name')] = el.value;
80+
data[el.getAttribute('name')] = el.value;
8181
}
8282
});
83-
83+
8484
return data;
8585
},
86-
86+
8787
// @TODO: should these FormData parse methods be exposed as events
8888
// if, say, someone wanted to filter or transform the data in a form
8989
// (i.e., radio from yes/no to true/false, or textarea from markdown to
9090
// html)?
9191
parseFormData = function(form) {
92-
var formData = new FormData(window.unwrap(form)),
92+
var formData = new FormData(form),
9393
customElementData = parseCustomElements(form, true);
94-
94+
9595
if (customElementData) {
9696
Object.keys(customElementData).forEach(function(fieldName) {
97-
formData.append(fieldName, customElementData[fieldName]);
97+
formData.append(fieldName, customElementData[fieldName]);
9898
});
9999
}
100-
100+
101101
return formData;
102-
102+
103103
},
104-
104+
105105
/**
106106
* Parse an `HTMLRadioElement`'s value, returning the value iff
107107
* the element has a present `checked` attribute.
@@ -115,9 +115,9 @@
115115
value = element.value;
116116
}
117117
return value;
118-
118+
119119
},
120-
120+
121121
/**
122122
* Parse an `HTMLOptionElement`'s value, returning the value iff
123123
* the element has a present `selected` attribute.
@@ -131,7 +131,7 @@
131131
}
132132
return elementValue;
133133
},
134-
134+
135135
/**
136136
* Parse an `HTMLOptGroupElement` return the `HTMLOptionElement` that
137137
* has a `checked` attribute.
@@ -148,7 +148,7 @@
148148
});
149149
return elementValue;
150150
},
151-
151+
152152
/**
153153
* Parse an `HTMLSelectElement`'s value.
154154
*
@@ -157,15 +157,15 @@
157157
*/
158158
parseSelectElementValues = function(element) {
159159
var elementValues = [];
160-
160+
161161
Array.prototype.forEach.call(element.options, function(optionElement){
162162
var tempElementValue = parseSelectOptionElementValue(optionElement);
163163
tempElementValue && elementValues.push(tempElementValue);
164164
});
165-
165+
166166
return elementValues;
167167
},
168-
168+
169169
/**
170170
* Parse an `HTMLInputElement`'s value.
171171
* @param HTMLInputElement element
@@ -174,7 +174,7 @@
174174
parseInputElementValue = function(element){
175175
var elementValue,
176176
elementType = element.type;
177-
177+
178178
if (element.disabled === true ||
179179
['submit', 'reset', 'button', 'image'].indexOf(elementType) !== -1) {
180180
// do nothing for these button types
@@ -184,10 +184,10 @@
184184
} else {
185185
elementValue = element.value;
186186
}
187-
187+
188188
return elementValue;
189189
},
190-
190+
191191
/**
192192
* Return the value of some `HTMLElement`s value attribute if possible.
193193
* @param HTMLElement element
@@ -196,7 +196,7 @@
196196
parseElementValue = function(element){
197197
var elementValue,
198198
elementTag = element.tagName.toLowerCase();
199-
199+
200200
if (elementTag === 'input') {
201201
elementValue = parseInputElementValue(element);
202202
}
@@ -207,11 +207,11 @@
207207
else if (elementTag === 'select') {
208208
elementValue = parseSelectElementValues(element);
209209
}
210-
210+
211211
return elementValue;
212-
212+
213213
},
214-
214+
215215
/**
216216
* Parse an `HTMLFormElement` into key value pairs
217217
* @param HTMLFormElement form
@@ -221,82 +221,82 @@
221221
var formObj = {},
222222
formElements = form.getElementsByTagName('input'),
223223
customElementsData = parseCustomElements(form);
224-
224+
225225
formElements = Array.prototype.slice.call(formElements);
226226
formElements = formElements.concat(Array.prototype.slice.call(form.getElementsByTagName('select')));
227227
formElements = formElements.concat(Array.prototype.slice.call(form.getElementsByTagName('textarea')));
228-
228+
229229
formElements.forEach(function(formElement){
230230
var key = formElement.name,
231231
val = parseElementValue(formElement);
232-
232+
233233
if (key && val) {
234234
formObj[key] = val;
235235
}
236236
});
237-
238-
Object.keys(customElementsData).forEach(function(fieldName) {
239-
formObj[fieldName] = customElementsData[fieldName];
237+
238+
Object.keys(customElementsData).forEach(function(fieldName) {
239+
formObj[fieldName] = customElementsData[fieldName];
240240
});
241-
241+
242242
return formObj;
243243
},
244-
244+
245245
/**
246246
* Send a url-encoded `HTMLFormElement` in the URL query string.
247247
* @param HTMLFormElement form
248248
*/
249249
sendUrlencodedForm = function(form){
250250
var sender = this.shadowRoot.getElementsByTagName('core-ajax')[0],
251-
// We must URL encode the data and place it in the body or
252-
// query paramter section of the URI (depending on the method).
253-
// core-ajax attempts to do this for us, but this requires we pass
254-
// an Object to core-ajax with the params and we cannot properly
255-
// express multiple values for a <select> (which is possible)
251+
// We must URL encode the data and place it in the body or
252+
// query paramter section of the URI (depending on the method).
253+
// core-ajax attempts to do this for us, but this requires we pass
254+
// an Object to core-ajax with the params and we cannot properly
255+
// express multiple values for a <select> (which is possible)
256256
// via a JavaScript Object.
257257
data = toQueryString(parseForm(form));
258-
258+
259259
if (this.cookies) {
260260
sender.withCredentials = true;
261261
}
262-
262+
263263
if (this.acceptableMethod === 'POST') {
264264
sender.body = data;
265265
}
266266
else {
267-
sender.url += (sender.url.indexOf('?') > 0 ? '&' : '?') + data;
267+
sender.url += (sender.url.indexOf('?') > 0 ? '&' : '?') + data;
268268
}
269-
269+
270270
sender.go();
271271
},
272-
272+
273273
/**
274274
* Send a multipart-encoded `HTMLFormElement` in the request body.
275275
* @param HTMLFormElement form
276276
*/
277277
sendMultipartForm = function(form) {
278278
var sender = this.shadowRoot.getElementsByTagName('core-ajax')[0],
279279
data = parseFormData(form);
280-
280+
281281
// make sure Polymer/core-ajax doesn't touch the Content-Type.
282282
// The browser must set this with the proper multipart boundary ID.
283283
sender.contentType = null;
284-
284+
285285
if (this.cookies) {
286286
sender.withCredentials = true;
287287
}
288-
288+
289289
sender.body = data;
290290
sender.go();
291291
},
292-
292+
293293
toQueryString = function(params) {
294294
var queryParams = [];
295-
295+
296296
Object.keys(params).forEach(function(key) {
297297
var val = params[key];
298298
key = encodeURIComponent(key);
299-
299+
300300
if (val && Object.prototype.toString.call(val) === '[object Array]') {
301301
val.forEach(function(valInArray) {
302302
queryParams.push(key + '=' + encodeURIComponent(valInArray));
@@ -306,24 +306,24 @@
306306
queryParams.push(val == null ? key : (key + '=' + encodeURIComponent(val)));
307307
}
308308
});
309-
309+
310310
return queryParams.join('&');
311311
},
312-
312+
313313
watchForInvalidFields = function(form) {
314314
var customEl = this,
315315
fields = form.getElementsByTagName('input'),
316316
invalidFields = [],
317317
timer = null;
318-
318+
319319
fields = Array.prototype.slice.call(fields);
320320
fields = fields.concat(Array.prototype.slice.call(form.getElementsByTagName('select')));
321321
fields = fields.concat(Array.prototype.slice.call(form.getElementsByTagName('textarea')));
322-
322+
323323
fields.forEach(function(field) {
324324
field.addEventListener('invalid', function() {
325325
invalidFields.push(this);
326-
326+
327327
// In case another element is invalid and the event
328328
// hasn't been triggered yet, hold off on firing the
329329
// invalid event on the custom el.

0 commit comments

Comments
 (0)