Completed
Push — master ( dd01b9...2c7d6b )
by Terzi
03:46
created

theme.js ➔ ... ➔ $(ꞌ.fancyboxꞌ).fancybox.afterLoad   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
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
            if ((msg = $target.data('confirmation')) && !window.confirm(msg)) {
0 ignored issues
show
Bug introduced by
The variable msg seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.msg.
Loading history...
67
                return false;
68
            }
69
70
            $('#batch_action').val($target.data('action'));
71
            $('#collection').submit();
72
73
            return false;
74
        });
75
    }
76
77
    static handleDateControls() {
78
        $('[data-filter-type="date"]').datetimepicker({
79
            format: 'YYYY-MM-DD'
80
        });
81
82
        $('[data-filter-type="time"]').datetimepicker({
83
            format: 'hh:mm:ss'
84
        });
85
86
        $('[data-filter-type="datetime"]').datetimepicker({
87
            format: 'YYYY-MM-DD hh:mm:ss'
88
        });
89
90
        $('[data-filter-type="daterange"]').daterangepicker({
91
            locale: {
92
                format: 'YYYY-MM-DD',
93
            },
94
            autoUpdateInput: false,
95
            ranges: {
96
                'Today': [moment(), moment()],
97
                'Yesterday': [
98
                    moment().subtract(1, 'days'),
99
                    moment().subtract(1, 'days'),
100
                ],
101
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
102
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
103
                'This Month': [
104
                    moment().startOf('month'),
105
                    moment().endOf('month'),
106
                ],
107
                'Last Month': [
108
                    moment().subtract(1, 'month').startOf('month'),
109
                    moment().subtract(1, 'month').endOf('month'),
110
                ],
111
            },
112
        }).on('apply.daterangepicker', function (ev, picker) {
113
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
114
        }).on('cancel.daterangepicker', function (ev, picker) {
0 ignored issues
show
Unused Code introduced by
The parameter ev is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter picker is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
115
            $(this).val('');
116
        });
117
    }
118
119
    static handleFancybox() {
120
        $('.fancybox').fancybox({
121
            afterLoad: function () {
122
                let width, height;
123
                if (width = $(this.element).data('width')) {
124
                    this.width = width;
125
                }
126
127
                if (height = $(this.element).data('height')) {
128
                    this.height = height;
129
                }
130
            },
131
        });
132
    }
133
}
134
135
$(() => new AdminArchitect);
136