// I'm inside init() function here
demandTable.formsTabDisable = function(){
demandTable.formsTabPanel.items.each( function(item) {
var formGroup = item;
formGroup.items.each( function(item2) {
item2.disable();
});
});
}
demandTable.formsTabEnable = function(){
demandTable.formsTabPanel.items.each( function(item) {
var formGroup = item;
formGroup.items.each( function(item2) {
item2.enable();
});
});
}
demandTable.usersDS = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url:'ajax2/Gemini?action=common.admin.profile.web.ajax.GetAllProfilesAction'}),
reader: new Ext.data.JsonReader({},
[
{name: 'userId'},
{name: 'name'}
]),
sortInfo:{field: 'name', direction: "ASC"}
});
demandTable.usersDS.load({waitMsg:'$loadmsg_text'});
demandTable.isFormTabValid = function () {
var valid = true;
demandTable.formsTabPanel.items.each( function(item) {
var formGroup = item;
formGroup.items.each( function(item2) {
try {
if (!item2.validate()) {
valid = false;
}
} catch (e) {
}
});
});
return valid;
}
demandTable.jumpToQuestion = function (questionId) {
demandTable.formGroupMap[questionId].focus();
}
demandTable.setQuestionCombo = function (questionId,value) {
demandTable.formsTabPanel.findById(questionId).setValue(value);
}
function getPreviousResponses (d) {
// grab old responses from the controls and put it in a questionId map
if (!demandTable.oldQuestionResponses) {
return;
}
// go through the old questions and put the responses in the oldResponses Map (key: questionId, value: response)
var oldResponses = [];
for (var i = 0; i < demandTable.oldQuestionResponses.length; i++) {
var q = demandTable.oldQuestionResponses[i].questions;
for (var j = 0; j < q.length; j++) {
try {
if (q[j].respTypeId == "000000000008") {
var responses = "";
var choices = Ext.util.JSON.decode( q[j].choices );
for (var k = 0; k < choices.length; k++) {
if (demandTable.formsTabPanel.findById( "dc."+q[j].questionId+"."+choices[k][0] ).getValue()) {
if (responses.length > 0) {
responses = responses + ",";
}
responses = responses + choices[k][0];
}
}
oldResponses[q[j].questionId] = responses;
} else if (q[j].respTypeId == "000000000005") {
oldResponses[q[j].questionId] = demandTable.formsTabPanel.findById( "dq."+q[j].questionId ).getValue();
oldResponses[q[j].questionId] = oldResponses[q[j].questionId].format( 'Y-m-d' );
} else {
oldResponses[q[j].questionId] = demandTable.formsTabPanel.findById( "dq."+q[j].questionId ).getValue();
}
//alert( 'save ' + q[j].questionId + ' ' + oldResponses[q[j].questionId] );
} catch (e) {
}
}
}
// go through the new questions and put the previous responses back
for (var i = 0; i < d.length; i++) {
var q = d[i].questions;
for (var j = 0; j < q.length; j++) {
if (oldResponses[q[j].questionId] && oldResponses[q[j].questionId].length > 0) {
q[j].response = oldResponses[q[j].questionId];
}
}
}
}
demandTable.initForms = function (d) {
//introspect( d );
// put the old responses back before we changes the forms
getPreviousResponses( d );
// remove all items first
demandTable.formsTabPanel.items.each( function(item) {
var formGroup = item;
formGroup.items.each( function(item2) {
formGroup.remove( item2 );
});
demandTable.formsTabPanel.remove( item );
});
demandTable.questionResponses = [];
var count = 0;
// now insert all forms
for (var i = 0; i < d.length; i++) {
var formGroup = new Ext.form.FieldSet({
title: d[i].formName,
collapsible: true,
autoHeight:true,
defaultType: 'textfield',
defaults: {labelSeparator: ' '}
});
demandTable.formsTabPanel.add( formGroup );
if (d[i].instructions) {
var formInstructionsGroup = new Ext.form.FieldSet({
title: '$instructions_text',
collapsible: true,
collapsed: true,
autoHeight:true
});
var formInstructions = new Ext.HTMLComponent({
html: d[i].instructions
});
formInstructionsGroup.add( formInstructions );
formGroup.add( formInstructionsGroup );
}
var q = d[i].questions;
var numFieldCnt = 0;
var dateFieldCnt = 0;
for (var j = 0; j < q.length; j++) {
var question;
var formFieldName = 'q'+q[j].questionId;
var label = q[j].question;
var questionId = q[j].questionId;
var allowBlank = q[j].allowBlank;
if (q[j].instructions && q[j].instructions.length > 0) {
label = label +'
';
}
if (q[j].respTypeId == "AnswerAbove") {
label = label +'
' + label + '
$answer_question_text '
});
} else if (q[j].respTypeId == "000000000003") {
question = new Ext.form.TextField({
fieldLabel: label,
width: 550,
maxLength: 1024,
allowBlank: q[j].allowBlank,
value: q[j].response
});
} else if (q[j].respTypeId == "000000000001") {
question = new Ext.form.NumberField({
fieldLabel: label,
width: 120,
allowBlank: q[j].allowBlank
});
if (q[j].response && q[j].response.length > 0) {
question.setValue( q[j].response );
question.decimalSeparator = '$CommonFunctions.localeDecimalSeparator()';
demandTable.numFields[numFieldCnt++] = question;
}
} else if (q[j].respTypeId == "000000000004") {
question = new Ext.form.TextArea({
fieldLabel: label,
width: 550,
maxLength: 1024,
allowBlank: q[j].allowBlank,
value: q[j].response
});
} else if (q[j].respTypeId == "000000000005") {
question = new Ext.form.DateField({
fieldLabel: label,
width: 120,
allowBlank: q[j].allowBlank,
value: TimeManager.convertToLocaleDate(q[j].response),
format: '$LocaleDateFormat'
});
demandTable.dateFields[dateFieldCnt++] = question;
} else if (q[j].respTypeId == "000000000002") {
question = new Ext.form.ComboBox({
fieldLabel: label,
store: new Ext.data.SimpleStore({
fields: ['id', 'value'],
data : [['', '$not_set_text'], ['Y', '$true_text'], ['N', '$false_text']]
}),
valueField:'id',
displayField:'value',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
editable: false,
selectOnFocus:true,
grow: false,
resizable:false,
width:120,
maxHeight: 150,
allowBlank: q[j].allowBlank ,
validator : function(v) {
if (!this.allowBlank && (!v || v == '$not_set_text')) {
return '$field_required_text';
}
return true;
}
});
var result = '';
if (q[j].response) {
result = q[j].response;
}
question.setValue( result );
} else if (q[j].respTypeId == "000000000007") {
question = new Ext.form.ComboBox({
fieldLabel: label,
store: new Ext.data.SimpleStore({
fields: ['id', 'value'],
data : Ext.util.JSON.decode( q[j].choices )
}),
valueField:'id',
displayField:'value',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
editable: false,
selectOnFocus:true,
grow: false,
resizable:false,
width:220,
maxHeight: 150,
allowBlank: q[j].allowBlank,
validator : function(v) {
if (!this.allowBlank && (!v || v == '$forms_not_answered_text')) {
return '$field_required_text';
}
return true;
}
});
var result = '';
if (q[j].response) {
result = q[j].response;
}
question.setValue( result );
} else if (q[j].respTypeId == "000000000008") {
var hidden = new Ext.form.Hidden({
name: 'c.'+q[j].questionId,
value: ''
});
formGroup.add( hidden );
question = new Ext.form.FieldSet({
title: label,
layout: 'fit',
width:400,
allowBlank: q[j].allowBlank,
validate : function() {
var checkedCnt = 0;
if (!allowBlank) {
for (var m = 0; m < this.checkboxes.length; m++) {
if (this.checkboxes[m].checked == true) {
checkedCnt++;
}
}
if (checkedCnt == 0) {
Ext.MessageBox.show({
title: '$required_response_title',
msg: '"' + this.title + '" ' + '$required_response_message',
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.WARNING
});
return false;
}
}
return true;
},
checkboxes: []
});
var checkboxSet = question.checkboxes;
var choices = Ext.util.JSON.decode( q[j].choices );
for (var k = 0; k < choices.length; k++) {
var choice = new Ext.form.Checkbox({
width:300,
id: 'dc.'+q[j].questionId + '.' + choices[k][0],
name: 'c.'+q[j].questionId + '.' + choices[k][0],
boxLabel: choices[k][1]
});
checkboxSet[k] = choice;
if (q[j].response && q[j].response.indexOf( choices[k][0] ) > -1) {
choice.setValue( true );
}
question.add( choice );
}
} else if (q[j].respTypeId == "000000000009") {
if (demandTable.usersDS.find('name','$forms_not_answered_text') == -1) {
var notAnsweredRecord = Ext.data.Record.create([{name: 'userId'},{name: 'name'}]);
var notAnsweredObj = new notAnsweredRecord({userId: '', name: '$forms_not_answered_text'});
demandTable.usersDS.insert(0, notAnsweredObj);
}
question = new Ext.form.ComboBox({
fieldLabel: label,
store: demandTable.usersDS,
valueField:'userId',
displayField:'name',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
editable: false,
selectOnFocus:true,
grow: false,
resizable:false,
width:300,
maxHeight: 150,
allowBlank: q[j].allowBlank,
validator : function(v) {
if (!this.allowBlank && (!v || v == '$forms_not_answered_text')) {
return '$field_required_text';
}
return true;
}
});
var result = '';
if (q[j].response) {
result = q[j].response;
}
question.setValue( result );
}
if (question) {
if (q[j].respTypeId != "AnswerAbove") {
question.id = 'dq.'+q[j].questionId;
}
question.name = 'q.'+q[j].questionId;
question.hiddenName = 'q.'+q[j].questionId;
formGroup.add( question );
if (q[j].respTypeId != "AnswerAbove") {
demandTable.formGroupMap[questionId] = question;
}
count++;
}
}
}
demandTable.oldQuestionResponses = d;
demandTable.formsTabPanel.doLayout( false );
}
demandTable.formsTabPanel = new Ext.Panel({
width: 650,
height: 371,
layout: 'form',
frame:true,
labelAlign: 'top',
waitMsgTarget: true,
autoScroll: true
});