PHP Code:
/**
* Class represents a division and zone selection.
*
* @param {Object} name - The name for this divzone object. Usually the name of
* a report with which it is associated.
*/
function DivisionAndZones( name ) {
this.name = name;
this.selectedDivision = Ext.state.Manager.get( this.name + 'division');
if( !this.selectedDivision ) {
this.selectedDivision = '01';
}
//Events that will be fired by this class
this.addEvents({
'divisionsFetchSuccess' : true,
'zonesFetchSuccess' : true,
'divZoneFetchFail' : true
});
this.divisionDS = new Ext.data.SimpleStore({
id: 0,
data: {},
fields: ['value', 'division'],
editable:false
});
this.divisionsCombo = new Ext.form.ComboBox( {
name: 'division',
fieldLabel: 'Division',
valueField: 'value',
displayField:'division',
store: this.divisionDS,
selectOnFocus: true,
mode: 'local',
typeAhead: true,
editable: false,
triggerAction: 'all',
value: this.selectedDivision,
width: 180,
listWidth: 180
});
this.zonesDS = new Ext.data.SimpleStore({
id: 0,
data: {},
fields: ['value', 'zone'],
editable:false
});
this.zonesCombo = new Ext.form.ComboBox( {
name: 'zone',
fieldLabel: 'Zone',
valueField: 'value',
displayField:'zone',
store: this.zonesDS,
selectOnFocus: true,
mode: 'local',
typeAhead: true,
editable: false,
triggerAction: 'all',
value: 'BI',
width: 180,
listWidth: 180
});
/**
* Set the zones for the given combobox
*
* @param {Object} params - The parameters to pass with the Ajax request. Parameters must include
* division for which we want the zones.
*/
this.setZones = function( params ){
var zones = Ext.state.Manager.get( 'zones' + this.selectedDivision );
//log( "-->Zones (zones" + this.selectedDivision + "): " + zones );
if (zones) {
this.zonesDS.loadData( zones );
this.fireEvent('zonesFetchSuccess');
}
else {
this.getDivisionsZones("zone", params);
}
}
/**
* Function called when zones have been fetched
*/
this.gotZone = function() {
selected = Ext.state.Manager.get( this.name + 'zone' );
if( selected ) {
this.zonesCombo.setValue(selected);
}
else {
this.zonesCombo.setValue("All Zones");
}
this.zonesCombo.enable();
}
//Add listener for zone fetch success
this.on( 'zonesFetchSuccess', this.gotZone );
/**
* Set the divisions for the given combobox
*
*/
this.setDivisions = function( params ) {
var divisions = Ext.state.Manager.get( 'divisions' );
//log( "-->Divisions (divisions): " + divisions );
if (divisions) {
this.divisionDS.loadData( divisions );
this.fireEvent('divisionsFetchSuccess');
}
else {
this.getDivisionsZones("division", params);
}
}
/**
* Function called when divisions have been fetched
*/
this.gotDivision = function() {
selected = Ext.state.Manager.get( this.name + 'division');
if (selected) {
this.divisionsCombo.setValue( selected );
}
this.divisionsCombo.enable();
}
//Add listener for division fetch success
this.on( 'divisionsFetchSuccess', this.gotDivision );
/**
*
* @param {Object} which - division or zone
* @param {Object} params
*/
this.getDivisionsZones = function( which, params ) {
if (which == 'division') {
this.divisionsCombo.disable();
}
else if( which == 'zone' ) {
this.zonesCombo.disable();
}
Ext.Ajax.request({
url: '/fit/alert/inputs.do?reportName=general',
waitMsg:'Fetching divisions and zones...',
params: params,
timeout: 180000,
success: function( response, options ){
try {
/*
json data returned is in the following format
[
{
"name":"division",
"selected":"05",
"values":[["01","01 ALABAMA"],...["06","06 GEORGIA"]]
},
{
"name":"zone",
"selected":"",
"values":[["","All"],,...,["All Zones","All Zones"]]
},
{"selected":"a","values":[],"name":"exceptionReportInput"},
{"selected":"03/27/08","values":[],"name":"from"},
{"selected":"03/27/08","values":[],"name":"to"}
]*/
var resp = eval( '(' + response.responseText + ')' );
if( resp.success ) {
if( resp.success == 'false' ) {
Ext.Msg.show( { title: 'Error', msg: response.error } );
}
}
else {
var inputs = eval( '(' + response.responseText + ')' );
if (which == 'division') {
for (i = 0; i < inputs.length; i++) {
if (inputs[i].name == "division" ) {
options.that.divisionDS.loadData(inputs[i].values);
Ext.state.Manager.set( 'divisions', inputs[i].values );
//log( "-->Setting divisions (divisions): " + inputs[i].values );
break;
}
}
options.that.fireEvent('divisionsFetchSuccess');
}
else if (which == 'zone') {
for (i = 0; i < inputs.length; i++) {
if (inputs[i].name == "zone" ) {
options.that.zonesDS.loadData(inputs[i].values);
Ext.state.Manager.set( 'zones' + options.that.selectedDivision, inputs[i].values );
//log( "-->Setting zones (zones" + options.that.selectedDivision + "): " + inputs[i].values );
break;
}
}
options.that.fireEvent('zonesFetchSuccess');
}
}
}
catch (e) {
Ext.Msg.show( { title: 'Error', msg: e.message } );
}
},
failure: function( response, options ){
try {
alert( "Failed to get division and zones: " + response.statusText );
options.that.divisionsCombo.enable();
options.that.zonesCombo.enable();
options.that.fireEvent( 'divZoneFetchFail' );
}
catch (e) {
Ext.Msg.show( { title: 'Error', msg: e.message } );
}
},
that: this
});
}
//When a new division is selected the zone combo must be updated with the
//zones for the newly selected division
this.divisionsCombo.on( 'select', function divisionChanged() {
this.selectedDivision = this.divisionsCombo.getValue();
Ext.state.Manager.set( this.name + 'division', this.selectedDivision );
//Set new zones
Ext.state.Manager.set( this.name + 'zone', "All Zones" );
this.setZones( { division: this.selectedDivision } );
}, this );
//When a new zone is selected save it
this.zonesCombo.on( 'select', function zonesChanged() {
Ext.state.Manager.set( this.name + 'zone', this.zonesCombo.getValue() );
}, this );
this.init = function() {
this.setDivisions( { "Division": this.division } );
};
}
//Make DivisionAndZones Observable so it can be observered and send events to the observers.
//Notably an event will be fired after the divisions and zones are received by
//the Ajax call
Ext.extend( DivisionAndZones, Ext.util.Observable );