tav-ui
Version:
143 lines (136 loc) • 4.51 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var pinyin = require('js-pinyin');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var pinyin__default = /*#__PURE__*/_interopDefaultLegacy(pinyin);
function getLetter(name, idx = 0) {
if (!name)
return "Z";
if (name.indexOf("\u91CD\u5E86") > -1)
return "C";
return pinyin__default["default"].getCamelChars(name)[idx];
}
function createGroupOptions(level) {
const cache = [];
for (let i = 0; i < level; i++) {
cache[i] = [];
}
return cache;
}
const DEFAULT_CONFIG = {
name: "name",
id: "id",
children: "children",
pid: "pid"
};
const getConfig = (config) => Object.assign({}, DEFAULT_CONFIG, config);
function handleOptions(options, keyConfig, fieldLength, isFirstLetterVisible) {
const treeOptions = [];
const groupOptions = createGroupOptions(fieldLength);
const listOptions = [];
if (fieldLength <= 0)
return {
tree: treeOptions,
group: groupOptions,
list: listOptions
};
let level = 0;
const conf = getConfig(keyConfig);
const { name, id, children } = conf;
function handler(options2, tree, group, list, pid = "", idPath = "", namePath = "") {
for (let i = 0; i < options2.length; i++) {
const temp = {
...options2[i],
name: `${options2[i][name]}`,
id: `${options2[i][id]}`,
pid: pid ? `${pid}` : "",
idPath: idPath ? `${idPath}-${options2[i][id]}` : `${options2[i][id]}`,
namePath: namePath ? `${namePath}-${options2[i][name]}` : `${options2[i][name]}`,
isIdSameAsOnlyOneChild: options2[i][children] && options2[i][children].length && options2[i][children].length === 1 && options2[i][children][0][id] === options2[i][id] ? true : false
};
if (isFirstLetterVisible) {
temp["firstLetter"] = getLetter(options2[i][name]);
}
level = temp.idPath.split("-").length - 1;
if (level <= fieldLength - 1) {
if (options2[i][children] && options2[i][children].length) {
temp["children"] = [];
tree.push(temp);
group[level].push(temp);
list.push(temp);
handler(options2[i][children], temp["children"], group, list, temp.id, temp.idPath || temp.id, temp.namePath || temp.name);
} else {
tree.push(temp);
group[level].push(temp);
list.push(temp);
}
}
if (i === options2.length - 1 && level === fieldLength - 1)
level = 0;
}
}
handler(options, treeOptions, groupOptions, listOptions);
return {
tree: treeOptions,
group: groupOptions,
list: listOptions
};
}
const DEFAULT_CASCADE_PRO_SELECT_RECORD = {
name: "",
id: "",
pid: "",
idPath: "",
namePath: ""
};
const DEFAULT_CASCADE_PRO_SELECT_RECORDS = [
DEFAULT_CASCADE_PRO_SELECT_RECORD
];
function getDefaultValue(defaultValue, fields) {
if (defaultValue && defaultValue.length > 0) {
const selectRecords = defaultValue.map((option) => {
const temp = { ...option };
let name = "";
let id = "";
let pid = "";
let idPath = "";
let namePath = "";
for (let i = 0; i < fields.length; i++) {
const key = fields[i];
if (option[key]) {
name = `${option[`${key}Name`]}`;
id = `${option[key]}`;
idPath = idPath ? `${idPath}-${option[key]}` : `${option[key]}`;
namePath = namePath ? `${namePath}-${option[`${key}Name`]}` : `${option[`${key}Name`]}`;
}
}
const idPathSplitResult = idPath.split("-");
pid = idPathSplitResult[idPathSplitResult.length - 1 - 1] ? idPathSplitResult[idPathSplitResult.length - 1 - 1] : "";
return {
...temp,
name,
id,
pid,
idPath,
namePath
};
});
const selectRecord = selectRecords.slice(-1)[0];
return {
selectRecord,
selectRecords
};
} else {
return {
selectRecord: DEFAULT_CASCADE_PRO_SELECT_RECORD,
selectRecords: DEFAULT_CASCADE_PRO_SELECT_RECORDS
};
}
}
exports.DEFAULT_CASCADE_PRO_SELECT_RECORD = DEFAULT_CASCADE_PRO_SELECT_RECORD;
exports.DEFAULT_CASCADE_PRO_SELECT_RECORDS = DEFAULT_CASCADE_PRO_SELECT_RECORDS;
exports.createGroupOptions = createGroupOptions;
exports.getDefaultValue = getDefaultValue;
exports.getLetter = getLetter;
exports.handleOptions = handleOptions;
//# sourceMappingURL=cascade-pro2.js.map