scripts.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * Functions used in Setup configuration forms
  4. */
  5. // show this window in top frame
  6. if (top != self) {
  7. window.top.location.href = location;
  8. }
  9. // ------------------------------------------------------------------
  10. // Messages
  11. //
  12. // stores hidden message ids
  13. var hiddenMessages = [];
  14. $(function() {
  15. var hidden = hiddenMessages.length;
  16. for (var i = 0; i < hidden; i++) {
  17. $('#'+hiddenMessages[i]).css('display', 'none');
  18. }
  19. if (hidden > 0) {
  20. var link = $('#show_hidden_messages');
  21. link.click(function(e) {
  22. e.preventDefault();
  23. for (var i = 0; i < hidden; i++) {
  24. $('#'+hiddenMessages[i]).show(500);
  25. }
  26. $(this).remove();
  27. });
  28. link.html(link.html().replace('#MSG_COUNT', hidden));
  29. link.css('display', '');
  30. }
  31. });
  32. //
  33. // END: Messages
  34. // ------------------------------------------------------------------
  35. // ------------------------------------------------------------------
  36. // Form validation and field operations
  37. //
  38. /**
  39. * Automatic form submission on change.
  40. */
  41. $('.autosubmit').live('change', function(e) {
  42. e.target.form.submit();
  43. });
  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. }
  104. var prefix = getIdPrefix($(this).find('input'));
  105. var pmadb_active = $('#' + prefix + 'pmadb').val() != '';
  106. if (pmadb_active) {
  107. ajaxValidate(this, 'Server_pmadb', getAllValues());
  108. }
  109. return true;
  110. }
  111. }
  112. });
  113. /**
  114. * Calls server-side validation procedures
  115. *
  116. * @param {Element} parent input field in <fieldset> or <fieldset>
  117. * @param {String} id validator id
  118. * @param {Object} values values hash {element1_id: value, ...}
  119. */
  120. function ajaxValidate(parent, id, values)
  121. {
  122. parent = $(parent);
  123. // ensure that parent is a fieldset
  124. if (parent.attr('tagName') != 'FIELDSET') {
  125. parent = parent.closest('fieldset');
  126. if (parent.length == 0) {
  127. return false;
  128. }
  129. }
  130. if (parent.data('ajax') != null) {
  131. parent.data('ajax').abort();
  132. }
  133. parent.data('ajax', $.ajax({
  134. url: 'validate.php',
  135. cache: false,
  136. type: 'POST',
  137. data: {
  138. token: parent.closest('form').find('input[name=token]').val(),
  139. id: id,
  140. values: $.toJSON(values)
  141. },
  142. success: function(response) {
  143. if (response == null) {
  144. return;
  145. }
  146. var error = {};
  147. if (typeof response != 'object') {
  148. error[parent.id] = [response];
  149. } else if (typeof response['error'] != 'undefined') {
  150. error[parent.id] = [response['error']];
  151. } else {
  152. for (var key in response) {
  153. var value = response[key];
  154. error[key] = jQuery.isArray(value) ? value : [value];
  155. }
  156. }
  157. displayErrors(error);
  158. },
  159. complete: function() {
  160. parent.removeData('ajax');
  161. }
  162. }));
  163. return true;
  164. }
  165. //
  166. // END: Form validation and field operations
  167. // ------------------------------------------------------------------
  168. // ------------------------------------------------------------------
  169. // User preferences allow/disallow UI
  170. //
  171. $(function() {
  172. $('.userprefs-allow').click(function(e) {
  173. if (this != e.target) {
  174. return;
  175. }
  176. var el = $(this).find('input');
  177. if (el.prop('disabled')) {
  178. return;
  179. }
  180. el.prop('checked', !el.prop('checked'));
  181. });
  182. });
  183. //
  184. // END: User preferences allow/disallow UI
  185. // ------------------------------------------------------------------