scripts.js 5.9 KB

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