admin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. $(document).ready(function() {
  2. // Collect values of input fields with name multi_select with same data-form-id to js array multi_data[data-form-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 checkbox by click on parent 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 all_checkboxes = $("input[data-form-id=" + $(this).attr("data-form-id") + "]");
  30. all_checkboxes.prop("checked", !all_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. window.location.href = window.location.href;
  140. }
  141. });
  142. })
  143. .one('click', '#isCanceled', function(e) {
  144. $('#ConfirmDeleteModal').modal('hide');
  145. });;
  146. }
  147. });
  148. $(document).on('click', '#delete_dkim_key', function(e) {
  149. e.preventDefault();
  150. var dkim_domain = $(this).data('dkim-domain');
  151. var dkim_selector = $(this).data('dkim-selector');
  152. $(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
  153. $("#ItemsToDelete").empty();
  154. $("#ItemsToDelete").append("<li>" + dkim_domain + ", " + dkim_selector + "</li>");
  155. })
  156. $('#ConfirmDeleteModal').modal({
  157. backdrop: 'static',
  158. keyboard: false
  159. })
  160. .one('click', '#IsConfirmed', function(e) {
  161. $.ajax({
  162. type: "POST",
  163. dataType: "json",
  164. data: { "domains": JSON.stringify(dkim_domain) },
  165. url: '/api/v1/delete/dkim',
  166. jsonp: false,
  167. complete: function (data) {
  168. mailcow_alert_box("warn", "asd");
  169. }
  170. });
  171. })
  172. .one('click', '#isCanceled', function(e) {
  173. $('#ConfirmDeleteModal').modal('hide');
  174. });;
  175. });
  176. $("#refresh_dovecot_log").on('click', function(e) {
  177. function unix_time_format(tm) {
  178. var date = new Date(tm ? tm * 1000 : 0);
  179. return date.toLocaleString();
  180. }
  181. e.preventDefault();
  182. if (typeof ft_dovecot_logs != 'undefined') {
  183. ft_dovecot_logs.destroy();
  184. }
  185. $.ajax({
  186. dataType: 'json',
  187. url: '/api/v1/get/logs/dovecot/1000',
  188. jsonp: false,
  189. error: function () {
  190. console.log('Cannot draw dovecot log table');
  191. },
  192. success: function (data) {
  193. $.each(data, function (i, item) {
  194. var danger_class = ["emerg", "alert", "crit"];
  195. var warning_class = ["warning"];
  196. var info_class = ["notice", "info", "debug"];
  197. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  198. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  199. }
  200. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  201. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  202. }
  203. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  204. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  205. }
  206. });
  207. ft_dovecot_logs = FooTable.init("#dovecot_log", {
  208. "columns": [
  209. {"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"}},
  210. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  211. {"name":"message","title":lang.message},
  212. ],
  213. "rows": data,
  214. "empty": lang.empty,
  215. "paging": {
  216. "enabled": true,
  217. "limit": 5,
  218. "size": pagination_size
  219. },
  220. "filtering": {
  221. "enabled": true,
  222. "position": "left",
  223. "placeholder": lang.filter_table
  224. },
  225. "sorting": {
  226. "enabled": true
  227. }
  228. });
  229. }
  230. });
  231. });
  232. $("#refresh_postfix_log").on('click', function(e) {
  233. function unix_time_format(tm) {
  234. var date = new Date(tm ? tm * 1000 : 0);
  235. return date.toLocaleString();
  236. }
  237. e.preventDefault();
  238. if (typeof ft_postfix_logs != 'undefined') {
  239. ft_postfix_logs.destroy();
  240. }
  241. $.ajax({
  242. dataType: 'json',
  243. url: '/api/v1/get/logs/postfix/1000',
  244. jsonp: false,
  245. error: function () {
  246. console.log('Cannot draw postfix log table');
  247. },
  248. success: function (data) {
  249. $.each(data, function (i, item) {
  250. var danger_class = ["emerg", "alert", "crit"];
  251. var warning_class = ["warning"];
  252. var info_class = ["notice", "info", "debug"];
  253. if (jQuery.inArray(item.priority, danger_class) !== -1) {
  254. item.priority = '<span class="label label-danger">' + item.priority + '</span>';
  255. }
  256. else if (jQuery.inArray(item.priority, warning_class) !== -1) {
  257. item.priority = '<span class="label label-warning">' + item.priority + '</span>';
  258. }
  259. else if (jQuery.inArray(item.priority, info_class) !== -1) {
  260. item.priority = '<span class="label label-info">' + item.priority + '</span>';
  261. }
  262. });
  263. ft_postfix_logs = FooTable.init("#postfix_log", {
  264. "columns": [
  265. {"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"}},
  266. {"name":"priority","title":lang.priority,"style":{"width":"80px"}},
  267. {"name":"message","title":lang.message},
  268. ],
  269. "rows": data,
  270. "empty": lang.empty,
  271. "paging": {
  272. "enabled": true,
  273. "limit": 5,
  274. "size": pagination_size
  275. },
  276. "filtering": {
  277. "enabled": true,
  278. "position": "left",
  279. "placeholder": lang.filter_table
  280. },
  281. "sorting": {
  282. "enabled": true
  283. }
  284. });
  285. }
  286. });
  287. });
  288. $("#refresh_dovecot_log").trigger('click');
  289. $("#refresh_postfix_log").trigger('click');
  290. });