bundle-dynamic-rows-grid.js 2.86 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

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

    return dynamicRowsGrid.extend({
        defaults: {
            label: '',
            columnsHeader: false,
            columnsHeaderAfterRender: true,
            addButton: false,
            isDefaultFieldScope: 'is_default',
            defaultRecords: {
                use: [],
                moreThanOne: false,
                state: {}
            },
            listens: {
                inputType: 'onInputTypeChange',
                isDefaultValue: 'onIsDefaultValue'
            }
        },

        /**
         * Handler for type select.
         *
         * @param {String} inputType - changed.
         */
        onInputTypeChange: function (inputType) {
            if (this.defaultRecords.moreThanOne && (inputType === 'radio' || inputType === 'select')) {
                _.each(this.defaultRecords.use, function (index, counter) {
                    this.source.set(
                        this.dataScope + '.bundle_selections.' + index + '.' + this.isDefaultFieldScope,
                        counter ? '0' : '1'
                    );
                }.bind(this));
            }
        },

        /**
         * Handler for is_default field.
         *
         * @param {Object} data - changed data.
         */
        onIsDefaultValue: function (data) {
            var cb,
                use = 0;

            this.defaultRecords.use = [];

            cb = function (elem, key) {

                if (~~elem) {
                    this.defaultRecords.use.push(key);
                    use++;
                }

                this.defaultRecords.moreThanOne = use > 1;
            }.bind(this);

            _.each(data, cb);
        },

        /**
         * Initialize elements from grid
         *
         * @param {Array} data
         *
         * @returns {Object} Chainable.
         */
        initElements: function (data) {
            var newData = this.getNewData(data),
                recordIndex;

            this.parsePagesData(data);

            if (newData.length) {
                if (this.insertData().length) {
                    recordIndex = data.length - newData.length - 1;

                    _.each(newData, function (newRecord) {
                        this.processingAddChild(newRecord, ++recordIndex, newRecord[this.identificationProperty]);
                    }, this);
                }
            }

            return this;
        },

        /**
         * Mapping value from grid
         *
         * @param {Array} data
         */
        mappingValue: function (data) {
            if (_.isEmpty(data)) {
                return;
            }

            this._super();
        }
    });
});