cspace-ui
Version:
CollectionSpace user interface for browsers
97 lines (91 loc) • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getColumnConfig = getColumnConfig;
exports.isSortable = isSortable;
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
};
}
/**
* Matches a sortBy field path that indexes into a repeating field, e.g. a subfield of a repeating
* group (".../titleGroupList/0/title") or an element of a repeating scalar (".../owners/0").
*/
const complexFieldPattern = /\/\d+(\/|$)/;
/**
* Determines if a column is sortable for a given search. A column is sortable if sortBy is truthy,
* and the search is not constrained by a related record, or if it is, the field to sort by is not
* complex. This is here to deal with DRYD-2136 (searches with related record constraints are
* done using CMIS, which can't see into complex fields). If that bug is ever fixed, then it will
* suffice just to check sortBy.
*
* @param {object} column A column configuration, expected to have a sortBy property
* @param {object} searchDescriptor The search descriptor for the current search
* @returns {boolean} true if the column may be sorted for this search
*/
function isSortable(column, searchDescriptor) {
const {
sortBy
} = column;
return !!(sortBy && (!searchDescriptor.getIn(['searchQuery', 'rel']) || !complexFieldPattern.test(sortBy)));
}