| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- /**
- * DevExtreme (core/component.js)
- * Version: 19.1.16
- * Build date: Tue Oct 18 2022
- *
- * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
- * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
- */
- "use strict";
- function _typeof(obj) {
- "@babel/helpers - typeof";
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
- return typeof obj
- } : function(obj) {
- return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
- }, _typeof(obj)
- }
- function _toConsumableArray(arr) {
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
- }
- function _nonIterableSpread() {
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
- }
- function _unsupportedIterableToArray(o, minLen) {
- if (!o) {
- return
- }
- if ("string" === typeof o) {
- return _arrayLikeToArray(o, minLen)
- }
- var n = Object.prototype.toString.call(o).slice(8, -1);
- if ("Object" === n && o.constructor) {
- n = o.constructor.name
- }
- if ("Map" === n || "Set" === n) {
- return Array.from(o)
- }
- if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
- return _arrayLikeToArray(o, minLen)
- }
- }
- function _iterableToArray(iter) {
- if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
- return Array.from(iter)
- }
- }
- function _arrayWithoutHoles(arr) {
- if (Array.isArray(arr)) {
- return _arrayLikeToArray(arr)
- }
- }
- function _arrayLikeToArray(arr, len) {
- if (null == len || len > arr.length) {
- len = arr.length
- }
- for (var i = 0, arr2 = new Array(len); i < len; i++) {
- arr2[i] = arr[i]
- }
- return arr2
- }
- function _classCallCheck(instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function")
- }
- }
- function _defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) {
- descriptor.writable = true
- }
- Object.defineProperty(target, descriptor.key, descriptor)
- }
- }
- function _createClass(Constructor, protoProps, staticProps) {
- if (protoProps) {
- _defineProperties(Constructor.prototype, protoProps)
- }
- if (staticProps) {
- _defineProperties(Constructor, staticProps)
- }
- Object.defineProperty(Constructor, "prototype", {
- writable: false
- });
- return Constructor
- }
- var Config = require("./config");
- var domAdapter = require("./dom_adapter");
- var extend = require("./utils/extend").extend;
- var Class = require("./class");
- var Action = require("./action");
- var errors = require("./errors");
- var coreDataUtils = require("./utils/data");
- var commonUtils = require("./utils/common");
- var typeUtils = require("./utils/type");
- var deferredUtils = require("../core/utils/deferred");
- var Deferred = deferredUtils.Deferred;
- var when = deferredUtils.when;
- var Callbacks = require("./utils/callbacks");
- var EventsMixin = require("./events_mixin");
- var publicComponentUtils = require("./utils/public_component");
- var devices = require("./devices");
- var isFunction = typeUtils.isFunction;
- var noop = commonUtils.noop;
- var cachedGetters = {};
- var cachedSetters = {};
- var PostponedOperations = function() {
- function PostponedOperations() {
- _classCallCheck(this, PostponedOperations);
- this._postponedOperations = {}
- }
- _createClass(PostponedOperations, [{
- key: "add",
- value: function(key, fn, postponedPromise) {
- if (key in this._postponedOperations) {
- postponedPromise && this._postponedOperations[key].promises.push(postponedPromise)
- } else {
- var completePromise = new Deferred;
- this._postponedOperations[key] = {
- fn: fn,
- completePromise: completePromise,
- promises: postponedPromise ? [postponedPromise] : []
- }
- }
- return this._postponedOperations[key].completePromise.promise()
- }
- }, {
- key: "callPostponedOperations",
- value: function() {
- for (var key in this._postponedOperations) {
- var operation = this._postponedOperations[key];
- if (typeUtils.isDefined(operation)) {
- if (operation.promises && operation.promises.length) {
- when.apply(void 0, _toConsumableArray(operation.promises)).done(operation.fn).then(operation.completePromise.resolve)
- } else {
- operation.fn().done(operation.completePromise.resolve)
- }
- }
- }
- this._postponedOperations = {}
- }
- }]);
- return PostponedOperations
- }();
- var Component = Class.inherit({
- _setDeprecatedOptions: function() {
- this._deprecatedOptions = {}
- },
- _getDeprecatedOptions: function() {
- return this._deprecatedOptions
- },
- _getOptionAliasesByName: function(optionName) {
- var _this = this;
- return Object.keys(this._deprecatedOptions).filter(function(aliasName) {
- return optionName === _this._deprecatedOptions[aliasName].alias
- })
- },
- _getDefaultOptions: function() {
- return {
- onInitialized: null,
- onOptionChanged: null,
- onDisposing: null,
- defaultOptionsRules: null
- }
- },
- _setDefaultOptions: function() {
- this._options = this._getDefaultOptions()
- },
- _defaultOptionsRules: function() {
- return []
- },
- _setOptionsByDevice: function(customRules) {
- var rules = this._defaultOptionsRules();
- if (Array.isArray(customRules)) {
- rules = rules.concat(customRules)
- }
- var rulesOptions = this._convertRulesToOptions(rules);
- extend(true, this._options, rulesOptions);
- for (var fieldName in this._optionsByReference) {
- if (Object.prototype.hasOwnProperty.call(rulesOptions, fieldName)) {
- this._options[fieldName] = rulesOptions[fieldName]
- }
- }
- },
- _convertRulesToOptions: function(rules) {
- var options = {};
- var currentDevice = devices.current();
- var deviceMatch = function(device, filter) {
- var filterArray = [];
- Array.prototype.push.call(filterArray, filter);
- return 1 === filterArray.length && typeUtils.isEmptyObject(filterArray[0]) || commonUtils.findBestMatches(device, filterArray).length > 0
- };
- for (var i = 0; i < rules.length; i++) {
- var rule = rules[i];
- var deviceFilter = rule.device || {};
- var match;
- if (isFunction(deviceFilter)) {
- match = deviceFilter(currentDevice)
- } else {
- match = deviceMatch(currentDevice, deviceFilter)
- }
- if (match) {
- extend(options, rule.options)
- }
- }
- return options
- },
- _isInitialOptionValue: function(name) {
- var optionValue = this.option(name);
- var initialOptionValue = this.initialOption(name);
- var isInitialOption = isFunction(optionValue) && isFunction(initialOptionValue) ? optionValue.toString() === initialOptionValue.toString() : commonUtils.equalByValue(optionValue, initialOptionValue);
- return isInitialOption
- },
- _setOptionsByReference: function() {
- this._optionsByReference = {}
- },
- _getOptionsByReference: function() {
- return this._optionsByReference
- },
- ctor: function(options) {
- this.NAME = publicComponentUtils.name(this.constructor);
- options = options || {};
- if (options.eventsStrategy) {
- this.setEventsStrategy(options.eventsStrategy)
- }
- this._options = {};
- this._updateLockCount = 0;
- this._optionChangedCallbacks = options._optionChangedCallbacks || Callbacks();
- this._disposingCallbacks = options._disposingCallbacks || Callbacks();
- this.postponedOperations = new PostponedOperations;
- this.beginUpdate();
- try {
- this._suppressDeprecatedWarnings();
- this._setOptionsByReference();
- this._setDeprecatedOptions();
- this._setDefaultOptions();
- if (options && options.onInitializing) {
- options.onInitializing.apply(this, [options])
- }
- this._setOptionsByDevice(options.defaultOptionsRules);
- this._resumeDeprecatedWarnings();
- this._initOptions(options)
- } finally {
- this.endUpdate()
- }
- },
- _initOptions: function(options) {
- this.option(options)
- },
- _optionValuesEqual: function(name, oldValue, newValue) {
- oldValue = coreDataUtils.toComparable(oldValue, true);
- newValue = coreDataUtils.toComparable(newValue, true);
- if (oldValue && newValue && typeUtils.isRenderer(oldValue) && typeUtils.isRenderer(newValue)) {
- return newValue.is(oldValue)
- }
- var oldValueIsNaN = oldValue !== oldValue;
- var newValueIsNaN = newValue !== newValue;
- if (oldValueIsNaN && newValueIsNaN) {
- return true
- }
- if (null === oldValue || "object" !== _typeof(oldValue) || domAdapter.isElementNode(oldValue)) {
- return oldValue === newValue
- }
- return false
- },
- _init: function() {
- this._createOptionChangedAction();
- this.on("disposing", function(args) {
- this._disposingCallbacks.fireWith(this, [args])
- }.bind(this))
- },
- _createOptionChangedAction: function() {
- this._optionChangedAction = this._createActionByOption("onOptionChanged", {
- excludeValidators: ["disabled", "readOnly"]
- })
- },
- _createDisposingAction: function() {
- this._disposingAction = this._createActionByOption("onDisposing", {
- excludeValidators: ["disabled", "readOnly"]
- })
- },
- _optionChanged: function(args) {
- switch (args.name) {
- case "onDisposing":
- case "onInitialized":
- break;
- case "onOptionChanged":
- this._createOptionChangedAction();
- break;
- case "defaultOptionsRules":
- }
- },
- _dispose: function() {
- this._optionChangedCallbacks.empty();
- this._createDisposingAction();
- this._disposingAction();
- this._disposeEvents();
- this._disposed = true
- },
- instance: function() {
- return this
- },
- beginUpdate: function() {
- this._updateLockCount++
- },
- endUpdate: function() {
- this._updateLockCount = Math.max(this._updateLockCount - 1, 0);
- if (!this._updateLockCount) {
- this.postponedOperations.callPostponedOperations();
- if (!this._initializing && !this._initialized) {
- this._initializing = true;
- try {
- this._init()
- } finally {
- this._initializing = false;
- this._updateLockCount++;
- this._createActionByOption("onInitialized", {
- excludeValidators: ["disabled", "readOnly"]
- })();
- this._updateLockCount--;
- this._initialized = true
- }
- }
- }
- },
- _logWarningIfDeprecated: function(option) {
- var info = this._deprecatedOptions[option];
- if (info && !this._deprecatedOptionsSuppressed) {
- this._logDeprecatedWarning(option, info)
- }
- },
- _logDeprecatedWarningCount: 0,
- _logDeprecatedWarning: function(option, info) {
- var message = info.message || "Use the '" + info.alias + "' option instead";
- errors.log("W0001", this.NAME, option, info.since, message);
- ++this._logDeprecatedWarningCount
- },
- _suppressDeprecatedWarnings: function() {
- this._deprecatedOptionsSuppressed = true
- },
- _resumeDeprecatedWarnings: function() {
- this._deprecatedOptionsSuppressed = false
- },
- _optionChanging: noop,
- _notifyOptionChanged: function(option, value, previousValue) {
- var that = this;
- if (this._initialized) {
- var optionNames = [option].concat(that._getOptionAliasesByName(option));
- for (var i = 0; i < optionNames.length; i++) {
- var name = optionNames[i];
- var args = {
- name: name.split(/[.[]/)[0],
- fullName: name,
- value: value,
- previousValue: previousValue
- };
- that._optionChangedCallbacks.fireWith(that, [extend(that._defaultActionArgs(), args)]);
- that._optionChangedAction(extend({}, args));
- if (!that._disposed && this._cancelOptionChange !== args.name) {
- that._optionChanged(args)
- }
- }
- }
- },
- initialOption: function(optionName) {
- var currentOptions;
- var currentInitialized = this._initialized;
- if (!this._initialOptions) {
- currentOptions = this._options;
- this._options = {};
- this._initialized = false;
- this._setDefaultOptions();
- this._setOptionsByDevice(currentOptions.defaultOptionsRules);
- this._initialOptions = this._options;
- this._options = currentOptions;
- this._initialized = currentInitialized
- }
- return this._initialOptions[optionName]
- },
- _defaultActionConfig: function() {
- return {
- context: this,
- component: this
- }
- },
- _defaultActionArgs: function() {
- return {
- component: this
- }
- },
- _createAction: function(actionSource, config) {
- var that = this;
- var action;
- return function(e) {
- if (!arguments.length) {
- e = {}
- }
- if (!typeUtils.isPlainObject(e)) {
- e = {
- actionValue: e
- }
- }
- action = action || new Action(actionSource, extend(config, that._defaultActionConfig()));
- return action.execute.call(action, extend(e, that._defaultActionArgs()))
- }
- },
- _createActionByOption: function(optionName, config) {
- var that = this;
- var action;
- var eventName;
- var actionFunc;
- var result = function() {
- if (!eventName) {
- config = config || {};
- if ("string" !== typeof optionName) {
- throw errors.Error("E0008")
- }
- if (0 === optionName.indexOf("on")) {
- eventName = that._getEventName(optionName)
- }
- actionFunc = that.option(optionName)
- }
- if (!action && !actionFunc && !config.beforeExecute && !config.afterExecute && !that.hasEvent(eventName)) {
- return
- }
- if (!action) {
- var beforeExecute = config.beforeExecute;
- config.beforeExecute = function(args) {
- beforeExecute && beforeExecute.apply(that, arguments);
- that.fireEvent(eventName, args.args)
- };
- that._suppressDeprecatedWarnings();
- action = that._createAction(actionFunc, config);
- that._resumeDeprecatedWarnings()
- }
- if (Config().wrapActionsBeforeExecute) {
- var beforeActionExecute = that.option("beforeActionExecute") || noop;
- var wrappedAction = beforeActionExecute(that, action, config) || action;
- return wrappedAction.apply(that, arguments)
- }
- return action.apply(that, arguments)
- };
- if (!Config().wrapActionsBeforeExecute) {
- var onActionCreated = that.option("onActionCreated") || noop;
- result = onActionCreated(that, result, config) || result
- }
- return result
- },
- _getEventName: function(actionName) {
- return actionName.charAt(2).toLowerCase() + actionName.substr(3)
- },
- hasActionSubscription: function(actionName) {
- return !!this.option(actionName) || this.hasEvent(this._getEventName(actionName))
- },
- isOptionDeprecated: function(name) {
- var deprecatedOptions = this._getDeprecatedOptions();
- return Object.prototype.hasOwnProperty.call(deprecatedOptions, name)
- },
- _setOptionSilent: function(name, value) {
- this._cancelOptionChange = name;
- this.option(name, value);
- this._cancelOptionChange = false
- },
- option: function() {
- var normalizeOptionName = function(that, name) {
- var deprecate;
- if (name) {
- if (!that._cachedDeprecateNames) {
- that._cachedDeprecateNames = [];
- for (var optionName in that._deprecatedOptions) {
- that._cachedDeprecateNames.push(optionName)
- }
- }
- for (var i = 0; i < that._cachedDeprecateNames.length; i++) {
- if (that._cachedDeprecateNames[i] === name) {
- deprecate = that._deprecatedOptions[name];
- break
- }
- }
- if (deprecate) {
- that._logWarningIfDeprecated(name);
- var alias = deprecate.alias;
- if (alias) {
- name = alias
- }
- }
- }
- return name
- };
- var getPreviousName = function(fullName) {
- var splitNames = fullName.split(".");
- splitNames.pop();
- return splitNames.join(".")
- };
- var getFieldName = function(fullName) {
- var splitNames = fullName.split(".");
- return splitNames[splitNames.length - 1]
- };
- var getOptionValue = function(options, name, unwrapObservables) {
- var getter = cachedGetters[name];
- if (!getter) {
- getter = cachedGetters[name] = coreDataUtils.compileGetter(name)
- }
- return getter(options, {
- functionsAsIs: true,
- unwrapObservables: unwrapObservables
- })
- };
- var clearOptionsField = function(options, name) {
- delete options[name];
- var previousFieldName = getPreviousName(name);
- var fieldName = getFieldName(name);
- var fieldObject = previousFieldName ? getOptionValue(options, previousFieldName, false) : options;
- if (fieldObject) {
- delete fieldObject[fieldName]
- }
- };
- var setOptionsField = function(options, fullName, value) {
- var fieldName = "";
- var fieldObject;
- do {
- if (fieldName) {
- fieldName = "." + fieldName
- }
- fieldName = getFieldName(fullName) + fieldName;
- fullName = getPreviousName(fullName);
- fieldObject = fullName ? getOptionValue(options, fullName, false) : options
- } while (!fieldObject);
- fieldObject[fieldName] = value
- };
- var normalizeOptionValue = function(that, options, name, value) {
- if (name) {
- var alias = normalizeOptionName(that, name);
- if (alias && alias !== name) {
- setOptionsField(options, alias, value);
- clearOptionsField(options, name)
- }
- }
- };
- var prepareOption = function prepareOption(that, options, name, value) {
- if (typeUtils.isPlainObject(value)) {
- for (var valueName in value) {
- prepareOption(that, options, name + "." + valueName, value[valueName])
- }
- }
- normalizeOptionValue(that, options, name, value)
- };
- var setOptionValue = function(that, name, value) {
- if (!cachedSetters[name]) {
- cachedSetters[name] = coreDataUtils.compileSetter(name)
- }
- var path = name.split(/[.[]/);
- cachedSetters[name](that._options, value, {
- functionsAsIs: true,
- merge: !that._getOptionsByReference()[name],
- unwrapObservables: path.length > 1 && !!that._getOptionsByReference()[path[0]]
- })
- };
- var setOption = function(that, name, value) {
- var previousValue = getOptionValue(that._options, name, false);
- if (that._optionValuesEqual(name, previousValue, value)) {
- return
- }
- if (that._initialized) {
- that._optionChanging(name, previousValue, value)
- }
- setOptionValue(that, name, value);
- that._notifyOptionChanged(name, value, previousValue)
- };
- return function(options, value) {
- var that = this;
- var name = options;
- if (arguments.length < 2 && "object" !== typeUtils.type(name)) {
- name = normalizeOptionName(that, name);
- return getOptionValue(that._options, name)
- }
- if ("string" === typeof name) {
- options = {};
- options[name] = value
- }
- that.beginUpdate();
- try {
- var optionName;
- for (optionName in options) {
- prepareOption(that, options, optionName, options[optionName])
- }
- for (optionName in options) {
- setOption(that, optionName, options[optionName])
- }
- } finally {
- that.endUpdate()
- }
- }
- }(),
- _getOptionValue: function(name, context) {
- var value = this.option(name);
- if (isFunction(value)) {
- return value.bind(context)()
- }
- return value
- }
- }).include(EventsMixin);
- module.exports = Component;
- module.exports.PostponedOperations = PostponedOperations;
|