bundle-dynamic-rows.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore',
  7. 'mageUtils',
  8. 'uiRegistry',
  9. 'Magento_Ui/js/dynamic-rows/dynamic-rows'
  10. ], function (_, utils, registry, dynamicRows) {
  11. 'use strict';
  12. return dynamicRows.extend({
  13. defaults: {
  14. label: '',
  15. collapsibleHeader: true,
  16. columnsHeader: false,
  17. deleteProperty: false,
  18. addButton: false
  19. },
  20. /**
  21. * Set new data to dataSource,
  22. * delete element
  23. *
  24. * @param {Array} data - record data
  25. */
  26. _updateData: function (data) {
  27. var elems = _.clone(this.elems()),
  28. path,
  29. dataArr,
  30. optionBaseData;
  31. dataArr = this.recordData.splice(this.startIndex, this.recordData().length - this.startIndex);
  32. dataArr.splice(0, this.pageSize);
  33. elems = _.sortBy(this.elems(), function (elem) {
  34. return ~~elem.index;
  35. });
  36. data.concat(dataArr).forEach(function (rec, idx) {
  37. if (elems[idx]) {
  38. elems[idx].recordId = rec[this.identificationProperty];
  39. }
  40. if (!rec.position) {
  41. rec.position = this.maxPosition;
  42. this.setMaxPosition();
  43. }
  44. path = this.dataScope + '.' + this.index + '.' + (this.startIndex + idx);
  45. optionBaseData = _.pick(rec, function (value) {
  46. return !_.isObject(value);
  47. });
  48. this.source.set(path, optionBaseData);
  49. this.source.set(path + '.bundle_button_proxy', []);
  50. this.source.set(path + '.bundle_selections', []);
  51. this.removeBundleItemsFromOption(idx);
  52. _.each(rec['bundle_selections'], function (obj, index) {
  53. this.source.set(path + '.bundle_button_proxy' + '.' + index, rec['bundle_button_proxy'][index]);
  54. this.source.set(path + '.bundle_selections' + '.' + index, obj);
  55. }, this);
  56. }, this);
  57. this.elems(elems);
  58. },
  59. /**
  60. * Removes nested dynamic-rows-grid rendered records from option
  61. *
  62. * @param {Number|String} index - element index
  63. */
  64. removeBundleItemsFromOption: function (index) {
  65. var bundleSelections = registry.get(this.name + '.' + index + '.' + this.bundleSelectionsName),
  66. bundleSelectionsLength = (bundleSelections.elems() || []).length,
  67. i;
  68. if (bundleSelectionsLength) {
  69. for (i = 0; i < bundleSelectionsLength; i++) {
  70. bundleSelections.elems()[0].destroy();
  71. }
  72. }
  73. },
  74. /**
  75. * {@inheritdoc}
  76. */
  77. processingAddChild: function (ctx, index, prop) {
  78. var recordIds = _.map(this.recordData(), function (rec) {
  79. return parseInt(rec['record_id'], 10);
  80. }),
  81. maxRecordId = _.max(recordIds);
  82. prop = maxRecordId > -1 ? maxRecordId + 1 : prop;
  83. this._super(ctx, index, prop);
  84. }
  85. });
  86. });