UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

214 lines (212 loc) • 8.07 kB
/** * DevExtreme (cjs/__internal/ui/validation_summary.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/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _events_engine = _interopRequireDefault(require("../../common/core/events/core/events_engine")); var _component_registrator = _interopRequireDefault(require("../../core/component_registrator")); var _renderer = _interopRequireDefault(require("../../core/renderer")); var _widget = require("../core/widget/widget"); var _collection_widget = _interopRequireDefault(require("../ui/collection/collection_widget.edit")); var _validation_engine = _interopRequireDefault(require("../ui/validation_engine")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } const VALIDATION_SUMMARY_CLASS = "dx-validationsummary"; const ITEM_CLASS = "dx-validationsummary-item"; const ITEM_DATA_KEY = "dx-validationsummary-item-data"; class ValidationSummary extends _collection_widget.default { _getDefaultOptions() { return Object.assign({}, super._getDefaultOptions(), { focusStateEnabled: false, noDataText: null }) } _setOptionsByReference() { super._setOptionsByReference(); Object.assign(this._optionsByReference, { validationGroup: true }) } _init() { super._init(); this._initGroupRegistration() } _initGroupRegistration() { const $element = this.$element(); const { validationGroup: validationGroup } = this.option(); const group = validationGroup || _validation_engine.default.findGroup($element, this._modelByElement($element)); const groupConfig = _validation_engine.default.addGroup(group, true); this._unsubscribeGroup(); this._groupWasInit = true; this._validationGroup = group; const groupSubscription = this._groupValidationHandler.bind(this); this.groupSubscription = groupSubscription; groupConfig.on("validated", groupSubscription) } _unsubscribeGroup() { const groupConfig = _validation_engine.default.getGroupConfig(this._validationGroup); null === groupConfig || void 0 === groupConfig || groupConfig.off("validated", this.groupSubscription) } _getOrderedItems(validators, items) { const orderedItems = []; for (const validator of validators) { const foundItems = items.filter(item => item.validator === validator); if (foundItems.length) { orderedItems.push(...foundItems) } } return orderedItems } _groupValidationHandler(params) { const items = this._getOrderedItems(params.validators, (params.brokenRules ?? []).map(rule => ({ text: rule.message, validator: rule.validator, index: rule.index }))); this.validators = params.validators; for (const validator of this.validators ?? []) { if (validator._validationSummary !== this) { let handler = this._itemValidationHandler.bind(this); const disposingHandler = () => { validator.off("validated", handler); validator._validationSummary = null; handler = null }; validator.on("validated", handler); validator.on("disposing", disposingHandler); validator._validationSummary = this } } this.option("items", items); this._announceOnGroupValidation() } _announceOnGroupValidation() { const { items: items } = this.option(); if (!(null !== items && void 0 !== items && items.length)) { this._lastAnnouncedText = ""; this._removeAnnounceContainer(); return } const text = items.map(item => item.text).join(". "); if (text !== this._lastAnnouncedText) { this._lastAnnouncedText = text; this._announceText(text) } } _removeAnnounceContainer() { var _this$_$announceConta; null === (_this$_$announceConta = this._$announceContainer) || void 0 === _this$_$announceConta || _this$_$announceConta.remove(); this._$announceContainer = void 0 } _renderAnnounceContainer() { this._removeAnnounceContainer(); this._$announceContainer = (0, _renderer.default)("<div>").addClass(_widget.SCREEN_READER_ONLY_CLASS).attr("role", "alert").appendTo(this.element()) } _announceText(text) { var _this$_$announceConta2; this._renderAnnounceContainer(); null === (_this$_$announceConta2 = this._$announceContainer) || void 0 === _this$_$announceConta2 || _this$_$announceConta2.text(text) } _itemValidationHandler(_ref) { let { isValid: isValid, validator: validator, brokenRules: brokenRules } = _ref; let { items: items } = this.option(); if (!items) { return } let itemsChanged = false; let itemIndex = 0; while (itemIndex < items.length) { const item = items[itemIndex]; if (item.validator === validator) { const foundRule = (brokenRules ?? []).find(rule => rule.index === item.index); if (isValid || !foundRule) { items.splice(itemIndex, 1); itemsChanged = true } else { if (foundRule.message !== item.text) { item.text = foundRule.message; itemsChanged = true } itemIndex += 1 } } else { itemIndex += 1 } } for (const rule of brokenRules ?? []) { var _items; const foundItem = null === (_items = items) || void 0 === _items ? void 0 : _items.find(item => item.validator === validator && item.index === rule.index); if (!foundItem) { var _items2; null === (_items2 = items) || void 0 === _items2 || _items2.push({ text: rule.message, validator: validator, index: rule.index }); itemsChanged = true } } if (itemsChanged && this.validators) { items = this._getOrderedItems(this.validators, items ?? []); this.option("items", items) } } _initMarkup() { this.$element().addClass("dx-validationsummary"); super._initMarkup() } _optionChanged(args) { if ("validationGroup" === args.name) { this._initGroupRegistration() } else { super._optionChanged(args) } } _itemClass() { return ITEM_CLASS } _itemDataKey() { return ITEM_DATA_KEY } _postprocessRenderItem(params) { _events_engine.default.on(params.itemElement, "click", () => { const { validator: validator } = params.itemData; if (validator && "focus" in validator) { var _validator$focus; null === (_validator$focus = validator.focus) || void 0 === _validator$focus || _validator$focus.call(validator) } }) } _dispose() { this._removeAnnounceContainer(); super._dispose(); this._unsubscribeGroup() } refreshValidationGroup() { this._initGroupRegistration() } }(0, _component_registrator.default)("dxValidationSummary", ValidationSummary); var _default = exports.default = ValidationSummary;