open-vault/ui/app/utils/field-to-attrs.js
Jordan Reimer 5c2a08de6d
Ember Upgrade to 3.24 (#13443)
* Update browserslist

* Add browserslistrc

* ember-cli-update --to 3.26, fix conflicts

* Run codemodes that start with ember-*

* More codemods - before cp*

* More codemods (curly data-test-*)

* WIP ember-basic-dropdown template errors

* updates ember-basic-dropdown and related deps to fix build issues

* updates basic dropdown instances to new version API

* updates more deps -- ember-template-lint is working again

* runs no-implicit-this codemod

* creates and runs no-quoteless-attributes codemod

* runs angle brackets codemod

* updates lint:hbs globs to only touch hbs files

* removes yield only templates

* creates and runs deprecated args transform

* supresses lint error for invokeAction on LinkTo component

* resolves remaining ambiguous path lint errors

* resolves simple-unless lint errors

* adds warnings for deprecated tagName arg on LinkTo components

* adds warnings for remaining curly component invocation

* updates global template lint rules

* resolves remaining template lint errors

* disables some ember specfic lint rules that target pre octane patterns

* js lint fix run

* resolves remaining js lint errors

* fixes test run

* adds npm-run-all dep

* fixes test attribute issues

* fixes console acceptance tests

* fixes tests

* adds yield only wizard/tutorial-active template

* fixes more tests

* attempts to fix more flaky tests

* removes commented out settled in transit test

* updates deprecations workflow and adds initializer to filter by version

* updates flaky policies acl old test

* updates to flaky transit test

* bumps ember deps down to LTS version

* runs linters after main merge

* fixes client count tests after bad merge conflict fixes

* fixes client count history test

* more updates to lint config

* another round of hbs lint fixes after extending stylistic rule

* updates lint-staged commands

* removes indent eslint rule since it seems to break things

* fixes bad attribute in transform-edit-form template

* test fixes

* fixes enterprise tests

* adds changelog

* removes deprecated ember-concurrency-test-waiters dep and adds @ember/test-waiters

* flaky test fix

Co-authored-by: hashishaw <cshaw@hashicorp.com>
2021-12-16 20:44:29 -07:00

105 lines
2.9 KiB
JavaScript

import { get } from '@ember/object';
import { expandProperties } from '@ember/object/computed';
/*
*
* @param modelClass DS.Model
* @param attributeNames Array[String]
* @param prefixName String
* @param map Map
* @returns Array[Object]
*
* A function that takes a model and an array of attributes
* and expands them in-place to an array of metadata about the attributes
*
* if passed a Model with attributes `foo` and `bar` and the array ['foo', 'bar']
* the returned array would take the form of:
*
* [
* {
* name: 'foo',
* type: 'string',
* options: {
* defaultValue: 'Foo'
* }
* },
* {
* name: 'bar',
* type: 'string',
* options: {
* defaultValue: 'Bar',
* editType: 'textarea',
* label: 'The Bar Field'
* }
* },
* ]
*
*/
export const expandAttributeMeta = function (modelClass, attributeNames, namePrefix, map) {
let fields = [];
// expand all attributes
attributeNames.forEach((field) => expandProperties(field, (x) => fields.push(x)));
let attributeMap = map || new Map();
modelClass.eachAttribute((name, meta) => {
let fieldName = namePrefix ? namePrefix + name : name;
let maybeFragment = get(modelClass, fieldName);
if (meta.isFragment && maybeFragment) {
// pass the fragment and all fields that start with
// the fragment name down to get extracted from the Fragment
expandAttributeMeta(
maybeFragment,
fields.filter((f) => f.startsWith(fieldName)),
fieldName + '.',
attributeMap
);
return;
}
attributeMap.set(fieldName, meta);
});
// we have all of the attributes in the map now,
// so we'll replace each key in `fields` with the expanded meta
fields = fields.map((field) => {
let meta = attributeMap.get(field);
if (meta) {
var { type, options } = meta;
}
return {
// using field name here because it is the full path,
// name on the attribute meta will be relative to the fragment it's on
name: field,
type: type,
options: options,
};
});
return fields;
};
/*
*
* @param modelClass DS.Model
* @param fieldGroups Array[Object]
* @returns Array
*
* A function meant for use on an Ember Data Model
*
* The function takes a array of groups, each group
* being a list of attributes on the model, for example
* `fieldGroups` could look like this
*
* [
* { default: ['commonName', 'format'] },
* { Options: ['altNames', 'ipSans', 'ttl', 'excludeCnFromSans'] },
* ]
*
* The array will get mapped over producing a new array with each attribute replaced with that attribute's metadata from the attr declaration
*/
export default function (modelClass, fieldGroups) {
return fieldGroups.map((group) => {
const groupKey = Object.keys(group)[0];
const fields = expandAttributeMeta(modelClass, group[groupKey]);
return { [groupKey]: fields };
});
}