Translated using Weblate (Norwegian Nynorsk)
[phpmyadmin.git] / setup / scripts.js
blob09f4baa5f0498328417c341beb1bf0a66d8c72d9
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 () {
16     if (window.location.protocol === 'https:') {
17         $('#no_https').remove();
18     } else {
19         $('#no_https a').click(function () {
20             var old_location = window.location;
21             window.location.href = 'https:' + old_location.href.substring(old_location.protocol.length);
22             return false;
23         });
24     }
26     var hiddenmessages = $('.hiddenmessage');
28     if (hiddenmessages.length > 0) {
29         hiddenmessages.hide();
30         var link = $('#show_hidden_messages');
31         link.click(function (e) {
32             e.preventDefault();
33             hiddenmessages.show();
34             $(this).remove();
35         });
36         link.html(link.html().replace('#MSG_COUNT', hiddenmessages.length));
37         link.show();
38     }
39 });
41 // set document width
42 $(document).ready(function () {
43     width = 0;
44     $('ul.tabs li').each(function () {
45         width += $(this).width() + 10;
46     });
47     var contentWidth = width;
48     width += 250;
49     $('body').css('min-width', width);
50     $('.tabs_contents').css('min-width', contentWidth);
51 });
54 // END: Messages
55 // ------------------------------------------------------------------
57 // ------------------------------------------------------------------
58 // Form validation and field operations
61 /**
62  * Calls server-side validation procedures
63  *
64  * @param {Element} parent  input field in <fieldset> or <fieldset>
65  * @param {String}  id      validator id
66  * @param {Object}  values  values hash {element1_id: value, ...}
67  */
68 function ajaxValidate (parent, id, values) {
69     parent = $(parent);
70     // ensure that parent is a fieldset
71     if (parent.attr('tagName') !== 'FIELDSET') {
72         parent = parent.closest('fieldset');
73         if (parent.length === 0) {
74             return false;
75         }
76     }
78     if (parent.data('ajax') !== null) {
79         parent.data('ajax').abort();
80     }
82     parent.data('ajax', $.ajax({
83         url: 'validate.php',
84         cache: false,
85         type: 'POST',
86         data: {
87             token: parent.closest('form').find('input[name=token]').val(),
88             id: id,
89             values: JSON.stringify(values)
90         },
91         success: function (response) {
92             if (response === null) {
93                 return;
94             }
96             var error = {};
97             if (typeof response !== 'object') {
98                 error[parent.id] = [response];
99             } else if (typeof response.error !== 'undefined') {
100                 error[parent.id] = [response.error];
101             } else {
102                 for (var key in response) {
103                     var value = response[key];
104                     error[key] = jQuery.isArray(value) ? value : [value];
105                 }
106             }
107             displayErrors(error);
108         },
109         complete: function () {
110             parent.removeData('ajax');
111         }
112     }));
114     return true;
118  * Automatic form submission on change.
119  */
120 $(document).on('change', '.autosubmit', function (e) {
121     e.target.form.submit();
124 $.extend(true, validators, {
125     // field validators
126     _field: {
127         /**
128          * hide_db field
129          *
130          * @param {boolean} isKeyUp
131          */
132         hide_db: function (isKeyUp) {
133             if (!isKeyUp && this.value !== '') {
134                 var data = {};
135                 data[this.id] = this.value;
136                 ajaxValidate(this, 'Servers/1/hide_db', data);
137             }
138             return true;
139         },
140         /**
141          * TrustedProxies field
142          *
143          * @param {boolean} isKeyUp
144          */
145         TrustedProxies: function (isKeyUp) {
146             if (!isKeyUp && this.value !== '') {
147                 var data = {};
148                 data[this.id] = this.value;
149                 ajaxValidate(this, 'TrustedProxies', data);
150             }
151             return true;
152         }
153     },
154     // fieldset validators
155     _fieldset: {
156         /**
157          * Validates Server fieldset
158          *
159          * @param {boolean} isKeyUp
160          */
161         Server: function (isKeyUp) {
162             if (!isKeyUp) {
163                 ajaxValidate(this, 'Server', getAllValues());
164             }
165             return true;
166         },
167         /**
168          * Validates Server_login_options fieldset
169          *
170          * @param {boolean} isKeyUp
171          */
172         Server_login_options: function (isKeyUp) {
173             return validators._fieldset.Server.apply(this, [isKeyUp]);
174         },
175         /**
176          * Validates Server_pmadb fieldset
177          *
178          * @param {boolean} isKeyUp
179          */
180         Server_pmadb: function (isKeyUp) {
181             if (isKeyUp) {
182                 return true;
183             }
185             var prefix = getIdPrefix($(this).find('input'));
186             if ($('#' + prefix + 'pmadb').val() !== '') {
187                 ajaxValidate(this, 'Server_pmadb', getAllValues());
188             }
190             return true;
191         }
192     }
196 // END: Form validation and field operations
197 // ------------------------------------------------------------------
199 // ------------------------------------------------------------------
200 // User preferences allow/disallow UI
203 $(function () {
204     $('.userprefs-allow').click(function (e) {
205         if (this !== e.target) {
206             return;
207         }
208         var el = $(this).find('input');
209         if (el.prop('disabled')) {
210             return;
211         }
212         el.prop('checked', !el.prop('checked'));
213     });
217 // END: User preferences allow/disallow UI
218 // ------------------------------------------------------------------