// Copyright (C) 2010 Combodo SARL // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 3 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA function ExtKeyWidget(id, sClass, sAttCode, sSuffix, bSelectMode, oWizHelper) { this.id = id; this.sClass = sClass; this.sAttCode = sAttCode; this.sSuffix = sSuffix; this.emptyHtml = ''; // content to be displayed when the search results are empty (when opening the dialog) this.emptyOnClose = true; // Workaround for the JQuery dialog being very slow when opening and closing if the content contains many INPUT tags this.oWizardHelper = oWizHelper; this.ajax_request = null; this.bSelectMode = bSelectMode; // true if the edited field is a SELECT, false if it's an autocomplete this.v_html = ''; var me = this; this.Init = function() { // make sure that the form is clean $('#'+this.id+'_btnRemove').attr('disabled','disabled'); $('#'+this.id+'_linksToRemove').val(''); } this.StopPendingRequest = function() { if (me.ajax_request) { me.ajax_request.Abort(); me.ajax_request = null; } } this.Search = function() { $('#ac_dlg_'+me.id).dialog('open'); this.UpdateSizes(); this.UpdateButtons(); } this.UpdateSizes = function() { var dlg = $('#ac_dlg_'+me.id); var searchForm = dlg.find('div.display_block:first'); // Top search form, enclosing display_block var results = $('#dr_'+me.id); padding_right = parseInt(dlg.css('padding-right').replace('px', '')); padding_left = parseInt(dlg.css('padding-left').replace('px', '')); padding_top = parseInt(dlg.css('padding-top').replace('px', '')); padding_bottom = parseInt(dlg.css('padding-bottom').replace('px', '')); width = dlg.innerWidth() - padding_right - padding_left - 22; // 5 (margin-left) + 5 (padding-left) + 5 (padding-right) + 5 (margin-right) + 2 for rounding ! height = dlg.innerHeight() - padding_top - padding_bottom -22; form_height = searchForm.outerHeight(); results.height(height - form_height - 40); // Leave some space for the buttons } this.UpdateButtons = function() { var okBtn = $('#btn_ok_'+me.id); if ($('#fr_'+me.id+' input[name=selectObject]:checked').length > 0) { okBtn.attr('disabled', ''); } else { okBtn.attr('disabled', 'disabled'); } } this.DoSearchObjects = function(id) { var theMap = { sAttCode: me.sAttCode, iInputId: me.id, sSuffix: me.sSuffix, } // Gather the parameters from the search form $('#fs_'+me.id+' :input').each( function(i) { if (this.name != '') { theMap[this.name] = this.value; } } ); if (me.oWizardHelper == null) { theMap['json'] = ''; } else { // Not inside a "search form", updating a real object me.oWizardHelper.UpdateWizard(); theMap['json'] = me.oWizardHelper.ToJSON(); } theMap['sRemoteClass'] = theMap['class']; // swap 'class' (defined in the form) and 'remoteClass' theMap['class'] = me.sClass; theMap.operation = 'searchObjectsToSelect'; // Override what is defined in the form itself sSearchAreaId = '#dr_'+me.id; //$(sSearchAreaId).html('

');
}
else
{
$('#label_'+me.id).addClass('ac_loading');
}
me.oWizardHelper.UpdateWizard();
var theMap = { sAttCode: me.sAttCode,
iInputId: me.id,
sSuffix: me.sSuffix,
'class': me.sClass,
'json': me.oWizardHelper.ToJSON(),
operation: 'objectCreationForm'
}
// Make sure that we cancel any pending request before issuing another
// since responses may arrive in arbitrary order
me.StopPendingRequest();
// Run the query and get the result back directly in HTML
me.ajax_request = $.post( '../pages/ajax.render.php', theMap,
function(data)
{
$('#ajax_'+me.id).html(data);
$('#ac_create_'+me.id).dialog('open');
$('#ac_create_'+me.id).dialog( "option", "close", me.OnCloseCreateObject );
// Modify the action of the cancel button
$('#ac_create_'+me.id+' button.cancel').unbind('click').click( me.CloseCreateObject );
me.ajax_request = null;
},
'html'
);
}
this.CloseCreateObject = function()
{
$('#ac_create_'+me.id).dialog( "close" );
}
this.OnCloseCreateObject = function()
{
if (me.bSelectMode)
{
$('#v_'+me.id).html(me.v_html);
}
else
{
$('#label_'+me.id).removeClass('ac_loading');
}
$('#label_'+me.id).focus();
$('#ac_create_'+me.id).dialog("destroy");
$('#ac_create_'+me.id).remove();
$('#ajax_'+me.id).html('');
}
this.DoCreateObject = function()
{
var sFormId = $('#dcr_'+me.id+' form').attr('id');
if (CheckFields(sFormId, true))
{
$('#'+sFormId).block();
var theMap = { sAttCode: me.sAttCode,
iInputId: me.id,
sSuffix: me.sSuffix,
'class': me.sClass,
'json': me.oWizardHelper.ToJSON()
}
// Gather the values from the form
// Gather the parameters from the search form
$('#'+sFormId+' :input').each(
function(i)
{
if (this.name != '')
{
theMap[this.name] = this.value;
}
}
);
// Override the 'operation' code
theMap['operation'] = 'doCreateObject';
theMap['class'] = me.sClass;
$('#ac_create_'+me.id).dialog('close');
// Make sure that we cancel any pending request before issuing another
// since responses may arrive in arbitrary order
me.StopPendingRequest();
// Run the query and get the result back directly in JSON
me.ajax_request = $.post( '../pages/ajax.render.php', theMap,
function(data)
{
if (me.bSelectMode)
{
// Add the newly created object to the drop-down list and select it
$('', { value : data.id }).text(data.name).appendTo('#'+me.id);
$('#'+me.id+' option[value="'+data.id+'"]').attr('selected', 'selected');
$('#'+me.id).focus();
}
else
{
// Put the value corresponding to the newly created object in the autocomplete
$('#label_'+me.id).val(data.name);
$('#'+me.id).val(data.id);
$('#label_'+me.id).removeClass('ac_loading');
$('#label_'+me.id).focus();
}
$('#'+me.id).trigger('validate');
me.ajax_request = null;
},
'json'
);
}
return false; // do NOT submit the form
}
}