Skip to content

Commit

Permalink
MAGETWO-56018: [GitHub] Fail to import custom options #5573
Browse files Browse the repository at this point in the history
  • Loading branch information
Olga Nakonechna committed Sep 14, 2016
1 parent 37d2cd8 commit 19385aa
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ protected function getOptionsGridConfig($sortOrder)
'collapsibleHeader' => true,
'sortOrder' => $sortOrder,
'dataProvider' => static::CUSTOM_OPTIONS_LISTING,
'links' => ['insertData' => '${ $.provider }:${ $.dataProvider }'],
'imports' => ['insertData' => '${ $.provider }:${ $.dataProvider }'],
],
],
],
Expand Down Expand Up @@ -478,7 +478,7 @@ protected function getImportOptionsModalConfig()
'render_url' => $this->urlBuilder->getUrl('mui/index/render'),
'realTimeLink' => true,
'behaviourType' => 'edit',
'externalFilterMode' => true,
'externalFilterMode' => false,
'currentProductId' => $this->locator->getProduct()->getId(),
'dataLinks' => [
'imports' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,108 @@
*/

define([
'Magento_Ui/js/dynamic-rows/dynamic-rows',
'Magento_Ui/js/dynamic-rows/dynamic-rows-grid',
'underscore',
'mageUtils'
], function (DynamicRows, utils) {
], function (DynamicRows, _, utils) {
'use strict';

var maxId = 0,

/**
* Stores max option_id value of the options from recordData once on initialization
* @param {Array} data - array with records data
*/
initMaxId = function (data) {
if (data && data.length) {
maxId = ~~_.max(data, function (record) {
return ~~record['option_id'];
})['option_id'];
}
};

return DynamicRows.extend({
defaults: {
dataProvider: '',
insertData: [],
listens: {
'insertData': 'processingInsertData'
}
mappingSettings: {
enabled: false,
distinct: false
},
update: true,
map: {
'option_id': 'option_id'
},
identificationProperty: 'option_id',
identificationDRProperty: 'option_id'
},

/**
* Calls 'initObservable' of parent
*
* @returns {Object} Chainable.
*/
initObservable: function () {
this._super()
.observe([
'insertData'
]);
/** @inheritdoc */
initialize: function () {
this._super();
initMaxId(this.recordData());

return this;
},

/**
* Parsed data
*
* @param {Array} data - array with data
* about selected records
*/
/** @inheritdoc */
processingInsertData: function (data) {
if (!data.length) {
return false;
}
var options = [],
currentOption;

data.each(function (options) {
options.options.each(function (option) {
var path = this.dataScope + '.' + this.index + '.' + this.recordIterator,
curOption = utils.copy(option);
if (!data) {
return;
}
data.each(function (item) {
if (!item.options) {
return;
}
item.options.each(function (option) {
currentOption = utils.copy(option);

if (curOption.hasOwnProperty('sort_order')) {
delete curOption['sort_order'];
if (currentOption.hasOwnProperty('sort_order')) {
delete currentOption['sort_order'];
}
currentOption['option_id'] = ++maxId;
options.push(currentOption);
});
});

this.source.set(path, curOption);
this.addChild(curOption, false);
}, this);
if (!options.length) {
return;
}
this.cacheGridData = options;
options.each(function (opt) {
this.mappingValue(opt);
}, this);

this.insertData([]);
},

/**
* Set empty array to dataProvider
*/
clearDataProvider: function () {
this.source.set(this.dataProvider, []);
},

/** @inheritdoc */
processingAddChild: function (ctx, index, prop) {
if (ctx && !_.isNumber(ctx['option_id'])) {
ctx['option_id'] = ++maxId;
} else if (!ctx) {
this.showSpinner(true);
this.addChild(ctx, index, prop);

return;
}

this._super(ctx, index, prop);
},

/**
* Mutes parent method
*/
updateInsertData: function () {
return false;
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ define([
listens: {
'insertData': 'processingInsertData',
'recordData': 'initElements setToInsertData'
},
mappingSettings: {
enabled: true,
distinct: true
}
},

Expand Down Expand Up @@ -100,10 +104,20 @@ define([
* @param {String|Number} recordId
*/
deleteRecord: function (index, recordId) {
this._super();

this.updateInsertData(recordId);
},

/**
* Updates insertData when record is deleted
*
* @param {String|Number} recordId
*/
updateInsertData: function (recordId) {
var data = this.getElementData(this.insertData(), recordId),
prop = this.map[this.identificationDRProperty];

this._super();
this.insertData(_.reject(this.source.get(this.dataProvider), function (recordData) {
return ~~recordData[prop] === ~~data[prop];
}, this));
Expand Down Expand Up @@ -163,7 +177,7 @@ define([
var changes = [],
tmpObj = {};

if (data.length !== this.relatedData) {
if (data.length !== this.relatedData.length) {
data.forEach(function (obj) {
tmpObj[this.identificationDRProperty] = obj[this.identificationDRProperty];

Expand Down Expand Up @@ -210,21 +224,27 @@ define([
var obj = {},
tmpObj = {};

_.each(this.map, function (prop, index) {
obj[index] = !_.isUndefined(data[prop]) ? data[prop] : '';
}, this);
if (this.mappingSettings.enabled) {
_.each(this.map, function (prop, index) {
obj[index] = !_.isUndefined(data[prop]) ? data[prop] : '';
}, this);
} else {
obj = data;
}

tmpObj[this.identificationDRProperty] = obj[this.identificationDRProperty];
if (this.mappingSettings.distinct) {
tmpObj[this.identificationDRProperty] = obj[this.identificationDRProperty];

if (_.findWhere(this.recordData(), tmpObj)) {
return false;
}
}

if (!obj.hasOwnProperty(this.positionProvider)) {
this.setMaxPosition();
obj[this.positionProvider] = this.maxPosition;
}

if (_.findWhere(this.recordData(), tmpObj)) {
return false;
}

this.source.set(this.dataScope + '.' + this.index + '.' + this.recordData().length, obj);
},

Expand Down
3 changes: 2 additions & 1 deletion lib/web/mage/requirejs/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ define(['module'], function (module) {
header,
errorHandler = fail || Function();

xhr.open('GET', url);

/*eslint-disable max-depth */
if ('setRequestHeader' in xhr && headers) {
for (header in headers) {
Expand Down Expand Up @@ -127,7 +129,6 @@ define(['module'], function (module) {
}
};

xhr.open('GET', url);
xhr.send();
}

Expand Down

0 comments on commit 19385aa

Please sign in to comment.