bundle-dynamic-rows-grid.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore',
  7. 'Magento_Ui/js/dynamic-rows/dynamic-rows-grid'
  8. ], function (_, dynamicRowsGrid) {
  9. 'use strict';
  10. return dynamicRowsGrid.extend({
  11. defaults: {
  12. label: '',
  13. columnsHeader: false,
  14. columnsHeaderAfterRender: true,
  15. addButton: false,
  16. isDefaultFieldScope: 'is_default',
  17. defaultRecords: {
  18. use: [],
  19. moreThanOne: false,
  20. state: {}
  21. },
  22. listens: {
  23. inputType: 'onInputTypeChange',
  24. isDefaultValue: 'onIsDefaultValue'
  25. }
  26. },
  27. /**
  28. * Handler for type select.
  29. *
  30. * @param {String} inputType - changed.
  31. */
  32. onInputTypeChange: function (inputType) {
  33. if (this.defaultRecords.moreThanOne && (inputType === 'radio' || inputType === 'select')) {
  34. _.each(this.defaultRecords.use, function (index, counter) {
  35. this.source.set(
  36. this.dataScope + '.bundle_selections.' + index + '.' + this.isDefaultFieldScope,
  37. counter ? '0' : '1'
  38. );
  39. }.bind(this));
  40. }
  41. },
  42. /**
  43. * Handler for is_default field.
  44. *
  45. * @param {Object} data - changed data.
  46. */
  47. onIsDefaultValue: function (data) {
  48. var cb,
  49. use = 0;
  50. this.defaultRecords.use = [];
  51. cb = function (elem, key) {
  52. if (~~elem) {
  53. this.defaultRecords.use.push(key);
  54. use++;
  55. }
  56. this.defaultRecords.moreThanOne = use > 1;
  57. }.bind(this);
  58. _.each(data, cb);
  59. },
  60. /**
  61. * Initialize elements from grid
  62. *
  63. * @param {Array} data
  64. *
  65. * @returns {Object} Chainable.
  66. */
  67. initElements: function (data) {
  68. var newData = this.getNewData(data),
  69. recordIndex;
  70. this.parsePagesData(data);
  71. if (newData.length) {
  72. if (this.insertData().length) {
  73. recordIndex = data.length - newData.length - 1;
  74. _.each(newData, function (newRecord) {
  75. this.processingAddChild(newRecord, ++recordIndex, newRecord[this.identificationProperty]);
  76. }, this);
  77. }
  78. }
  79. return this;
  80. },
  81. /**
  82. * Mapping value from grid
  83. *
  84. * @param {Array} data
  85. */
  86. mappingValue: function (data) {
  87. if (_.isEmpty(data)) {
  88. return;
  89. }
  90. this._super();
  91. }
  92. });
  93. });