devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
195 lines (194 loc) • 6.61 kB
JavaScript
/**
* DevExtreme (esm/__internal/core/options/index.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 {
equalByValue,
noop
} from "../../../core/utils/common";
import {
getPathParts
} from "../../../core/utils/data";
import {
isFunction,
isObject,
type
} from "../../../core/utils/type";
import {
OptionManager
} from "../../core/options/option_manager";
import {
convertRulesToOptions,
getFieldName,
getNestedOptionValue,
getParentName
} from "../../core/options/utils";
export class Options {
constructor(options, defaultOptions, optionsByReference, deprecatedOptions) {
this._default = defaultOptions;
this._deprecated = deprecatedOptions;
this._deprecatedNames = [];
this._initDeprecatedNames();
this._optionManager = new OptionManager(options, optionsByReference);
this._optionManager.onRelevantNamesPrepared((newOptions, name, value, silent) => this._setRelevantNames(newOptions, name, value, silent));
this._rules = []
}
set _initial(value) {
this._initialOptions = value
}
get _initial() {
if (!this._initialOptions) {
const rulesOptions = this._getByRules(this.silent("defaultOptionsRules"));
this._initialOptions = this._default;
this._optionManager._setByReference(this._initialOptions, rulesOptions)
}
return this._initialOptions
}
_initDeprecatedNames() {
for (const optionName in this._deprecated) {
this._deprecatedNames.push(optionName)
}
}
_getByRules(rules) {
const allRules = Array.isArray(rules) ? this._rules.concat(rules) : this._rules;
return convertRulesToOptions(allRules)
}
_notifyDeprecated(option) {
const info = this._deprecated[option];
if (info) {
this._deprecatedCallback(option, info)
}
}
_setRelevantNames(options, name, value, silent) {
if (name) {
const normalizedName = this._normalizeName(name, silent);
if (normalizedName && normalizedName !== name) {
this._setField(options, normalizedName, value);
this._clearField(options, name)
}
}
}
_setField(options, fullName, value) {
let fieldName = "";
let fieldObject = null;
let currentName = fullName;
do {
fieldName = fieldName ? `.${fieldName}` : "";
fieldName = getFieldName(currentName) + fieldName;
currentName = getParentName(currentName);
fieldObject = currentName ? this._optionManager.get(options, currentName, false) : options
} while (!fieldObject);
fieldObject[fieldName] = value
}
_clearField(options, name) {
delete options[name];
const previousFieldName = getParentName(name);
const fieldObject = previousFieldName ? this._optionManager.get(options, previousFieldName, false) : options;
if (fieldObject) {
delete fieldObject[getFieldName(name)]
}
}
_normalizeName(name, silent) {
if (!this._deprecatedNames.length || !name) {
return name
}
for (const deprecatedName of this._deprecatedNames) {
const deprecate = deprecatedName === name && this._deprecated[name];
if (deprecate) {
if (!silent) {
this._notifyDeprecated(name)
}
return deprecate.alias || name
}
}
return name
}
addRules(rules) {
this._rules = rules.concat(this._rules)
}
applyRules(rules) {
const options = this._getByRules(rules);
this.silent(options)
}
dispose() {
this._deprecatedCallback = noop;
this._startChangeCallback = noop;
this._endChangeCallback = noop;
this._optionManager.dispose()
}
onChanging(callBack) {
this._optionManager.onChanging(callBack)
}
onChanged(callBack) {
this._optionManager.onChanged(callBack)
}
validateOptions(callBack) {
this._optionManager.onValidateOptions(callBack)
}
onDeprecated(callBack) {
this._deprecatedCallback = callBack
}
onStartChange(callBack) {
this._startChangeCallback = callBack
}
onEndChange(callBack) {
this._endChangeCallback = callBack
}
isInitial(name) {
const value = this.silent(name);
const initialValue = this.initial(name);
const areFunctions = isFunction(value) && isFunction(initialValue);
return areFunctions ? value.toString() === initialValue.toString() : equalByValue(value, initialValue)
}
initial(name) {
return getNestedOptionValue(this._initial, name)
}
option(options, value) {
const isGetter = arguments.length < 2 && "object" !== type(options);
if (isGetter) {
return this._optionManager.get(void 0, this._normalizeName(options))
}
this._startChangeCallback();
try {
this._optionManager.set(options, value)
} finally {
this._endChangeCallback()
}
return
}
silent(options, value) {
const isGetter = arguments.length < 2 && "object" !== type(options);
if (isGetter) {
return this._optionManager.get(void 0, options, void 0)
}
this._optionManager.set(options, value, void 0, true);
return
}
reset(name) {
if (name) {
const fullPath = getPathParts(name);
const value = fullPath.reduce((currentValue, field) => currentValue ? currentValue[field] : this.initial(field), null);
const defaultValue = isObject(value) ? Object.assign({}, value) : value;
this._optionManager.set(name, defaultValue, false)
}
}
getAliasesByName(name) {
return Object.keys(this._deprecated).filter(aliasName => name === this._deprecated[aliasName].alias)
}
isDeprecated(name) {
return Object.prototype.hasOwnProperty.call(this._deprecated, name)
}
cache(name, value) {
const isGetter = arguments.length < 2;
const optionName = `_cached_${name}`;
if (isGetter) {
return this.option(optionName)
}
this.option(optionName, value);
return
}
}