cspace-ui
Version:
CollectionSpace user interface for browsers
72 lines (68 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getColumnConfig = getColumnConfig;
exports.readListItems = readListItems;
var _immutable = _interopRequireDefault(require("immutable"));
var _get = _interopRequireDefault(require("lodash/get"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// todo: where should this live?
/**
* Get the column configuration from the main config. This uses the search descriptor in order
* to read the recordType or subresource which we're trying to display columnar data for. This
* is derived from the columns.js files for each procedure.
*
* @param {object} config The cspace config
* @param {object} searchDescriptor The search descriptor for the current search
* @param {string} columnSetName The column set, e.g. default, narrow, etc
* @returns The configuration for the column set
*/
function getColumnConfig(config, searchDescriptor, columnSetName) {
const recordType = searchDescriptor.get('recordType');
const subresource = searchDescriptor.get('subresource');
const columnConfigurer = subresource ? config.subresources[subresource] : config.recordTypes[recordType];
let columnConfig = (0, _get.default)(columnConfigurer, ['columns', columnSetName]);
if (!columnConfig && columnSetName !== 'default') {
// Fall back to the default column set if the named one doesn't exist.
columnConfig = (0, _get.default)(columnConfigurer, ['columns', 'default']);
}
if (!columnConfig) {
columnConfig = [];
}
return columnConfig;
}
/**
* Extract the search result list items from a given search result.
*
* @param {*} config The cspace config
* @param {*} listType The listType, e.g. abstract-common-list
* @param {*} searchResult The response object from the search
* @returns An Immutable.List containing the items for a search
*/
function readListItems(config, listType, searchResult) {
if (!searchResult) {
return {
list: null,
items: null
};
}
const listTypeConfig = config.listTypes[listType];
const {
listNodeName,
itemNodeName
} = listTypeConfig;
const list = searchResult.get(listNodeName) || _immutable.default.Map();
let items = list.get(itemNodeName);
if (!items) {
items = _immutable.default.List();
}
if (!_immutable.default.List.isList(items)) {
// If there's only one result, it won't be returned as a list.
items = _immutable.default.List.of(items);
}
return {
list,
items
};
}