* Serialize the form child element values to a string, or an Array. * The output format is defined with the `formatType` argument. * This is loosely based upon http://stackoverflow.com/a/40705993/2834898. * * @param {HTMLFormElement} form * @param {boolean} [intoAnAr
(form, intoAnArray = false, formatType = 'unformatted', serializedSpaceCharacter = '+', forcedOutputFormat = null)
| 9435 | * @private |
| 9436 | */ |
| 9437 | static _serialize(form, intoAnArray = false, formatType = 'unformatted', serializedSpaceCharacter = '+', forcedOutputFormat = null) { |
| 9438 | const result = []; |
| 9439 | |
| 9440 | if (typeof form === 'object' && form.nodeName.toLowerCase() === 'form') { |
| 9441 | Array.prototype.slice.call(form.elements).forEach(element => { |
| 9442 | if (element.name && |
| 9443 | !element.disabled && |
| 9444 | ['file', 'reset', 'submit', 'button'].indexOf(element.type) === -1) { |
| 9445 | if (element.type === 'select-multiple') { |
| 9446 | Array.prototype.slice.call(element.options).forEach(option => { |
| 9447 | if (option.selected) { |
| 9448 | //TODO Should we unformat/format/localize the selection option (which be default should be read-only)? |
| 9449 | if (intoAnArray) { |
| 9450 | result.push({ name: element.name, value: option.value }); |
| 9451 | } else { // into a string |
| 9452 | result.push(`${encodeURIComponent(element.name)}=${encodeURIComponent(option.value)}`); |
| 9453 | } |
| 9454 | } |
| 9455 | }); |
| 9456 | } else if (['checkbox', 'radio'].indexOf(element.type) === -1 || element.checked) { |
| 9457 | let valueResult; |
| 9458 | if (this.isManagedByAutoNumeric(element)) { |
| 9459 | let anObject; |
| 9460 | switch (formatType) { |
| 9461 | case 'unformatted': |
| 9462 | anObject = this.getAutoNumericElement(element); |
| 9463 | if (!AutoNumericHelper.isNull(anObject)) { |
| 9464 | valueResult = this.unformat(element, anObject.getSettings()); |
| 9465 | } |
| 9466 | |
| 9467 | break; |
| 9468 | case 'localized': |
| 9469 | anObject = this.getAutoNumericElement(element); |
| 9470 | if (!AutoNumericHelper.isNull(anObject)) { |
| 9471 | // Here I need to clone the setting object, otherwise I would modify it when changing the `outputFormat` option value |
| 9472 | const currentSettings = AutoNumericHelper.cloneObject(anObject.getSettings()); |
| 9473 | if (!AutoNumericHelper.isNull(forcedOutputFormat)) { |
| 9474 | currentSettings.outputFormat = forcedOutputFormat; |
| 9475 | } |
| 9476 | |
| 9477 | valueResult = this.localize(element, currentSettings); |
| 9478 | } |
| 9479 | |
| 9480 | break; |
| 9481 | case 'formatted': |
| 9482 | default: |
| 9483 | valueResult = element.value; |
| 9484 | } |
| 9485 | } else { |
| 9486 | valueResult = element.value; |
| 9487 | } |
| 9488 | |
| 9489 | if (AutoNumericHelper.isUndefined(valueResult)) { |
| 9490 | AutoNumericHelper.throwError('This error should never be hit. If it has, something really wrong happened!'); |
| 9491 | } |
| 9492 | |
| 9493 | if (intoAnArray) { |
| 9494 | result.push({ name: element.name, value: valueResult }); |
no test coverage detected