validation_summary.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * DevExtreme (ui/validation_summary.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var registerComponent = require("../core/component_registrator");
  11. var eventsEngine = require("../events/core/events_engine");
  12. var grep = require("../core/utils/common").grep;
  13. var extend = require("../core/utils/extend").extend;
  14. var iteratorUtils = require("../core/utils/iterator");
  15. var ValidationMixin = require("./validation/validation_mixin");
  16. var ValidationEngine = require("./validation_engine");
  17. var CollectionWidget = require("./collection/ui.collection_widget.edit");
  18. var VALIDATION_SUMMARY_CLASS = "dx-validationsummary";
  19. var ITEM_CLASS = VALIDATION_SUMMARY_CLASS + "-item";
  20. var ITEM_DATA_KEY = VALIDATION_SUMMARY_CLASS + "-item-data";
  21. var ValidationSummary = CollectionWidget.inherit({
  22. _getDefaultOptions: function() {
  23. return extend(this.callBase(), {
  24. focusStateEnabled: false,
  25. noDataText: null
  26. })
  27. },
  28. _setOptionsByReference: function() {
  29. this.callBase();
  30. extend(this._optionsByReference, {
  31. validationGroup: true
  32. })
  33. },
  34. _init: function() {
  35. this.callBase();
  36. this._initGroupRegistration()
  37. },
  38. _initGroupRegistration: function() {
  39. var group = this._findGroup();
  40. var groupConfig = ValidationEngine.addGroup(group);
  41. this._unsubscribeGroup();
  42. this._groupWasInit = true;
  43. this._validationGroup = group;
  44. this.groupSubscription = this._groupValidationHandler.bind(this);
  45. groupConfig.on("validated", this.groupSubscription)
  46. },
  47. _unsubscribeGroup: function() {
  48. var groupConfig = ValidationEngine.getGroupConfig(this._validationGroup);
  49. groupConfig && groupConfig.off("validated", this.groupSubscription)
  50. },
  51. _getOrderedItems: function(validators, items) {
  52. var orderedItems = [];
  53. iteratorUtils.each(validators, function(_, validator) {
  54. var firstItem = grep(items, function(item) {
  55. if (item.validator === validator) {
  56. return true
  57. }
  58. })[0];
  59. if (firstItem) {
  60. orderedItems.push(firstItem)
  61. }
  62. });
  63. return orderedItems
  64. },
  65. _groupValidationHandler: function(params) {
  66. var that = this;
  67. var items = that._getOrderedItems(params.validators, iteratorUtils.map(params.brokenRules, function(rule) {
  68. return {
  69. text: rule.message,
  70. validator: rule.validator
  71. }
  72. }));
  73. that.validators = params.validators;
  74. iteratorUtils.each(that.validators, function(_, validator) {
  75. if (validator._validationSummary !== this) {
  76. var handler = that._itemValidationHandler.bind(that);
  77. var disposingHandler = function() {
  78. validator.off("validated", handler);
  79. validator._validationSummary = null;
  80. handler = null
  81. };
  82. validator.on("validated", handler);
  83. validator.on("disposing", disposingHandler);
  84. validator._validationSummary = this
  85. }
  86. });
  87. that.option("items", items)
  88. },
  89. _itemValidationHandler: function(itemValidationResult) {
  90. var items = this.option("items");
  91. var isValid = itemValidationResult.isValid;
  92. var elementIndex;
  93. var replacementFound = false;
  94. var newMessage = itemValidationResult.brokenRule && itemValidationResult.brokenRule.message;
  95. var validator = itemValidationResult.validator;
  96. iteratorUtils.each(items, function(index, item) {
  97. if (item.validator === validator) {
  98. if (isValid) {
  99. elementIndex = index
  100. } else {
  101. item.text = newMessage
  102. }
  103. replacementFound = true;
  104. return false
  105. }
  106. });
  107. if (isValid ^ replacementFound) {
  108. return
  109. }
  110. if (isValid) {
  111. items.splice(elementIndex, 1)
  112. } else {
  113. items.push({
  114. text: newMessage,
  115. validator: validator
  116. })
  117. }
  118. items = this._getOrderedItems(this.validators, items);
  119. this.option("items", items)
  120. },
  121. _initMarkup: function() {
  122. this.$element().addClass(VALIDATION_SUMMARY_CLASS);
  123. this.callBase()
  124. },
  125. _optionChanged: function(args) {
  126. switch (args.name) {
  127. case "validationGroup":
  128. this._initGroupRegistration();
  129. break;
  130. default:
  131. this.callBase(args)
  132. }
  133. },
  134. _itemClass: function() {
  135. return ITEM_CLASS
  136. },
  137. _itemDataKey: function() {
  138. return ITEM_DATA_KEY
  139. },
  140. _postprocessRenderItem: function(params) {
  141. eventsEngine.on(params.itemElement, "click", function() {
  142. params.itemData.validator && params.itemData.validator.focus && params.itemData.validator.focus()
  143. })
  144. },
  145. _dispose: function() {
  146. this.callBase();
  147. this._unsubscribeGroup()
  148. }
  149. }).include(ValidationMixin);
  150. registerComponent("dxValidationSummary", ValidationSummary);
  151. module.exports = ValidationSummary;
  152. module.exports.default = module.exports;