| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- * DevExtreme (ui/validation_summary.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";
- var registerComponent = require("../core/component_registrator");
- var eventsEngine = require("../events/core/events_engine");
- var grep = require("../core/utils/common").grep;
- var extend = require("../core/utils/extend").extend;
- var iteratorUtils = require("../core/utils/iterator");
- var ValidationMixin = require("./validation/validation_mixin");
- var ValidationEngine = require("./validation_engine");
- var CollectionWidget = require("./collection/ui.collection_widget.edit");
- var VALIDATION_SUMMARY_CLASS = "dx-validationsummary";
- var ITEM_CLASS = VALIDATION_SUMMARY_CLASS + "-item";
- var ITEM_DATA_KEY = VALIDATION_SUMMARY_CLASS + "-item-data";
- var ValidationSummary = CollectionWidget.inherit({
- _getDefaultOptions: function() {
- return extend(this.callBase(), {
- focusStateEnabled: false,
- noDataText: null
- })
- },
- _setOptionsByReference: function() {
- this.callBase();
- extend(this._optionsByReference, {
- validationGroup: true
- })
- },
- _init: function() {
- this.callBase();
- this._initGroupRegistration()
- },
- _initGroupRegistration: function() {
- var group = this._findGroup();
- var groupConfig = ValidationEngine.addGroup(group);
- this._unsubscribeGroup();
- this._groupWasInit = true;
- this._validationGroup = group;
- this.groupSubscription = this._groupValidationHandler.bind(this);
- groupConfig.on("validated", this.groupSubscription)
- },
- _unsubscribeGroup: function() {
- var groupConfig = ValidationEngine.getGroupConfig(this._validationGroup);
- groupConfig && groupConfig.off("validated", this.groupSubscription)
- },
- _getOrderedItems: function(validators, items) {
- var orderedItems = [];
- iteratorUtils.each(validators, function(_, validator) {
- var firstItem = grep(items, function(item) {
- if (item.validator === validator) {
- return true
- }
- })[0];
- if (firstItem) {
- orderedItems.push(firstItem)
- }
- });
- return orderedItems
- },
- _groupValidationHandler: function(params) {
- var that = this;
- var items = that._getOrderedItems(params.validators, iteratorUtils.map(params.brokenRules, function(rule) {
- return {
- text: rule.message,
- validator: rule.validator
- }
- }));
- that.validators = params.validators;
- iteratorUtils.each(that.validators, function(_, validator) {
- if (validator._validationSummary !== this) {
- var handler = that._itemValidationHandler.bind(that);
- var disposingHandler = function() {
- validator.off("validated", handler);
- validator._validationSummary = null;
- handler = null
- };
- validator.on("validated", handler);
- validator.on("disposing", disposingHandler);
- validator._validationSummary = this
- }
- });
- that.option("items", items)
- },
- _itemValidationHandler: function(itemValidationResult) {
- var items = this.option("items");
- var isValid = itemValidationResult.isValid;
- var elementIndex;
- var replacementFound = false;
- var newMessage = itemValidationResult.brokenRule && itemValidationResult.brokenRule.message;
- var validator = itemValidationResult.validator;
- iteratorUtils.each(items, function(index, item) {
- if (item.validator === validator) {
- if (isValid) {
- elementIndex = index
- } else {
- item.text = newMessage
- }
- replacementFound = true;
- return false
- }
- });
- if (isValid ^ replacementFound) {
- return
- }
- if (isValid) {
- items.splice(elementIndex, 1)
- } else {
- items.push({
- text: newMessage,
- validator: validator
- })
- }
- items = this._getOrderedItems(this.validators, items);
- this.option("items", items)
- },
- _initMarkup: function() {
- this.$element().addClass(VALIDATION_SUMMARY_CLASS);
- this.callBase()
- },
- _optionChanged: function(args) {
- switch (args.name) {
- case "validationGroup":
- this._initGroupRegistration();
- break;
- default:
- this.callBase(args)
- }
- },
- _itemClass: function() {
- return ITEM_CLASS
- },
- _itemDataKey: function() {
- return ITEM_DATA_KEY
- },
- _postprocessRenderItem: function(params) {
- eventsEngine.on(params.itemElement, "click", function() {
- params.itemData.validator && params.itemData.validator.focus && params.itemData.validator.focus()
- })
- },
- _dispose: function() {
- this.callBase();
- this._unsubscribeGroup()
- }
- }).include(ValidationMixin);
- registerComponent("dxValidationSummary", ValidationSummary);
- module.exports = ValidationSummary;
- module.exports.default = module.exports;
|