UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

423 lines (422 loc) 15.5 kB
import { isString, createHashMap, each, indexOf, assert, isObject, isNumber, isStringSafe, isArray, map } from "../../../zrender/lib/core/util.mjs"; import env from "../../../zrender/lib/core/env.mjs"; import { getRandomIdBase, round, getPrecision } from "./number.mjs"; function interpolateNumber(p0, p1, percent) { return (p1 - p0) * percent + p0; } var DUMMY_COMPONENT_NAME_PREFIX = "series\0"; var INTERNAL_COMPONENT_ID_PREFIX = "\0_ec_\0"; function normalizeToArray(value) { return value instanceof Array ? value : value == null ? [] : [value]; } function defaultEmphasis(opt, key, subOpts) { if (opt) { opt[key] = opt[key] || {}; opt.emphasis = opt.emphasis || {}; opt.emphasis[key] = opt.emphasis[key] || {}; for (var i = 0, len = subOpts.length; i < len; i++) { var subOptName = subOpts[i]; if (!opt.emphasis[key].hasOwnProperty(subOptName) && opt[key].hasOwnProperty(subOptName)) { opt.emphasis[key][subOptName] = opt[key][subOptName]; } } } } var TEXT_STYLE_OPTIONS = ["fontStyle", "fontWeight", "fontSize", "fontFamily", "rich", "tag", "color", "textBorderColor", "textBorderWidth", "width", "height", "lineHeight", "align", "verticalAlign", "baseline", "shadowColor", "shadowBlur", "shadowOffsetX", "shadowOffsetY", "textShadowColor", "textShadowBlur", "textShadowOffsetX", "textShadowOffsetY", "backgroundColor", "borderColor", "borderWidth", "borderRadius", "padding"]; function getDataItemValue(dataItem) { return isObject(dataItem) && !isArray(dataItem) && !(dataItem instanceof Date) ? dataItem.value : dataItem; } function isDataItemOption(dataItem) { return isObject(dataItem) && !(dataItem instanceof Array); } function mappingToExists(existings, newCmptOptions, mode) { var isNormalMergeMode = mode === "normalMerge"; var isReplaceMergeMode = mode === "replaceMerge"; var isReplaceAllMode = mode === "replaceAll"; existings = existings || []; newCmptOptions = (newCmptOptions || []).slice(); var existingIdIdxMap = createHashMap(); each(newCmptOptions, function(cmptOption, index) { if (!isObject(cmptOption)) { newCmptOptions[index] = null; return; } }); var result = prepareResult(existings, existingIdIdxMap, mode); if (isNormalMergeMode || isReplaceMergeMode) { mappingById(result, existings, existingIdIdxMap, newCmptOptions); } if (isNormalMergeMode) { mappingByName(result, newCmptOptions); } if (isNormalMergeMode || isReplaceMergeMode) { mappingByIndex(result, newCmptOptions, isReplaceMergeMode); } else if (isReplaceAllMode) { mappingInReplaceAllMode(result, newCmptOptions); } makeIdAndName(result); return result; } function prepareResult(existings, existingIdIdxMap, mode) { var result = []; if (mode === "replaceAll") { return result; } for (var index = 0; index < existings.length; index++) { var existing = existings[index]; if (existing && existing.id != null) { existingIdIdxMap.set(existing.id, index); } result.push({ existing: mode === "replaceMerge" || isComponentIdInternal(existing) ? null : existing, newOption: null, keyInfo: null, brandNew: null }); } return result; } function mappingById(result, existings, existingIdIdxMap, newCmptOptions) { each(newCmptOptions, function(cmptOption, index) { if (!cmptOption || cmptOption.id == null) { return; } var optionId = makeComparableKey(cmptOption.id); var existingIdx = existingIdIdxMap.get(optionId); if (existingIdx != null) { var resultItem = result[existingIdx]; assert(!resultItem.newOption, 'Duplicated option on id "' + optionId + '".'); resultItem.newOption = cmptOption; resultItem.existing = existings[existingIdx]; newCmptOptions[index] = null; } }); } function mappingByName(result, newCmptOptions) { each(newCmptOptions, function(cmptOption, index) { if (!cmptOption || cmptOption.name == null) { return; } for (var i = 0; i < result.length; i++) { var existing = result[i].existing; if (!result[i].newOption && existing && (existing.id == null || cmptOption.id == null) && !isComponentIdInternal(cmptOption) && !isComponentIdInternal(existing) && keyExistAndEqual("name", existing, cmptOption)) { result[i].newOption = cmptOption; newCmptOptions[index] = null; return; } } }); } function mappingByIndex(result, newCmptOptions, brandNew) { each(newCmptOptions, function(cmptOption) { if (!cmptOption) { return; } var resultItem; var nextIdx = 0; while ((resultItem = result[nextIdx]) && (resultItem.newOption || isComponentIdInternal(resultItem.existing) || resultItem.existing && cmptOption.id != null && !keyExistAndEqual("id", cmptOption, resultItem.existing))) { nextIdx++; } if (resultItem) { resultItem.newOption = cmptOption; resultItem.brandNew = brandNew; } else { result.push({ newOption: cmptOption, brandNew, existing: null, keyInfo: null }); } nextIdx++; }); } function mappingInReplaceAllMode(result, newCmptOptions) { each(newCmptOptions, function(cmptOption) { result.push({ newOption: cmptOption, brandNew: true, existing: null, keyInfo: null }); }); } function makeIdAndName(mapResult) { var idMap = createHashMap(); each(mapResult, function(item) { var existing = item.existing; existing && idMap.set(existing.id, item); }); each(mapResult, function(item) { var opt = item.newOption; assert(!opt || opt.id == null || !idMap.get(opt.id) || idMap.get(opt.id) === item, "id duplicates: " + (opt && opt.id)); opt && opt.id != null && idMap.set(opt.id, item); !item.keyInfo && (item.keyInfo = {}); }); each(mapResult, function(item, index) { var existing = item.existing; var opt = item.newOption; var keyInfo = item.keyInfo; if (!isObject(opt)) { return; } keyInfo.name = opt.name != null ? makeComparableKey(opt.name) : existing ? existing.name : DUMMY_COMPONENT_NAME_PREFIX + index; if (existing) { keyInfo.id = makeComparableKey(existing.id); } else if (opt.id != null) { keyInfo.id = makeComparableKey(opt.id); } else { var idNum = 0; do { keyInfo.id = "\0" + keyInfo.name + "\0" + idNum++; } while (idMap.get(keyInfo.id)); } idMap.set(keyInfo.id, item); }); } function keyExistAndEqual(attr, obj1, obj2) { var key1 = convertOptionIdName(obj1[attr], null); var key2 = convertOptionIdName(obj2[attr], null); return key1 != null && key2 != null && key1 === key2; } function makeComparableKey(val) { return convertOptionIdName(val, ""); } function convertOptionIdName(idOrName, defaultValue) { if (idOrName == null) { return defaultValue; } return isString(idOrName) ? idOrName : isNumber(idOrName) || isStringSafe(idOrName) ? idOrName + "" : defaultValue; } function isNameSpecified(componentModel) { var name = componentModel.name; return !!(name && name.indexOf(DUMMY_COMPONENT_NAME_PREFIX)); } function isComponentIdInternal(cmptOption) { return cmptOption && cmptOption.id != null && makeComparableKey(cmptOption.id).indexOf(INTERNAL_COMPONENT_ID_PREFIX) === 0; } function makeInternalComponentId(idSuffix) { return INTERNAL_COMPONENT_ID_PREFIX + idSuffix; } function setComponentTypeToKeyInfo(mappingResult, mainType, componentModelCtor) { each(mappingResult, function(item) { var newOption = item.newOption; if (isObject(newOption)) { item.keyInfo.mainType = mainType; item.keyInfo.subType = determineSubType(mainType, newOption, item.existing, componentModelCtor); } }); } function determineSubType(mainType, newCmptOption, existComponent, componentModelCtor) { var subType = newCmptOption.type ? newCmptOption.type : existComponent ? existComponent.subType : componentModelCtor.determineSubType(mainType, newCmptOption); return subType; } function compressBatches(batchA, batchB) { var mapA = {}; var mapB = {}; makeMap(batchA || [], mapA); makeMap(batchB || [], mapB, mapA); return [mapToArray(mapA), mapToArray(mapB)]; function makeMap(sourceBatch, map2, otherMap) { for (var i = 0, len = sourceBatch.length; i < len; i++) { var seriesId = convertOptionIdName(sourceBatch[i].seriesId, null); if (seriesId == null) { return; } var dataIndices = normalizeToArray(sourceBatch[i].dataIndex); var otherDataIndices = otherMap && otherMap[seriesId]; for (var j = 0, lenj = dataIndices.length; j < lenj; j++) { var dataIndex = dataIndices[j]; if (otherDataIndices && otherDataIndices[dataIndex]) { otherDataIndices[dataIndex] = null; } else { (map2[seriesId] || (map2[seriesId] = {}))[dataIndex] = 1; } } } } function mapToArray(map2, isData) { var result = []; for (var i in map2) { if (map2.hasOwnProperty(i) && map2[i] != null) { if (isData) { result.push(+i); } else { var dataIndices = mapToArray(map2[i], true); dataIndices.length && result.push({ seriesId: i, dataIndex: dataIndices }); } } } return result; } } function queryDataIndex(data, payload) { if (payload.dataIndexInside != null) { return payload.dataIndexInside; } else if (payload.dataIndex != null) { return isArray(payload.dataIndex) ? map(payload.dataIndex, function(value) { return data.indexOfRawIndex(value); }) : data.indexOfRawIndex(payload.dataIndex); } else if (payload.name != null) { return isArray(payload.name) ? map(payload.name, function(value) { return data.indexOfName(value); }) : data.indexOfName(payload.name); } } function makeInner() { var key = "__ec_inner_" + innerUniqueIndex++; return function(hostObj) { return hostObj[key] || (hostObj[key] = {}); }; } var innerUniqueIndex = getRandomIdBase(); function parseFinder(ecModel, finderInput, opt) { var _a = preParseFinder(finderInput, opt), mainTypeSpecified = _a.mainTypeSpecified, queryOptionMap = _a.queryOptionMap, others = _a.others; var result = others; var defaultMainType = opt ? opt.defaultMainType : null; if (!mainTypeSpecified && defaultMainType) { queryOptionMap.set(defaultMainType, {}); } queryOptionMap.each(function(queryOption, mainType) { var queryResult = queryReferringComponents(ecModel, mainType, queryOption, { useDefault: defaultMainType === mainType, enableAll: opt && opt.enableAll != null ? opt.enableAll : true, enableNone: opt && opt.enableNone != null ? opt.enableNone : true }); result[mainType + "Models"] = queryResult.models; result[mainType + "Model"] = queryResult.models[0]; }); return result; } function preParseFinder(finderInput, opt) { var finder; if (isString(finderInput)) { var obj = {}; obj[finderInput + "Index"] = 0; finder = obj; } else { finder = finderInput; } var queryOptionMap = createHashMap(); var others = {}; var mainTypeSpecified = false; each(finder, function(value, key) { if (key === "dataIndex" || key === "dataIndexInside") { others[key] = value; return; } var parsedKey = key.match(/^(\w+)(Index|Id|Name)$/) || []; var mainType = parsedKey[1]; var queryType = (parsedKey[2] || "").toLowerCase(); if (!mainType || !queryType || opt && opt.includeMainTypes && indexOf(opt.includeMainTypes, mainType) < 0) { return; } mainTypeSpecified = mainTypeSpecified || !!mainType; var queryOption = queryOptionMap.get(mainType) || queryOptionMap.set(mainType, {}); queryOption[queryType] = value; }); return { mainTypeSpecified, queryOptionMap, others }; } var SINGLE_REFERRING = { useDefault: true, enableAll: false, enableNone: false }; var MULTIPLE_REFERRING = { useDefault: false, enableAll: true, enableNone: true }; function queryReferringComponents(ecModel, mainType, userOption, opt) { opt = opt || SINGLE_REFERRING; var indexOption = userOption.index; var idOption = userOption.id; var nameOption = userOption.name; var result = { models: null, specified: indexOption != null || idOption != null || nameOption != null }; if (!result.specified) { var firstCmpt = void 0; result.models = opt.useDefault && (firstCmpt = ecModel.getComponent(mainType)) ? [firstCmpt] : []; return result; } if (indexOption === "none" || indexOption === false) { assert(opt.enableNone, '`"none"` or `false` is not a valid value on index option.'); result.models = []; return result; } if (indexOption === "all") { assert(opt.enableAll, '`"all"` is not a valid value on index option.'); indexOption = idOption = nameOption = null; } result.models = ecModel.queryComponents({ mainType, index: indexOption, id: idOption, name: nameOption }); return result; } function setAttribute(dom, key, value) { dom.setAttribute ? dom.setAttribute(key, value) : dom[key] = value; } function getAttribute(dom, key) { return dom.getAttribute ? dom.getAttribute(key) : dom[key]; } function getTooltipRenderMode(renderModeOption) { if (renderModeOption === "auto") { return env.domSupported ? "html" : "richText"; } else { return renderModeOption || "html"; } } function groupData(array, getKey) { var buckets = createHashMap(); var keys = []; each(array, function(item) { var key = getKey(item); (buckets.get(key) || (keys.push(key), buckets.set(key, []))).push(item); }); return { keys, buckets }; } function interpolateRawValues(data, precision, sourceValue, targetValue, percent) { var isAutoPrecision = precision == null || precision === "auto"; if (targetValue == null) { return targetValue; } if (isNumber(targetValue)) { var value = interpolateNumber(sourceValue || 0, targetValue, percent); return round(value, isAutoPrecision ? Math.max(getPrecision(sourceValue || 0), getPrecision(targetValue)) : precision); } else if (isString(targetValue)) { return percent < 1 ? sourceValue : targetValue; } else { var interpolated = []; var leftArr = sourceValue; var rightArr = targetValue; var length_1 = Math.max(leftArr ? leftArr.length : 0, rightArr.length); for (var i = 0; i < length_1; ++i) { var info = data.getDimensionInfo(i); if (info && info.type === "ordinal") { interpolated[i] = (percent < 1 && leftArr ? leftArr : rightArr)[i]; } else { var leftVal = leftArr && leftArr[i] ? leftArr[i] : 0; var rightVal = rightArr[i]; var value = interpolateNumber(leftVal, rightVal, percent); interpolated[i] = round(value, isAutoPrecision ? Math.max(getPrecision(leftVal), getPrecision(rightVal)) : precision); } } return interpolated; } } export { MULTIPLE_REFERRING, SINGLE_REFERRING, TEXT_STYLE_OPTIONS, compressBatches, convertOptionIdName, defaultEmphasis, getAttribute, getDataItemValue, getTooltipRenderMode, groupData, interpolateRawValues, isComponentIdInternal, isDataItemOption, isNameSpecified, makeInner, makeInternalComponentId, mappingToExists, normalizeToArray, parseFinder, preParseFinder, queryDataIndex, queryReferringComponents, setAttribute, setComponentTypeToKeyInfo };