/**
* functions used in setup script
*
* @version $Id$
*/
// show this window in top frame
if (top != self) {
window.top.location.href = location;
}
// default values for fields
var defaultValues = {};
// language strings
var PMA_messages = {};
/**
* Returns field type
*
* @param Element field
*/
function getFieldType(field) {
field = $(field);
var tagName = field.attr('tagName');
if (tagName == 'INPUT') {
return field.attr('type');
} else if (tagName == 'SELECT') {
return 'select';
} else if (tagName == 'TEXTAREA') {
return 'text';
}
return '';
}
/**
* Sets field value
*
* value must be of type:
* o undefined (omitted) - restore default value (form default, not PMA default)
* o String - if field_type is 'text'
* o boolean - if field_type is 'checkbox'
* o Array of values - if field_type is 'select'
*
* @param Element field
* @param String field_type see getFieldType
* @param mixed value
*/
function setFieldValue(field, field_type, value) {
field = $(field);
switch (field_type) {
case 'text':
field.attr('value', (value != undefined ? value : field.attr('defaultValue')));
break;
case 'checkbox':
field.attr('checked', (value != undefined ? value : field.attr('defaultChecked')));
break;
case 'select':
var options = field.attr('options');
var i, imax = options.length;
if (value == undefined) {
for (i = 0; i < imax; i++) {
options[i].selected = options[i].defaultSelected;
}
} else {
for (i = 0; i < imax; i++) {
options[i].selected = (value.indexOf(options[i].value) != -1);
}
}
break;
}
markField(field);
}
/**
* Gets field value
*
* Will return one of:
* o String - if type is 'text'
* o boolean - if type is 'checkbox'
* o Array of values - if type is 'select'
*
* @param Element field
* @param String field_type see getFieldType
* @return mixed
*/
function getFieldValue(field, field_type) {
field = $(field);
switch (field_type) {
case 'text':
return field.attr('value');
case 'checkbox':
return field.attr('checked');
case 'select':
var options = field.attr('options');
var i, imax = options.length, items = [];
for (i = 0; i < imax; i++) {
if (options[i].selected) {
items.push(options[i].value);
}
}
return items;
}
}
/**
* Returns values for all fields in fieldsets
*/
function getAllValues() {
var elements = $('fieldset input, fieldset select, fieldset textarea');
var values = {};
var type, value;
for (var i = 0; i < elements.length; i++) {
type = getFieldType(elements[i]);
value = getFieldValue(elements[i], type);
if (typeof value != 'undefined') {
// we only have single selects, fatten array
if (type == 'select') {
value = value[0];
}
values[elements[i].name] = value;
}
}
return values;
}
/**
* Checks whether field has its default value
*
* @param Element field
* @param String type
* @return boolean
*/
function checkFieldDefault(field, type) {
field = $(field);
var field_id = field.attr('id');
if (typeof defaultValues[field_id] == 'undefined') {
return true;
}
var isDefault = true;
var currentValue = getFieldValue(field, type);
if (type != 'select') {
isDefault = currentValue == defaultValues[field_id];
} else {
// compare arrays, will work for our representation of select values
if (currentValue.length != defaultValues[field_id].length) {
isDefault = false;
}
else {
for (var i = 0; i < currentValue.length; i++) {
if (currentValue[i] != defaultValues[field_id][i]) {
isDefault = false;
break;
}
}
}
}
return isDefault;
}
/**
* Returns element's id prefix
* @param Element element
*/
function getIdPrefix(element) {
return $(element).attr('id').replace(/[^-]+$/, '');
}
// ------------------------------------------------------------------
// Messages
//
// stores hidden message ids
var hiddenMessages = [];
$(function() {
var hidden = hiddenMessages.length;
for (var i = 0; i < hidden; i++) {
$('#'+hiddenMessages[i]).css('display', 'none');
}
if (hidden > 0) {
var link = $('#show_hidden_messages');
link.click(function(e) {
e.preventDefault();
for (var i = 0; i < hidden; i++) {
$('#'+hiddenMessages[i]).show(500);
}
$(this).remove();
});
link.html(link.html().replace('#MSG_COUNT', hidden));
link.css('display', '');
}
});
//
// END: Messages
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// Form validation and field operations
//
// form validator assignments
var validate = {};
// form validator list
var validators = {
// regexp: numeric value
_regexp_numeric: new RegExp('^[0-9]*$'),
/**
* Validates positive number
*
* @param boolean isKeyUp
*/
validate_positive_number: function (isKeyUp) {
var result = this.value != '0' && validators._regexp_numeric.test(this.value);
return result ? true : PMA_messages['error_nan_p'];
},
/**
* Validates non-negative number
*
* @param boolean isKeyUp
*/
validate_non_negative_number: function (isKeyUp) {
var result = validators._regexp_numeric.test(this.value);
return result ? true : PMA_messages['error_nan_nneg'];
},
/**
* Validates port number
*
* @param boolean isKeyUp
*/
validate_port_number: function(isKeyUp) {
var result = validators._regexp_numeric.test(this.value) && this.value != '0';
if (!result || this.value > 65536) {
result = PMA_messages['error_incorrect_port'];
}
return result;
},
// field validators
_field: {
/**
* hide_db field
*
* @param boolean isKeyUp
*/
hide_db: function(isKeyUp) {
if (!isKeyUp && this.value != '') {
var data = {};
data[this.id] = this.value;
ajaxValidate(this, 'Servers/1/hide_db', data);
}
return true;
},
/**
* TrustedProxies field
*
* @param boolean isKeyUp
*/
TrustedProxies: function(isKeyUp) {
if (!isKeyUp && this.value != '') {
var data = {};
data[this.id] = this.value;
ajaxValidate(this, 'TrustedProxies', data);
}
return true;
}
},
// fieldset validators
_fieldset: {
/**
* Validates Server fieldset
*
* @param boolean isKeyUp
*/
Server: function(isKeyUp) {
if (!isKeyUp) {
ajaxValidate(this, 'Server', getAllValues());
}
return true;
},
/**
* Validates Server_login_options fieldset
*
* @param boolean isKeyUp
*/
Server_login_options: function(isKeyUp) {
return validators._fieldset.Server.apply(this, [isKeyUp]);
},
/**
* Validates Server_pmadb fieldset
*
* @param boolean isKeyUp
*/
Server_pmadb: function(isKeyUp) {
if (isKeyUp) {
return true;
}
var prefix = getIdPrefix($(this).find('input'));
var pmadb_active = $('#' + prefix + 'pmadb').val() != '';
if (pmadb_active) {
ajaxValidate(this, 'Server_pmadb', getAllValues());
}
return true;
}
}
};
/**
* Calls server-side validation procedures
*
* @param Element parent input field in