admin.js 9.9 KB

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