user.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. $(document).ready(function() {
  2. // Show and activate password fields after box was checked
  3. // Hidden by default
  4. if ( !$("#togglePwNew").is(':checked') ) {
  5. $(".passFields").hide();
  6. }
  7. $('#togglePwNew').click(function() {
  8. $("#user_new_pass").attr("disabled", !this.checked);
  9. $("#user_new_pass2").attr("disabled", !this.checked);
  10. var $this = $(this);
  11. if ($this.is(':checked')) {
  12. $(".passFields").slideDown();
  13. } else {
  14. $(".passFields").slideUp();
  15. }
  16. });
  17. // Show generate button after time selection
  18. $('#generate_tla').hide();
  19. $('#validity').change(function(){
  20. $('#generate_tla').show();
  21. });
  22. // Init Bootstrap Switch
  23. $.fn.bootstrapSwitch.defaults.onColor = 'success';
  24. $("#tls_out").bootstrapSwitch();
  25. $("#tls_in").bootstrapSwitch();
  26. // Log modal
  27. $('#logModal').on('show.bs.modal', function(e) {
  28. var logText = $(e.relatedTarget).data('log-text');
  29. $(e.currentTarget).find('#logText').html('<pre style="background:none;font-size:11px;line-height:1.1;border:0px">' + logText + '</pre>');
  30. });
  31. // Collect values of input fields with name multi_select with same data-id to js array multi_data[data-id]
  32. var multi_data = [];
  33. $(document).on('change', 'input[name=multi_select]:checkbox', function() {
  34. if ($(this).is(':checked') && $(this).data('id')) {
  35. var id = $(this).data('id');
  36. if (typeof multi_data[id] == "undefined") {
  37. multi_data[id] = [];
  38. }
  39. multi_data[id].push($(this).val());
  40. }
  41. else {
  42. var id = $(this).data('id');
  43. multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
  44. }
  45. });
  46. // Select checkbox by click on parent tr
  47. $(document).on('click', 'tbody>tr', function(e) {
  48. if (e.target.type == "checkbox") {
  49. e.stopPropagation();
  50. } else {
  51. var checkbox = $(this).find(':checkbox');
  52. checkbox.trigger('click');
  53. }
  54. });
  55. // Select or deselect all checkboxes with same data-id
  56. $(document).on('click', '#toggle_multi_select_all', function(e) {
  57. e.preventDefault();
  58. id = $(this).data("id");
  59. multi_data[id] = [];
  60. var all_checkboxes = $("input[data-id=" + id + "]:enabled");
  61. all_checkboxes.prop("checked", !all_checkboxes.prop("checked")).change();
  62. });
  63. // General API edit actions
  64. $(document).on('click', '#edit_selected', function(e) {
  65. e.preventDefault();
  66. var id = $(this).data('id');
  67. if (typeof multi_data[id] == "undefined") return;
  68. data_array = multi_data[id];
  69. api_url = $(this).data('api-url');
  70. api_attr = $(this).data('api-attr');
  71. if (Object.keys(data_array).length !== 0) {
  72. $.ajax({
  73. type: "POST",
  74. dataType: "json",
  75. data: { "items": JSON.stringify(data_array), "attr": JSON.stringify(api_attr), "csrf_token": csrf_token },
  76. url: '/api/v1/' + api_url,
  77. jsonp: false,
  78. complete: function (data) {
  79. // var reponse = (JSON.parse(data.responseText));
  80. // console.log(reponse.type);
  81. // console.log(reponse.msg);
  82. location.assign(window.location);
  83. }
  84. });
  85. }
  86. });
  87. // General API delete actions
  88. $(document).on('click', '#delete_selected', function(e) {
  89. e.preventDefault();
  90. var id = $(this).data('id');
  91. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  92. data_array = multi_data[id];
  93. api_url = $(this).data('api-url');
  94. $(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
  95. $("#ItemsToDelete").empty();
  96. for (var i in data_array) {
  97. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  98. }
  99. })
  100. $('#ConfirmDeleteModal').modal({
  101. backdrop: 'static',
  102. keyboard: false
  103. })
  104. .one('click', '#IsConfirmed', function(e) {
  105. $.ajax({
  106. type: "POST",
  107. dataType: "json",
  108. data: { "items": JSON.stringify(data_array), "csrf_token": csrf_token },
  109. url: '/api/v1/' + api_url,
  110. jsonp: false,
  111. complete: function (data) {
  112. location.reload(true);
  113. }
  114. });
  115. })
  116. .one('click', '#isCanceled', function(e) {
  117. $('#ConfirmDeleteModal').modal('hide');
  118. });;
  119. });
  120. });
  121. jQuery(function($){
  122. // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
  123. var entityMap = {
  124. '&': '&amp;',
  125. '<': '&lt;',
  126. '>': '&gt;',
  127. '"': '&quot;',
  128. "'": '&#39;',
  129. '/': '&#x2F;',
  130. '`': '&#x60;',
  131. '=': '&#x3D;'
  132. };
  133. function escapeHtml(string) {
  134. return String(string).replace(/[&<>"'`=\/]/g, function (s) {
  135. return entityMap[s];
  136. });
  137. }
  138. function draw_sync_job_table() {
  139. ft_aliasdomain_table = FooTable.init('#sync_job_table', {
  140. "columns": [
  141. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  142. {"sorted": true,"name":"server_w_port","title":"Server"},
  143. {"name":"enc1","title":lang.encryption},
  144. {"name":"user1","title":lang.username},
  145. {"name":"exclude","title":lang.excludes},
  146. {"name":"mins_interval","title":lang.interval + " (min)"},
  147. {"name":"last_run","title":lang.last_run},
  148. {"name":"log","title":"Log"},
  149. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  150. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  151. ],
  152. "empty": lang.empty,
  153. "rows": $.ajax({
  154. dataType: 'json',
  155. url: '/api/v1/get/syncjob',
  156. jsonp: false,
  157. error: function () {
  158. console.log('Cannot draw sync job table');
  159. },
  160. success: function (data) {
  161. $.each(data, function (i, item) {
  162. item.log = '<a href="#logModal" data-toggle="modal" data-log-text="' + escapeHtml(item.returned_text) + '">Open logs</a>'
  163. item.exclude = '<code>' + item.exclude + '</code>'
  164. item.server_w_port = item.host1 + ':' + item.port1;
  165. item.action = '<div class="btn-group">' +
  166. '<a href="/edit.php?syncjob=' + item.id + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  167. '</div>';
  168. item.chkbox = '<input type="checkbox" data-id="syncjob" name="multi_select" value="' + item.id + '" />';
  169. });
  170. }
  171. }),
  172. "paging": {
  173. "enabled": true,
  174. "limit": 5,
  175. "size": pagination_size
  176. },
  177. "sorting": {
  178. "enabled": true
  179. }
  180. });
  181. }
  182. draw_sync_job_table();
  183. });