-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
form-data-json.js
759 lines (700 loc) · 23.3 KB
/
form-data-json.js
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/**
* Form Data Json Converter
* @link https://github.com/brainfoolong/form-data-json
* @licence MIT
*/
class FormDataJson {
/**
* Default options for toJson()
* @type {{}}
*/
static defaultOptionsToJson = {
/**
* Include all disabled field values
* @type {boolean}
*/
'includeDisabled': false,
/**
* Include all button field values
* @type {boolean}
*/
'includeButtonValues': false,
/**
* Include all unchecked radio/checkboxes as given value when they are unchecked
* If undefined, than the unchecked field will be ignored in output
* @type {*}
*/
'uncheckedValue': undefined,
/**
* A function, where first parameter is the input field to check for
* Must return true if the field should be included
* All other return values, as well as no return value, will skip the input field in the progress
* @type {function|null}
*/
'inputFilter': null,
/**
* Does return an array list, with same values as native Array.from(new FormData(form))
* A list will look like [["inputName", "inputValue"], ["inputName", "inputValue"]]
* The input name will not be changed and the list can contain multiple equal names
* @type {boolean}
*/
'flatList': false,
/**
* If true, then this does skip empty fields from the output
* Empty is considered to be:
* An empty array (for multiple selects/checkboxes)
* An empty input field (length = 0 or null)
* This does recursively remove keys
* Example: {"agb":"1", "user" : [{"name" : ""},{"name" : ""}]} will be {"agb":"1"}
* @type {boolean}
*/
'skipEmpty': false,
/**
* A function that will be called when all file fields are read as base64 data uri
* First passed parameter to this function are the form values including all file data
* Note: If set, the original return value from toJson() returns null
* @type {function|null}
*/
'filesCallback': null,
/**
* By default, files are read as base64 data url
* Possible values are: readAsDataURL, readAsBinaryString, readAsText, readAsArrayBuffer
* @type {string}
*/
'fileReaderFunction': 'readAsDataURL',
/**
* If true than values try to be a real Array instead of Object where possible
* If false than all values that are multiple (multiple select, same input names checkboxes, unnamed array indexes, etc...) will be objects
* @type {boolean}
*/
'arrayify': true
}
/**
* Default options for fromJson()
* @type {{}}
*/
static defaultOptionsFromJson = {
/**
* Does expect the given values are in a flatList, previously exported with toJson
* Instead of the default bevhiour with nested objects
* @type {boolean}
*/
'flatList': false,
/**
* If true, than all fields that are not exist in the passed values object, will be cleared/emptied
* Not exist means, the value must be undefined
* @type {boolean}
*/
'clearOthers': false,
/**
* If true, than all fields that are not exist in the passed values object, will be reset
* Not exist means, the value must be undefined
* @type {boolean}
*/
'resetOthers': false,
/**
* If true, when a fields value has changed, a "change" event will be fired
* @type {boolean}
*/
'triggerChangeEvent': false
}
/**
* All input types that are buttons
* @type {string[]}
*/
static buttonInputTypes = ['button', 'submit', 'reset', 'image']
/**
* All input types that have a checked status
* @type {string[]}
*/
static checkedInputTypes = ['checkbox', 'radio']
/**
* Get values from all form elements inside the given element
* @param {*} el
* @param {Object=} options
* @return {Object|Array}
*/
static toJson (el, options) {
options = FormDataJson.merge(FormDataJson.defaultOptionsToJson, options)
/**
* Check if given input is valid for given filters based on given options
* @param {HTMLElement} input
* @return {boolean}
*/
function isValidInput (input) {
// filter elements by input filter
if (typeof options.inputFilter === 'function' && options.inputFilter(input) !== true) {
return false
}
// ignore disabled fields
if (!options.includeDisabled && input.disabled) {
return false
}
const inputType = (input.type || 'text').toLowerCase()
// ignore button values
if (!options.includeButtonValues && (input instanceof HTMLButtonElement || FormDataJson.buttonInputTypes.indexOf(inputType) > -1)) {
return false
}
// ignore unchecked fields when no value is given
if (typeof options.uncheckedValue === 'undefined' && FormDataJson.checkedInputTypes.indexOf(inputType) > -1 && !input.checked) {
return false
}
return true
}
const tree = FormDataJson.getFieldTree(el, isValidInput)
let returnObject = options.flatList ? [] : {}
const files = []
/**
* Recursive get values
* @param {Object} inputs
* @param {Object} values
*/
function recursiveGetInputValues (inputs, values) {
for (let key in inputs) {
const row = inputs[key]
const objectKey = options.flatList ? row.name : key
// next level
if (row.type === 'nested') {
if (options.flatList) {
recursiveGetInputValues(row.tree, values)
} else {
values[key] = {}
recursiveGetInputValues(row.tree, values[key])
}
continue
}
const input = row.input
const inputType = row.inputType
if (inputType === 'file') {
if (options.filesCallback) {
files.push({ 'object': values, 'key': objectKey, 'input': input })
}
continue
}
let value = null
if (row.type === 'radio') {
for (let i = 0; i < row.inputs.length; i++) {
const radioInput = row.inputs[i]
if (radioInput.checked) {
value = radioInput.value
break
}
}
if (value === null) {
value = options.uncheckedValue
}
} else if (inputType === 'checkbox') {
value = input.checked ? input.value : options.uncheckedValue
} else if (input instanceof HTMLSelectElement) {
let arr = []
for (let i = 0; i < input.options.length; i++) {
let option = input.options[i]
if (option.selected) {
arr.push((typeof option.value !== 'undefined' ? option.value : option.text).toString())
}
}
if (input.multiple) {
value = arr
} else {
value = arr.length ? arr[0] : null
}
} else {
value = input.value
}
if (options.flatList) {
values.push([row.name, value])
} else {
values[objectKey] = value
}
}
}
/**
* Make an object to array if possible
* @param {Object} object
* @return {*}
* @private
*/
function arrayify (object) {
if (FormDataJson.isObject(object)) {
let count = 0
let valid = true
for (let key in object) {
if (FormDataJson.isObject(object[key]) && !(object[key] instanceof Element)) {
object[key] = arrayify(object[key])
}
if (parseInt(key) !== count) {
valid = false
}
count++
}
if (valid) {
let arr = []
for (let i in object) {
arr.push(object[i])
}
return arr
}
}
return object
}
/**
* Does some final cleanup before output data
* @return {*}
*/
function output () {
if (options.arrayify) {
returnObject = arrayify(returnObject)
}
if (options.skipEmpty) returnObject = removeEmpty(returnObject) || (options.flatList ? [] : {})
return returnObject
}
/**
* Recursively remove empty keys
* @param {Object} object
* @param {number} depth
* @return {Object|Array}
*/
function removeEmpty (object, depth) {
const isArray = FormDataJson.isArray(object)
let newObject = isArray ? [] : {}
let count = 0
for (let key in object) {
let value = object[key]
if (options.flatList && !depth) {
value = value[1]
}
if (value === null || value === undefined || value === '') {
continue
}
if (FormDataJson.isObject(value) || FormDataJson.isArray(value)) {
value = removeEmpty(value, (depth || 0) + 1) || ''
}
if (typeof value !== 'object' && FormDataJson.stringify(value) === '') {
continue
}
if (isArray) {
newObject.push(options.flatList && !depth ? [object[key][0], value] : value)
} else {
newObject[key] = value
}
count++
}
return count ? newObject : null
}
recursiveGetInputValues(tree, returnObject)
if (files.length) {
let filesDone = 0
let filesRequired = 0
for (let i = 0; i < files.length; i++) {
let row = files[i]
let useObject = row.object
filesRequired += row.input.files.length
for (let j = 0; j < row.input.files.length; j++) {
let file = row.input.files[j]
let reader = new FileReader()
reader.onload = function () {
if (row.input.multiple) {
if (!FormDataJson.isArray(useObject[row.key])) {
useObject[row.key] = []
}
useObject[row.key].push(reader.result)
} else {
useObject[row.key] = reader.result
}
filesDone++
if (filesDone === filesRequired) {
options.filesCallback(output())
}
}
reader[options.fileReaderFunction](file)
}
}
return null
} else if (options.filesCallback) {
options.filesCallback(output())
return null
}
return output()
}
/**
* Fill given form values into all form elements inside given element
* @param {*} el
* @param {Object|Array} values
* @param {Object=} options
* @param {string=} keyPrefix Internal only
*/
static fromJson (el, values, options, keyPrefix) {
if (!FormDataJson.isObject(values) && !FormDataJson.isArray(values)) return
options = FormDataJson.merge(FormDataJson.defaultOptionsFromJson, options)
const tree = FormDataJson.getFieldTree(el)
const lastUsedFlatListIndex = {}
if (options.clearOthers) {
FormDataJson.clear(el)
}
if (options.resetOthers) {
FormDataJson.reset(el)
}
/**
* Recursive set values
* @param {*} inputs
* @param {*} newValues
*/
function resursiveSetInputValues (inputs, newValues) {
if (!FormDataJson.isObject(newValues) && !FormDataJson.isArray(newValues)) return
for (let key in inputs) {
const row = inputs[key]
const objectKey = options.flatList ? row.name : key
// next level
if (row.type === 'nested') {
if (options.flatList) {
resursiveSetInputValues(row.tree, newValues)
} else if (typeof newValues[objectKey] === 'object') {
// check for a very special case of array checkbox values using autoIncrement names and value to set is given an array
// the array contains the values to be checked in a random order, that's because we need to check this specially
const newSubValues = newValues[objectKey]
let newTree = row.tree
if (FormDataJson.isArray(newSubValues)) {
newTree = {}
for (let subTreeKey in row.tree) {
const subRow = row.tree[subTreeKey]
if (subRow.autoIncrementInputs) {
if (subRow.input.type.toLowerCase() === 'checkbox' && newSubValues.indexOf(subRow.input.value) > -1) {
subRow.input.checked = true
continue
}
}
newTree[subTreeKey] = subRow
}
}
resursiveSetInputValues(newTree, newSubValues)
}
continue
}
// flat list must search correct entry for given input name
if (options.flatList) {
for (let i in newValues) {
const value = newValues[i]
if (value && value[0] === row.name) {
if (lastUsedFlatListIndex[row.name] !== i) {
lastUsedFlatListIndex[row.name] = i
FormDataJson.setInputValue(row, value[1], options.triggerChangeEvent)
break
}
}
}
continue
}
// skip fields that are not presented in the value list
if (typeof newValues[objectKey] === 'undefined') {
continue
}
FormDataJson.setInputValue(row, newValues[objectKey] || null, options.triggerChangeEvent)
}
}
resursiveSetInputValues(tree, values)
}
/**
* Reset all input fields in the given element to their default values
* @param {*} el
*/
static reset (el) {
const tree = FormDataJson.getFieldTree(el)
/**
* Recursive reset
* @param {*} inputs
*/
function recursiveResetInputValues (inputs) {
for (let key in inputs) {
const row = inputs[key]
// next level
if (row.type === 'nested') {
recursiveResetInputValues(row.tree)
continue
}
if (row.type === 'radio') {
for (let i = 0; i < row.inputs.length; i++) {
const radioInput = row.inputs[i]
radioInput.checked = radioInput.defaultChecked
}
continue
}
// ignore button elements, as reset would reset button labels, which is mostly not that what anybody want
if (row.inputType && FormDataJson.buttonInputTypes.indexOf(row.inputType) > -1) {
continue
}
const input = row.input
if (FormDataJson.checkedInputTypes.indexOf(row.inputType) > -1) {
input.checked = input.defaultChecked
} else if (input instanceof HTMLSelectElement) {
const options = input.querySelectorAll('option')
for (let i = 0; i < options.length; i++) {
const option = options[i]
option.selected = option.defaultSelected
}
} else if (input.getAttribute('value')) {
input.value = input.getAttribute('value')
} else if (typeof input.defaultValue === 'string' || typeof input.defaultValue === 'number') {
input.value = input.defaultValue
}
}
}
recursiveResetInputValues(tree)
}
/**
* Clear all input fields (to empty, unchecked, unselected) in the given element
* @param {*} el
*/
static clear (el) {
const tree = FormDataJson.getFieldTree(el)
/**
* Recursive clear
* @param {*} inputs
*/
function recursiveClearInputValues (inputs) {
for (let key in inputs) {
const row = inputs[key]
// next level
if (row.type === 'nested') {
recursiveClearInputValues(row.tree)
continue
}
if (row.input) {
// ignore button elements, as clear would unset button labels, which is mostly not that what anybody want
if (FormDataJson.buttonInputTypes.indexOf(row.inputType) > -1) {
continue
}
}
FormDataJson.setInputValue(row, null)
}
}
recursiveClearInputValues(tree)
}
/**
* Set input value
* @param {*} row
* @param {*|null} newValue Null will unset the value
* @param {boolean=} triggerChangeEvent
* @private
*/
static setInputValue (row, newValue, triggerChangeEvent) {
const triggerChange = triggerChangeEvent ? function (el) {
let ev = null
if (typeof (Event) === 'function') {
ev = new Event('change', { 'bubbles': true })
} else {
ev = document.createEvent('Event')
ev.initEvent('change', true, true)
}
el.dispatchEvent(ev)
} : null
if (row.type === 'radio') {
let changed = []
for (let i = 0; i < row.inputs.length; i++) {
const radioInput = row.inputs[i]
if (radioInput.checked) changed.push(radioInput)
radioInput.checked = false
if (newValue !== null && FormDataJson.stringify(radioInput.value) === FormDataJson.stringify(newValue)) {
if (!radioInput.checked) changed.push(radioInput)
radioInput.checked = true
break
}
}
if (triggerChange) {
for (let i in changed) {
triggerChange(changed[i])
}
}
return
}
const input = row.input
const inputType = row.inputType
// ignore file input, they cannot be set
if (inputType === 'file') {
return
}
let changed = false
if (inputType === 'checkbox') {
newValue = newValue === true || (newValue !== null && FormDataJson.stringify(input.value) === FormDataJson.stringify(newValue))
if (newValue !== input.checked) changed = true
input.checked = newValue
} else if (input instanceof HTMLSelectElement) {
let newValueArr = newValue
if (newValueArr === null || newValueArr === undefined) {
newValueArr = []
} else if (FormDataJson.isObject(newValueArr)) {
newValueArr = Object.values(newValueArr)
} else if (!FormDataJson.isArray(newValueArr)) {
newValueArr = [newValueArr]
}
for (let i = 0; i < input.options.length; i++) {
const option = input.options[i]
const optionValue = (typeof option.value !== 'undefined' ? option.value : option.text).toString()
if (option.selected !== false) changed = true
option.selected = false
for (let j = 0; j < newValueArr.length; j++) {
if (optionValue === FormDataJson.stringify(newValueArr[j])) {
if (option.selected !== true) changed = true
option.selected = true
break
}
}
}
} else {
if (input.value !== newValue) changed = true
input.value = newValue
}
if (changed && triggerChange) {
triggerChange(input)
}
}
/**
* Convert any value to a string
* A object/array/undefined will be an ampty string
* Boolean will be 1 or 0
* @param {*} value
* @private
*/
static stringify (value) {
if (value === undefined) return ''
if (typeof value === 'object') return ''
if (typeof value === 'boolean') return value ? '1' : '0'
return value + ''
}
/**
* Get all input fields as a multidimensional tree, as it would later appear in json data, but with input elements as values
* @param {*} el
* @param {function=} isValidInput A function to check for valid input
* @return {Object}
* @private
*/
static getFieldTree (el, isValidInput) {
el = FormDataJson.getElement(el)
if (!el) {
return []
}
let inputs = el.querySelectorAll('select, textarea, input, button')
let inputTree = {}
let autoIncrementCounts = {}
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i]
if (!input.name || input.name.length === 0) continue
if (isValidInput && isValidInput(input) !== true) continue
const inputType = (input.type || 'text').toLowerCase()
let name = input.name
const keyParts = input.name.replace(/\]/g, '').split('[')
const isAutoIncrement = name.substr(-2) === '[]'
if (isAutoIncrement) {
if (input instanceof HTMLSelectElement && input.multiple) {
// special for multiple selects, we skip the last empty part to fix double nested arrays
keyParts.pop()
} else if (inputType === 'radio') {
// special for radio buttons, as they group multiple inputs to the same name
// so a radio could never be an auto increment array name
keyParts.pop()
}
}
const keyPartsLength = keyParts.length
let useObject = inputTree
let currentName = ''
for (let j = 0; j < keyPartsLength; j++) {
let keyPart = keyParts[j]
let newCurrentName = currentName ? currentName + '[' + keyPart + ']' : keyPart
// auto increment key part
if (keyPart === '') {
if (typeof autoIncrementCounts[newCurrentName] === 'undefined') {
autoIncrementCounts[newCurrentName] = 0
}
keyPart = autoIncrementCounts[newCurrentName].toString()
autoIncrementCounts[newCurrentName]++
}
currentName = currentName ? currentName + '[' + keyPart + ']' : keyPart
// last level
if (keyPartsLength - 1 === j) {
// radio elements are special
if ((input.type || 'text').toLowerCase() === 'radio') {
if (typeof useObject[keyPart] === 'undefined') {
useObject[keyPart] = {
'type': 'radio',
'name': input.name,
'inputType': inputType,
'inputs': [],
'autoIncrementInputs': 0
}
}
if (isAutoIncrement) useObject[keyPart].autoIncrementInputs = 1
useObject[keyPart].inputs.push(input)
} else {
useObject[keyPart] = {
'type': 'default',
'name': input.name,
'inputType': inputType,
'input': input,
'autoIncrementInputs': isAutoIncrement ? 1 : 0
}
}
} else {
// it could be possible, then really weird a non-standard conform input names result in already
// existing data which we need to override here, for which the check to .tree is for
if (typeof useObject[keyPart] === 'undefined' || typeof useObject[keyPart].tree === 'undefined') {
useObject[keyPart] = { 'type': 'nested', 'tree': {} }
}
useObject = useObject[keyPart].tree
}
}
}
return inputTree
}
/**
* Check if arg is a real object (not null) and no array
* @param {*} arg
* @return {boolean}
* @private
*/
static isObject (arg) {
return arg && typeof arg === 'object' && Object.prototype.toString.call(arg) !== '[object Array]'
}
/**
* Check if arg is arr
* @param {*} arg
* @return {boolean}
* @private
*/
static isArray (arg) {
return Array.isArray(arg)
}
/**
* Get html element out of given parameter
* @param {HTMLElement|JQuery|$|string} param
* @return {HTMLElement|null}
* @private
*/
static getElement (param) {
if (typeof param === 'string') return document.querySelector(param)
if (param instanceof HTMLElement) return param
if (typeof jQuery !== 'undefined' && param instanceof jQuery) return param[0]
if (typeof $ !== 'undefined' && param instanceof $) return param[0]
console.warn('FormDataJson: Unsupported element passed. Supported is HTMLElement, a string query selector, JQuery or $ object')
return null
}
/**
* Merge from left to right and return a new object
* @param {Object} a
* @param {Object} b
* @return {Object}
* @private
*/
static merge (a, b) {
let c = {}
for (let key in a) {
c[key] = a[key]
}
for (let key in b) {
c[key] = b[key]
}
return c
}
}
// module exports
if (typeof module !== 'undefined' && module.exports) {
module.exports = FormDataJson
}