123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @api
- */
- define([
- 'underscore',
- './dynamic-rows'
- ], function (_, dynamicRows) {
- 'use strict';
- return dynamicRows.extend({
- defaults: {
- dataProvider: '',
- insertData: [],
- map: null,
- cacheGridData: [],
- deleteProperty: false,
- positionProvider: 'position',
- dataLength: 0,
- identificationProperty: 'id',
- identificationDRProperty: 'id',
- listens: {
- 'insertData': 'processingInsertData',
- 'recordData': 'initElements setToInsertData'
- },
- mappingSettings: {
- enabled: true,
- distinct: true
- }
- },
- /**
- * Calls 'initObservable' of parent
- *
- * @returns {Object} Chainable.
- */
- initObservable: function () {
- this._super()
- .observe([
- 'insertData'
- ]);
- return this;
- },
- /**
- * Set data from recordData to insertData
- */
- setToInsertData: function () {
- var insertData = [],
- obj;
- if (this.recordData().length && !this.update) {
- _.each(this.recordData(), function (recordData) {
- obj = {};
- obj[this.map[this.identificationProperty]] = recordData[this.identificationProperty];
- insertData.push(obj);
- }, this);
- if (insertData.length) {
- this.source.set(this.dataProvider, insertData);
- }
- }
- },
- /**
- * Initialize children
- *
- * @returns {Object} Chainable.
- */
- initChildren: function () {
- this.getChildItems().forEach(function (data, index) {
- this.processingAddChild(data, this.startIndex + index, data[this.identificationDRProperty]);
- }, this);
- return this;
- },
- /**
- * Initialize elements from grid
- *
- * @param {Array} data
- *
- * @returns {Object} Chainable.
- */
- initElements: function (data) {
- var newData = this.getNewData(data);
- this.parsePagesData(data);
- if (newData.length) {
- if (this.insertData().length) {
- this.processingAddChild(newData[0], data.length - 1, newData[0][this.identificationProperty]);
- }
- }
- return this;
- },
- /**
- * Delete record instance
- * update data provider dataScope
- *
- * @param {String|Number} index - record index
- * @param {String|Number} recordId
- */
- deleteRecord: function (index, recordId) {
- this.updateInsertData(recordId);
- this._super();
- },
- /**
- * 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.insertData(_.reject(this.source.get(this.dataProvider), function (recordData) {
- return recordData[prop].toString() === data[prop].toString();
- }, this));
- },
- /**
- * Find data object by index
- *
- * @param {Array} array - data collection
- * @param {Number} index - element index
- * @param {String} property - to find by property
- *
- * @returns {Object} data object
- */
- getElementData: function (array, index, property) {
- var obj = {},
- result;
- property ? obj[property] = index : obj[this.map[this.identificationDRProperty]] = index;
- result = _.findWhere(array, obj);
- if (!result) {
- property ?
- obj[property] = index.toString() :
- obj[this.map[this.identificationDRProperty]] = index.toString();
- }
- result = _.findWhere(array, obj);
- return result;
- },
- /**
- * Processing pages before addChild
- *
- * @param {Object} ctx - element context
- * @param {Number|String} index - element index
- * @param {Number|String} prop - additional property to element
- */
- processingAddChild: function (ctx, index, prop) {
- if (this._elems.length > this.pageSize) {
- return false;
- }
- this.showSpinner(true);
- this.addChild(ctx, index, prop);
- },
- /**
- * Contains old data with new
- *
- * @param {Array} data
- *
- * @returns {Array} changed data
- */
- getNewData: function (data) {
- var changes = [],
- tmpObj = {};
- if (data.length !== this.relatedData.length) {
- _.each(data, function (obj) {
- tmpObj[this.identificationDRProperty] = obj[this.identificationDRProperty];
- if (!_.findWhere(this.relatedData, tmpObj)) {
- changes.push(obj);
- }
- }, this);
- }
- return changes;
- },
- /**
- * Processing insert data
- *
- * @param {Object} data
- */
- processingInsertData: function (data) {
- var changes,
- obj = {};
- changes = this._checkGridData(data);
- this.cacheGridData = data;
- if (changes.length) {
- obj[this.identificationDRProperty] = changes[0][this.map[this.identificationProperty]];
- if (_.findWhere(this.recordData(), obj)) {
- return false;
- }
- changes.forEach(function (changedObject) {
- this.mappingValue(changedObject);
- }, this);
- }
- },
- /**
- * Mapping value from grid
- *
- * @param {Array} data
- */
- mappingValue: function (data) {
- var obj = {},
- tmpObj = {};
- if (this.mappingSettings.enabled) {
- _.each(this.map, function (prop, index) {
- obj[index] = !_.isUndefined(data[prop]) ? data[prop] : '';
- }, this);
- } else {
- obj = data;
- }
- 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;
- }
- this.source.set(this.dataScope + '.' + this.index + '.' + this.recordData().length, obj);
- },
- /**
- * Check changed records
- *
- * @param {Array} data - array with records data
- * @returns {Array} Changed records
- */
- _checkGridData: function (data) {
- var cacheLength = this.cacheGridData.length,
- curData = data.length,
- max = cacheLength > curData ? this.cacheGridData : data,
- changes = [],
- obj = {};
- max.forEach(function (record, index) {
- obj[this.map[this.identificationDRProperty]] = record[this.map[this.identificationDRProperty]];
- if (!_.where(this.cacheGridData, obj).length) {
- changes.push(data[index]);
- }
- }, this);
- return changes;
- }
- });
- });
|