All files / web/bundles/pimui/js pim-initselect2.js

26.32% Statements 15/57
11.11% Branches 2/18
31.58% Functions 6/19
26.32% Lines 15/57

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  1183x         1183x         729x     466x     10x       14979x   14979x                         14979x                               14979x       14979x 10083x 10083x     14979x                                                                                                                                                             10083x   10083x          
/* jshint unused:vars */
define(
    ['jquery', 'underscore', 'pim/formatter/choices/base', 'pim/user-context', 'jquery.select2'],
    function ($, _, ChoicesFormatter, UserContext) {
        'use strict';
 
        return {
            resultsPerPage: 20,
            defaultOptions: {
                allowClear: false,
                formatSearching: function () {
                    return _.__('pim_common.select2.search');
                },
                formatNoMatches: function () {
                    return _.__('pim_common.select2.no_match');
                },
                formatLoadMore: function () {
                    return _.__('pim_common.select2.load_more');
                }
            },
            init: function ($target, options) {
                var self = this;
 
                $target.find('input.select2:not(.select2-offscreen)').each(function () {
                    var options = self.initOptions(options);
 
                    var $el = $(this);
                    var value = _.map(_.compact($el.val().split(',')), $.trim);
                    var tags  = _.map(_.compact($el.attr('data-tags').split(',')), $.trim);
 
                    $el.select2($.extend(true, options, {
                        tags: _.union(tags, value).sort(),
                        tokenSeparators: [',', ' ']
                    }));
                });
 
                $target.find('select.select2:not(.select2-offscreen)').each(function () {
                    var options = self.initOptions(options);
 
                    var $el = $(this);
                    var $empty = $el.children('[value=""]');
 
                    if ($empty.length && $empty.html()) {
                        $el.attr('data-placeholder', $empty.html());
                        $empty.html('');
                    }
 
                    $el.select2($.extend(true, options, {
                        allowClear: true
                    }));
                });
 
                $target.find('input.pim-ajax-entity:not(.select2-offscreen)').each(function () {
                    self.initSelect.call(self, $(this));
                });
 
                if ($target.hasClass('select-field')) {
                    options = self.initOptions(options);
                    $target.select2('destroy').select2(options);
                }
 
                return $target;
            },
            initSelect: function ($select, options) {
                options = this.initOptions(options);
                var self = this;
                var queryTimer;
 
                if ($select.attr('data-multiple')) {
                    options.multiple = true;
                }
                if (!options.multiple) {
                    if (!$select.attr('data-required')) {
                        options.allowClear = true;
                    }
                    options.placeholder = ' ';
                }
 
                options.minimumInputLength = $select.attr('data-min-input-length');
                options.query = function (options) {
                    var page = 1;
 
                    if (options.context && options.context.page) {
                        page = options.context.page;
                    }
 
                    window.clearTimeout(queryTimer);
                    queryTimer = window.setTimeout(function () {
                        $.ajax({
                            url: $select.attr('data-url'),
                            data: {
                                search: options.term,
                                options: {
                                    limit: self.resultsPerPage,
                                    page: page,
                                    locale: UserContext.get('catalogLocale')
                                }
                            },
                            dataType: 'json',
                            type: 'GET',
                            success: function (data) {
                                if (_.isUndefined(data.results)) {
                                    data.results = ChoicesFormatter.format(data);
                                }
                                options.callback({
                                    results: data.results,
                                    more: data.results.length === self.resultsPerPage,
                                    context: {
                                        page: page + 1
                                    }
                                });
                            }
                        });
                    }, 400);
                };
                options.initSelection = function (element, callback) {
                    var choices = $.parseJSON($select.attr('data-choices'));
 
                    callback(choices);
                };
 
                $select.select2(options);
            },
            getSelectOptions: function (data) {
                return data;
            },
            getAjaxParameters: function () {
                return {};
            },
            hasCachableResults: function () {
                return true;
            },
            matchLocalResults: function (data, term) {
                var matchingResults = _.filter(data.results, function (result) {
                    return $.fn.select2.defaults.matcher(term, result.text);
                });
 
                return _.extend({}, data, {results: matchingResults});
            },
            initOptions: function (options) {
                var defaultOptions = $.extend(true, {}, this.defaultOptions);
 
                return $.extend(true, defaultOptions, options);
            }
        };
    }
);