all files / src/ angular-bootstrap-locale-dialog.js

100% Statements 33/33
100% Branches 15/15
100% Functions 9/9
100% Lines 33/33
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                                                                                                                                                              
(function() {
    'use strict';
 
    angular
        .module('ui.bootstrap.locale-dialog', [
            'ui.bootstrap'
        ])
 
        .filter('filterLocales', filterLocales)
 
        .controller('$localeSelectorDialogController', $localeSelectorDialogController)
 
        .factory('$localeSelectorDialog', $localeSelectorDialog)
    ;
 
    /* @ngInject */
    function filterLocales () {
        return function (items, search) {
 
            // do not filter nothing
            if ((!angular.isString(search)) || (search.length < 1)) {
                return items;
            }
 
            // filtered result
            var filtered = {};
 
            // try match each item
            angular.forEach(items, function(value, key) {
 
                // create a regex from the search value
                var searchRegex = new RegExp(search, 'i');
 
                // try to match the regex on one of each [key, value.country, value.name]
                if ((key.match(searchRegex)) || (value.name.match(searchRegex)) || (value.country.match(searchRegex))) {
                    // add the item to the filtered result
                    filtered[key] = value;
                }
            });
 
            // return the filtered data
            return filtered;
        };
    }
 
    /* @ngInject */
    function $localeSelectorDialogController (options, $uibModalInstance) {
        var vm = this;
 
        vm.options          =   options || { };
 
        vm.dismiss          =   $uibModalInstance.dismiss;
        vm.selectLocale     =   selectLocale;
 
        activate();
 
        //////////////
 
        function activate () {
            if (!angular.isObject(vm.options.locales)) {
                vm.options.locales = {};
            }
            
            vm.count = Object.keys(vm.options.locales).length;
        }
 
        function selectLocale (locale) {
            return $uibModalInstance.close(locale);
        }
    }
 
    /* @ngInject */
    function $localeSelectorDialog ($uibModal) {
        var service = {
            open: openDialog
        };
        return service;
 
        ////////////////
 
        function openDialog (options) {
 
            // default options
            var defaultOptions = {
                templateUrl: 'angular-bootstrap-locale-dialog/angular-bootstrap-locale-dialog.html',
                showFlags: false,
                showSearch: false,
                contributeUrl: null,
                locales: []
            };
 
            // we can object or nothing
            if (!angular.isObject(options)) {
                options = {};
            }
 
            // apply the default options
            options = angular.extend(defaultOptions, options);
 
            // open the dialog and return this $uibModal.open promise
            return $uibModal.open({
                templateUrl: options.templateUrl,
                bindToController: true,
                controllerAs: 'vm',
                controller: '$localeSelectorDialogController',
                resolve: {
                    options: options
                }
            });
        }
    }
})();