2016-10-22 14:57:10 +00:00
|
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
|
|
|
/**
|
|
|
|
* Functions used in configuration forms and on user preferences pages
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks whether browser supports web storage
|
|
|
|
*
|
|
|
|
* @param type the type of storage i.e. localStorage or sessionStorage
|
|
|
|
*
|
|
|
|
* @returns bool
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function isStorageSupported (type, warn) {
|
2016-10-22 14:57:10 +00:00
|
|
|
try {
|
|
|
|
window[type].setItem('PMATest', 'test');
|
|
|
|
// Check whether key-value pair was set successfully
|
|
|
|
if (window[type].getItem('PMATest') === 'test') {
|
|
|
|
// Supported, remove test variable from storage
|
|
|
|
window[type].removeItem('PMATest');
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-14 09:18:00 +00:00
|
|
|
} catch (error) {
|
2016-10-22 14:57:10 +00:00
|
|
|
// Not supported
|
2017-04-20 10:55:30 +00:00
|
|
|
if (warn) {
|
|
|
|
PMA_ajaxShowMessage(PMA_messages.strNoLocalStorage, false);
|
|
|
|
}
|
2016-10-22 14:57:10 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbind all event handlers before tearing down a page
|
|
|
|
*/
|
|
|
|
AJAX.registerTeardown('config.js', function () {
|
2018-04-14 09:18:00 +00:00
|
|
|
$('.optbox input[id], .optbox select[id], .optbox textarea[id]').off('change').off('keyup');
|
|
|
|
$('.optbox input[type=button][name=submit_reset]').off('click');
|
|
|
|
$('div.tabs_contents').off();
|
|
|
|
$('#import_local_storage, #export_local_storage').off('click');
|
|
|
|
$('form.prefs-form').off('change').off('submit');
|
2016-10-22 14:57:10 +00:00
|
|
|
$(document).off('click', 'div.click-hide-message');
|
2018-04-14 09:18:00 +00:00
|
|
|
$('#prefs_autoload').find('a').off('click');
|
2016-10-22 14:57:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
AJAX.registerOnload('config.js', function () {
|
|
|
|
var $topmenu_upt = $('#topmenu2.user_prefs_tabs');
|
|
|
|
$topmenu_upt.find('li.active a').attr('rel', 'samepage');
|
|
|
|
$topmenu_upt.find('li:not(.active) a').attr('rel', 'newpage');
|
|
|
|
});
|
|
|
|
|
|
|
|
// default values for fields
|
|
|
|
var defaultValues = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns field type
|
|
|
|
*
|
|
|
|
* @param {Element} field
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function getFieldType (field) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
var tagName = $field.prop('tagName');
|
2018-04-14 09:18:00 +00:00
|
|
|
if (tagName === 'INPUT') {
|
2016-10-22 14:57:10 +00:00
|
|
|
return $field.attr('type');
|
2018-04-14 09:18:00 +00:00
|
|
|
} else if (tagName === 'SELECT') {
|
2016-10-22 14:57:10 +00:00
|
|
|
return 'select';
|
2018-04-14 09:18:00 +00:00
|
|
|
} else if (tagName === 'TEXTAREA') {
|
2016-10-22 14:57:10 +00:00
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables the "restore default value" button
|
|
|
|
*
|
|
|
|
* @param {Element} field
|
|
|
|
* @param {boolean} display
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function setRestoreDefaultBtn (field, display) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $el = $(field).closest('td').find('.restore-default img');
|
|
|
|
$el[display ? 'show' : 'hide']();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks field depending on its value (system default or custom)
|
|
|
|
*
|
|
|
|
* @param {Element} field
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function markField (field) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
var type = getFieldType($field);
|
|
|
|
var isDefault = checkFieldDefault($field, type);
|
|
|
|
|
|
|
|
// checkboxes uses parent <span> for marking
|
2018-04-14 09:18:00 +00:00
|
|
|
var $fieldMarker = (type === 'checkbox') ? $field.parent() : $field;
|
2016-10-22 14:57:10 +00:00
|
|
|
setRestoreDefaultBtn($field, !isDefault);
|
|
|
|
$fieldMarker[isDefault ? 'removeClass' : 'addClass']('custom');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {@link #getFieldType}
|
2018-04-14 09:18:00 +00:00
|
|
|
* @param {String|Boolean} value
|
2016-10-22 14:57:10 +00:00
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function setFieldValue (field, field_type, value) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
switch (field_type) {
|
|
|
|
case 'text':
|
|
|
|
case 'number':
|
2018-04-14 09:18:00 +00:00
|
|
|
$field.val(value);
|
2016-10-22 14:57:10 +00:00
|
|
|
break;
|
|
|
|
case 'checkbox':
|
2018-04-14 09:18:00 +00:00
|
|
|
$field.prop('checked', value);
|
2016-10-22 14:57:10 +00:00
|
|
|
break;
|
|
|
|
case 'select':
|
|
|
|
var options = $field.prop('options');
|
2018-04-14 09:18:00 +00:00
|
|
|
var i;
|
|
|
|
var imax = options.length;
|
2016-10-22 14:57:10 +00:00
|
|
|
var i, imax = options.length;
|
2018-04-14 09:18:00 +00:00
|
|
|
for (i = 0; i < imax; i++) {
|
|
|
|
options[i].selected = (value.indexOf(options[i].value) != -1);
|
2016-10-22 14:57:10 +00:00
|
|
|
}
|
|
|
|
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 returned by {@link #getFieldType}
|
|
|
|
* @type Boolean|String|String[]
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function getFieldValue (field, field_type) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
switch (field_type) {
|
|
|
|
case 'text':
|
|
|
|
case 'number':
|
|
|
|
return $field.prop('value');
|
|
|
|
case 'checkbox':
|
|
|
|
return $field.prop('checked');
|
|
|
|
case 'select':
|
|
|
|
var options = $field.prop('options');
|
2018-04-14 09:18:00 +00:00
|
|
|
var i;
|
|
|
|
var imax = options.length;
|
|
|
|
var items = [];
|
2016-10-22 14:57:10 +00:00
|
|
|
for (i = 0; i < imax; i++) {
|
|
|
|
if (options[i].selected) {
|
|
|
|
items.push(options[i].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns values for all fields in fieldsets
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function getAllValues () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $elements = $('fieldset input, fieldset select, fieldset textarea');
|
|
|
|
var values = {};
|
2018-04-14 09:18:00 +00:00
|
|
|
var type;
|
|
|
|
var value;
|
2016-10-22 14:57:10 +00:00
|
|
|
for (var i = 0; i < $elements.length; i++) {
|
|
|
|
type = getFieldType($elements[i]);
|
|
|
|
value = getFieldValue($elements[i], type);
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof value !== 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
// we only have single selects, fatten array
|
2018-04-14 09:18:00 +00:00
|
|
|
if (type === 'select') {
|
2016-10-22 14:57:10 +00:00
|
|
|
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
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function checkFieldDefault (field, type) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
var field_id = $field.attr('id');
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof defaultValues[field_id] === 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var isDefault = true;
|
|
|
|
var currentValue = getFieldValue($field, type);
|
2018-04-14 09:18:00 +00:00
|
|
|
if (type !== 'select') {
|
|
|
|
isDefault = currentValue === defaultValues[field_id];
|
2016-10-22 14:57:10 +00:00
|
|
|
} else {
|
|
|
|
// compare arrays, will work for our representation of select values
|
2018-04-14 09:18:00 +00:00
|
|
|
if (currentValue.length !== defaultValues[field_id].length) {
|
2016-10-22 14:57:10 +00:00
|
|
|
isDefault = false;
|
2018-04-14 09:18:00 +00:00
|
|
|
} else {
|
2016-10-22 14:57:10 +00:00
|
|
|
for (var i = 0; i < currentValue.length; i++) {
|
2018-04-14 09:18:00 +00:00
|
|
|
if (currentValue[i] !== defaultValues[field_id][i]) {
|
2016-10-22 14:57:10 +00:00
|
|
|
isDefault = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns element's id prefix
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function getIdPrefix (element) {
|
2016-10-22 14:57:10 +00:00
|
|
|
return $(element).attr('id').replace(/[^-]+$/, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Form validation and field operations
|
|
|
|
//
|
|
|
|
|
|
|
|
// form validator assignments
|
|
|
|
var validate = {};
|
|
|
|
|
|
|
|
// form validator list
|
|
|
|
var validators = {
|
|
|
|
// regexp: numeric value
|
|
|
|
_regexp_numeric: /^[0-9]+$/,
|
|
|
|
// regexp: extract parts from PCRE expression
|
|
|
|
_regexp_pcre_extract: /(.)(.*)\1(.*)?/,
|
|
|
|
/**
|
|
|
|
* Validates positive number
|
|
|
|
*
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
*/
|
|
|
|
PMA_validatePositiveNumber: function (isKeyUp) {
|
|
|
|
if (isKeyUp && this.value === '') {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-14 09:18:00 +00:00
|
|
|
var result = this.value !== '0' && validators._regexp_numeric.test(this.value);
|
2016-10-22 14:57:10 +00:00
|
|
|
return result ? true : PMA_messages.error_nan_p;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Validates non-negative number
|
|
|
|
*
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
*/
|
|
|
|
PMA_validateNonNegativeNumber: function (isKeyUp) {
|
|
|
|
if (isKeyUp && this.value === '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var result = validators._regexp_numeric.test(this.value);
|
|
|
|
return result ? true : PMA_messages.error_nan_nneg;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Validates port number
|
|
|
|
*
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
*/
|
|
|
|
PMA_validatePortNumber: function (isKeyUp) {
|
|
|
|
if (this.value === '') {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-14 09:18:00 +00:00
|
|
|
var result = validators._regexp_numeric.test(this.value) && this.value !== '0';
|
2016-10-22 14:57:10 +00:00
|
|
|
return result && this.value <= 65535 ? true : PMA_messages.error_incorrect_port;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Validates value according to given regular expression
|
|
|
|
*
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
* @param {string} regexp
|
|
|
|
*/
|
|
|
|
PMA_validateByRegex: function (isKeyUp, regexp) {
|
|
|
|
if (isKeyUp && this.value === '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// convert PCRE regexp
|
|
|
|
var parts = regexp.match(validators._regexp_pcre_extract);
|
|
|
|
var valid = this.value.match(new RegExp(parts[2], parts[3])) !== null;
|
|
|
|
return valid ? true : PMA_messages.error_invalid_value;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Validates upper bound for numeric inputs
|
|
|
|
*
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
* @param {int} max_value
|
|
|
|
*/
|
|
|
|
PMA_validateUpperBound: function (isKeyUp, max_value) {
|
|
|
|
var val = parseInt(this.value, 10);
|
|
|
|
if (isNaN(val)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return val <= max_value ? true : PMA_sprintf(PMA_messages.error_value_lte, max_value);
|
|
|
|
},
|
|
|
|
// field validators
|
|
|
|
_field: {
|
|
|
|
},
|
|
|
|
// fieldset validators
|
|
|
|
_fieldset: {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers validator for given field
|
|
|
|
*
|
|
|
|
* @param {String} id field id
|
|
|
|
* @param {String} type validator (key in validators object)
|
|
|
|
* @param {boolean} onKeyUp whether fire on key up
|
|
|
|
* @param {Array} params validation function parameters
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function validateField (id, type, onKeyUp, params) {
|
|
|
|
if (typeof validators[type] === 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof validate[id] === 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
validate[id] = [];
|
|
|
|
}
|
2019-12-08 07:14:01 +00:00
|
|
|
if (validate[id].length === 0) {
|
|
|
|
validate[id].push([type, params, onKeyUp]);
|
|
|
|
}
|
2016-10-22 14:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns validation functions associated with form field
|
|
|
|
*
|
|
|
|
* @param {String} field_id form field id
|
|
|
|
* @param {boolean} onKeyUpOnly see validateField
|
|
|
|
* @type Array
|
|
|
|
* @return array of [function, parameters to be passed to function]
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function getFieldValidators (field_id, onKeyUpOnly) {
|
2016-10-22 14:57:10 +00:00
|
|
|
// look for field bound validator
|
|
|
|
var name = field_id && field_id.match(/[^-]+$/)[0];
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof validators._field[name] !== 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
return [[validators._field[name], null]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for registered validators
|
|
|
|
var functions = [];
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof validate[field_id] !== 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
// validate[field_id]: array of [type, params, onKeyUp]
|
|
|
|
for (var i = 0, imax = validate[field_id].length; i < imax; i++) {
|
|
|
|
if (onKeyUpOnly && !validate[field_id][i][2]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
functions.push([validators[validate[field_id][i][0]], validate[field_id][i][1]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return functions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays errors for given form fields
|
|
|
|
*
|
|
|
|
* WARNING: created DOM elements must be identical with the ones made by
|
2018-04-14 09:18:00 +00:00
|
|
|
* PhpMyAdmin\Config\FormDisplayTemplate::displayInput()!
|
2016-10-22 14:57:10 +00:00
|
|
|
*
|
|
|
|
* @param {Object} error_list list of errors in the form {field id: error array}
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function displayErrors (error_list) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var tempIsEmpty = function (item) {
|
|
|
|
return item !== '';
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var field_id in error_list) {
|
|
|
|
var errors = error_list[field_id];
|
|
|
|
var $field = $('#' + field_id);
|
2018-04-14 09:18:00 +00:00
|
|
|
var isFieldset = $field.attr('tagName') === 'FIELDSET';
|
2016-10-22 14:57:10 +00:00
|
|
|
var $errorCnt;
|
|
|
|
if (isFieldset) {
|
|
|
|
$errorCnt = $field.find('dl.errors');
|
|
|
|
} else {
|
|
|
|
$errorCnt = $field.siblings('.inline_errors');
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove empty errors (used to clear error list)
|
|
|
|
errors = $.grep(errors, tempIsEmpty);
|
|
|
|
|
|
|
|
// CSS error class
|
|
|
|
if (!isFieldset) {
|
|
|
|
// checkboxes uses parent <span> for marking
|
2018-04-14 09:18:00 +00:00
|
|
|
var $fieldMarker = ($field.attr('type') === 'checkbox') ? $field.parent() : $field;
|
2016-10-22 14:57:10 +00:00
|
|
|
$fieldMarker[errors.length ? 'addClass' : 'removeClass']('field-error');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length) {
|
|
|
|
// if error container doesn't exist, create it
|
|
|
|
if ($errorCnt.length === 0) {
|
|
|
|
if (isFieldset) {
|
|
|
|
$errorCnt = $('<dl class="errors" />');
|
|
|
|
$field.find('table').before($errorCnt);
|
|
|
|
} else {
|
|
|
|
$errorCnt = $('<dl class="inline_errors" />');
|
|
|
|
$field.closest('td').append($errorCnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var html = '';
|
|
|
|
for (var i = 0, imax = errors.length; i < imax; i++) {
|
|
|
|
html += '<dd>' + errors[i] + '</dd>';
|
|
|
|
}
|
|
|
|
$errorCnt.html(html);
|
|
|
|
} else if ($errorCnt !== null) {
|
|
|
|
// remove useless error container
|
|
|
|
$errorCnt.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:14:01 +00:00
|
|
|
/**
|
|
|
|
* Validates fields and fieldsets and call displayError function as required
|
|
|
|
*/
|
|
|
|
function setDisplayError() {
|
|
|
|
var elements = $('.optbox input[id], .optbox select[id], .optbox textarea[id]');
|
|
|
|
// run all field validators
|
|
|
|
var errors = {};
|
|
|
|
for (var i = 0; i < elements.length; i++) {
|
|
|
|
validate_field(elements[i], false, errors);
|
|
|
|
}
|
|
|
|
// run all fieldset validators
|
|
|
|
$('fieldset.optbox').each(function () {
|
|
|
|
validate_fieldset(this, false, errors);
|
|
|
|
});
|
|
|
|
displayErrors(errors);
|
|
|
|
}
|
|
|
|
|
2016-10-22 14:57:10 +00:00
|
|
|
/**
|
|
|
|
* Validates fieldset and puts errors in 'errors' object
|
|
|
|
*
|
|
|
|
* @param {Element} fieldset
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
* @param {Object} errors
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function validate_fieldset (fieldset, isKeyUp, errors) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $fieldset = $(fieldset);
|
2018-04-14 09:18:00 +00:00
|
|
|
if ($fieldset.length && typeof validators._fieldset[$fieldset.attr('id')] !== 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
var fieldset_errors = validators._fieldset[$fieldset.attr('id')].apply($fieldset[0], [isKeyUp]);
|
|
|
|
for (var field_id in fieldset_errors) {
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof errors[field_id] === 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
errors[field_id] = [];
|
|
|
|
}
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof fieldset_errors[field_id] === 'string') {
|
2016-10-22 14:57:10 +00:00
|
|
|
fieldset_errors[field_id] = [fieldset_errors[field_id]];
|
|
|
|
}
|
|
|
|
$.merge(errors[field_id], fieldset_errors[field_id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates form field and puts errors in 'errors' object
|
|
|
|
*
|
|
|
|
* @param {Element} field
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
* @param {Object} errors
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function validate_field (field, isKeyUp, errors) {
|
|
|
|
var args;
|
|
|
|
var result;
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
var field_id = $field.attr('id');
|
|
|
|
errors[field_id] = [];
|
|
|
|
var functions = getFieldValidators(field_id, isKeyUp);
|
|
|
|
for (var i = 0; i < functions.length; i++) {
|
|
|
|
if (typeof functions[i][1] !== 'undefined' && functions[i][1] !== null) {
|
|
|
|
args = functions[i][1].slice(0);
|
|
|
|
} else {
|
|
|
|
args = [];
|
|
|
|
}
|
|
|
|
args.unshift(isKeyUp);
|
|
|
|
result = functions[i][0].apply($field[0], args);
|
|
|
|
if (result !== true) {
|
2018-04-14 09:18:00 +00:00
|
|
|
if (typeof result === 'string') {
|
2016-10-22 14:57:10 +00:00
|
|
|
result = [result];
|
|
|
|
}
|
|
|
|
$.merge(errors[field_id], result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates form field and parent fieldset
|
|
|
|
*
|
|
|
|
* @param {Element} field
|
|
|
|
* @param {boolean} isKeyUp
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function validate_field_and_fieldset (field, isKeyUp) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $(field);
|
|
|
|
var errors = {};
|
|
|
|
validate_field($field, isKeyUp, errors);
|
|
|
|
validate_fieldset($field.closest('fieldset.optbox'), isKeyUp, errors);
|
|
|
|
displayErrors(errors);
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:18:00 +00:00
|
|
|
function loadInlineConfig () {
|
2016-10-22 14:57:10 +00:00
|
|
|
if (!Array.isArray(configInlineParams)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < configInlineParams.length; ++i) {
|
|
|
|
if (typeof configInlineParams[i] === 'function') {
|
|
|
|
configInlineParams[i]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:18:00 +00:00
|
|
|
function setupValidation () {
|
2016-10-22 14:57:10 +00:00
|
|
|
validate = {};
|
|
|
|
configScriptLoaded = true;
|
2018-04-14 09:18:00 +00:00
|
|
|
if (configScriptLoaded && typeof configInlineParams !== 'undefined') {
|
2016-10-22 14:57:10 +00:00
|
|
|
loadInlineConfig();
|
|
|
|
}
|
|
|
|
// register validators and mark custom values
|
|
|
|
var $elements = $('.optbox input[id], .optbox select[id], .optbox textarea[id]');
|
|
|
|
$elements.each(function () {
|
|
|
|
markField(this);
|
|
|
|
var $el = $(this);
|
2018-04-14 09:18:00 +00:00
|
|
|
$el.on('change', function () {
|
2016-10-22 14:57:10 +00:00
|
|
|
validate_field_and_fieldset(this, false);
|
|
|
|
markField(this);
|
|
|
|
});
|
|
|
|
var tagName = $el.attr('tagName');
|
|
|
|
// text fields can be validated after each change
|
2018-04-14 09:18:00 +00:00
|
|
|
if (tagName === 'INPUT' && $el.attr('type') === 'text') {
|
2016-10-22 14:57:10 +00:00
|
|
|
$el.keyup(function () {
|
|
|
|
validate_field_and_fieldset($el, true);
|
|
|
|
markField($el);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// disable textarea spellcheck
|
2018-04-14 09:18:00 +00:00
|
|
|
if (tagName === 'TEXTAREA') {
|
2016-10-22 14:57:10 +00:00
|
|
|
$el.attr('spellcheck', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// check whether we've refreshed a page and browser remembered modified
|
|
|
|
// form values
|
|
|
|
var $check_page_refresh = $('#check_page_refresh');
|
2018-04-14 09:18:00 +00:00
|
|
|
if ($check_page_refresh.length === 0 || $check_page_refresh.val() === '1') {
|
2016-10-22 14:57:10 +00:00
|
|
|
// run all field validators
|
|
|
|
var errors = {};
|
|
|
|
for (var i = 0; i < $elements.length; i++) {
|
|
|
|
validate_field($elements[i], false, errors);
|
|
|
|
}
|
|
|
|
// run all fieldset validators
|
|
|
|
$('fieldset.optbox').each(function () {
|
|
|
|
validate_fieldset(this, false, errors);
|
|
|
|
});
|
|
|
|
|
|
|
|
displayErrors(errors);
|
|
|
|
} else if ($check_page_refresh) {
|
|
|
|
$check_page_refresh.val('1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AJAX.registerOnload('config.js', function () {
|
|
|
|
setupValidation();
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// END: Form validation and field operations
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Tabbed forms
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets active tab
|
|
|
|
*
|
|
|
|
* @param {String} tab_id
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function setTab (tab_id) {
|
|
|
|
$('ul.tabs').each(function () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $this = $(this);
|
2017-04-20 10:55:30 +00:00
|
|
|
if (!$this.find('li a[href="#' + tab_id + '"]').length) {
|
2016-10-22 14:57:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-20 10:55:30 +00:00
|
|
|
$this.find('li').removeClass('active').find('a[href="#' + tab_id + '"]').parent().addClass('active');
|
2016-10-22 14:57:10 +00:00
|
|
|
$this.parent().find('div.tabs_contents fieldset').hide().filter('#' + tab_id).show();
|
2017-04-20 10:55:30 +00:00
|
|
|
var hashValue = 'tab_' + tab_id;
|
|
|
|
location.hash = hashValue;
|
|
|
|
$this.parent().find('input[name=tab_hash]').val(hashValue);
|
2016-10-22 14:57:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:18:00 +00:00
|
|
|
function setupConfigTabs () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var forms = $('form.config-form');
|
2018-04-14 09:18:00 +00:00
|
|
|
forms.each(function () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $this = $(this);
|
|
|
|
var $tabs = $this.find('ul.tabs');
|
|
|
|
if (!$tabs.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// add tabs events and activate one tab (the first one or indicated by location hash)
|
|
|
|
$tabs.find('li').removeClass('active');
|
|
|
|
$tabs.find('a')
|
|
|
|
.click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
setTab($(this).attr('href').substr(1));
|
|
|
|
})
|
|
|
|
.filter(':first')
|
|
|
|
.parent()
|
|
|
|
.addClass('active');
|
|
|
|
$this.find('div.tabs_contents fieldset').hide().filter(':first').show();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:18:00 +00:00
|
|
|
function adjustPrefsNotification () {
|
|
|
|
var $prefsAutoLoad = $('#prefs_autoload');
|
|
|
|
var $tableNameControl = $('#table_name_col_no');
|
|
|
|
var $prefsAutoShowing = ($prefsAutoLoad.css('display') !== 'none');
|
|
|
|
|
|
|
|
if ($prefsAutoShowing && $tableNameControl.length) {
|
|
|
|
$tableNameControl.css('top', '55px');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 14:57:10 +00:00
|
|
|
AJAX.registerOnload('config.js', function () {
|
|
|
|
setupConfigTabs();
|
2018-04-14 09:18:00 +00:00
|
|
|
adjustPrefsNotification();
|
2016-10-22 14:57:10 +00:00
|
|
|
|
|
|
|
// tab links handling, check each 200ms
|
|
|
|
// (works with history in FF, further browser support here would be an overkill)
|
|
|
|
var prev_hash;
|
|
|
|
var tab_check_fnc = function () {
|
2018-04-14 09:18:00 +00:00
|
|
|
if (location.hash !== prev_hash) {
|
2016-10-22 14:57:10 +00:00
|
|
|
prev_hash = location.hash;
|
2017-04-20 10:55:30 +00:00
|
|
|
if (prev_hash.match(/^#tab_[a-zA-Z0-9_]+$/)) {
|
2016-10-22 14:57:10 +00:00
|
|
|
// session ID is sometimes appended here
|
2017-04-20 10:55:30 +00:00
|
|
|
var hash = prev_hash.substr(5).split('&')[0];
|
2016-10-22 14:57:10 +00:00
|
|
|
if ($('#' + hash).length) {
|
|
|
|
setTab(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
tab_check_fnc();
|
|
|
|
setInterval(tab_check_fnc, 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// END: Tabbed forms
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Form reset buttons
|
|
|
|
//
|
|
|
|
|
|
|
|
AJAX.registerOnload('config.js', function () {
|
2018-04-14 09:18:00 +00:00
|
|
|
$('.optbox input[type=button][name=submit_reset]').on('click', function () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var fields = $(this).closest('fieldset').find('input, select, textarea');
|
|
|
|
for (var i = 0, imax = fields.length; i < imax; i++) {
|
2018-04-14 09:18:00 +00:00
|
|
|
setFieldValue(fields[i], getFieldType(fields[i]), defaultValues[fields[i].id]);
|
2016-10-22 14:57:10 +00:00
|
|
|
}
|
2019-12-08 07:14:01 +00:00
|
|
|
setDisplayError();
|
2016-10-22 14:57:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// END: Form reset buttons
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// "Restore default" and "set value" buttons
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restores field's default value
|
|
|
|
*
|
|
|
|
* @param {String} field_id
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function restoreField (field_id) {
|
2016-10-22 14:57:10 +00:00
|
|
|
var $field = $('#' + field_id);
|
|
|
|
if ($field.length === 0 || defaultValues[field_id] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setFieldValue($field, getFieldType($field), defaultValues[field_id]);
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:18:00 +00:00
|
|
|
function setupRestoreField () {
|
2016-10-22 14:57:10 +00:00
|
|
|
$('div.tabs_contents')
|
2018-04-14 09:18:00 +00:00
|
|
|
.on('mouseenter', '.restore-default, .set-value', function () {
|
2016-10-22 14:57:10 +00:00
|
|
|
$(this).css('opacity', 1);
|
|
|
|
})
|
2018-04-14 09:18:00 +00:00
|
|
|
.on('mouseleave', '.restore-default, .set-value', function () {
|
2016-10-22 14:57:10 +00:00
|
|
|
$(this).css('opacity', 0.25);
|
|
|
|
})
|
2018-04-14 09:18:00 +00:00
|
|
|
.on('click', '.restore-default, .set-value', function (e) {
|
2016-10-22 14:57:10 +00:00
|
|
|
e.preventDefault();
|
|
|
|
var href = $(this).attr('href');
|
|
|
|
var field_sel;
|
|
|
|
if ($(this).hasClass('restore-default')) {
|
|
|
|
field_sel = href;
|
|
|
|
restoreField(field_sel.substr(1));
|
|
|
|
} else {
|
|
|
|
field_sel = href.match(/^[^=]+/)[0];
|
|
|
|
var value = href.match(/\=(.+)$/)[1];
|
|
|
|
setFieldValue($(field_sel), 'text', value);
|
|
|
|
}
|
|
|
|
$(field_sel).trigger('change');
|
|
|
|
})
|
|
|
|
.find('.restore-default, .set-value')
|
|
|
|
// inline-block for IE so opacity inheritance works
|
2018-04-14 09:18:00 +00:00
|
|
|
.css({ display: 'inline-block', opacity: 0.25 });
|
2016-10-22 14:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AJAX.registerOnload('config.js', function () {
|
|
|
|
setupRestoreField();
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// END: "Restore default" and "set value" buttons
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// User preferences import/export
|
|
|
|
//
|
|
|
|
|
|
|
|
AJAX.registerOnload('config.js', function () {
|
|
|
|
offerPrefsAutoimport();
|
|
|
|
var $radios = $('#import_local_storage, #export_local_storage');
|
|
|
|
if (!$radios.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable JavaScript dependent fields
|
|
|
|
$radios
|
|
|
|
.prop('disabled', false)
|
|
|
|
.add('#export_text_file, #import_text_file')
|
|
|
|
.click(function () {
|
|
|
|
var enable_id = $(this).attr('id');
|
|
|
|
var disable_id;
|
|
|
|
if (enable_id.match(/local_storage$/)) {
|
|
|
|
disable_id = enable_id.replace(/local_storage$/, 'text_file');
|
|
|
|
} else {
|
|
|
|
disable_id = enable_id.replace(/text_file$/, 'local_storage');
|
|
|
|
}
|
|
|
|
$('#opts_' + disable_id).addClass('disabled').find('input').prop('disabled', true);
|
|
|
|
$('#opts_' + enable_id).removeClass('disabled').find('input').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
// detect localStorage state
|
2017-04-20 10:55:30 +00:00
|
|
|
var ls_supported = isStorageSupported('localStorage', true);
|
2016-10-22 14:57:10 +00:00
|
|
|
var ls_exists = ls_supported ? (window.localStorage.config || false) : false;
|
|
|
|
$('div.localStorage-' + (ls_supported ? 'un' : '') + 'supported').hide();
|
|
|
|
$('div.localStorage-' + (ls_exists ? 'empty' : 'exists')).hide();
|
|
|
|
if (ls_exists) {
|
|
|
|
updatePrefsDate();
|
|
|
|
}
|
|
|
|
$('form.prefs-form').change(function () {
|
|
|
|
var $form = $(this);
|
|
|
|
var disabled = false;
|
|
|
|
if (!ls_supported) {
|
|
|
|
disabled = $form.find('input[type=radio][value$=local_storage]').prop('checked');
|
2018-04-14 09:18:00 +00:00
|
|
|
} else if (!ls_exists && $form.attr('name') === 'prefs_import' &&
|
2016-10-22 14:57:10 +00:00
|
|
|
$('#import_local_storage')[0].checked
|
2018-04-14 09:18:00 +00:00
|
|
|
) {
|
2016-10-22 14:57:10 +00:00
|
|
|
disabled = true;
|
|
|
|
}
|
|
|
|
$form.find('input[type=submit]').prop('disabled', disabled);
|
|
|
|
}).submit(function (e) {
|
|
|
|
var $form = $(this);
|
2018-04-14 09:18:00 +00:00
|
|
|
if ($form.attr('name') === 'prefs_export' && $('#export_local_storage')[0].checked) {
|
2016-10-22 14:57:10 +00:00
|
|
|
e.preventDefault();
|
|
|
|
// use AJAX to read JSON settings and save them
|
|
|
|
savePrefsToLocalStorage($form);
|
2018-04-14 09:18:00 +00:00
|
|
|
} else if ($form.attr('name') === 'prefs_import' && $('#import_local_storage')[0].checked) {
|
2016-10-22 14:57:10 +00:00
|
|
|
// set 'json' input and submit form
|
|
|
|
$form.find('input[name=json]').val(window.localStorage.config);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', 'div.click-hide-message', function () {
|
|
|
|
$(this)
|
2018-04-14 09:18:00 +00:00
|
|
|
.hide()
|
|
|
|
.parent('.group')
|
|
|
|
.css('height', '')
|
|
|
|
.next('form')
|
|
|
|
.show();
|
2016-10-22 14:57:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves user preferences to localStorage
|
|
|
|
*
|
|
|
|
* @param {Element} form
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function savePrefsToLocalStorage (form) {
|
2016-10-22 14:57:10 +00:00
|
|
|
$form = $(form);
|
|
|
|
var submit = $form.find('input[type=submit]');
|
|
|
|
submit.prop('disabled', true);
|
|
|
|
$.ajax({
|
|
|
|
url: 'prefs_manage.php',
|
|
|
|
cache: false,
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
ajax_request: true,
|
2017-04-20 10:55:30 +00:00
|
|
|
server: PMA_commonParams.get('server'),
|
2016-10-22 14:57:10 +00:00
|
|
|
submit_get_json: true
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
if (typeof data !== 'undefined' && data.success === true) {
|
|
|
|
window.localStorage.config = data.prefs;
|
|
|
|
window.localStorage.config_mtime = data.mtime;
|
|
|
|
window.localStorage.config_mtime_local = (new Date()).toUTCString();
|
|
|
|
updatePrefsDate();
|
|
|
|
$('div.localStorage-empty').hide();
|
|
|
|
$('div.localStorage-exists').show();
|
|
|
|
var group = $form.parent('.group');
|
|
|
|
group.css('height', group.height() + 'px');
|
|
|
|
$form.hide('fast');
|
|
|
|
$form.prev('.click-hide-message').show('fast');
|
|
|
|
} else {
|
|
|
|
PMA_ajaxShowMessage(data.error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
submit.prop('disabled', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates preferences timestamp in Import form
|
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function updatePrefsDate () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var d = new Date(window.localStorage.config_mtime_local);
|
|
|
|
var msg = PMA_messages.strSavedOn.replace(
|
|
|
|
'@DATE@',
|
|
|
|
PMA_formatDateTime(d)
|
|
|
|
);
|
|
|
|
$('#opts_import_local_storage').find('div.localStorage-exists').html(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-20 10:55:30 +00:00
|
|
|
* Prepares message which informs that localStorage preferences are available and can be imported or deleted
|
2016-10-22 14:57:10 +00:00
|
|
|
*/
|
2018-04-14 09:18:00 +00:00
|
|
|
function offerPrefsAutoimport () {
|
2016-10-22 14:57:10 +00:00
|
|
|
var has_config = (isStorageSupported('localStorage')) && (window.localStorage.config || false);
|
|
|
|
var $cnt = $('#prefs_autoload');
|
|
|
|
if (!$cnt.length || !has_config) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$cnt.find('a').click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var $a = $(this);
|
2018-04-14 09:18:00 +00:00
|
|
|
if ($a.attr('href') === '#no') {
|
2016-10-22 14:57:10 +00:00
|
|
|
$cnt.remove();
|
|
|
|
$.post('index.php', {
|
2017-04-20 10:55:30 +00:00
|
|
|
server: PMA_commonParams.get('server'),
|
|
|
|
prefs_autoload: 'hide'
|
|
|
|
}, null, 'html');
|
|
|
|
return;
|
2018-04-14 09:18:00 +00:00
|
|
|
} else if ($a.attr('href') === '#delete') {
|
2017-04-20 10:55:30 +00:00
|
|
|
$cnt.remove();
|
|
|
|
localStorage.clear();
|
|
|
|
$.post('index.php', {
|
|
|
|
server: PMA_commonParams.get('server'),
|
2016-10-22 14:57:10 +00:00
|
|
|
prefs_autoload: 'hide'
|
|
|
|
}, null, 'html');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$cnt.find('input[name=json]').val(window.localStorage.config);
|
|
|
|
$cnt.find('form').submit();
|
|
|
|
});
|
|
|
|
$cnt.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// END: User preferences import/export
|
|
|
|
// ------------------------------------------------------------------
|