Passed
Push — master ( 9c80d9...628b52 )
by MusikAnimal
05:31
created

web/static/js/categoryedits.js   A

Complexity

Total Complexity 13
Complexity/F 1.63

Size

Lines of Code 78
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 16
dl 0
loc 78
rs 10
c 1
b 0
f 0
wmc 13
mnd 1
bc 10
fnc 8
bpm 1.25
cpm 1.625
noi 0
1
$(function () {
2
    if (!$('body.categoryedits').length) {
3
        return;
4
    }
5
6
    var $select2Input = $('#category_selector'),
7
        nsName = $select2Input.data('ns');
8
9
    var params = {
10
        ajax: {
11
            url: $select2Input.data('api'),
12
            dataType: 'jsonp',
13
            jsonpCallback: 'categorySuggestionCallback',
14
            delay: 200,
15
            data: function (search) {
16
                return {
17
                    action: 'query',
18
                    list: 'prefixsearch',
19
                    format: 'json',
20
                    pssearch: search.term || '',
21
                    psnamespace: 14,
22
                    cirrusUseCompletionSuggester: 'yes'
23
                };
24
            },
25
            processResults: function (data) {
26
                var query = data ? data.query : {},
27
                    results = [];
28
29
                if (query && query.prefixsearch.length) {
30
                    results = query.prefixsearch.map(function (elem) {
31
                        var title = elem.title.replace(new RegExp('^' + nsName + ':'), '');
32
                        return {
33
                            id: title.score(),
34
                            text: title
35
                        };
36
                    });
37
                }
38
39
                return {results: results}
40
            }
41
        },
42
        placeholder: $.i18n('category-search'),
43
        maximumSelectionLength: 10,
44
        minimumInputLength: 1
45
    };
46
47
    $select2Input.select2(params);
48
49
    $('form').on('submit', function () {
50
        $('#category_input').val(
51
            $select2Input.val().join('|')
52
        );
53
    });
54
55
    setupToggleTable(window.countsByCategory, window.categoryChart, null, function (newData) {
56
        var total = 0;
57
        Object.keys(newData).forEach(function (category) {
58
            total += parseInt(newData[category], 10);
59
        });
60
        var categoriesCount = Object.keys(newData).length;
61
        /** global: i18nLang */
62
        $('.category--category').text(
63
            categoriesCount.toLocaleString(i18nLang) + " " +
64
            $.i18n('num-categories', categoriesCount)
65
        );
66
        $('.category--count').text(total.toLocaleString(i18nLang));
67
    });
68
69
    // Load the contributions browser, or set up the listeners if it is already present.
70
    var initFunc = $('.contributions-table').length ? 'setupContributionsNavListeners' : 'loadContributions';
71
    window[initFunc](
72
        function (params) {
73
            return 'categoryedits-contributions/' + params.project + '/' + params.username + '/' +
74
                    params.categories + '/' + params.start + '/' + params.end;
75
        },
76
        'Category'
77
    );
78
});
79