Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
appointment.js 3.26 KiB
function appointment_type_behaviour(checkboxes, outObject, api_call) {
    var appointment_types_data = null;

    var global_sequential_time = 0;
    var global_time = 0;
    var global_parallel_time = 0;

    function recompute_parallel_time(object) {
        var result = 0;
        var object_id = -1;
        if (object) {
            object_id = object.attr('id');
        }
        $.each(checkboxes, function (index, value) {
            var checkbox = $(value);
            var val = parseInt(checkbox.attr('value'));
            if (checkbox.is(":checked") || checkbox.attr('id') == object_id) {
                var appointment_type = appointment_types_data[val];
                if (appointment_type.can_be_parallelized) {
                    result = Math.max(result, appointment_type.default_duration)
                }
            }
        });
        return result;
    }

    function recompute_sequential_time(object) {
        var result = 0;
        var object_id = -1;
        if (object) {
            object_id = object.attr('id');
        }
        $.each(checkboxes, function (index, value) {
            var checkbox = $(value);
            var val = parseInt(checkbox.attr('value'));
            if (checkbox.is(":checked") || checkbox.attr('id') == object_id) {
                var appointment_type = appointment_types_data[val];
                if (!appointment_type.can_be_parallelized) {
                    result += appointment_type.default_duration
                }
            }
        });
        return result;
    }

    function compute_time(object) {
        var val = parseInt(object.attr('value'));
        var time = 0;
        var appointment_type = appointment_types_data[val];
        if (appointment_type == null) {
            console.log("Cannot find appointment type with id: " + val);
        } else {
            time = appointment_type.default_duration;
            if (appointment_type.can_be_parallelized) {
                if (object.is(":checked")) {
                    global_parallel_time = Math.max(global_parallel_time, time);
                } else {
                    global_parallel_time = recompute_parallel_time();
                }
            } else {
                if (object.is(":checked")) {
                    global_sequential_time += time;
                } else {
                    global_sequential_time -= time;
                }
            }
            global_time = Math.max(global_parallel_time, global_sequential_time);
            $(outObject).val(global_time + "");
        }
    }


    $(checkboxes).change(function () {
        var object = $(this);
        if (appointment_types_data === null) {
            $.get(api_call, function (data) {
                appointment_types_data = {};
                $.each(data.appointment_types, function (index, appointment_type) {
                    appointment_types_data[appointment_type.id] = appointment_type;
                });
                global_parallel_time = recompute_parallel_time(object);
                global_sequential_time = recompute_sequential_time(object);
                compute_time(object)
            });
        } else {
            compute_time(object);
        }

    });

}