devextreme-react
Version:
DevExtreme React UI and Visualization Components
101 lines (99 loc) • 4.27 kB
JavaScript
/*!
* devextreme-react
* Version: 24.2.6
* Build date: Mon Mar 17 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file in the root of the project for details.
*
* https://github.com/DevExpress/devextreme-react
*/
import { buildNodeFullName } from './config-node';
import { buildNode, buildTemplates } from './tree';
import { mergeNameParts } from './utils';
function compareTemplates(current, currentFullName, prev, changesAccum) {
const currentTemplatesOptions = {};
const currentTemplates = {};
const prevTemplatesOptions = {};
const prevTemplates = {};
buildTemplates(current, currentTemplatesOptions, currentTemplates);
buildTemplates(prev, prevTemplatesOptions, prevTemplates);
changesAccum.addRemovedValues(currentTemplatesOptions, prevTemplatesOptions, currentFullName);
// TODO: support switching to default templates
// appendRemovedValues(currentTemplates, prevTemplates, "", changesAccum.templates);
Object.keys(currentTemplatesOptions).forEach((key) => {
if (currentTemplatesOptions[key] === prevTemplatesOptions[key]) {
return;
}
changesAccum.options[mergeNameParts(currentFullName, key)] = currentTemplatesOptions[key];
});
Object.keys(currentTemplates).forEach((key) => {
const currentTemplate = currentTemplates[key];
const prevTemplate = prevTemplates[key];
if (prevTemplate && currentTemplate.content === prevTemplate.content) {
return;
}
changesAccum.templates[key] = currentTemplate;
});
}
function compare(current, prev, changesAccum) {
const fullName = buildNodeFullName(current);
if (!prev) {
changesAccum.options[fullName] = buildNode(current, changesAccum.templates, true);
return;
}
changesAccum.addRemovedValues(current.options, prev.options, fullName);
changesAccum.addRemovedValues(current.configCollections, prev.configCollections, fullName);
changesAccum.addRemovedValues(current.configs, prev.configs, fullName);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
compareCollections(current, fullName, prev, changesAccum);
Object.keys(current.configs).forEach((key) => {
compare(current.configs[key], prev.configs[key], changesAccum);
});
Object.keys(current.options).forEach((key) => {
if (current.options[key] === prev.options[key]) {
return;
}
changesAccum.options[mergeNameParts(fullName, key)] = current.options[key];
});
compareTemplates(current, fullName, prev, changesAccum);
}
function appendRemovedValues(current, prev, path, changesAccum) {
const removedKeys = Object.keys(prev).filter((key) => !Object.keys(current).includes(key));
removedKeys.forEach((key) => {
changesAccum.push(mergeNameParts(path, key));
});
}
function getChanges(current, prev) {
const changesAccum = {
options: {},
removedOptions: [],
templates: {},
addRemovedValues(currentOptions, prevOptions, path) {
appendRemovedValues(currentOptions, prevOptions, path, this.removedOptions);
},
};
compare(current, prev, changesAccum);
return changesAccum;
}
function compareCollections(current, currentFullName, prev, changesAccum) {
Object.keys(current.configCollections).forEach((key) => {
const currentCollection = current.configCollections[key];
const prevCollection = prev.configCollections[key] || [];
if (!currentCollection || currentCollection.length !== prevCollection.length) {
const updatedCollection = [];
currentCollection.forEach((item) => {
const config = buildNode(item, changesAccum.templates, true);
updatedCollection.push(config);
});
changesAccum.options[mergeNameParts(currentFullName, key)] = updatedCollection;
return;
}
for (let i = 0; i < currentCollection.length; i += 1) {
compare(currentCollection[i], prevCollection[i], changesAccum);
}
});
}
export { getChanges, };