scripts.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. //set document width
  33. $(document).ready(function(){
  34. width = 0;
  35. $('ul.tabs li').each(function(){
  36. tabWidth = $(this).width() + 10;
  37. width += tabWidth;
  38. });
  39. contentWidth = width;
  40. width += 250;
  41. $('body').css('min-width', width);
  42. $('.tabs_contents').css('min-width', contentWidth);
  43. });
  44. //
  45. // END: Messages
  46. // ------------------------------------------------------------------
  47. // ------------------------------------------------------------------
  48. // Form validation and field operations
  49. //
  50. /**
  51. * Calls server-side validation procedures
  52. *
  53. * @param {Element} parent input field in <fieldset> or <fieldset>
  54. * @param {String} id validator id
  55. * @param {Object} values values hash {element1_id: value, ...}
  56. */
  57. function ajaxValidate(parent, id, values)
  58. {
  59. parent = $(parent);
  60. // ensure that parent is a fieldset
  61. if (parent.attr('tagName') != 'FIELDSET') {
  62. parent = parent.closest('fieldset');
  63. if (parent.length === 0) {
  64. return false;
  65. }
  66. }
  67. if (parent.data('ajax') !== null) {
  68. parent.data('ajax').abort();
  69. }
  70. parent.data('ajax', $.ajax({
  71. url: 'validate.php',
  72. cache: false,
  73. type: 'POST',
  74. data: {
  75. token: parent.closest('form').find('input[name=token]').val(),
  76. id: id,
  77. values: JSON.stringify(values)
  78. },
  79. success: function (response) {
  80. if (response === null) {
  81. return;
  82. }
  83. var error = {};
  84. if (typeof response != 'object') {
  85. error[parent.id] = [response];
  86. } else if (typeof response.error != 'undefined') {
  87. error[parent.id] = [response.error];
  88. } else {
  89. for (var key in response) {
  90. var value = response[key];
  91. error[key] = jQuery.isArray(value) ? value : [value];
  92. }
  93. }
  94. displayErrors(error);
  95. },
  96. complete: function () {
  97. parent.removeData('ajax');
  98. }
  99. }));
  100. return true;
  101. }
  102. /**
  103. * Automatic form submission on change.
  104. */
  105. $('.autosubmit').live('change', function (e) {
  106. e.target.form.submit();
  107. });
  108. $.extend(true, validators, {
  109. // field validators
  110. _field: {
  111. /**
  112. * hide_db field
  113. *
  114. * @param {boolean} isKeyUp
  115. */
  116. hide_db: function (isKeyUp) {
  117. if (!isKeyUp && this.value !== '') {
  118. var data = {};
  119. data[this.id] = this.value;
  120. ajaxValidate(this, 'Servers/1/hide_db', data);
  121. }
  122. return true;
  123. },
  124. /**
  125. * TrustedProxies field
  126. *
  127. * @param {boolean} isKeyUp
  128. */
  129. TrustedProxies: function (isKeyUp) {
  130. if (!isKeyUp && this.value !== '') {
  131. var data = {};
  132. data[this.id] = this.value;
  133. ajaxValidate(this, 'TrustedProxies', data);
  134. }
  135. return true;
  136. }
  137. },
  138. // fieldset validators
  139. _fieldset: {
  140. /**
  141. * Validates Server fieldset
  142. *
  143. * @param {boolean} isKeyUp
  144. */
  145. Server: function (isKeyUp) {
  146. if (!isKeyUp) {
  147. ajaxValidate(this, 'Server', getAllValues());
  148. }
  149. return true;
  150. },
  151. /**
  152. * Validates Server_login_options fieldset
  153. *
  154. * @param {boolean} isKeyUp
  155. */
  156. Server_login_options: function (isKeyUp) {
  157. return validators._fieldset.Server.apply(this, [isKeyUp]);
  158. },
  159. /**
  160. * Validates Server_pmadb fieldset
  161. *
  162. * @param {boolean} isKeyUp
  163. */
  164. Server_pmadb: function (isKeyUp) {
  165. if (isKeyUp) {
  166. return true;
  167. }
  168. var prefix = getIdPrefix($(this).find('input'));
  169. if ($('#' + prefix + 'pmadb').val() !== '') {
  170. ajaxValidate(this, 'Server_pmadb', getAllValues());
  171. }
  172. return true;
  173. }
  174. }
  175. });
  176. //
  177. // END: Form validation and field operations
  178. // ------------------------------------------------------------------
  179. // ------------------------------------------------------------------
  180. // User preferences allow/disallow UI
  181. //
  182. $(function () {
  183. $('.userprefs-allow').click(function (e) {
  184. if (this != e.target) {
  185. return;
  186. }
  187. var el = $(this).find('input');
  188. if (el.prop('disabled')) {
  189. return;
  190. }
  191. el.prop('checked', !el.prop('checked'));
  192. });
  193. });
  194. //
  195. // END: User preferences allow/disallow UI
  196. // ------------------------------------------------------------------