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) },
  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) },
  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. ],
  130. "rows": $.ajax({
  131. dataType: 'json',
  132. url: '/api/v1/get/domain/all',
  133. jsonp: false,
  134. error: function (data) {
  135. console.log('Cannot draw domain table');
  136. },
  137. success: function (data) {
  138. $.each(data, function (i, item) {
  139. item.aliases = item.aliases_in_domain + " / " + item.max_num_aliases_for_domain;
  140. item.mailboxes = item.mboxes_in_domain + " / " + item.max_num_mboxes_for_domain;
  141. item.quota = item.quota_used_in_domain + "/" + item.max_quota_for_domain;
  142. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  143. item.chkbox = '<input type="checkbox" data-id="domain" name="multi_select" value="' + item.domain_name + '" />';
  144. if (role == "admin") {
  145. item.action = '<div class="btn-group">' +
  146. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  147. '<a href="/delete.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  148. '</div>';
  149. }
  150. else {
  151. item.action = '<div class="btn-group">' +
  152. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  153. '</div>';
  154. }
  155. });
  156. }
  157. }),
  158. "empty": lang.empty,
  159. "paging": {
  160. "enabled": true,
  161. "limit": 5,
  162. "size": pagination_size
  163. },
  164. "filtering": {
  165. "enabled": true,
  166. "position": "left",
  167. "placeholder": lang.filter_table
  168. },
  169. "sorting": {
  170. "enabled": true
  171. }
  172. });
  173. }
  174. function draw_mailbox_table() {
  175. ft_mailbox_table = FooTable.init('#mailbox_table', {
  176. "columns": [
  177. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  178. {"sorted": true,"name":"username","style":{"word-break":"break-all","min-width":"120px"},"title":lang.username},
  179. {"name":"name","title":lang.fname,"style":{"word-break":"break-all","min-width":"120px"},"breakpoints":"xs sm"},
  180. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  181. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  182. res = value.split("/");
  183. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  184. },
  185. "sortValue": function(value){
  186. res = value.split("/");
  187. return res[0];
  188. },
  189. },
  190. {"name":"spam_aliases","filterable": false,"title":lang.spam_aliases,"breakpoints":"xs sm md"},
  191. {"name":"in_use","filterable": false,"type":"html","title":lang.in_use},
  192. {"name":"messages","filterable": false,"title":lang.msg_num,"breakpoints":"xs sm md"},
  193. {"name":"active","filterable": false,"title":lang.active},
  194. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","min-width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm md"}
  195. ],
  196. "empty": lang.empty,
  197. "rows": $.ajax({
  198. dataType: 'json',
  199. url: '/api/v1/get/mailbox/all',
  200. jsonp: false,
  201. error: function () {
  202. console.log('Cannot draw mailbox table');
  203. },
  204. success: function (data) {
  205. $.each(data, function (i, item) {
  206. item.quota = item.quota_used + "/" + item.quota;
  207. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  208. item.chkbox = '<input type="checkbox" data-id="mailbox" name="multi_select" value="' + item.username + '" />';
  209. if (role == "admin") {
  210. item.action = '<div class="btn-group">' +
  211. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  212. '<a href="/delete.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  213. '<a href="/index.php?duallogin=' + encodeURI(item.username) + '" class="btn btn-xs btn-success"><span class="glyphicon glyphicon-user"></span> Login</a>' +
  214. '</div>';
  215. }
  216. else {
  217. item.action = '<div class="btn-group">' +
  218. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  219. '<a href="/delete.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  220. '</div>';
  221. }
  222. item.in_use = '<div class="progress">' +
  223. '<div class="progress-bar progress-bar-' + item.percent_class + ' role="progressbar" aria-valuenow="' + item.percent_in_use + '" aria-valuemin="0" aria-valuemax="100" ' +
  224. 'style="min-width:2em;width:' + item.percent_in_use + '%">' + item.percent_in_use + '%' + '</div></div>';
  225. });
  226. }
  227. }),
  228. "paging": {
  229. "enabled": true,
  230. "limit": 5,
  231. "size": pagination_size
  232. },
  233. "filtering": {
  234. "enabled": true,
  235. "position": "left",
  236. "placeholder": lang.filter_table
  237. },
  238. "sorting": {
  239. "enabled": true
  240. }
  241. });
  242. }
  243. function draw_resource_table() {
  244. ft_resource_table = FooTable.init('#resource_table', {
  245. "columns": [
  246. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  247. {"sorted": true,"name":"description","title":lang.description,"style":{"width":"250px"}},
  248. {"name":"kind","title":lang.kind},
  249. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  250. {"name":"multiple_bookings","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.multiple_bookings,"breakpoints":"xs sm"},
  251. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  252. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  253. ],
  254. "empty": lang.empty,
  255. "rows": $.ajax({
  256. dataType: 'json',
  257. url: '/api/v1/get/resource/all',
  258. jsonp: false,
  259. error: function () {
  260. console.log('Cannot draw resource table');
  261. },
  262. success: function (data) {
  263. $.each(data, function (i, item) {
  264. item.action = '<div class="btn-group">' +
  265. '<a href="/edit.php?resource=' + encodeURI(item.name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  266. '<a href="/delete.php?resource=' + encodeURI(item.name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  267. '</div>';
  268. item.chkbox = '<input type="checkbox" data-id="resource" name="multi_select" value="' + item.name + '" />';
  269. });
  270. }
  271. }),
  272. "paging": {
  273. "enabled": true,
  274. "limit": 5,
  275. "size": pagination_size
  276. },
  277. "filtering": {
  278. "enabled": true,
  279. "position": "left",
  280. "placeholder": lang.filter_table
  281. },
  282. "sorting": {
  283. "enabled": true
  284. }
  285. });
  286. }
  287. function draw_alias_table() {
  288. ft_alias_table = FooTable.init('#alias_table', {
  289. "columns": [
  290. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  291. {"sorted": true,"name":"address","title":lang.alias,"style":{"width":"250px"}},
  292. {"name":"goto","title":lang.target_address},
  293. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  294. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  295. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  296. ],
  297. "empty": lang.empty,
  298. "rows": $.ajax({
  299. dataType: 'json',
  300. url: '/api/v1/get/alias/all',
  301. jsonp: false,
  302. error: function () {
  303. console.log('Cannot draw alias table');
  304. },
  305. success: function (data) {
  306. $.each(data, function (i, item) {
  307. item.action = '<div class="btn-group">' +
  308. '<a href="/edit.php?alias=' + encodeURI(item.address) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  309. '<a href="/delete.php?alias=' + encodeURI(item.address) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-pencil"></span> ' + lang.remove + '</a>' +
  310. '</div>';
  311. item.chkbox = '<input type="checkbox" data-id="alias" name="multi_select" value="' + item.address + '" />';
  312. if (item.is_catch_all == 1) {
  313. item.address = '<div class="label label-default">Catch-All</div> ' + item.address;
  314. }
  315. if (item.in_primary_domain !== "") {
  316. item.domain = "↳ " + item.domain + " (" + item.in_primary_domain + ")";
  317. }
  318. });
  319. }
  320. }),
  321. "paging": {
  322. "enabled": true,
  323. "limit": 5,
  324. "size": pagination_size
  325. },
  326. "filtering": {
  327. "enabled": true,
  328. "position": "left",
  329. "placeholder": lang.filter_table
  330. },
  331. "sorting": {
  332. "enabled": true
  333. }
  334. });
  335. }
  336. function draw_aliasdomain_table() {
  337. ft_aliasdomain_table = FooTable.init('#aliasdomain_table', {
  338. "columns": [
  339. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  340. {"sorted": true,"name":"alias_domain","title":lang.alias,"style":{"width":"250px"}},
  341. {"name":"target_domain","title":lang.target_domain},
  342. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  343. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  344. ],
  345. "empty": lang.empty,
  346. "rows": $.ajax({
  347. dataType: 'json',
  348. url: '/api/v1/get/alias-domain/all',
  349. jsonp: false,
  350. error: function () {
  351. console.log('Cannot draw alias domain table');
  352. },
  353. success: function (data) {
  354. $.each(data, function (i, item) {
  355. item.action = '<div class="btn-group">' +
  356. '<a href="/edit.php?aliasdomain=' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  357. '<a href="/delete.php?aliasdomain=' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  358. '</div>';
  359. item.chkbox = '<input type="checkbox" data-id="alias-domain" name="multi_select" value="' + item.alias_domain + '" />';
  360. });
  361. }
  362. }),
  363. "paging": {
  364. "enabled": true,
  365. "limit": 5,
  366. "size": pagination_size
  367. },
  368. "filtering": {
  369. "enabled": true,
  370. "position": "left",
  371. "placeholder": lang.filter_table
  372. },
  373. "sorting": {
  374. "enabled": true
  375. }
  376. });
  377. }
  378. draw_domain_table();
  379. draw_mailbox_table();
  380. draw_resource_table();
  381. draw_alias_table();
  382. draw_aliasdomain_table();
  383. });