All files / web/bundles/pimui/js/product/field simple-select-field.js

84.09% Statements 37/44
68.42% Branches 13/19
87.5% Functions 14/16
84.09% Lines 37/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                  147x                           147x                           1422x 1422x 1422x   1422x                                                   1422x             1422x 1422x 1422x         27x                   27x 5x   5x     22x       22x 143x     22x       374x 374x 374x         250x 250x 250x     374x 371x   371x 14x   357x   371x               1422x                   1376x                           25x 25x   25x   25x                     500x                
'use strict';
/**
 * Simple select field
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'pim/field',
        'underscore',
        'pim/template/product/field/simple-select',
        'routing',
        'pim/attribute-option/create',
        'pim/security-context',
        'pim/initselect2',
        'pim/user-context',
        'pim/i18n'
    ],
    function ($, Field, _, fieldTemplate, Routing, createOption, SecurityContext, initSelect2, UserContext, i18n) {
        return Field.extend({
            fieldTemplate: _.template(fieldTemplate),
            choicePromise: null,
            promiseIdentifier: null,
            choiceUrl: null,
            events: {
                'change .field-input:first input[type="hidden"].select-field': 'updateModel',
                'click .add-attribute-option': 'createOption'
            },
 
            /**
             * {@inheritdoc}
             */
            getTemplateContext: function () {
                return Field.prototype.getTemplateContext.apply(this, arguments).then(function (templateContext) {
                    var isAllowed = SecurityContext.isGranted('pim_enrich_attribute_edit');
                    templateContext.userCanAddOption = this.editable && isAllowed;
 
                    return templateContext;
                }.bind(this));
            },
 
            /**
             * Create a new option for this simple select field
             */
            createOption: function () {
                if (!SecurityContext.isGranted('pim_enrich_attribute_edit')) {
                    return;
                }
 
                createOption(this.attribute).then(function (option) {
                    if (this.isEditable()) {
                        this.setCurrentValue(option.code);
                    }
 
                    this.choicePromise = null;
                    this.render();
                }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            renderInput: function (context) {
                return this.fieldTemplate(context);
            },
 
            /**
             * {@inheritdoc}
             */
            postRender: function () {
                this.$('[data-toggle="tooltip"]').tooltip();
                this.getChoiceUrl().then(function (choiceUrl) {
                    var options = {
                        ajax: {
                            url: choiceUrl,
                            cache: true,
                            data: function (term, page) {
                                return {
                                    search: term,
                                    options: {
                                        limit: 20,
                                        page: page,
                                        catalogLocale: UserContext.get('catalogLocale')
                                    }
                                };
                            },
                            results: function (response) {
                                if (response.results) {
                                    response.more = 20 === _.keys(response.results).length;
 
                                    return response;
                                }
 
                                var data = {
                                    more: 20 === _.keys(response).length,
                                    results: []
                                };
                                _.each(response, function (value) {
                                    data.results.push(this.convertBackendItem(value));
                                }.bind(this));
 
                                return data;
                            }.bind(this)
                        },
                        initSelection: function (element, callback) {
                            var id = $(element).val();
                            Eif ('' !== id) {
                                if (
                                    null === this.choicePromise
                                    || this.promiseIdentifier !== id
                                    || this.choiceUrl !== choiceUrl
                                ) {
                                    this.choiceUrl = choiceUrl;
                                    this.choicePromise = $.get(choiceUrl, {options: {identifiers: [id]}});
                                    this.promiseIdentifier = id;
                                }
 
                                this.choicePromise.then(function (response) {
                                    var selected = _.findWhere(response, {code: id});
 
                                    if (!selected) {
                                        selected = _.findWhere(response.results, {id: id});
                                    } else {
                                        selected = this.convertBackendItem(selected);
                                    }
                                    callback(selected);
                                }.bind(this));
                            }
                        }.bind(this),
                        placeholder: ' ',
                        allowClear: true
                    };
 
                    initSelect2.init(this.$('input.select-field'), options);
                }.bind(this));
            },
 
            /**
             * Get the URL to retrieve the choice list for this select field
             *
             * @returns {Promise}
             */
            getChoiceUrl: function () {
                return $.Deferred().resolve(
                    Routing.generate(
                        'pim_enrich_attributeoption_get',
                        {
                            identifier: this.attribute.code
                        }
                    )
                ).promise();
            },
 
            /**
             * {@inheritdoc}
             */
            updateModel: function () {
                var data = this.$('.field-input:first input[type="hidden"].select-field').val();
                data = '' === data ? this.attribute.empty_value : data;
 
                this.choicePromise = null;
 
                this.setCurrentValue(data);
            },
 
            /**
             * Convert the item returned from the backend to fit select2 needs
             *
             * @param {object} item
             *
             * @return {object}
             */
            convertBackendItem: function (item) {
                return {
                    id: item.code,
                    text: i18n.getLabel(item.labels, UserContext.get('catalogLocale'), item.code)
                };
            }
        });
    }
);