mailbox.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 actions
  35. $(document).on('click', '#edit_selected', function(e) {
  36. e.preventDefault();
  37. var id = $(this).data('id');
  38. if (typeof multi_data[id] == "undefined") return;
  39. data_array = multi_data[id];
  40. api_url = $(this).data('api-url');
  41. api_attr = $(this).data('api-attr');
  42. if (Object.keys(data_array).length !== 0) {
  43. $.ajax({
  44. type: "POST",
  45. dataType: "json",
  46. data: { "items": JSON.stringify(data_array), "attr": JSON.stringify(api_attr), "csrf_token": csrf_token },
  47. url: '/api/v1/' + api_url,
  48. jsonp: false,
  49. complete: function (data) {
  50. // var reponse = (JSON.parse(data.responseText));
  51. // console.log(reponse.type);
  52. // console.log(reponse.msg);
  53. location.assign(window.location);
  54. }
  55. });
  56. }
  57. });
  58. // General API delete actions
  59. $(document).on('click', '#delete_selected', function(e) {
  60. e.preventDefault();
  61. var id = $(this).data('id');
  62. if (typeof multi_data[id] == "undefined" || multi_data[id] == "") return;
  63. data_array = multi_data[id];
  64. api_url = $(this).data('api-url');
  65. $(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
  66. $("#ItemsToDelete").empty();
  67. for (var i in data_array) {
  68. $("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
  69. }
  70. })
  71. $('#ConfirmDeleteModal').modal({
  72. backdrop: 'static',
  73. keyboard: false
  74. })
  75. .one('click', '#IsConfirmed', function(e) {
  76. $.ajax({
  77. type: "POST",
  78. dataType: "json",
  79. data: { "items": JSON.stringify(data_array), "csrf_token": csrf_token },
  80. url: '/api/v1/' + api_url,
  81. jsonp: false,
  82. complete: function (data) {
  83. location.assign(window.location);
  84. }
  85. });
  86. })
  87. .one('click', '#isCanceled', function(e) {
  88. $('#ConfirmDeleteModal').modal('hide');
  89. });;
  90. });
  91. });
  92. jQuery(function($){
  93. // Calculation human readable file sizes
  94. function humanFileSize(bytes) {
  95. if(Math.abs(bytes) < 1024) {
  96. return bytes + ' B';
  97. }
  98. var units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  99. var u = -1;
  100. do {
  101. bytes /= 1024;
  102. ++u;
  103. } while(Math.abs(bytes) >= 1024 && u < units.length - 1);
  104. return bytes.toFixed(1)+' '+units[u];
  105. }
  106. function unix_time_format(tm) {
  107. var date = new Date(tm ? tm * 1000 : 0);
  108. return date.toLocaleString();
  109. }
  110. function draw_domain_table() {
  111. ft_domain_table = FooTable.init('#domain_table', {
  112. "columns": [
  113. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  114. {"sorted": true,"name":"domain_name","title":lang.domain,"style":{"width":"250px"}},
  115. {"name":"aliases","title":lang.aliases,"breakpoints":"xs sm"},
  116. {"name":"mailboxes","title":lang.mailboxes},
  117. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  118. res = value.split("/");
  119. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  120. },
  121. "sortValue": function(value){
  122. res = value.split("/");
  123. return res[0];
  124. },
  125. },
  126. {"name":"max_quota_for_mbox","title":lang.mailbox_quota,"breakpoints":"xs sm"},
  127. {"name":"backupmx","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.backup_mx,"breakpoints":"xs sm"},
  128. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  129. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  130. ],
  131. "rows": $.ajax({
  132. dataType: 'json',
  133. url: '/api/v1/get/domain/all',
  134. jsonp: false,
  135. error: function (data) {
  136. console.log('Cannot draw domain table');
  137. },
  138. success: function (data) {
  139. $.each(data, function (i, item) {
  140. item.aliases = item.aliases_in_domain + " / " + item.max_num_aliases_for_domain;
  141. item.mailboxes = item.mboxes_in_domain + " / " + item.max_num_mboxes_for_domain;
  142. item.quota = item.quota_used_in_domain + "/" + item.max_quota_for_domain;
  143. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  144. item.chkbox = '<input type="checkbox" data-id="domain" name="multi_select" value="' + item.domain_name + '" />';
  145. if (role == "admin") {
  146. item.action = '<div class="btn-group">' +
  147. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  148. '<a href="/delete.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  149. '</div>';
  150. }
  151. else {
  152. item.action = '<div class="btn-group">' +
  153. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  154. '</div>';
  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_mailbox_table() {
  176. ft_mailbox_table = FooTable.init('#mailbox_table', {
  177. "columns": [
  178. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  179. {"sorted": true,"name":"username","style":{"word-break":"break-all","min-width":"120px"},"title":lang.username},
  180. {"name":"name","title":lang.fname,"style":{"word-break":"break-all","min-width":"120px"},"breakpoints":"xs sm"},
  181. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  182. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  183. res = value.split("/");
  184. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  185. },
  186. "sortValue": function(value){
  187. res = value.split("/");
  188. return res[0];
  189. },
  190. },
  191. {"name":"spam_aliases","filterable": false,"title":lang.spam_aliases,"breakpoints":"xs sm md"},
  192. {"name":"in_use","filterable": false,"type":"html","title":lang.in_use},
  193. {"name":"messages","filterable": false,"title":lang.msg_num,"breakpoints":"xs sm md"},
  194. {"name":"active","filterable": false,"title":lang.active},
  195. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","min-width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm md"}
  196. ],
  197. "empty": lang.empty,
  198. "rows": $.ajax({
  199. dataType: 'json',
  200. url: '/api/v1/get/mailbox/all',
  201. jsonp: false,
  202. error: function () {
  203. console.log('Cannot draw mailbox table');
  204. },
  205. success: function (data) {
  206. $.each(data, function (i, item) {
  207. item.quota = item.quota_used + "/" + item.quota;
  208. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  209. item.chkbox = '<input type="checkbox" data-id="mailbox" name="multi_select" value="' + item.username + '" />';
  210. if (role == "admin") {
  211. item.action = '<div class="btn-group">' +
  212. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  213. '<a href="/delete.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  214. '<a href="/index.php?duallogin=' + encodeURI(item.username) + '" class="btn btn-xs btn-success"><span class="glyphicon glyphicon-user"></span> Login</a>' +
  215. '</div>';
  216. }
  217. else {
  218. item.action = '<div class="btn-group">' +
  219. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  220. '<a href="/delete.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  221. '</div>';
  222. }
  223. item.in_use = '<div class="progress">' +
  224. '<div class="progress-bar progress-bar-' + item.percent_class + ' role="progressbar" aria-valuenow="' + item.percent_in_use + '" aria-valuemin="0" aria-valuemax="100" ' +
  225. 'style="min-width:2em;width:' + item.percent_in_use + '%">' + item.percent_in_use + '%' + '</div></div>';
  226. });
  227. }
  228. }),
  229. "paging": {
  230. "enabled": true,
  231. "limit": 5,
  232. "size": pagination_size
  233. },
  234. "filtering": {
  235. "enabled": true,
  236. "position": "left",
  237. "placeholder": lang.filter_table
  238. },
  239. "sorting": {
  240. "enabled": true
  241. }
  242. });
  243. }
  244. function draw_resource_table() {
  245. ft_resource_table = FooTable.init('#resource_table', {
  246. "columns": [
  247. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  248. {"sorted": true,"name":"description","title":lang.description,"style":{"width":"250px"}},
  249. {"name":"kind","title":lang.kind},
  250. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  251. {"name":"multiple_bookings","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.multiple_bookings,"breakpoints":"xs sm"},
  252. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  253. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  254. ],
  255. "empty": lang.empty,
  256. "rows": $.ajax({
  257. dataType: 'json',
  258. url: '/api/v1/get/resource/all',
  259. jsonp: false,
  260. error: function () {
  261. console.log('Cannot draw resource table');
  262. },
  263. success: function (data) {
  264. $.each(data, function (i, item) {
  265. item.action = '<div class="btn-group">' +
  266. '<a href="/edit.php?resource=' + encodeURI(item.name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  267. '<a href="/delete.php?resource=' + encodeURI(item.name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  268. '</div>';
  269. item.chkbox = '<input type="checkbox" data-id="resource" name="multi_select" value="' + item.name + '" />';
  270. });
  271. }
  272. }),
  273. "paging": {
  274. "enabled": true,
  275. "limit": 5,
  276. "size": pagination_size
  277. },
  278. "filtering": {
  279. "enabled": true,
  280. "position": "left",
  281. "placeholder": lang.filter_table
  282. },
  283. "sorting": {
  284. "enabled": true
  285. }
  286. });
  287. }
  288. function draw_alias_table() {
  289. ft_alias_table = FooTable.init('#alias_table', {
  290. "columns": [
  291. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  292. {"sorted": true,"name":"address","title":lang.alias,"style":{"width":"250px"}},
  293. {"name":"goto","title":lang.target_address},
  294. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  295. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  296. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  297. ],
  298. "empty": lang.empty,
  299. "rows": $.ajax({
  300. dataType: 'json',
  301. url: '/api/v1/get/alias/all',
  302. jsonp: false,
  303. error: function () {
  304. console.log('Cannot draw alias table');
  305. },
  306. success: function (data) {
  307. $.each(data, function (i, item) {
  308. item.action = '<div class="btn-group">' +
  309. '<a href="/edit.php?alias=' + encodeURI(item.address) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  310. '<a href="/delete.php?alias=' + encodeURI(item.address) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-pencil"></span> ' + lang.remove + '</a>' +
  311. '</div>';
  312. item.chkbox = '<input type="checkbox" data-id="alias" name="multi_select" value="' + item.address + '" />';
  313. if (item.is_catch_all == 1) {
  314. item.address = '<div class="label label-default">Catch-All</div> ' + item.address;
  315. }
  316. if (item.in_primary_domain !== "") {
  317. item.domain = "↳ " + item.domain + " (" + item.in_primary_domain + ")";
  318. }
  319. });
  320. }
  321. }),
  322. "paging": {
  323. "enabled": true,
  324. "limit": 5,
  325. "size": pagination_size
  326. },
  327. "filtering": {
  328. "enabled": true,
  329. "position": "left",
  330. "placeholder": lang.filter_table
  331. },
  332. "sorting": {
  333. "enabled": true
  334. }
  335. });
  336. }
  337. function draw_aliasdomain_table() {
  338. ft_aliasdomain_table = FooTable.init('#aliasdomain_table', {
  339. "columns": [
  340. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  341. {"sorted": true,"name":"alias_domain","title":lang.alias,"style":{"width":"250px"}},
  342. {"name":"target_domain","title":lang.target_domain},
  343. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  344. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  345. ],
  346. "empty": lang.empty,
  347. "rows": $.ajax({
  348. dataType: 'json',
  349. url: '/api/v1/get/alias-domain/all',
  350. jsonp: false,
  351. error: function () {
  352. console.log('Cannot draw alias domain table');
  353. },
  354. success: function (data) {
  355. $.each(data, function (i, item) {
  356. item.action = '<div class="btn-group">' +
  357. '<a href="/edit.php?aliasdomain=' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  358. '<a href="/delete.php?aliasdomain=' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  359. '</div>';
  360. item.chkbox = '<input type="checkbox" data-id="alias-domain" name="multi_select" value="' + item.alias_domain + '" />';
  361. });
  362. }
  363. }),
  364. "paging": {
  365. "enabled": true,
  366. "limit": 5,
  367. "size": pagination_size
  368. },
  369. "filtering": {
  370. "enabled": true,
  371. "position": "left",
  372. "placeholder": lang.filter_table
  373. },
  374. "sorting": {
  375. "enabled": true
  376. }
  377. });
  378. }
  379. draw_domain_table();
  380. draw_mailbox_table();
  381. draw_resource_table();
  382. draw_alias_table();
  383. draw_aliasdomain_table();
  384. });