Progress Screen

by edgardleal

HTML

<input type="checkbox" id="ch1" value="Teste" /><br>
<input type="checkbox" id="ch2" /><br>
<input type="checkbox" id="ch3" /><br>
<input type="checkbox" id="ch4" /><br>
<input type="checkbox" id="ch5" /><br>

<button>Imprimir</button>

JavaScript

function print(count, callBack){
    if(!count) count = 1;
    var i = 1;
    function printOne(){
        console.log('Iteração: '  + i);
        if(callBack){
            var x = i;
            setInterval(function(){callBack(count, x);}, 1000);
        }
        i++;
        if(i <= count)
            setTimeout(printOne, 1000);
    }
    
    printOne();
}


function DialogPrinter(callBack){
    var self = this;    
    this.max = 0;
    this.value = 0;
    this.init = function(){
        if(!self.dialog){
            self.dialog = $('<div/>').appendTo('body')
            .dialog({autoOpen : false, modal : true,
                     close : function(){
                     
                       if(callBack && typeof callBack ==="function")
                           callBack.call(self.max, self.value);
                     }}); 
        }
        if(!self.label){
           self.label = $('<div/>').appendTo(self.dialog)
              .text('Imprimindo 0 de 0');   
        }        
        if(!self.progressbar){
           self.progressbar = $('<div/>')
           .css({"margin-top": "10px"})
           .appendTo(self.dialog).progressbar();   
        }
        if(!self.btnCancel){
            self.btnCancel = $('<span/>')
            .text('Cancelar')
            .button({                
                icons: {primary : 'ui-icon-close'},
                text : "Cancelar"})
            .click(function(){
                   self.close();
                })   
            .css({"margin-top" : "10px"})
            .appendTo(self.dialog);
        }
        
    };
    this.progress = function(total, _value){
        self.max = total;
        self.value = _value;
        self.progressbar.progressbar({value : _value, max : total});
        self.label.text('Imprimindo ' + _value + ' de ' + total);
        if(total==_value) self.close();
    };    
    this.init();
    this.show = function(){
        self.max = 0;
        self.value = 0;
      ...