Passed
Push — master ( f9354c...3a3e74 )
by Andreas
22:28
created

workingon.js ➔ countup   B

Complexity

Conditions 6

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
dl 0
loc 32
rs 8.4186
c 0
b 0
f 0
1
2
function show_loading() {
3
    $('#org_openpsa_mypage_workingon_widget .org_openpsa_mypage_workingon').hide();
4
    $('#org_openpsa_mypage_workingon_loading').show();
5
}
6
7
function send_working_on(action) {
8
    var description = $("#working_description").serialize(),
9
        task = $("#working_task_selection").val().replace(/[\[|"|\]]/g, ''),
10
        invoiceable = $('#working_invoiceable').is(':checked'),
11
        send_url = MIDCOM_PAGE_PREFIX + "workingon/set/";
12
13
    $.ajax({
14
        type: "POST",
15
        url: send_url,
16
        data: description + "&task=" + task + "&invoiceable=" + invoiceable + '&action=' + action,
17
        success: function(msg) {
18
            $("#org_openpsa_mypage_workingon_widget").html(msg);
19
        },
20
        error: function() {
21
            location.href = location.href;
22
        },
23
        beforeSend: function() {
24
            show_loading();
25
        }
26
    });
27
}
28
29
function countup(start) {
30
    var holder = $('#org_openpsa_mypage_workingon_time'),
31
        last_update;
32
33
    function update() {
34
        var now = Math.round(new Date().getTime() / 1000) * 1000;
35
        if (now !== last_update) {
36
            let diff = (now - start) / 1000,
37
                diff_s = diff % 60,
38
                diff_m = Math.floor(diff / 60),
39
                diff_h = Math.floor(diff_m / 60),
40
                formatted = '';
41
42
            if (diff_h > 0) {
43
                formatted += diff_h + ':';
44
            }
45
            if (diff_m < 10) {
46
                diff_m = '0' + diff_m;
47
            }
48
            formatted += diff_m + ':';
49
            if (diff_s < 10) {
50
                diff_s = '0' + diff_s;
51
            }
52
            formatted += diff_s;
53
54
            holder.text(formatted);
55
            last_update = now;
56
        }
57
    }
58
59
    setInterval(update, 100);
60
}
61
62
var org_openpsa_workingon = {
63
    setup_widget: function() {
64
        $('#org_openpsa_mypage_workingon_start')
65
            .prop("disabled", true)
66
            .on('click', function() {
67
                send_working_on('start');
68
            });
69
70
        $('#org_openpsa_mypage_workingon_stop')
71
            .on('click', function() {
72
                send_working_on('stop');
73
            });
74
75
        if ($('#working_task_selection').val() === '') {
76
            $('#org_openpsa_mypage_workingon_stop').hide();
77
        } else {
78
            $('#org_openpsa_mypage_workingon_start').hide();
79
        }
80
    },
81
    select: function(event, ui) {
82
        midcom_helper_datamanager2_autocomplete.select(event, ui);
83
        $('#org_openpsa_mypage_workingon_start').prop("disabled", $('#working_task_selection').val() === '');
84
    }
85
}
86