bundle-checkbox.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'Magento_Ui/js/form/element/single-checkbox',
  7. 'uiRegistry'
  8. ], function (Checkbox, registry) {
  9. 'use strict';
  10. return Checkbox.extend({
  11. defaults: {
  12. clearing: false,
  13. parentContainer: '',
  14. parentSelections: '',
  15. changer: '',
  16. exports: {
  17. value: '${$.parentName}:isDefaultValue'
  18. }
  19. },
  20. /**
  21. * @inheritdoc
  22. */
  23. initObservable: function () {
  24. this._super().
  25. observe('elementTmpl');
  26. return this;
  27. },
  28. /**
  29. * @inheritdoc
  30. */
  31. initConfig: function () {
  32. this._super();
  33. this.imports.changeType = this.retrieveParentName(this.parentContainer) + '.' + this.changer + ':value';
  34. return this;
  35. },
  36. /**
  37. * @inheritdoc
  38. */
  39. onUpdate: function () {
  40. if (this.prefer === 'radio' && this.checked() && !this.clearing) {
  41. this.clearValues();
  42. }
  43. this._super();
  44. },
  45. /**
  46. * Checkbox to radio type changer.
  47. *
  48. * @param {String} type - type to change.
  49. */
  50. changeType: function (type) {
  51. var typeMap = registry.get(this.retrieveParentName(this.parentContainer) + '.' + this.changer).typeMap;
  52. this.prefer = typeMap[type];
  53. this.elementTmpl(this.templates[typeMap[type]]);
  54. },
  55. /**
  56. * Clears values in components like this.
  57. */
  58. clearValues: function () {
  59. var records = registry.get(this.retrieveParentName(this.parentSelections)),
  60. index = this.index,
  61. uid = this.uid;
  62. records.elems.each(function (record) {
  63. record.elems.filter(function (comp) {
  64. return comp.index === index && comp.uid !== uid;
  65. }).each(function (comp) {
  66. comp.clearing = true;
  67. comp.clear();
  68. comp.clearing = false;
  69. });
  70. });
  71. },
  72. /**
  73. * Retrieve name for the most global parent with provided index.
  74. *
  75. * @param {String} parent - parent name.
  76. * @returns {String}
  77. */
  78. retrieveParentName: function (parent) {
  79. return this.name.replace(new RegExp('^(.+?\\.)?' + parent + '\\..+'), '$1' + parent);
  80. }
  81. });
  82. });