publishes/mix/resources/js/theme.js   A
last analyzed

Complexity

Total Complexity 25
Complexity/F 1.32

Size

Lines of Code 101
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 58
mnd 6
bc 6
fnc 19
dl 0
loc 101
rs 10
bpm 0.3157
cpm 1.3157
noi 0
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A AdminArchitect.constructor 0 8 2
B AdminArchitect.handleTranslations 0 22 7
A AdminArchitect.handleTranslatable 0 9 2
A AdminArchitect.handleCollections 0 9 3
A AdminArchitect.handleFancybox 0 14 4
B AdminArchitect.handleBatchActions 0 30 6
1
class AdminArchitect {
2
    constructor() {
3
        [
4
            'Collections', 'BatchActions',
5
            'Fancybox', 'Translatable', 'Translations',
6
        ].map((method) => {
7
            AdminArchitect['handle' + method].call();
8
        });
9
    }
10
11
    static handleTranslatable() {
12
        // When Mui tab is switched it will switch all sibling Mui tabs.
13
        $('a[data-toggle="tab"]', '.nav-translatable').on('shown.bs.tab', function (e) {
14
            let fn = $(e.target),
15
                lc = fn.data('locale');
16
17
            fn.closest('form').find('a[data-locale="' + lc + '"]').tab('show');
18
        });
19
    }
20
21
    static handleTranslations() {
22
        const activate = function (fn) {
23
            fn.addClass('active').siblings('button').removeClass('active');
24
        };
25
26
        $('.global button[data-locale]').click(({target}) => {
27
            const fn = $(target), locale = fn.data('locale');
28
            $(fn).closest('table').find('tbody button[data-locale="' + locale + '"]').each(function (i, button) {
29
                $(button).trigger('click');
30
            });
31
            activate(fn);
32
        });
33
34
        $('tbody button[data-locale]').click(({target}) => {
35
            const fn = $(target), locale = fn.data('locale');
36
            fn.closest('tr').find('.translatable').each((i, e) => {
37
                const item = $(e);
38
                item[item.data('locale') === locale ? 'removeClass' : 'addClass']('hidden');
39
            });
40
            activate(fn);
41
        });
42
    }
43
44
    static handleCollections() {
45
        $(document).on('click', '.toggle-collection', ({target}) => {
46
            const fn = $(target);
47
48
            $('input[type=checkbox].collection-item').each((i, e) => {
49
                $(e).prop('checked', fn.prop('checked'));
50
            });
51
        });
52
    }
53
54
    static handleBatchActions() {
55
        const selected = () => {
56
            return $('input[type=checkbox]:checked.collection-item');
57
        };
58
59
        $(document).on('click', '.batch-actions a[data-action]', ({target}) => {
60
            if (!selected().length) {
61
                return false;
62
            }
63
64
            const $target = $(target);
65
66
            const msg = $target.data('confirmation') || '';
67
            if (msg.length && !window.confirm(msg)) {
68
                return false;
69
            }
70
71
            let dataAction;
72
            if ($target.is('a[data-action]')) {
73
                dataAction = $target.data('action');
74
            } else {
75
                dataAction = $target.closest('a[data-action]').data('action');
76
            }
77
78
            $('#batch_action').val(dataAction);
79
            $('#collection').submit();
80
81
            return false;
82
        });
83
    }
84
85
    static handleFancybox() {
86
        $('.fancybox').fancybox({
87
            afterLoad: function () {
88
                let width, height;
89
                if (width = $(this.element).data('width')) {
90
                    this.width = width;
91
                }
92
93
                if (height = $(this.element).data('height')) {
94
                    this.height = height;
95
                }
96
            },
97
        });
98
    }
99
}
100
101
$(() => new AdminArchitect);
102