Merge branch 'master' of ssh://repo.or.cz/srv/git/phpmyadmin/madhuracj into OpenGIS
[phpmyadmin/madhuracj.git] / setup / scripts.js
blobe39ccacfb93bf82d2432b909efa8fd50f023c7eb
1 /**
2  * Functions used in Setup configuration forms
3  */
5 // show this window in top frame
6 if (top != self) {
7     window.top.location.href = location;
10 // ------------------------------------------------------------------
11 // Messages
14 // stores hidden message ids
15 var hiddenMessages = [];
17 $(function() {
18     var hidden = hiddenMessages.length;
19     for (var i = 0; i < hidden; i++) {
20         $('#'+hiddenMessages[i]).css('display', 'none');
21     }
22     if (hidden > 0) {
23         var link = $('#show_hidden_messages');
24         link.click(function(e) {
25             e.preventDefault();
26             for (var i = 0; i < hidden; i++) {
27                 $('#'+hiddenMessages[i]).show(500);
28             }
29             $(this).remove();
30         });
31         link.html(link.html().replace('#MSG_COUNT', hidden));
32         link.css('display', '');
33     }
34 });
37 // END: Messages
38 // ------------------------------------------------------------------
40 // ------------------------------------------------------------------
41 // Form validation and field operations
44 $.extend(true, validators, {
45     // field validators
46     _field: {
47         /**
48          * hide_db field
49          *
50          * @param {boolean} isKeyUp
51          */
52         hide_db: function(isKeyUp) {
53             if (!isKeyUp && this.value != '') {
54                 var data = {};
55                 data[this.id] = this.value;
56                 ajaxValidate(this, 'Servers/1/hide_db', data);
57             }
58             return true;
59         },
60         /**
61          * TrustedProxies field
62          *
63          * @param {boolean} isKeyUp
64          */
65         TrustedProxies: function(isKeyUp) {
66             if (!isKeyUp && this.value != '') {
67                 var data = {};
68                 data[this.id] = this.value;
69                 ajaxValidate(this, 'TrustedProxies', data);
70             }
71             return true;
72         }
73     },
74     // fieldset validators
75     _fieldset: {
76         /**
77          * Validates Server fieldset
78          *
79          * @param {boolean} isKeyUp
80          */
81         Server: function(isKeyUp) {
82             if (!isKeyUp) {
83                 ajaxValidate(this, 'Server', getAllValues());
84             }
85             return true;
86         },
87         /**
88          * Validates Server_login_options fieldset
89          *
90          * @param {boolean} isKeyUp
91          */
92         Server_login_options: function(isKeyUp) {
93             return validators._fieldset.Server.apply(this, [isKeyUp]);
94         },
95         /**
96          * Validates Server_pmadb fieldset
97          *
98          * @param {boolean} isKeyUp
99          */
100         Server_pmadb: function(isKeyUp) {
101             if (isKeyUp) {
102                 return true;
103             }
105             var prefix = getIdPrefix($(this).find('input'));
106             var pmadb_active = $('#' + prefix + 'pmadb').val() != '';
107             if (pmadb_active) {
108                 ajaxValidate(this, 'Server_pmadb', getAllValues());
109             }
111             return true;
112         }
113     }
117  * Calls server-side validation procedures
119  * @param {Element} parent  input field in <fieldset> or <fieldset>
120  * @param {String}  id      validator id
121  * @param {Object}  values  values hash {element1_id: value, ...}
122  */
123 function ajaxValidate(parent, id, values)
125     parent = $(parent);
126     // ensure that parent is a fieldset
127     if (parent.attr('tagName') != 'FIELDSET') {
128         parent = parent.closest('fieldset');
129         if (parent.length == 0) {
130             return false;
131         }
132     }
134     if (parent.data('ajax') != null) {
135         parent.data('ajax').abort();
136     }
138     parent.data('ajax', $.ajax({
139         url: 'validate.php',
140         cache: false,
141         type: 'POST',
142         data: {
143             token: parent.closest('form').find('input[name=token]').val(),
144             id: id,
145             values: $.toJSON(values)
146         },
147         success: function(response) {
148             if (response == null) {
149                 return;
150             }
152             var error = {};
153             if (typeof response != 'object') {
154                 error[parent.id] = [response];
155             } else if (typeof response['error'] != 'undefined') {
156                 error[parent.id] = [response['error']];
157             } else {
158                 for (var key in response) {
159                     var value = response[key];
160                     error[key] = jQuery.isArray(value) ? value : [value];
161                 }
162             }
163             displayErrors(error);
164         },
165         complete: function() {
166             parent.removeData('ajax');
167         }
168     }));
170     return true;
174 // END: Form validation and field operations
175 // ------------------------------------------------------------------
177 // ------------------------------------------------------------------
178 // User preferences allow/disallow UI
181 $(function() {
182    $('.userprefs-allow').click(function(e) {
183        if (this != e.target) {
184            return;
185        }
186        var el = $(this).find('input');
187        if (el.attr('disabled')) {
188            return;
189        }
190        el.attr('checked', !el.attr('checked'));
191    });
195 // END: User preferences allow/disallow UI
196 // ------------------------------------------------------------------