Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track and remove nested hidden fields (#5805)
Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Jason Varga <jason@pixelfear.com>
- Loading branch information
1 parent
5437106
commit 4b6dd3c
Showing
25 changed files
with
460 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { clone } from '../../bootstrap/globals.js' | ||
|
||
export default class { | ||
constructor(values, jsonFields) { | ||
this.values = clone(values); | ||
|
||
this.jsonFields = clone(jsonFields || []) | ||
.filter((field, index) => jsonFields.indexOf(field) === index) | ||
.sort(); | ||
} | ||
|
||
omit(hiddenKeys) { | ||
this.jsonDecode() | ||
.omitHiddenFields(hiddenKeys) | ||
.jsonEncode(); | ||
|
||
return this.values; | ||
} | ||
|
||
jsonDecode() { | ||
this.jsonFields.forEach(dottedKey => { | ||
this.jsonDecodeValue(dottedKey); | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
omitHiddenFields(hiddenKeys) { | ||
hiddenKeys.forEach(dottedKey => { | ||
this.forgetValue(dottedKey); | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
jsonEncode() { | ||
clone(this.jsonFields).reverse().forEach(dottedKey => { | ||
this.jsonEncodeValue(dottedKey); | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
dottedKeyToJsPath(dottedKey) { | ||
return dottedKey.replace(/\.*(\d+)\./g, '[$1].'); | ||
} | ||
|
||
missingValue(dottedKey) { | ||
var properties = Array.isArray(dottedKey) ? dottedKey : dottedKey.split('.'); | ||
var value = properties.reduce((prev, curr) => prev && prev[curr], clone(this.values)); | ||
|
||
return value === undefined; | ||
} | ||
|
||
jsonDecodeValue(dottedKey) { | ||
if (this.missingValue(dottedKey)) return; | ||
|
||
let values = clone(this.values); | ||
let jsPath = this.dottedKeyToJsPath(dottedKey); | ||
let fieldValue = eval('values.' + jsPath); | ||
let decodedFieldValue = JSON.parse(fieldValue); | ||
|
||
eval('values.' + jsPath + ' = decodedFieldValue'); | ||
|
||
this.values = values; | ||
} | ||
|
||
jsonEncodeValue(dottedKey) { | ||
if (this.missingValue(dottedKey)) return; | ||
|
||
let values = clone(this.values); | ||
let jsPath = this.dottedKeyToJsPath(dottedKey); | ||
let fieldValue = eval('values.' + jsPath); | ||
let encodedFieldValue = JSON.stringify(fieldValue); | ||
|
||
eval('values.' + jsPath + ' = encodedFieldValue'); | ||
|
||
this.values = values; | ||
} | ||
|
||
forgetValue(dottedKey) { | ||
if (this.missingValue(dottedKey)) return; | ||
|
||
let values = clone(this.values); | ||
let jsPath = this.dottedKeyToJsPath(dottedKey); | ||
|
||
eval('delete values.' + jsPath); | ||
|
||
this.values = values; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.