Test Failed
Push — develop ( a91838...329856 )
by Freddie
13:33
created

main.js ➔ getDateTimeFormat   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
dl 0
loc 20
rs 9.7
1
/** global: Intl */
2
/** global: FormData */
3
/** global: InfiniteScroll */
4
/** global: URLSearchParams */
5
jQuery(document).ready(function ($) {
6
    'use strict';
7
    if ($('.notification-list').length) {
8
        $('.notification-list').slimScroll({
9
            height: '100%'
10
        });
11
    }
12
    if ($('.menu-list').length) {
13
        $('.menu-list').slimScroll({
14
            height: '100%'
15
        });
16
    }
17
    if ($('.navbar-nav > .nav-item > a').length) {
18
        $('.navbar-nav > .nav-item > a').click(function () {
19
            if ($(this).hasClass('active')) {
20
                $(this).removeClass('active');
21
                $(this).attr('aria-expanded', 'false');
22
23
                return;
24
            }
25
26
            $('.navbar-nav > .nav-item > a').each(function () {
27
                $(this).removeClass('active');
28
                $(this).attr('aria-expanded', 'false');
29
30
                const target = $(this).data('target');
31
32
                $(target).removeClass('show');
33
                $(target).attr('aria-expanded', 'false');
34
            })
35
36
            $(this).addClass('active');
37
            $(this).attr('aria-expanded', 'true');
38
        });
39
    }
40
    if ($('.sidebar-nav-fixed a').length) {
41
        $('.sidebar-nav-fixed a').click(function (event) {
42
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
43
                && location.hostname == this.hostname
44
            ) {
45
                var target = $(this.hash);
46
47
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
48
49
                if (target.length) {
50
                    event.preventDefault();
51
                    $('html, body').animate({
52
                        scrollTop: target.offset().top - 90
53
                    }, 1000, function () {
54
                        var $target = $(target);
55
                        $target.focus();
56
                        if ($target.is(':focus')) {
57
                            return false;
58
                        }
59
60
                        $target.attr('tabindex', '-1');
61
                        $target.focus();
62
                        return true;
63
                    });
64
                }
65
            };
66
67
            $('.sidebar-nav-fixed a').each(function () {
68
                $(this).removeClass('active');
69
            })
70
71
            $(this).addClass('active');
72
        });
73
    }
74
75
    if ($('[data-toggle="tooltip"]').length) {
76
        $('[data-toggle="tooltip"]').tooltip()
77
    }
78
79
    if ($('[data-toggle="popover"]').length) {
80
        $('[data-toggle="popover"]').popover()
81
    }
82
83
    $('.money-format').each(function () {
84
        $(this).html(getMoneyFormat($(this).html()));
85
    });
86
87
    $('.datetime-format').each(function () {
88
        $(this).html(getDateTimeFormat($(this).html()));
89
    });
90
91
    $(document).on('submit', 'form[data-confirmation]', function (event) {
92
        var $form = $(this),
93
            $confirm = $('#confirmationModal');
94
95
        if ($confirm.data('result') !== 'yes') {
96
            event.preventDefault();
97
98
            $confirm
99
                .off('click', '#btnYes')
100
                .on('click', '#btnYes', function () {
101
                    $confirm.data('result', 'yes');
102
                    $form.find('input[type="submit"]').attr('disabled', 'disabled');
103
                    $form.submit();
104
                })
105
                .modal('show');
106
        }
107
    }).on('submit', 'form:not([data-confirmation])', function () {
108
        $('.overlay').show();
109
    }).on('click', '.show-overlay', function (e) {
0 ignored issues
show
Unused Code introduced by
The parameter e 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...
110
        if (!window.event.ctrlKey) {
111
            $('.overlay').show();
112
        }
113
    }).on('change', '.money-format', function () {
114
        const $html = $(this);
115
        const isInput = $html.is('input');
116
        const number = isInput ? $html.val() : $html.html();
117
        const money = getMoneyFormat(number);
118
119
        $html.data('mf-amount', number);
120
121
        isInput ? $html.val(money) : $html.html(money);
122
    });
123
124
    $('[name$=_filter_form]').on('submit', function (e) {
125
        e.preventDefault();
126
127
        const url = $(this).attr('action');
128
        const method = $(this).attr('method');
129
        const data = $(this).serialize();
130
131
        $.ajax({
132
            url: url,
133
            method: method,
134
            dataType: 'html',
135
            data: data,
136
            headers: {
137
                'X-XSRF-Token': getCookie('XSRF-Token')
138
            },
139
            beforeSend: function () {
140
                $('.overlay').show();
141
            }
142
        }).always(function () {
143
            $('.overlay').hide();
144
        }).done(function (html) {
145
            infScroll.pageIndex = 1;
0 ignored issues
show
Bug introduced by
The variable infScroll seems to be never declared. If this is a global, consider adding a /** global: infScroll */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
146
147
            $('.dashboard-content .table > tbody').empty().html(html);
148
149
            const moneyFormats = document.querySelectorAll('.dashboard-content .table > tbody > tr > td.money-format');
150
151
            [].forEach.call(moneyFormats, function (moneyFormat) {
152
                if (isNaN(moneyFormat.innerHTML)) {
153
                    return undefined;
154
                }
155
156
                moneyFormat.innerHTML = getMoneyFormat(moneyFormat.innerHTML);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
157
            });
158
        });
159
    });
160
161
    if ($(document).datepicker) {
162
        $('.datepicker').datepicker({
163
            format: window.flex.defaultDateFormat,
164
            language: window.flex.defaultLocale,
165
            clearBtn: true,
166
            autoclose: true,
167
            toggleActive: true,
168
        });
169
    }
170
171
    if (typeof InfiniteScroll === 'function') {
172
        const infScroll = new InfiniteScroll('.dashboard-content', {
173
            path: function () {
174
                const page= (parseInt((new URLSearchParams(window.location.search)).get('page') || this.pageIndex) + 1);
175
                const form = document.querySelector('.dashboard-sidebar form');
176
                let queryFilters = '';
177
178
                if (form) {
179
                    queryFilters = '&' + new URLSearchParams(new FormData(form)).toString();
180
                }
181
182
                return '?page=' + page + queryFilters;
183
            },
184
            responseType: 'html',
185
            status: '.infinite-scroll-status',
186
            history: false,
187
        }).on('load', function (html) {
188
            if (html === '') {
189
                infScroll.dispatchEvent('last');
190
191
                return;
192
            }
193
194
            document.querySelector('.dashboard-content .table > tbody').innerHTML += html;
195
196
            const moneyFormats = document.querySelectorAll('.dashboard-content .table > tbody > tr > td.money-format');
197
198
            [].forEach.call(moneyFormats, function (moneyFormat) {
199
                if (isNaN(moneyFormat.innerHTML)) {
200
                    return undefined;
201
                }
202
203
                moneyFormat.innerHTML = getMoneyFormat(moneyFormat.innerHTML);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
204
            });
205
        });
206
207
        window.infScroll = infScroll;
208
    }
209
});
210
211
function getCookie(cname)
212
{
213
    const name = cname + '=';
214
    const ca = document.cookie.split(';');
215
216
    for (let i = 0; i < ca.length; i++) {
217
        let c = ca[i];
218
219
        while (c.charAt(0) == ' ') {
220
            c = c.substring(1);
221
        }
222
223
        if (c.indexOf(name) === 0) {
224
            return c.substring(name.length, c.length);
225
        }
226
    }
227
228
    return '';
229
}
230
231
function getMoneyFormat(number)
232
{
233
    if (isNaN(number)) {
234
        return 0;
235
    }
236
237
    if (!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function')) {
238
        return number;
239
    }
240
241
    return (number * 1).toLocaleString(window.flex.defaultLocale, {
242
        style: 'currency',
243
        currency: window.flex.defaultCurrency,
244
        minimumFractionDigits: 0,
245
        maximumFractionDigits: 0,
246
    });
247
}
248
249
function getDateTimeFormat(datetime)
250
{
251
    if (!datetime || datetime === undefined || datetime === null || datetime === '') {
252
        return '';
253
    }
254
255
    try {
256
        return (new Date((new Date(datetime)).setMilliseconds(getTimeZoneOffset()))).toLocaleDateString(window.flex.defaultLocale, {
257
            weekday: 'long',
258
            year: 'numeric',
259
            month: 'long',
260
            day: 'numeric',
261
            hour: 'numeric',
262
            minute: 'numeric',
263
            second: 'numeric',
264
        });
265
    } catch (e) {
266
        return datetime;
267
    }
268
}
269
270
function getTimeZone()
271
{
272
    if (!(typeof Intl === 'object' && Intl && typeof Intl.DateTimeFormat === 'function')) {
273
        return window.flex.defaultTimezone;
274
    }
275
276
    return Intl.DateTimeFormat().resolvedOptions().timeZone;
277
}
278
279
function getTimeZoneOffset()
280
{
281
    return -1  * ((new Date()).getTimezoneOffset() * 60 * 1000);
282
}
283