Tela de impressão

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 = 0;
    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);
        else count =1;
    }
    
    printOne();
}


function DialogPrinter(callBack){
    var self = this;    
    self.max = 0;
    self.value = 0;
    self.reset = function(){
        self.max = 0;
        self.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/>').appendTo(self.dialog);   
        }
        self.reset();
    };
    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.dialog.dialog('open');
    };
    this.close = function(){
       self.dialog.dialog('close');
    };

}
var form = new DialogPrinter();
$('button:contains(Imprimir)').click(function(){
    form.show();    
    print(5, function(total, _value){
       form.progress(total, _value);
        console.log(_value + ' total: ' + total);
    });
   
});