@lyra/default-layout
Version:
The default layout components for Lyra
64 lines (47 loc) • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = getOrderedTools;
var _defaultLayout = require('config:@lyra/default-layout');
var _defaultLayout2 = _interopRequireDefault(_defaultLayout);
var _tool = require('all:part:@lyra/base/tool');
var _tool2 = _interopRequireDefault(_tool);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getOrderedTools() {
const config = _defaultLayout2.default.toolSwitcher || {};
const order = config.order || [];
const hidden = config.hidden || [];
if (!order.length && !hidden.length) {
return _tool2.default;
}
const keyed = _tool2.default.reduce((target, tool) => {
const title = tool.title || '<unknown>';
if (!tool.name) {
console.warn(`Tool "${title}" does not have the required "name"-property`); // eslint-disable-line no-console
return target;
}
if (target[tool.name]) {
const existing = target[tool.name].tool.title;
console.warn(`Tools with duplicate name "${tool.name}" found ("${title}" and "${existing}")`); // eslint-disable-line no-console
return target;
}
const toolIndex = order.indexOf(tool.name);
target[tool.name] = {
tool: tool,
index: toolIndex === -1 ? +Infinity : toolIndex
};
return target;
}, {});
const isVisible = tool => hidden.indexOf(tool.name) === -1;
return _tool2.default.filter(isVisible).sort((tool1, tool2) => {
const toolA = keyed[tool1.name];
const toolB = keyed[tool2.name];
const indexA = toolA ? toolA.index : +Infinity;
const indexB = toolB ? toolB.index : +Infinity;
if (indexA === indexB) {
return 0;
}
return indexA - indexB;
});
}