Lock page when changes are done in the SQL editor
[phpmyadmin.git] / setup / scripts.js
blob53c33bebbd0bd1005d808e7c6f0ce2dbba4e67e2
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * Functions used in Setup configuration forms
4  */
6 // show this window in top frame
7 if (top != self) {
8     window.top.location.href = location;
11 // ------------------------------------------------------------------
12 // Messages
15 $(function () {
17     if (window.location.protocol === 'https:') {
18         $('#no_https').remove();
19     } else {
20         $('#no_https a').click(function () {
21             var old_location = window.location;
22             window.location.href = 'https:' + old_location.href.substring(old_location.protocol.length);
23             return false;
24         });
25     }
27     var hiddenmessages = $('.hiddenmessage');
29     if (hiddenmessages.length > 0) {
30         hiddenmessages.hide();
31         var link = $('#show_hidden_messages');
32         link.click(function (e) {
33             e.preventDefault();
34             hiddenmessages.show();
35             $(this).remove();
36         });
37         link.html(link.html().replace('#MSG_COUNT', hiddenmessages.length));
38         link.css('display', '');
39     }
40 });
42 //set document width
43 $(document).ready(function(){
44     width = 0;
45     $('ul.tabs li').each(function(){
46         width += $(this).width() + 10;
47     });
48     var contentWidth = width;
49     width += 250;
50     $('body').css('min-width', width);
51     $('.tabs_contents').css('min-width', contentWidth);
52 });
55 // END: Messages
56 // ------------------------------------------------------------------
58 // ------------------------------------------------------------------
59 // Form validation and field operations
62 /**
63  * Calls server-side validation procedures
64  *
65  * @param {Element} parent  input field in <fieldset> or <fieldset>
66  * @param {String}  id      validator id
67  * @param {Object}  values  values hash {element1_id: value, ...}
68  */
69 function ajaxValidate(parent, id, values)
71     parent = $(parent);
72     // ensure that parent is a fieldset
73     if (parent.attr('tagName') != 'FIELDSET') {
74         parent = parent.closest('fieldset');
75         if (parent.length === 0) {
76             return false;
77         }
78     }
80     if (parent.data('ajax') !== null) {
81         parent.data('ajax').abort();
82     }
84     parent.data('ajax', $.ajax({
85         url: 'validate.php',
86         cache: false,
87         type: 'POST',
88         data: {
89             token: parent.closest('form').find('input[name=token]').val(),
90             id: id,
91             values: JSON.stringify(values)
92         },
93         success: function (response) {
94             if (response === null) {
95                 return;
96             }
98             var error = {};
99             if (typeof response != 'object') {
100                 error[parent.id] = [response];
101             } else if (typeof response.error != 'undefined') {
102                 error[parent.id] = [response.error];
103             } else {
104                 for (var key in response) {
105                     var value = response[key];
106                     error[key] = jQuery.isArray(value) ? value : [value];
107                 }
108             }
109             displayErrors(error);
110         },
111         complete: function () {
112             parent.removeData('ajax');
113         }
114     }));
116     return true;
120  * Automatic form submission on change.
121  */
122 $(document).on('change', '.autosubmit', function (e) {
123     e.target.form.submit();
126 $.extend(true, validators, {
127     // field validators
128     _field: {
129         /**
130          * hide_db field
131          *
132          * @param {boolean} isKeyUp
133          */
134         hide_db: function (isKeyUp) {
135             if (!isKeyUp && this.value !== '') {
136                 var data = {};
137                 data[this.id] = this.value;
138                 ajaxValidate(this, 'Servers/1/hide_db', data);
139             }
140             return true;
141         },
142         /**
143          * TrustedProxies field
144          *
145          * @param {boolean} isKeyUp
146          */
147         TrustedProxies: function (isKeyUp) {
148             if (!isKeyUp && this.value !== '') {
149                 var data = {};
150                 data[this.id] = this.value;
151                 ajaxValidate(this, 'TrustedProxies', data);
152             }
153             return true;
154         }
155     },
156     // fieldset validators
157     _fieldset: {
158         /**
159          * Validates Server fieldset
160          *
161          * @param {boolean} isKeyUp
162          */
163         Server: function (isKeyUp) {
164             if (!isKeyUp) {
165                 ajaxValidate(this, 'Server', getAllValues());
166             }
167             return true;
168         },
169         /**
170          * Validates Server_login_options fieldset
171          *
172          * @param {boolean} isKeyUp
173          */
174         Server_login_options: function (isKeyUp) {
175             return validators._fieldset.Server.apply(this, [isKeyUp]);
176         },
177         /**
178          * Validates Server_pmadb fieldset
179          *
180          * @param {boolean} isKeyUp
181          */
182         Server_pmadb: function (isKeyUp) {
183             if (isKeyUp) {
184                 return true;
185             }
187             var prefix = getIdPrefix($(this).find('input'));
188             if ($('#' + prefix + 'pmadb').val() !== '') {
189                 ajaxValidate(this, 'Server_pmadb', getAllValues());
190             }
192             return true;
193         }
194     }
198 // END: Form validation and field operations
199 // ------------------------------------------------------------------
201 // ------------------------------------------------------------------
202 // User preferences allow/disallow UI
205 $(function () {
206     $('.userprefs-allow').click(function (e) {
207         if (this != e.target) {
208             return;
209         }
210         var el = $(this).find('input');
211         if (el.prop('disabled')) {
212             return;
213         }
214         el.prop('checked', !el.prop('checked'));
215     });
219 // END: User preferences allow/disallow UI
220 // ------------------------------------------------------------------