HEX
Server: Apache/2.4.25 (Debian)
System: Linux server17 4.9.0-19-amd64 #1 SMP Debian 4.9.320-2 (2022-06-30) x86_64
User: web37 (1062)
PHP: 7.4.30
Disabled: show_source, highlight_file, apache_child_terminate, apache_get_modules, apache_note, apache_setenv, virtual, dl, disk_total_space, posix_getpwnam, posix_getpwuid, posix_mkfifo, posix_mknod, posix_setpgid, posix_setsid, posix_setuid, posix_uname, proc_nice, openlog, syslog, pfsockopen
Upload Files
File: /var/www/web37/htdocs/fickanzeiger/javascript/jquery/jquery.backgroundTask.js
(function($){

    $.backgroundTask = function(taskId){
    
        var that = {
            taskId: taskId,
            form: $("#backgroundTaskControlForm")[0],
            buttonsUpdated: false,
            executed: false,
            
            init: function(){
            
                var buttons = new Array("start", "stop", "pause", "resume");
                
                for (i in buttons) {
                    var buttonName = buttons[i];
                    
                    if (that.form[buttonName]) {
                        $(that.form[buttonName]).click(that[buttonName + "OnClick"]);
                    }
                }
                
                that.onStartSuccess();
            },
            
            sendControlAction: function(action){
                $.post(AppRouter.getRewrittedUrl('/admin/task/' + action), {
                    taskId: that.taskId
                });
            },
            
            startOnClick: function(){
                that.executed = true;
                $(that.form.start).hide();
                $(that.form.pause).show();
                $(that.form.stop).show();
                
                if (typeof(tinyMCE) != 'undefined') {
                    tinyMCE.triggerSave(true,true);
                }

                // form has own action
                if ($(that.form).attr('action')) {
                    $(that.form).ajaxSubmit(that.onStartSuccess);
                } else {
                    var query = $(that.form).formSerialize();
                    query += "&taskId=" + that.taskId;

                    $.post(AppRouter.getRewrittedUrl('/admin/task/init'), query, that.onStartSuccess);
                }
            },
            
            stopOnClick: function(){
                $(that.form.start).show();
                $(that.form.pause).hide();
                $(that.form.resume).hide();
                that.sendControlAction("stop");
            },
            
            pauseOnClick: function(){
                $(that.form.pause).hide();
                $(that.form.resume).show();
                that.sendControlAction("pause");
            },
            
            resumeOnClick: function(){
                $(that.form.resume).hide();
                $(that.form.pause).show();
                that.sendControlAction("resume");
            },
            
            onStartSuccess: function(){
                $("#progressBar").css("width", "0%");
                that.updateStatus();
            },
            
            updateStatus: function(){
                $.post(AppRouter.getRewrittedUrl('/admin/task/getStatus'), {
                    taskId: that.taskId
                }, that.onUpdateStatusSuccess, "json");
            },
            
            updateButtonsState: function(status){
                that.buttonsUpdated = true;
                
                if (status == "active") {
                    $(that.form.start).hide();
                    $(that.form.pause).show();
                    $(that.form.stop).show();
                }
                
                if (status == "pause") {
                    $(that.form.start).hide();
                    $(that.form.pause).hide();
                    $(that.form.resume).show();
                    $(that.form.stop).show();
                }
            },
            
            onUpdateStatusSuccess: function(response){
                var task = response;
                
                if (task.status == "active" || task.status == "finish" || task.status == "pause") {
                    if (!that.buttonsUpdated) {
                        that.updateButtonsState(task.status);
                    }
                    
                    $("#progressBarOutline").show();
                    
                    $("#taskParsedItems").html(task.parsedItems);
                    $("#taskStatus").html(task.status);
                    
                    if (task.totalItems > 0) 
                        var percentage = (Math.floor(task.parsedItems * 100 / task.totalItems)).toString() + "%";
                    else 
                        var percentage = "100%";
                    
                    $("#progressBar").css("width", percentage);
                    $("#taskPercentage").html(percentage);
                    $("#progressInformation").show();
                }
                
                if (!task.status || task.status == "finish") {
                    $(that.form.resume).hide();
                    $(that.form.pause).hide();
                    $(that.form.stop).hide();
                    $(that.form.start).show();
                    $('#progressBarOutline').hide();
                    
                    if (that.executed) {
                        window.location.reload(false);
                    }
                }
                else {
                    window.setTimeout(that.updateStatus, 1000);
                }
                
            }
        }
        
        that.init();
    }
    
})(jQuery);