
// $Id: afp_code.js,v 1.1 2011/08/22 09:21:56 lenz-mobile Exp $

// Funzionamento tabella jQuery paginata dati

//
//  <div id="callOk"></div>
//
// <script>
// $(function() {
//        AFP1 = new AjaxFilteredPopup;
//        AFP1.webHere = "<%= stWebHere %>";
//        AFP1.popup   = "callsOK";
//        AFP1.recipe  = "";
//        AFP1.divId   = "callOK";
//        AFP1.setupPage();
// });
// </script>
//




AjaxFilteredPopup.prototype.icons = {
    'start': {
        'on': 'control_start_blue',
        'off': 'control_start',
        'click': function(event) {
            event.data.obj.runQuery( "F" )
        }
    },
    'end':   {
        'on': 'control_end_blue',
        'off': 'control_end',
        'click': function(event) {
            event.data.obj.runQuery( "L" )
        }
    },
    'ffwd':  {
        'on': 'control_fastforward_blue',
        'off': 'control_fastforward',
        'click': function(event) {
            event.data.obj.runQuery( "N" )
        }
    },
    'rew':   { 
        'on': 'control_rewind_blue',
        'off': 'control_rewind',
        'click': function(event) {
            event.data.obj.runQuery( "P" )
        }
    },

    'exp_excel': {
        'on': 'page_excel',
        'click': function(event) {
            event.data.obj.openAsNewUrl( "XS" );
        }
    },
    'exp_csv': {
        'on': 'page_white_text',
        'click': function(event) {
            event.data.obj.openAsNewUrl( "XC" );
        }
    },
    'exp_xml': {
        'on': 'page_white_code_red',
        'click': function(event) {
            event.data.obj.openAsNewUrl( "XX" );
        }
    },

    'layout': {
        'on': 'layout_edit',
        'click': function( event ) {            
            event.data.obj.showColumnLayout();
        }
    }
}


function AjaxFilteredPopup() {
    this.webHere   = "";
    this.divId     = "";
    this.popup     = "";
    this.recipe    = "";
    this.myCols    = [];
    this.dialogPtr = "";

}



// var iconsBase = "/queuemetrics/img/icons_silk";
// var myCols    = [];
// var dialog;

AjaxFilteredPopup.prototype.setupPage = function( iconBase ) {
    var myself = this;

    $.ajax( {
        type:        "get",
        url:         myself.webHere + "/qm/afp_skeleton.jsp",
        dataType:    "html",
        success: function(resp) {

            resp = resp.replace( "__myId__", myself.divId);
            $( "#" + myself.divId ).html( resp );
            myself.setupPage_afterHTML( iconBase );
            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert( "Loading skeleton");
        }
    });
    
}


AjaxFilteredPopup.prototype.setupPage_afterHTML = function( iconBase ) {

    this.iconBase = iconBase;

    var myself = this;

//    var h = "<div id='dialog" + this.divId + "' title='Basic dialog2'>"
//        + "<div class='colDialog_data'></div>"
//        + "</div>";
//
//    $( "#" + this.divId + " .dialog").html( h );
    //this.dialog( { autoOpen: false } );
    this.dialogPtr = $("#dialog" + this.divId);
    this.dialogPtr.dialog( {autoOpen: false,
        modal:true,
	buttons: {
            Ok: function() {
		$( this ).dialog( "close" );
                //alert( $(this).html() );

                var listOfCols = "";
                $(this).find( ":checked" ).each( function(x) {                    
                        listOfCols += "|" + this.id;                    
                } );
                
                myself.queryJson( "C", listOfCols, "", true );
            }
        }
    } );

    this.setControl( "exp_excel", true );
    this.setControl( "exp_csv", true );
    this.setControl( "exp_xml", true );
    this.setControl( "layout", true );

    this.setupButtons( 0, 0);
    // caricamento iniziale
    this.runQuery( "F" );
    
}

AjaxFilteredPopup.prototype.runQuery = function( action ) {
    this.queryJson( action, "", "", true );
}


AjaxFilteredPopup.prototype.setControl = function( name, isOn) {

    var iconsBase = this.webHere + "/img/icons_silk/";

    if ( isOn ) {
        var img = iconsBase + this.icons[ name ][ "on" ] + ".png";
        $( "#" + this.divId + ' .ctrl_' + name )
            .attr( "src", img )
            .css('cursor', 'hand')
            .unbind( "click",  this.icons[ name ][ "click" ] )
            .bind( "click", {obj: this}, this.icons[ name ][ "click" ] );
    } else {
        var img2 = iconsBase + this.icons[ name ][ "off" ] + ".png";
        $( "#" + this.divId + ' .ctrl_' + name )
            .attr( "src", img2 )
            .css('cursor', 'auto')
            .unbind( "click", this.icons[ name ][ "click" ] );
    }

}

/**
 * Clicks
 */


AjaxFilteredPopup.prototype.openAsNewUrl = function( op ) {
    var link = "qm_ajax_filtered_popup_ds.do"
        + "?AFP_clp=" + this.popup
        + "&AFP_rcp=" + this.recipe
        + "&AFP_op=" + op
        + "&cbrk=" + Math.random();
    window.open(link, "_new", "");
}

AjaxFilteredPopup.prototype.queryJson = function( op, columns, sortBy, sortAsc ) {

    this.disableUITransaction(true);
    var myself = this;

    $.ajax( {
        type:        "post",
        url:         myself.webHere + "/tpf",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType:    "json",
        data: {
            'TPF_VERBO': "qm_ajax_filtered_popup_ds",
            'TPF_XPAR1': '',
            'AFP_op':    op,
            'AFP_clp':   myself.popup,
            'AFP_rcp':   myself.recipe,
            'AFP_cl':    columns,
            'AFP_st':    sortBy,
            'AFP_clo':   (sortAsc ? "A" : "D")
        },
        success: function(resp) {
            myself.setupJson( resp );
            myself.disableUITransaction(false);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            myself.showError( "Loading AJAX data", errorThrown );
            myself.disableUITransaction(false);
        }
    });
}


AjaxFilteredPopup.prototype.setupJson = function( data ) {

    if ( data['error'].length > 0  ) {
        // ho un errore server
        $( "#errmsg" ).html( data['error'] ).show();
    } else {
        $( "#errmsg" ).html( data['error'] ).hide();

        $( "#" + this.divId + " .ctrl_innerhtml" ).html( data['tabella'] );
        this.setupButtons( data['pag_corr'] * 1, data['pag_tot'] * 1 );
        this.myCols = data['columns'];
        $( "#" + this.divId + " .ctrl_title" ).html( data['title'] );
    }
}

AjaxFilteredPopup.prototype.disableUITransaction = function( disable ) {
    // TBD
}

AjaxFilteredPopup.prototype.setupButtons = function( thisPage, nPages ) {
    if ( nPages == 0 ) {
        this.setControl( 'start', false );
        this.setControl( 'rew',   false );
        this.setControl( 'ffwd',  false );
        this.setControl( 'end',   false );
        this.setupBanners( "-", "-" );
    } else {
        this.setControl( 'start', (thisPage != 0) );
        this.setControl( 'rew',   (thisPage > 0) );
        this.setControl( 'ffwd',  (thisPage < (nPages -1)) );
        this.setControl( 'end',   (thisPage < (nPages-1 )) );
        this.setupBanners( thisPage+1, nPages );
    }
}

AjaxFilteredPopup.prototype.setupBanners = function( thisPage, nPages ) {
    $( "#" + this.divId + " .ctrl_page" ).html( thisPage );
    $( "#" + this.divId + " .ctrl_npages" ).html( nPages );
}

AjaxFilteredPopup.prototype.showColumnLayout = function() {

    var html = "";
    for ( var i = 0; i < this.myCols.length; i++ ) {
        var title = this.myCols[i][1];
        var name  = this.myCols[i][0];
        var isOn  = (((this.myCols[i][2])*1) == 1);

        html += "<input type=\"checkbox\" class=\"btnColumns\" id=\""
            + name +"\" "+ ( isOn ? "checked='checked'" : "")
            + " /><label for=\"" + name + "\">"+ title + "</label> <br>";
    }

    

    $( "#dialog" + this.divId + " .colDialog_data").html( html );
    //$( "#dialog" + this.divId + " .btnColumns" ).button();
    this.dialogPtr.dialog("open");
    
}


AjaxFilteredPopup.prototype.showError = function( error, debugMsg ) {
    var err = "<b style='color:red;'>Error: " + error + "</b>";
    // alert( debugMsg );
    $( "#" + this.divId + " .ctrl_title" ).html( err );
}

// $Log: afp_code.js,v $
// Revision 1.1  2011/08/22 09:21:56  lenz-mobile
// #1380: AJAX form design for AFP
//
//



