admin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. $(document).ready(function() {
  2. // Collect values of input fields with name multi_select to js array multi_data[data-id-of-checkbox]
  3. var multi_data = [];
  4. $(document).on('change', 'input[name=multi_select]:checkbox', function() {
  5. if ($(this).is(':checked') && $(this).attr('data-form-id')) {
  6. var id = $(this).data('form-id');
  7. if (typeof multi_data[id] == "undefined") {
  8. multi_data[id] = [];
  9. }
  10. multi_data[id].push($(this).val());
  11. }
  12. else {
  13. var id = $(this).data('form-id');
  14. multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
  15. }
  16. });
  17. // Select by click on tr
  18. $(document).on('click', 'tr', function(e) {
  19. if (e.target.type == "checkbox") {
  20. e.stopPropagation();
  21. } else {
  22. var checkbox = $(this).find(':checkbox');
  23. checkbox.trigger('click');
  24. }
  25. });
  26. // Select or deselect all checkboxes with same data-form-id
  27. $(document).on('click', '#toggle_multi_select_all', function(e) {
  28. e.preventDefault();
  29. var closests_checkboxes = $("input[data-form-id=" + $(this).attr("data-form-id") + "]");
  30. closests_checkboxes.prop("checked", !closests_checkboxes.prop("checked")).change();
  31. });
  32. // Draw domain admin table
  33. $.ajax({
  34. dataType: 'json',
  35. url: '/api/v1/get/domain-admin/all',
  36. jsonp: false,
  37. error: function () {
  38. console.log('Cannot draw domain admin table');
  39. },
  40. success: function (data) {
  41. $.each(data, function (i, item) {
  42. item.action = '<div class="btn-group">' +
  43. '<a href="/edit.php?domainadmin=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  44. '<a href="/delete.php?domainadmin=' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  45. '</div>';
  46. });
  47. $('#domainadminstable').footable({
  48. "columns": [
  49. {"sorted": true,"name":"username","title":lang.username,"style":{"width":"250px"}},
  50. {"name":"selected_domains","title":lang.admin_domains,"breakpoints":"xs sm"},
  51. {"name":"tfa_active","title":"TFA", "filterable": false,"style":{"maxWidth":"80px","width":"80px"}},
  52. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  53. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  54. ],
  55. "rows": data,
  56. "empty": lang.empty,
  57. "paging": {
  58. "enabled": true,
  59. "limit": 5,
  60. "size": pagination_size
  61. },
  62. "filtering": {
  63. "enabled": true,
  64. "position": "left",
  65. "placeholder": lang.filter_table
  66. },
  67. "sorting": {
  68. "enabled": true
  69. }
  70. });
  71. }
  72. });
  73. // Draw fwd hosts table
  74. $.ajax({
  75. dataType: 'json',
  76. url: '/api/v1/get/fwdhost/all',
  77. jsonp: false,
  78. error: function () {
  79. console.log('Cannot draw forwarding hosts table');
  80. },
  81. success: function (data) {
  82. $.each(data, function (i, item) {
  83. item.action = '<div class="btn-group">' +
  84. '<a href="/delete.php?forwardinghost=' + encodeURI(item.host) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  85. '</div>';
  86. if (item.keep_spam == "yes") {
  87. item.keep_spam = lang.no;
  88. }
  89. else {
  90. item.keep_spam = lang.yes;
  91. }
  92. item.chkbox = '<input type="checkbox" data-form-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
  93. });
  94. $('#forwardinghoststable').footable({
  95. "columns": [
  96. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  97. {"name":"host","type":"text","title":lang.host,"style":{"width":"250px"}},
  98. {"name":"source","title":lang.source,"breakpoints":"xs sm"},
  99. {"name":"keep_spam","title":lang.spamfilter, "type": "text","style":{"maxWidth":"80px","width":"80px"}},
  100. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  101. ],
  102. "rows": data,
  103. "empty": lang.empty,
  104. "paging": {
  105. "enabled": true,
  106. "limit": 5,
  107. "size": pagination_size
  108. },
  109. "sorting": {
  110. "enabled": true
  111. }
  112. });
  113. }
  114. });
  115. $(document).on('click', '#delete_fwdhosts', function(e) {
  116. e.preventDefault();
  117. var id = $(this).closest("form").attr('data-id');
  118. if (typeof multi_data[id] == "undefined") return;
  119. data_array = multi_data[id];
  120. if (Object.keys(data_array).length !== 0) {
  121. $(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
  122. $("#ItemsToDelete").empty();
  123. for (var i in data_array) {
  124. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  125. }
  126. })
  127. $('#ConfirmDeleteModal').modal({
  128. backdrop: 'static',
  129. keyboard: false
  130. })
  131. .one('click', '#IsConfirmed', function(e) {
  132. $.ajax({
  133. type: "POST",
  134. dataType: "json",
  135. data: { "forwardinghost": JSON.stringify(data_array) },
  136. url: '/api/v1/delete/fwdhost',
  137. jsonp: false,
  138. complete: function (data) {
  139. location.reload();
  140. }
  141. });
  142. })
  143. .one('click', '#isCanceled', function(e) {
  144. $('#ConfirmDeleteModal').modal('hide');
  145. });;
  146. }
  147. });
  148. $("#refresh_dovecot_log").on('click', function(e) {
  149. function unix_time_format(tm) {
  150. var date = new Date(tm ? tm * 1000 : 0);
  151. return date.toLocaleString();
  152. }
  153. e.preventDefault();
  154. if (typeof ft_dovecot_logs != 'undefined') {
  155. ft_dovecot_logs.destroy();
  156. }
  157. $.ajax({
  158. dataType: 'json',
  159. url: '/api/v1/get/logs/dovecot/1000',
  160. jsonp: false,
  161. error: function () {
  162. console.log('Cannot draw dovecot log table');
  163. },
  164. success: function (data) {
  165. $.each(data, function (i, item) {
  166. var danger_class = ["emerg", "alert", "crit"];
  167. var warning_class = ["warning"];
  168. var info_class = ["notice", "info", "debug"];
  169. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  170. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  171. }
  172. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  173. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  174. }
  175. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  176. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  177. }
  178. });
  179. ft_dovecot_logs = FooTable.init("#dovecot_log", {
  180. "columns": [
  181. {"name":"time","formatter":function unix_time_format(tm) {var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  182. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  183. {"name":"message","title":lang.message},
  184. ],
  185. "rows": data,
  186. "empty": lang.empty,
  187. "paging": {
  188. "enabled": true,
  189. "limit": 5,
  190. "size": pagination_size
  191. },
  192. "filtering": {
  193. "enabled": true,
  194. "position": "left",
  195. "placeholder": lang.filter_table
  196. },
  197. "sorting": {
  198. "enabled": true
  199. }
  200. });
  201. }
  202. });
  203. });
  204. $("#refresh_postfix_log").on('click', function(e) {
  205. function unix_time_format(tm) {
  206. var date = new Date(tm ? tm * 1000 : 0);
  207. return date.toLocaleString();
  208. }
  209. e.preventDefault();
  210. if (typeof ft_postfix_logs != 'undefined') {
  211. ft_postfix_logs.destroy();
  212. }
  213. $.ajax({
  214. dataType: 'json',
  215. url: '/api/v1/get/logs/postfix/1000',
  216. jsonp: false,
  217. error: function () {
  218. console.log('Cannot draw postfix log table');
  219. },
  220. success: function (data) {
  221. $.each(data, function (i, item) {
  222. var danger_class = ["emerg", "alert", "crit"];
  223. var warning_class = ["warning"];
  224. var info_class = ["notice", "info", "debug"];
  225. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  226. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  227. }
  228. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  229. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  230. }
  231. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  232. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  233. }
  234. });
  235. ft_postfix_logs = FooTable.init("#postfix_log", {
  236. "columns": [
  237. {"name":"time","formatter":function unix_time_format(tm) { var date = new Date(tm ? tm * 1000 : 0); return date.toLocaleString();},"title":lang.time,"style":{"width":"170px"}},
  238. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  239. {"name":"message","title":lang.message},
  240. ],
  241. "rows": data,
  242. "empty": lang.empty,
  243. "paging": {
  244. "enabled": true,
  245. "limit": 5,
  246. "size": pagination_size
  247. },
  248. "filtering": {
  249. "enabled": true,
  250. "position": "left",
  251. "placeholder": lang.filter_table
  252. },
  253. "sorting": {
  254. "enabled": true
  255. }
  256. });
  257. }
  258. });
  259. });
  260. $("#refresh_dovecot_log").trigger('click');
  261. $("#refresh_postfix_log").trigger('click');
  262. });