<commit_before>/**
 * scania-angular-select2
 * https://github.com/scania-bootstrap/scania-angular-select2
 * License: MIT
 *
 * @ngdoc directive
 * @name scSelect2
 * @module scania.angular.select2
 *
 * @description AngularJS directive for Select2
 */
(function () {
    'use strict';
    function scSelect2($compile, $timeout) {
        return {
            restrict: 'A',
            scope: {
                ngModel: '=',
                templateSelection: '=',
                templateResult: '='
            },
            link: function ($scope, element, $attr) {
                if ($attr.language) {
                    var domElem = '<script src="/bower_components/select2/select2_locale_' + $attr.language + '.js" async defer></script>';
                    $(element).append($compile(domElem)($scope));
                }
                var options = _.pick($(element).data(), function (value, key) {
                    return !startsWith(key, '$');
                });
                options.formatSelection = $scope.templateSelection || $.fn.select2.defaults.formatSelection;
                options.formatResult = $scope.templateResult || $.fn.select2.defaults.formatResult;

                var selectorName = $attr.multiple ? 'multiselect' : 'select';
                var select = $('select.sc-' + selectorName + '[id="' + $attr.id + '"]');

                $timeout(function () {
                    select.select2(options);

                    if (!$scope.ngModel) return;

                    //True for both single and multiselect
                    if ($scope.ngModel.then && typeof $scope.ngModel.then === 'function') {
                        $scope.ngModel.then(function (response) {
                            //Multi select can have 1 or several default selected options,use each to initialize the select
                            //Single select has 1 default selected option, no iteration is needed to initialize the select
                            var selectedItems = $attr.multiple ? response.data : new Array(response.data);
                            populatePreselectedOptions(select, selectedItems);
                        });
                    }
                    else {
                        if (!_.isArray($scope.ngModel) && !_.isObject($scope.ngModel)) throw "" + $scope.ngModel + " in " + $attr.id + " is not an object nor an array. Select2 must bind to an object or an array.";
                        var selectedItems = _.isArray($scope.ngModel) ? $scope.ngModel : new Array($scope.ngModel);
                        populatePreselectedOptions(select, selectedItems);
                    }

                });

                options.placeholderOption = $attr.multiple ? '' : 'first';

                function populatePreselectedOptions(scSelect, selectedItems) {
                    var selectedOptions = [];
                    _.each(selectedItems, function (selectedItem) {
                        var selectedId = selectedItem[options.value];
                        var selectedOption = _.find(scSelect[0], function (option) {
                            return selectedId == option.value;
                        });
                        selectedOptions.push({id: selectedId, text: selectedOption.label});
                    });
                    if (selectedItems.length == 1) selectedOptions = selectedOptions.pop();
                    scSelect.select2('data', selectedOptions);
                }
            }
        };

        function startsWith(str, target) {
            return str.indexOf(target) === 0;
        }
    }


    /**
     * @ngdoc module
     * @name scania.angular.select2
     *
     * @description AngularJS directive for Select2
     */<commit_msg>bugfix resolve inner directives before compiling select2<commit_after>/**
 * scania-angular-select2
 * https://github.com/scania-bootstrap/scania-angular-select2
 * License: MIT
 *
 * @ngdoc directive
 * @name scSelect2
 * @module scania.angular.select2
 *
 * @description AngularJS directive for Select2
 */
(function () {
    'use strict';
    function scSelect2($compile, $timeout) {
        return {
            restrict: 'A',
            scope: {
                ngModel: '=',
                templateSelection: '=',
                templateResult: '='
            },
            link: function ($scope, element, $attr) {
                if ($attr.language) {
                    var domElem = '<script src="/bower_components/select2/select2_locale_' + $attr.language + '.js" async defer></script>';
                    $(element).append($compile(domElem)($scope));
                }
                var options = _.pick($(element).data(), function (value, key) {
                    return !startsWith(key, '$');
                });
                options.formatSelection = $scope.templateSelection || $.fn.select2.defaults.formatSelection;
                options.formatResult = $scope.templateResult || $.fn.select2.defaults.formatResult;

                var selectorName = $attr.multiple ? 'multiselect' : 'select',
                    select = {};

                $timeout(function () {
                    select = $('select.sc-' + selectorName + '[id="' + $attr.id + '"]');
                    select.select2(options);

                    if (!$scope.ngModel) return;

                    //True for both single and multiselect
                    if ($scope.ngModel.then && typeof $scope.ngModel.then === 'function') {
                        $scope.ngModel.then(function (response) {
                            //Multi select can have 1 or several default selected options,use each to initialize the select
                            //Single select has 1 default selected option, no iteration is needed to initialize the select
                            var selectedItems = $attr.multiple ? response.data : new Array(response.data);
                            populatePreselectedOptions(select, selectedItems);
                        });
                    }
                    else {
                        if (!_.isArray($scope.ngModel) && !_.isObject($scope.ngModel)) return;
                        var selectedItems = _.isArray($scope.ngModel) ? $scope.ngModel : new Array($scope.ngModel);
                        populatePreselectedOptions(select, selectedItems);
                    }

                });

                options.placeholderOption = $attr.multiple ? '' : 'first';

                function populatePreselectedOptions(scSelect, selectedItems) {
                    var selectedOptions = [];
                    _.each(selectedItems, function (selectedItem) {
                        var selectedId = selectedItem[key];
                        var selectedOption = _.find(scSelect[0], function (option) {
                            return selectedId == option.value;
                        });
                        if(!selectedOption) {
                            console.error("Data-value for " + scSelect[0].id +" must have the same value as its track by.");
                            return;
                        }
                        selectedOptions.push({id: selectedId, text: selectedOption.label});

                    });
                    if (selectedItems.length == 1) selectedOptions = selectedOptions.pop();
                    scSelect.select2('data', selectedOptions);
                }
            }
        };

        function startsWith(str, target) {
            return str.indexOf(target) === 0;
        }
    }


    /**
     * @ngdoc module
     * @name scania.angular.select2
     *
     * @description AngularJS directive for Select2
     */
{'fraction_non_alphanumeric': 0.11317809288409002, 'fraction_numerical': 0.005333680239365162, 'mean_word_length': 1.5148838730781813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '900801', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1753, 'n_words': 413}