UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

173 lines (172 loc) • 6.36 kB
/** * DevExtreme (esm/__internal/ui/m_validation_summary.js) * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import _extends from "@babel/runtime/helpers/esm/extends"; import eventsEngine from "../../common/core/events/core/events_engine"; import registerComponent from "../../core/component_registrator"; import { grep } from "../../core/utils/common"; import { extend } from "../../core/utils/extend"; import { each, map } from "../../core/utils/iterator"; import CollectionWidget from "../ui/collection/m_collection_widget.edit"; import ValidationEngine from "./m_validation_engine"; const VALIDATION_SUMMARY_CLASS = "dx-validationsummary"; const ITEM_CLASS = "dx-validationsummary-item"; const ITEM_DATA_KEY = "dx-validationsummary-item-data"; class ValidationSummary extends CollectionWidget { _getDefaultOptions() { return _extends({}, super._getDefaultOptions(), { focusStateEnabled: false, noDataText: null }) } _setOptionsByReference() { super._setOptionsByReference(); extend(this._optionsByReference, { validationGroup: true }) } _init() { super._init(); this._initGroupRegistration() } _initGroupRegistration() { const $element = this.$element(); const { validationGroup: validationGroup } = this.option(); const group = validationGroup || ValidationEngine.findGroup($element, this._modelByElement($element)); const groupConfig = ValidationEngine.addGroup(group, true); this._unsubscribeGroup(); this._groupWasInit = true; this._validationGroup = group; this.groupSubscription = this._groupValidationHandler.bind(this); groupConfig.on("validated", this.groupSubscription) } _unsubscribeGroup() { const groupConfig = ValidationEngine.getGroupConfig(this._validationGroup); null === groupConfig || void 0 === groupConfig || groupConfig.off("validated", this.groupSubscription) } _getOrderedItems(validators, items) { let orderedItems = []; each(validators, ((_, validator) => { const foundItems = grep(items, (item => { if (item.validator === validator) { return true } })); if (foundItems.length) { orderedItems = orderedItems.concat(foundItems) } })); return orderedItems } _groupValidationHandler(params) { const items = this._getOrderedItems(params.validators, map(params.brokenRules, (rule => ({ text: rule.message, validator: rule.validator, index: rule.index })))); this.validators = params.validators; each(this.validators, ((_, validator) => { if (validator._validationSummary !== this) { let handler = this._itemValidationHandler.bind(this); const disposingHandler = function() { validator.off("validated", handler); validator._validationSummary = null; handler = null }; validator.on("validated", handler); validator.on("disposing", disposingHandler); validator._validationSummary = this } })); this.option("items", items) } _itemValidationHandler(_ref) { let { isValid: isValid, validator: validator, brokenRules: brokenRules } = _ref; let { items: items } = this.option(); let itemsChanged = false; let itemIndex = 0; while (itemIndex < items.length) { const item = items[itemIndex]; if (item.validator === validator) { const foundRule = grep(brokenRules || [], (rule => rule.index === item.index))[0]; if (isValid || !foundRule) { items.splice(itemIndex, 1); itemsChanged = true; continue } if (foundRule.message !== item.text) { item.text = foundRule.message; itemsChanged = true } } itemIndex++ } each(brokenRules, ((_, rule) => { const foundItem = grep(items, (item => item.validator === validator && item.index === rule.index))[0]; if (!foundItem) { items.push({ text: rule.message, validator: validator, index: rule.index }); itemsChanged = true } })); if (itemsChanged) { 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) { eventsEngine.on(params.itemElement, "click", (() => { var _params$itemData$vali, _params$itemData$vali2; null === (_params$itemData$vali = params.itemData.validator) || void 0 === _params$itemData$vali || null === (_params$itemData$vali2 = _params$itemData$vali.focus) || void 0 === _params$itemData$vali2 || _params$itemData$vali2.call(_params$itemData$vali) })) } _dispose() { super._dispose(); this._unsubscribeGroup() } refreshValidationGroup() { this._initGroupRegistration() } } registerComponent("dxValidationSummary", ValidationSummary); export default ValidationSummary;