2 * functions used in setup script
7 // show this window in top frame
9 window.top.location.href = location;
12 // default values for fields
13 var defaultValues = {};
16 var PMA_messages = {};
21 * @param Element field
23 function getFieldType(field) {
25 var tagName = field.attr('tagName');
26 if (tagName == 'INPUT') {
27 return field.attr('type');
28 } else if (tagName == 'SELECT') {
30 } else if (tagName == 'TEXTAREA') {
39 * value must be of type:
40 * o undefined (omitted) - restore default value (form default, not PMA default)
41 * o String - if field_type is 'text'
42 * o boolean - if field_type is 'checkbox'
43 * o Array of values - if field_type is 'select'
45 * @param Element field
46 * @param String field_type see getFieldType
49 function setFieldValue(field, field_type, value) {
53 field.attr('value', (value != undefined ? value : field.attr('defaultValue')));
56 field.attr('checked', (value != undefined ? value : field.attr('defaultChecked')));
59 var options = field.attr('options');
60 var i, imax = options.length;
61 if (value == undefined) {
62 for (i = 0; i < imax; i++) {
63 options[i].selected = options[i].defaultSelected;
66 for (i = 0; i < imax; i++) {
67 options[i].selected = (value.indexOf(options[i].value) != -1);
79 * o String - if type is 'text'
80 * o boolean - if type is 'checkbox'
81 * o Array of values - if type is 'select'
83 * @param Element field
84 * @param String field_type see getFieldType
87 function getFieldValue(field, field_type) {
91 return field.attr('value');
93 return field.attr('checked');
95 var options = field.attr('options');
96 var i, imax = options.length, items = [];
97 for (i = 0; i < imax; i++) {
98 if (options[i].selected) {
99 items.push(options[i].value);
107 * Returns values for all fields in fieldsets
109 function getAllValues() {
110 var elements = $('fieldset input, fieldset select, fieldset textarea');
113 for (var i = 0; i < elements.length; i++) {
114 type = getFieldType(elements[i]);
115 value = getFieldValue(elements[i], type);
116 if (typeof value != 'undefined') {
117 // we only have single selects, fatten array
118 if (type == 'select') {
121 values[elements[i].name] = value;
128 * Checks whether field has its default value
130 * @param Element field
134 function checkFieldDefault(field, type) {
136 var field_id = field.attr('id');
137 if (typeof defaultValues[field_id] == 'undefined') {
140 var isDefault = true;
141 var currentValue = getFieldValue(field, type);
142 if (type != 'select') {
143 isDefault = currentValue == defaultValues[field_id];
145 // compare arrays, will work for our representation of select values
146 if (currentValue.length != defaultValues[field_id].length) {
150 for (var i = 0; i < currentValue.length; i++) {
151 if (currentValue[i] != defaultValues[field_id][i]) {
162 * Returns element's id prefix
163 * @param Element element
165 function getIdPrefix(element) {
166 return $(element).attr('id').replace(/[^-]+$/, '');
169 // ------------------------------------------------------------------
173 // stores hidden message ids
174 var hiddenMessages = [];
177 var hidden = hiddenMessages.length;
178 for (var i = 0; i < hidden; i++) {
179 $('#'+hiddenMessages[i]).css('display', 'none');
182 var link = $('#show_hidden_messages');
183 link.click(function(e) {
185 for (var i = 0; i < hidden; i++) {
186 $('#'+hiddenMessages[i]).show(500);
190 link.html(link.html().replace('#MSG_COUNT', hidden));
191 link.css('display', '');
197 // ------------------------------------------------------------------
199 // ------------------------------------------------------------------
200 // Form validation and field operations
203 // form validator assignments
206 // form validator list
208 // regexp: numeric value
209 _regexp_numeric: new RegExp('^[0-9]*$'),
211 * Validates positive number
213 * @param boolean isKeyUp
215 validate_positive_number: function (isKeyUp) {
216 var result = this.value != '0' && validators._regexp_numeric.test(this.value);
217 return result ? true : PMA_messages['error_nan_p'];
220 * Validates non-negative number
222 * @param boolean isKeyUp
224 validate_non_negative_number: function (isKeyUp) {
225 var result = validators._regexp_numeric.test(this.value);
226 return result ? true : PMA_messages['error_nan_nneg'];
229 * Validates port number
231 * @param boolean isKeyUp
233 validate_port_number: function(isKeyUp) {
234 var result = validators._regexp_numeric.test(this.value) && this.value != '0';
235 if (!result || this.value > 65536) {
236 result = PMA_messages['error_incorrect_port'];
245 * @param boolean isKeyUp
247 hide_db: function(isKeyUp) {
248 if (!isKeyUp && this.value != '') {
250 data[this.id] = this.value;
251 ajaxValidate(this, 'Servers/1/hide_db', data);
256 * TrustedProxies field
258 * @param boolean isKeyUp
260 TrustedProxies: function(isKeyUp) {
261 if (!isKeyUp && this.value != '') {
263 data[this.id] = this.value;
264 ajaxValidate(this, 'TrustedProxies', data);
269 // fieldset validators
272 * Validates Server fieldset
274 * @param boolean isKeyUp
276 Server: function(isKeyUp) {
278 ajaxValidate(this, 'Server', getAllValues());
283 * Validates Server_login_options fieldset
285 * @param boolean isKeyUp
287 Server_login_options: function(isKeyUp) {
288 return validators._fieldset.Server.apply(this, [isKeyUp]);
291 * Validates Server_pmadb fieldset
293 * @param boolean isKeyUp
295 Server_pmadb: function(isKeyUp) {
300 var prefix = getIdPrefix($(this).find('input'));
301 var pmadb_active = $('#' + prefix + 'pmadb').val() != '';
303 ajaxValidate(this, 'Server_pmadb', getAllValues());
312 * Calls server-side validation procedures
314 * @param Element parent input field in <fieldset> or <fieldset>
315 * @param String id validator id
316 * @param Object values values hash (element_id: value)
318 function ajaxValidate(parent, id, values) {
320 // ensure that parent is a fieldset
321 if (parent.attr('tagName') != 'FIELDSET') {
322 parent = parent.closest('fieldset');
323 if (parent.length == 0) {
328 if (parent.data('ajax') != null) {
329 parent.data('ajax').abort();
332 parent.data('ajax', $.ajax({
337 token: parent.closest('form').find('input[name=token]').val(),
339 values: $.toJSON(values)
341 success: function(response) {
342 if (response == null) {
347 if (typeof response != 'object') {
348 error[parent.id] = [response];
349 } else if (typeof response['error'] != 'undefined') {
350 error[parent.id] = [response['error']];
352 for (key in response) {
353 var value = response[key];
354 error[key] = jQuery.isArray(value) ? value : [value];
357 displayErrors(error);
359 complete: function() {
360 parent.removeData('ajax');
368 * Registers validator for given field
370 * @param String id field id
371 * @param String type validator (key in validators object)
372 * @param boolean onKeyUp whether fire on key up
373 * @param mixed params validation function parameters
375 function validateField(id, type, onKeyUp, params) {
376 if (typeof validators[type] == 'undefined') {
379 if (typeof validate[id] == 'undefined') {
382 validate[id].push([type, params, onKeyUp]);
386 * Returns valdiation functions associated with form field
388 * @param String field_id form field id
389 * @param boolean onKeyUpOnly see validateField
390 * @return Array array of [function, paramseters to be passed to function]
392 function getFieldValidators(field_id, onKeyUpOnly) {
393 // look for field bound validator
394 var name = field_id.match(/[^-]+$/)[0];
395 if (typeof validators._field[name] != 'undefined') {
396 return [[validators._field[name], null]];
399 // look for registered validators
401 if (typeof validate[field_id] != 'undefined') {
402 // validate[field_id]: array of [type, params, onKeyUp]
403 for (var i = 0, imax = validate[field_id].length; i < imax; i++) {
404 if (onKeyUpOnly && !validate[field_id][i][2]) {
407 functions.push([validators[validate[field_id][i][0]], validate[field_id][i][1]]);
415 * Displays errors for given form fields
417 * WARNING: created DOM elements must be identical with the ones made by
418 * display_input() in FormDisplay.tpl.php!
420 * @param Object error list (key: field id, value: error array)
422 function displayErrors(error_list) {
423 for (field_id in error_list) {
424 var errors = error_list[field_id];
425 var field = $('#'+field_id);
426 var isFieldset = field.attr('tagName') == 'FIELDSET';
427 var errorCnt = isFieldset
428 ? field.find('dl.errors')
429 : field.siblings('.inline_errors');
431 // remove empty errors (used to clear error list)
432 errors = $.grep(errors, function(item) {
437 // if error container doesn't exist, create it
438 if (errorCnt.length == 0) {
440 errorCnt = $('<dl class="errors" />');
441 field.find('table').before(errorCnt);
443 errorCnt = $('<dl class="inline_errors" />');
444 field.closest('td').append(errorCnt);
449 for (var i = 0, imax = errors.length; i < imax; i++) {
450 html += '<dd>' + errors[i] + '</dd>';
453 } else if (errorCnt !== null) {
454 // remove useless error container
461 * Validates fieldset and puts errors in 'errors' object
463 * @param Element field
464 * @param boolean isKeyUp
465 * @param Object errors
467 function validate_fieldset(fieldset, isKeyUp, errors) {
468 fieldset = $(fieldset);
469 if (fieldset.length && typeof validators._fieldset[fieldset.attr('id')] != 'undefined') {
470 var fieldset_errors = validators._fieldset[fieldset.attr('id')].apply(fieldset[0], [isKeyUp]);
471 for (field_id in fieldset_errors) {
472 if (typeof errors[field_id] == 'undefined') {
473 errors[field_id] = [];
475 if (typeof fieldset_errors[field_id] == 'string') {
476 fieldset_errors[field_id] = [fieldset_errors[field_id]];
478 $.merge(errors[field_id], fieldset_errors[field_id]);
484 * Validates form field and puts errors in 'errors' object
486 * @param Element field
487 * @param boolean isKeyUp
488 * @param Object errors
490 function validate_field(field, isKeyUp, errors) {
492 var field_id = field.attr('id');
493 errors[field_id] = [];
494 var functions = getFieldValidators(field_id, isKeyUp);
495 for (var i = 0; i < functions.length; i++) {
496 var result = functions[i][0].apply(field[0], [isKeyUp, functions[i][1]]);
497 if (result !== true) {
498 if (typeof result == 'string') {
501 $.merge(errors[field_id], result);
507 * Validates form field and parent fieldset
509 * @param Element field
510 * @param boolean isKeyUp
512 function validate_field_and_fieldset(field, isKeyUp) {
515 validate_field(field, isKeyUp, errors);
516 validate_fieldset(field.closest('fieldset'), isKeyUp, errors);
517 displayErrors(errors);
521 * Marks field depending on its value (system default or custom)
523 * @param Element field
525 function markField(field) {
527 var type = getFieldType(field);
528 var isDefault = checkFieldDefault(field, type);
530 // checkboxes uses parent <span> for marking
531 var fieldMarker = (type == 'checkbox') ? field.parent() : field;
532 setRestoreDefaultBtn(field, !isDefault);
533 fieldMarker[isDefault ? 'removeClass' : 'addClass']('custom');
537 * Enables or disables the "restore default value" button
539 * @param Element field
540 * @param bool display
542 function setRestoreDefaultBtn(field, display) {
543 var el = $(field).closest('td').find('.restore-default');
544 el.css('display', (el.css('display') ? '' : 'none'));
548 // register validators and mark custom values
549 var elements = $('input[id], select[id], textarea[id]');
550 $('input[id], select[id], textarea[id]').each(function(){
553 el.bind('change', function() {
554 validate_field_and_fieldset(this, false);
557 var tagName = el.attr('tagName');
558 // text fields can be validated after each change
559 if (tagName == 'INPUT' && el.attr('type') == 'text') {
560 el.keyup(function() {
561 validate_field_and_fieldset(el, true);
565 // disable textarea spellcheck
566 if (tagName == 'TEXTAREA') {
567 el.attr('spellcheck', false);
571 // check whether we've refreshed a page and browser remembered modified
573 var check_page_refresh = $('#check_page_refresh');
574 if (check_page_refresh.length == 0 || check_page_refresh.val() == '1') {
575 // run all field validators
577 for (var i = 0; i < elements.length; i++) {
578 validate_field(elements[i], false, errors);
580 // run all fieldset validators
581 $('fieldset').each(function(){
582 validate_fieldset(this, false, errors);
585 displayErrors(errors);
586 } else if (check_page_refresh) {
587 check_page_refresh.val('1');
592 // END: Form validation and field operations
593 // ------------------------------------------------------------------
595 // ------------------------------------------------------------------
602 * @param Element tab_link
604 function setTab(tab_link) {
605 var tabs_menu = $(tab_link).closest('.tabs');
607 var links = tabs_menu.find('a');
609 for (var i = 0, imax = links.length; i < imax; i++) {
611 contents = $(link.attr('href'));
612 if (links[i] == tab_link) {
613 link.addClass('active');
614 contents.css('display', 'block');
616 link.removeClass('active');
617 contents.css('display', 'none');
620 location.hash = 'tab_' + $(tab_link).attr('href').substr(1);
624 var tabs = $('.tabs');
625 var url_tab = location.hash.match(/^#tab_.+/)
626 ? $('a[href$="' + location.hash.substr(5) + '"]') : null;
628 url_tab = url_tab[0];
630 // add tabs events and activate one tab (the first one or indicated by location hash)
631 for (var i = 0, imax = tabs.length; i < imax; i++) {
632 var links = $(tabs[i]).find('a');
633 var selected_tab = links[0];
634 for (var j = 0, jmax = links.length; j < jmax; j++) {
635 $(links[j]).click(function(e) {
639 if (links[j] == url_tab) {
640 selected_tab = links[j];
643 setTab(selected_tab);
645 // tab links handling, check each 200ms
646 // (works with history in FF, further browser support here would be an overkill)
647 var prev_hash = location.hash;
648 setInterval(function() {
649 if (location.hash != prev_hash) {
650 prev_hash = location.hash;
651 var url_tab = location.hash.match(/^#tab_.+/)
652 ? $('a[href$="' + location.hash.substr(5) + '"]') : null;
662 // ------------------------------------------------------------------
664 // ------------------------------------------------------------------
665 // Form reset buttons
669 $('input[type=button]').click(function(e) {
670 var fields = $(this).closest('fieldset').find('input, select, textarea');
671 for (var i = 0, imax = fields.length; i < imax; i++) {
672 setFieldValue(fields[i], getFieldType(fields[i]));
678 // END: Form reset buttons
679 // ------------------------------------------------------------------
681 // ------------------------------------------------------------------
682 // "Restore default" and "set value" buttons
686 * Restores field's default value
688 * @param String field_id
690 function restoreField(field_id) {
691 var field = $('#'+field_id);
692 if (field.length == 0 || defaultValues[field_id] == undefined) {
695 setFieldValue(field, getFieldType(field), defaultValues[field_id]);
699 $('.restore-default, .set-value').each(function() {
701 link.css('opacity', 0.25);
702 if (!link.hasClass('restore-default')) {
703 // restore-default is handled by markField
704 link.css('display', '');
707 mouseenter: function() {$(this).css('opacity', 1);},
708 mouseleave: function() {$(this).css('opacity', 0.25);},
711 var href = $(this).attr('href').substr(1);
713 if ($(this).hasClass('restore-default')) {
715 restoreField(field_id);
717 field_id = href.match(/^[^=]+/)[0];
718 var value = href.match(/=(.+)$/)[1];
719 setFieldValue($('#'+field_id), 'text', value);
721 $('#'+field_id).trigger('change');
728 // END: "Restore default" and "set value" buttons
729 // ------------------------------------------------------------------