scripts.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. /**
  4. * Functions used in Setup configuration forms
  5. */
  6. /* global displayErrors, getAllValues, getIdPrefix, validators */
  7. // js/config.js
  8. // show this window in top frame
  9. if (top !== self) {
  10. window.top.location.href = location;
  11. } // ------------------------------------------------------------------
  12. // Messages
  13. //
  14. $(function () {
  15. if (window.location.protocol === 'https:') {
  16. $('#no_https').remove();
  17. } else {
  18. $('#no_https a').on('click', function () {
  19. var oldLocation = window.location;
  20. window.location.href = 'https:' + oldLocation.href.substring(oldLocation.protocol.length);
  21. return false;
  22. });
  23. }
  24. var hiddenMessages = $('.hiddenmessage');
  25. if (hiddenMessages.length > 0) {
  26. hiddenMessages.hide();
  27. var link = $('#show_hidden_messages');
  28. link.on('click', function (e) {
  29. e.preventDefault();
  30. hiddenMessages.show();
  31. $(this).remove();
  32. });
  33. link.html(link.html().replace('#MSG_COUNT', hiddenMessages.length));
  34. link.show();
  35. }
  36. }); // set document width
  37. $(function () {
  38. var width = 0;
  39. $('ul.tabs li').each(function () {
  40. width += $(this).width() + 10;
  41. });
  42. var contentWidth = width;
  43. width += 250;
  44. $('body').css('min-width', width);
  45. $('.tabs_contents').css('min-width', contentWidth);
  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. var $parent = $(parent); // 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 success(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] = Array.isArray(value) ? value : [value];
  92. }
  93. }
  94. displayErrors(error);
  95. },
  96. complete: function complete() {
  97. $parent.removeData('ajax');
  98. }
  99. }));
  100. return true;
  101. }
  102. /**
  103. * Automatic form submission on change.
  104. */
  105. $(document).on('change', '.autosubmit', 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 hide_db(isKeyUp) {
  117. // eslint-disable-line camelcase
  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 TrustedProxies(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 Server(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 Server_login_options(isKeyUp) {
  158. // eslint-disable-line camelcase
  159. return validators.fieldset.Server.apply(this, [isKeyUp]);
  160. },
  161. /**
  162. * Validates Server_pmadb fieldset
  163. *
  164. * @param {boolean} isKeyUp
  165. */
  166. Server_pmadb: function Server_pmadb(isKeyUp) {
  167. // eslint-disable-line camelcase
  168. if (isKeyUp) {
  169. return true;
  170. }
  171. var prefix = getIdPrefix($(this).find('input'));
  172. if ($('#' + prefix + 'pmadb').val() !== '') {
  173. ajaxValidate(this, 'Server_pmadb', getAllValues());
  174. }
  175. return true;
  176. }
  177. }
  178. }); //
  179. // END: Form validation and field operations
  180. // ------------------------------------------------------------------
  181. // ------------------------------------------------------------------
  182. // User preferences allow/disallow UI
  183. //
  184. $(function () {
  185. $('.userprefs-allow').on('click', function (e) {
  186. if (this !== e.target) {
  187. return;
  188. }
  189. var el = $(this).find('input');
  190. if (el.prop('disabled')) {
  191. return;
  192. }
  193. el.prop('checked', !el.prop('checked'));
  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. });