UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

125 lines (124 loc) • 4.06 kB
/** * DevExtreme (esm/__internal/data/data_source/utils.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import ArrayStore from "../../../common/data/array_store"; import { CustomStore } from "../../../common/data/custom_store"; import { normalizeSortingInfo } from "../../../common/data/utils"; import ajaxUtils from "../../../core/utils/ajax"; import { extend } from "../../../core/utils/extend"; import { isObject, isPlainObject } from "../../../core/utils/type"; import Store from "../../data/abstract_store"; export const CANCELED_TOKEN = "canceled"; export const isPending = deferred => "pending" === deferred.state(); export const normalizeStoreLoadOptionAccessorArguments = originalArguments => { switch (originalArguments.length) { case 0: return; case 1: return originalArguments[0]; default: return originalArguments.slice() } }; const mapRecursive = (items, level, mapper) => { if (!Array.isArray(items)) { return items } if (!level) { return items.map(mapper) } return items.map(item => { const groupItem = isObject(item) ? item : {}; return Object.assign({}, groupItem, { items: mapRecursive(groupItem.items, level - 1, mapper) }) }) }; export const mapDataRespectingGrouping = (items, mapper, groupInfo) => { const level = groupInfo ? normalizeSortingInfo(groupInfo).length : 0; const mapped = mapRecursive(items, level, mapper); return Array.isArray(mapped) ? mapped : [] }; export const normalizeLoadResult = (data, extra) => { const loadResult = isObject(data) ? data : {}; const resultData = loadResult.data ? loadResult.data : data; const resultExtra = loadResult.data ? data : extra; return { data: Array.isArray(resultData) ? resultData : [resultData], extra: resultExtra } }; const CUSTOM_STORE_OPTION_NAMES = ["useDefaultSearch", "key", "load", "loadMode", "cacheRawData", "byKey", "lookup", "totalCount", "insert", "update", "remove"]; const createCustomStoreFromLoadFunc = options => { const storeConfig = {}; CUSTOM_STORE_OPTION_NAMES.forEach(optionName => { storeConfig[optionName] = options[optionName]; delete options[optionName] }); return new CustomStore(storeConfig) }; const createStoreFromConfig = storeConfig => { const alias = String(storeConfig.type); delete storeConfig.type; const store = Store.create(alias, storeConfig); return store }; const createCustomStoreFromUrl = (url, normalizationOptions) => new CustomStore({ load: () => ajaxUtils.sendRequest({ url: url, dataType: "json" }), loadMode: null === normalizationOptions || void 0 === normalizationOptions ? void 0 : normalizationOptions.fromUrlLoadMode }); const resolveStore = options => { if ("load" in options) { return createCustomStoreFromLoadFunc(options) } const { store: store } = options; if (Array.isArray(store)) { return new ArrayStore(store) } if (isPlainObject(store)) { return createStoreFromConfig(extend({}, store)) } const passedThrough = store; return passedThrough }; export const normalizeDataSourceOptions = (options, normalizationOptions) => { let source = options; if ("string" === typeof source) { source = { paginate: false, store: createCustomStoreFromUrl(source, normalizationOptions) } } if (void 0 === source) { source = [] } const normalized = Array.isArray(source) || source instanceof Store ? { store: source } : extend({}, source); if (void 0 === normalized.store) { normalized.store = [] } const store = resolveStore(normalized); return Object.assign({}, normalized, { store: store }) };