fpt-group.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'Magento_Ui/js/form/components/group',
  7. 'uiRegistry',
  8. 'Magento_Ui/js/lib/validation/validator',
  9. 'mage/translate',
  10. 'underscore'
  11. ], function (Group, uiRegistry, validation, $t, _) {
  12. 'use strict';
  13. return Group.extend({
  14. defaults: {
  15. visible: true,
  16. label: '',
  17. showLabel: true,
  18. required: false,
  19. template: 'ui/group/group',
  20. fieldTemplate: 'ui/form/field',
  21. breakLine: true,
  22. validateWholeGroup: false,
  23. additionalClasses: {}
  24. },
  25. /** @inheritdoc */
  26. initialize: function () {
  27. validation.addRule('validate-fpt-group', function (value) {
  28. if (value.indexOf('?') !== -1) {
  29. return false;
  30. }
  31. return true;
  32. }, $t(
  33. 'Set unique country-state combinations within the same fixed product tax. ' +
  34. 'Verify the combinations and try again.'
  35. ));
  36. this._super();
  37. },
  38. /**
  39. *
  40. * @private
  41. */
  42. _handleOptionsAvailability: function () {
  43. var parent,
  44. dup;
  45. dup = {};
  46. parent = uiRegistry.get(uiRegistry.get(this.parentName).parentName);
  47. _.each(parent.elems(), function (elem) {
  48. var country,
  49. state,
  50. val,
  51. key;
  52. country = uiRegistry.get(elem.name + '.countryState.country');
  53. state = uiRegistry.get(elem.name + '.countryState.state');
  54. val = uiRegistry.get(elem.name + '.countryState.val');
  55. key = country.value() + (state.value() > 0 ? state.value() : 0);
  56. dup[key]++;
  57. if (!dup[key]) {
  58. dup[key] = 1;
  59. val.value('');
  60. } else {
  61. dup[key]++;
  62. val.value(country.value() + '?' + country.name);
  63. }
  64. });
  65. },
  66. /** @inheritdoc */
  67. initElement: function (elem) {
  68. var obj;
  69. obj = this;
  70. this._super();
  71. elem.on('value', function () {
  72. obj._handleOptionsAvailability();
  73. });
  74. return this;
  75. }
  76. });
  77. });