<commit_before>/*
 * This file is part of the Sulu CMS.
 *
 * (c) MASSIVE ART WebServices GmbH
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

define([
    'config',
    'suluproduct/models/product-addon',
    'text!suluproduct/components/products/components/addons/overlay.html',
    'text!suluproduct/components/products/components/addons/price.html'
], function(Config, ProductAddon, OverlayTemplate, PriceTemplate) {
    'use strict';

    var currencies = null,
        currentSelectedAddon = null,
        currentProductAddon = null,

        constants = {
            datagridInstanceName: 'product-addon-datagrid',
            toolbarInstanceName: 'product-addon-toolbar',
            overlayInstanceName: 'product-addon-overlay',
            selectInstanceName: 'product-addon-select',
            priceListElementId: '#addon-price-list',
            loaderElementClass: '.loader'
        },

        /**
         * Bind custom events.
         */
        bindCustomEvents = function() {
            // Delete product.
            this.sandbox.on('sulu.toolbar.delete', function() {
                this.sandbox.emit('sulu.product.delete', this.options.data.id);
            }.bind(this));

            // Listen for status changes, enable the save button.
            this.sandbox.on('product.state.change', function(status) {
                if (!this.options.data ||
                    !this.options.data.attributes.status ||
                    this.options.data.attributes.status.id !== status.id
                ) {
                    this.status = status;
                    this.options.data.attributes.status = this.status;
                    setHeaderBar.call(this, false);
                }
            }, this);

            // Save status if the save button in the toolbar is clicked.
            this.sandbox.on('sulu.toolbar.save', function() {
                saveProduct.call(this);
            }, this);

            // Listen on the saved event for changes in the status of the product.
            this.sandbox.on('sulu.products.saved', function() {
                setHeaderBar.call(this, true);
                this.options.data.attributes.status = this.status;
            }, this);

            // Enable toolbar items if elements are selected.
            this.sandbox.on('husky.datagrid.' + constants.datagridInstanceName + '.number.selections', function(number) {
                var action = number > 0 ? 'enable' : 'disable';
                this.sandbox.emit(
                    'husky.toolbar.' + constants.toolbarInstanceName + '.item.' + action,
                    'deleteSelected',
                    false)
            }, this);

            // Start prices component if a product is selected in the addon auto-complete search.
            this.sandbox.on('husky.auto-complete.addons-search.select', function(selectedAddon) {
                // Remove prices (only important if another product was already selected before).
                this.sandbox.dom.empty(this.$find(constants.priceListElementId));

                // Start the price list loader.
                startLoader.call(this, constants.priceListElementId);

                var selectedAddon = $.getJSON('api/products/' + selectedAddon.id + '?locale=' + this.options.locale);
                selectedAddon.done(function(data) {
                    startOverlayPricesComponent.call(this, data, null);
                }.bind(this));
            }, this);
        },

        /**
         * Bind dom events.
         */
        bindDomEvents = function() {
            this.sandbox.dom.on(this.$el, 'change', function(event) {
                var $target = $(event.currentTarget);
                if (!!$target[0]) {
                    var currency = $target[0].dataset.currency;
                    var checked = $target[0].checked;
                    var $priceField = this.sandbox.dom.find('#addon-price-' + currency);

                    if (!!$priceField[0]) {
                        $priceField[0].disabled = !checked;
                    }
                }
            }.bind(this), '.change-price');
        },

        /**
         * Starts a loader and adds it to the dom.
         *
         * @param string elementId
         */
        startLoader = function(elementId) {
            var $container = this.sandbox.dom.createElement('<div class="' + constants.loaderClass + '"/>');
            var $element = this.sandbox.dom.find(elementId);
            this.sandbox.dom.append($element, $container);
            this.sandbox.start([
                {
                    name: 'loader@husky',
                    options: {
                        el: $container,
                        size: '60px',
                        color: '#ccc'
                    }
                }
            ]);
        },
<commit_msg>Refactored disable of price field<commit_after>/*
 * This file is part of the Sulu CMS.
 *
 * (c) MASSIVE ART WebServices GmbH
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

define([
    'config',
    'suluproduct/models/product-addon',
    'text!suluproduct/components/products/components/addons/overlay.html',
    'text!suluproduct/components/products/components/addons/price.html'
], function(Config, ProductAddon, OverlayTemplate, PriceTemplate) {
    'use strict';

    var currencies = null,
        currentSelectedAddon = null,
        currentProductAddon = null,

        constants = {
            datagridInstanceName: 'product-addon-datagrid',
            toolbarInstanceName: 'product-addon-toolbar',
            overlayInstanceName: 'product-addon-overlay',
            selectInstanceName: 'product-addon-select',
            priceListElementId: '#addon-price-list',
            loaderElementClass: '.loader'
        },

        /**
         * Bind custom events.
         */
        bindCustomEvents = function() {
            // Delete product.
            this.sandbox.on('sulu.toolbar.delete', function() {
                this.sandbox.emit('sulu.product.delete', this.options.data.id);
            }.bind(this));

            // Listen for status changes, enable the save button.
            this.sandbox.on('product.state.change', function(status) {
                if (!this.options.data ||
                    !this.options.data.attributes.status ||
                    this.options.data.attributes.status.id !== status.id
                ) {
                    this.status = status;
                    this.options.data.attributes.status = this.status;
                    setHeaderBar.call(this, false);
                }
            }, this);

            // Save status if the save button in the toolbar is clicked.
            this.sandbox.on('sulu.toolbar.save', function() {
                saveProduct.call(this);
            }, this);

            // Listen on the saved event for changes in the status of the product.
            this.sandbox.on('sulu.products.saved', function() {
                setHeaderBar.call(this, true);
                this.options.data.attributes.status = this.status;
            }, this);

            // Enable toolbar items if elements are selected.
            this.sandbox.on('husky.datagrid.' + constants.datagridInstanceName + '.number.selections', function(number) {
                var action = number > 0 ? 'enable' : 'disable';
                this.sandbox.emit(
                    'husky.toolbar.' + constants.toolbarInstanceName + '.item.' + action,
                    'deleteSelected',
                    false)
            }, this);

            // Start prices component if a product is selected in the addon auto-complete search.
            this.sandbox.on('husky.auto-complete.addons-search.select', function(selectedAddon) {
                // Remove prices (only important if another product was already selected before).
                this.sandbox.dom.empty(this.$find(constants.priceListElementId));

                // Start the price list loader.
                startLoader.call(this, constants.priceListElementId);

                var selectedAddon = $.getJSON('api/products/' + selectedAddon.id + '?locale=' + this.options.locale);
                selectedAddon.done(function(data) {
                    startOverlayPricesComponent.call(this, data, null);
                }.bind(this));
            }, this);
        },

        /**
         * Bind dom events.
         */
        bindDomEvents = function() {
            // Disable price field if checkbox is not checked.
            this.sandbox.dom.on(this.$el, 'change', function(event) {
                var $target = $(event.currentTarget);
                var checked = $target.is(':checked');
                var $priceField = this.sandbox.dom.find('#addon-price-' + $target.data('currency'));

                $priceField.prop('disabled', !checked);
            }.bind(this), '.change-price');
        },

        /**
         * Starts a loader and adds it to the dom.
         *
         * @param string elementId
         */
        startLoader = function(elementId) {
            var $container = this.sandbox.dom.createElement('<div class="' + constants.loaderClass + '"/>');
            var $element = this.sandbox.dom.find(elementId);
            this.sandbox.dom.append($element, $container);
            this.sandbox.start([
                {
                    name: 'loader@husky',
                    options: {
                        el: $container,
                        size: '60px',
                        color: '#ccc'
                    }
                }
            ]);
        },

{'fraction_non_alphanumeric': 0.11691771697919869, 'fraction_numerical': 0.001127164668511118, 'mean_word_length': 1.6159206647011526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5155802', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2434, 'n_words': 541}