open-vault/ui/app/utils/field-to-attrs.js
Matthew Irish 23778935b0
UI - make engine list more consistent with the auth method list (#4598)
* remove expanding behavior from engines list and add a configuration route

* use page header component, secret tab component for the template on the secret engine configuration route

* move abstraction to secret-list-header and remove secret-tabs

* add attrs to secret engine model and adjust mount controller code to support that

* fix top level nav so that we can use the back button properly

* fix tests
2018-05-23 11:25:52 -05:00

105 lines
2.8 KiB
JavaScript

import Ember from 'ember';
/*
*
* @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 => Ember.expandProperties(field, x => fields.push(x)));
let attributeMap = map || new Map();
modelClass.eachAttribute((name, meta) => {
let fieldName = namePrefix ? namePrefix + name : name;
let maybeFragment = Ember.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 };
});
}