Skip to content
Code-Schnipsel Gruppen Projekte
CodeGeneratorSelect.js 2,47 KiB
Newer Older
Patrick Müller's avatar
Patrick Müller committed
/**
 * Select available CodeGenerators
 *
 * @module package/quiqqer/coupons/bin/backend/controls/settings/CodeGeneratorSelect
Patrick Müller's avatar
Patrick Müller committed
 * @author www.pcsg.de (Patrick Müller)
 */
define('package/quiqqer/coupons/bin/backend/controls/settings/CodeGeneratorSelect', [
Patrick Müller's avatar
Patrick Müller committed

    'qui/controls/buttons/Select',
    'qui/controls/loader/Loader',

    'Locale',
    'Ajax',

    'css!package/quiqqer/coupons/bin/backend/controls/settings/CodeGeneratorSelect.css'
Patrick Müller's avatar
Patrick Müller committed

], function (QUISelect, QUILoader, QUILocale, QUIAjax) {
    "use strict";

    const lg = 'quiqqer/coupons';
Patrick Müller's avatar
Patrick Müller committed

    return new Class({
        Extends: QUISelect,
        Type: 'package/quiqqer/coupons/bin/backend/controls/settings/CodeGeneratorSelect',
Patrick Müller's avatar
Patrick Müller committed

        Binds: [
            '$onImport'
        ],

        options: {
            showIcons: false
        },

        initialize: function (options) {
            this.parent(options);

            this.$Input = null;
            this.Loader = new QUILoader();

            this.addEvents({
                onImport: this.$onImport
            });
        },

        /**
         * Event: onImport
         */
        $onImport: function () {
            const self = this;
Patrick Müller's avatar
Patrick Müller committed

            this.$Input = this.getElm();
Patrick Müller's avatar
Patrick Müller committed
            this.$Input.hidden = true;

            const Elm = this.create().inject(this.$Input, 'after');
Patrick Müller's avatar
Patrick Müller committed

            Elm.addClass('field-container-field');

            this.Loader.inject(Elm);
            this.Loader.show();

            this.$getCodeGenerators().then(function (codeGenerators) {
Patrick Müller's avatar
Patrick Müller committed
                self.Loader.hide();

                for (let i = 0, len = codeGenerators.length; i < len; i++) {
Patrick Müller's avatar
Patrick Müller committed
                    self.appendChild(
                        codeGenerators[i],
                        codeGenerators[i]
                    );
                }

                if (self.$Input.value !== '') {
                    self.setValue(self.$Input.value);
                }

                self.addEvent('onChange', function (value) {
Patrick Müller's avatar
Patrick Müller committed
                    self.$Input.value = value;
                });
            });
        },

        /**
         * Get list of all CodeGenerators
         *
         * @return {Promise}
         */
        $getCodeGenerators: function () {
            return new Promise(function (resolve, reject) {
Patrick Müller's avatar
Patrick Müller committed
                QUIAjax.get('package_quiqqer_coupons_ajax_settings_getCodeGenerators', resolve, {
Patrick Müller's avatar
Patrick Müller committed
                    'package': 'quiqqer/coupons',
                    onError: reject
Patrick Müller's avatar
Patrick Müller committed
                });
            });
        }
    });
});