dtable-utils
Version:
dtable common utils
152 lines (140 loc) • 4.76 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var selectOption = require('../constants/select-option.js');
/**
* Get options from single-select/multiple-select column.
* @param {object} column e.g. { type, data: { options: [] } }
* @returns options, array
*/
var getColumnOptions = function getColumnOptions(column) {
if (!column || !column.data || !Array.isArray(column.data.options)) {
return [];
}
return column.data.options;
};
/**
* generate unique option id
* @param {array} options e.g. [{ id, ... }, ...]
* @returns generated option id, string
*/
var generateOptionID = function generateOptionID(options) {
if (options.length === 1) return String(Math.floor(Math.random() * Math.pow(10, 6)));
var optionID;
var isIDUnique = false;
while (!isIDUnique) {
optionID = String(Math.floor(Math.random() * Math.pow(10, 6)));
// eslint-disable-next-line
isIDUnique = options.every(function (option) {
return option.id !== optionID;
});
if (isIDUnique) {
break;
}
}
return optionID;
};
var getRandomOptionColor = function getRandomOptionColor(options) {
var defaultOptions = selectOption.SELECT_OPTION_COLORS.slice(12, 24);
var colorIdx = Math.floor(Math.random() * defaultOptions.length);
if (!Array.isArray(options) || options.length === 0) {
return defaultOptions[colorIdx];
}
// Avoid using the same color for adjacent labels
var adjacentOptions = options.slice(-(defaultOptions.length - 1));
var adjacentOptionsColorIdxArr = [];
var selectOptionColorObj = {};
defaultOptions.forEach(function (colorItem, index) {
selectOptionColorObj[colorItem.COLOR] = index;
});
adjacentOptions.forEach(function (option) {
var optionColorIdx = selectOptionColorObj[option.color];
adjacentOptionsColorIdxArr.push(optionColorIdx);
});
// eslint-disable-next-line
while (adjacentOptionsColorIdxArr.indexOf(colorIdx) != -1) {
colorIdx = Math.floor(Math.random() * defaultOptions.length);
}
return defaultOptions[colorIdx] || defaultOptions[0];
};
/**
* generate option
* @param {array} options e.g. [{ id, ... }, ...]
* @param {string} optionName
* @param {string} optionColor used to find system support color options. The new color option will be generated if not found by "optionColor" or not supported
* @returns generated option, object
*/
var createOption = function createOption(options, optionName) {
var optionColor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var id = generateOptionID(options);
var colors = optionColor && selectOption.SELECT_OPTION_COLORS.find(function (systemColor) {
return systemColor.COLOR === optionColor;
});
if (!colors) {
colors = getRandomOptionColor(options);
}
return {
id: id,
name: optionName,
color: colors.COLOR,
textColor: colors.TEXT_COLOR
};
};
/**
* Generate cell option by name.
* @param {array} options e.g. [{ id, ... }, ...]
* @param {string} optionName used as the option name
* @returns return the option id if exist, otherwise generate a new option, object
*/
var generatorCellOption = function generatorCellOption(options, optionName) {
var existOption = options.find(function (option) {
return option.name === optionName;
});
if (existOption) {
return {
selectedOptionId: existOption.id
};
}
var newOption = createOption(options, optionName) || {};
return {
cellOption: newOption,
selectedOptionId: newOption.id
};
};
/**
* Generate cell options by names.
* @param {array} options e.g. [{ id, ... }, ...]
* @param {array} optionNames used as the options names
* @returns Return the options ids if exist, otherwise generate new options, object
*/
var generatorCellOptions = function generatorCellOptions(options, optionNames) {
var cellOptions = [];
var selectedOptionIds = [];
optionNames.forEach(function (optionName) {
var existingOption = options.find(function (option) {
return option.name === optionName;
});
if (existingOption) {
selectedOptionIds.push(existingOption.id);
} else {
var cellOption = createOption(options, optionName);
if (cellOption) {
cellOptions.push(cellOption);
selectedOptionIds.push(cellOption.id);
}
}
});
if (cellOptions.length === 0) {
return {
selectedOptionIds: selectedOptionIds
};
}
return {
cellOptions: cellOptions,
selectedOptionIds: selectedOptionIds
};
};
exports.createOption = createOption;
exports.generateOptionID = generateOptionID;
exports.generatorCellOption = generatorCellOption;
exports.generatorCellOptions = generatorCellOptions;
exports.getColumnOptions = getColumnOptions;