Skip to content

Commit

Permalink
Merge pull request #330 from magento-frontend/MAGETWO-52788-PR
Browse files Browse the repository at this point in the history
Story
MAGETWO-52788 Dynamic Rows: support status being changed
  • Loading branch information
VladimirZaets authored Sep 5, 2016
2 parents 5c6b2d6 + ac39341 commit c869e6f
Show file tree
Hide file tree
Showing 16 changed files with 588 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</label>

<div class="admin__field-control" data-role="grid-wrapper">
<div class="admin__control-table-pagination" visible="!!$data.recordData().length">
<div class="admin__control-table-pagination" visible="!!element.getRecordCount()">
<div class="admin__data-grid-pager">
<button class="action-previous" type="button" data-bind="attr: {title: $t('Previous Page')}, click: previousPage, disable: isFirst()"></button>
<input class="admin__control-text" type="number" data-bind="attr: {id: ++ko.uid}, value: currentPage">
Expand All @@ -51,10 +51,10 @@

<th repeat="foreach: labels, item: '$label'"
class="data-grid-th"
translate="$label().label"
visible="$label().visible"
disable="$label().disabled"
css="setClasses($label())">
<span translate="$label().label"/>
</th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function modifyData(array $data)
'is_default' => ($productLink->getIsDefault()) ? '1' : '0',
'selection_price_value' => $productLink->getPrice(),
'selection_price_type' => $productLink->getPriceType(),
'selection_qty' => (bool)$integerQty ? (int)$productLink->getQty() : $productLink->getQty(),
'selection_qty' => $integerQty ? (int)$productLink->getQty() : $productLink->getQty(),
'selection_can_change_qty' => $productLink->getCanChangeQuantity(),
'selection_qty_is_integer' => (bool)$integerQty,
'position' => $productLink->getPosition(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ define([
return Abstract.extend({
defaults: {
valueUpdate: 'input',
isInteger: true
isInteger: true,
validation: {
'validate-number': true
}
},

/**
* update event
* @inheritdoc
*/
onUpdate: function () {
this.validation['validate-number'] = true;
this.validation['validate-digits'] = this.isInteger;
this.validate();
this._super();
},

/**
* @inheritdoc
*/
hasChanged: function () {
var notEqual = this.value() !== this.initialValue.toString();

return !this.visible() ? false : notEqual;
}

});
});
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,8 +478,7 @@ protected function getImportOptionsModalConfig()
'ns' => static::CUSTOM_OPTIONS_LISTING,
'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 @@ -211,8 +211,7 @@
<actionDelete class="Magento\Ui\Component\Form\Element\ActionDelete">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
<item name="elementTmpl" xsi:type="string">ui/dynamic-rows/cells/action-delete</item>
<item name="component" xsi:type="string">Magento_Ui/js/dynamic-rows/action-delete</item>
<item name="template" xsi:type="string">ui/dynamic-rows/cells/action-delete</item>
</item>
</argument>
Expand Down
28 changes: 28 additions & 0 deletions app/code/Magento/Ui/view/base/web/js/dynamic-rows/action-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'Magento_Ui/js/form/element/abstract'
], function (Abstract) {
'use strict';

return Abstract.extend({
defaults: {
links: {
value: false
}
},

/**
* Delete record handler.
*
* @param {Number} index
* @param {Number} id
*/
deleteRecord: function (index, id) {
this.bubble('deleteRecord', index, id);
}
});
});
3 changes: 2 additions & 1 deletion app/code/Magento/Ui/view/base/web/js/dynamic-rows/dnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ define([
drEl.instance = recordNode = this.processingStyles(recordNode, elem);
drEl.instanceCtx = this.getRecord(originRecord[0]);
drEl.eventMousedownY = isTouchDevice ? event.originalEvent.touches[0].pageY : event.pageY;
drEl.minYpos = $table.offset().top - originRecord.offset().top + $table.find('thead').outerHeight();
drEl.minYpos =
$table.offset().top - originRecord.offset().top + $table.find('thead').outerHeight();
drEl.maxYpos = drEl.minYpos + $table.find('tbody').outerHeight() - originRecord.outerHeight();
$tableWrapper.append(recordNode);

Expand Down
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];
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
Loading

0 comments on commit c869e6f

Please sign in to comment.