mailbox.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. $(document).ready(function() {
  2. // Auto-fill domain quota when adding new domain
  3. auto_fill_quota = function(domain) {
  4. $.get("/api/v1/get/domain/" + domain, function(data){
  5. var result = $.parseJSON(JSON.stringify(data));
  6. max_new_mailbox_quota = ( result.max_new_mailbox_quota / 1048576);
  7. if (max_new_mailbox_quota != '0') {
  8. $("#quotaBadge").html('max. ' + max_new_mailbox_quota + ' MiB');
  9. $('#addInputQuota').attr({"disabled": false, "value": "", "type": "number", "max": max_new_mailbox_quota});
  10. $('#addInputQuota').val(max_new_mailbox_quota);
  11. }
  12. else {
  13. $("#quotaBadge").html('max. ' + max_new_mailbox_quota + ' MiB');
  14. $('#addInputQuota').attr({"disabled": true, "value": "", "type": "text", "value": "n/a"});
  15. $('#addInputQuota').val(max_new_mailbox_quota);
  16. }
  17. });
  18. }
  19. $('#addSelectDomain').on('change', function() {
  20. auto_fill_quota($('#addSelectDomain').val());
  21. });
  22. auto_fill_quota($('#addSelectDomain').val());
  23. });
  24. jQuery(function($){
  25. // Calculation human readable file sizes
  26. function humanFileSize(bytes) {
  27. if(Math.abs(bytes) < 1024) {
  28. return bytes + ' B';
  29. }
  30. var units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  31. var u = -1;
  32. do {
  33. bytes /= 1024;
  34. ++u;
  35. } while(Math.abs(bytes) >= 1024 && u < units.length - 1);
  36. return bytes.toFixed(1)+' '+units[u];
  37. }
  38. function unix_time_format(tm) {
  39. var date = new Date(tm ? tm * 1000 : 0);
  40. return date.toLocaleString();
  41. }
  42. function draw_domain_table() {
  43. ft_domain_table = FooTable.init('#domain_table', {
  44. "columns": [
  45. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  46. {"sorted": true,"name":"domain_name","title":lang.domain,"style":{"width":"250px"}},
  47. {"name":"aliases","title":lang.aliases,"breakpoints":"xs sm"},
  48. {"name":"mailboxes","title":lang.mailboxes},
  49. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  50. res = value.split("/");
  51. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  52. },
  53. "sortValue": function(value){
  54. res = value.split("/");
  55. return res[0];
  56. },
  57. },
  58. {"name":"max_quota_for_mbox","title":lang.mailbox_quota,"breakpoints":"xs sm"},
  59. {"name":"backupmx","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.backup_mx,"breakpoints":"xs sm"},
  60. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  61. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  62. ],
  63. "rows": $.ajax({
  64. dataType: 'json',
  65. url: '/api/v1/get/domain/all',
  66. jsonp: false,
  67. error: function (data) {
  68. console.log('Cannot draw domain table');
  69. },
  70. success: function (data) {
  71. $.each(data, function (i, item) {
  72. item.aliases = item.aliases_in_domain + " / " + item.max_num_aliases_for_domain;
  73. item.mailboxes = item.mboxes_in_domain + " / " + item.max_num_mboxes_for_domain;
  74. item.quota = item.quota_used_in_domain + "/" + item.max_quota_for_domain;
  75. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  76. item.chkbox = '<input type="checkbox" data-id="domain" name="multi_select" value="' + item.domain_name + '" />';
  77. if (role == "admin") {
  78. item.action = '<div class="btn-group">' +
  79. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  80. '<a href="#" id="delete_selected" data-id="single-domain" data-api-url="delete/domain" data-item="' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  81. '</div>';
  82. }
  83. else {
  84. item.action = '<div class="btn-group">' +
  85. '<a href="/edit.php?domain=' + encodeURI(item.domain_name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  86. '</div>';
  87. }
  88. });
  89. }
  90. }),
  91. "empty": lang.empty,
  92. "paging": {
  93. "enabled": true,
  94. "limit": 5,
  95. "size": pagination_size
  96. },
  97. "filtering": {
  98. "enabled": true,
  99. "position": "left",
  100. "placeholder": lang.filter_table
  101. },
  102. "sorting": {
  103. "enabled": true
  104. }
  105. });
  106. }
  107. function draw_mailbox_table() {
  108. ft_mailbox_table = FooTable.init('#mailbox_table', {
  109. "columns": [
  110. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  111. {"sorted": true,"name":"username","style":{"word-break":"break-all","min-width":"120px"},"title":lang.username},
  112. {"name":"name","title":lang.fname,"style":{"word-break":"break-all","min-width":"120px"},"breakpoints":"xs sm"},
  113. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  114. {"name":"quota","style":{"whiteSpace":"nowrap"},"title":lang.domain_quota,"formatter": function(value){
  115. res = value.split("/");
  116. return humanFileSize(res[0]) + " / " + humanFileSize(res[1]);
  117. },
  118. "sortValue": function(value){
  119. res = value.split("/");
  120. return res[0];
  121. },
  122. },
  123. {"name":"spam_aliases","filterable": false,"title":lang.spam_aliases,"breakpoints":"xs sm md"},
  124. {"name":"in_use","filterable": false,"type":"html","title":lang.in_use},
  125. {"name":"messages","filterable": false,"title":lang.msg_num,"breakpoints":"xs sm md"},
  126. {"name":"active","filterable": false,"title":lang.active},
  127. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","min-width":"250px"},"type":"html","title":lang.action,"breakpoints":"xs sm md"}
  128. ],
  129. "empty": lang.empty,
  130. "rows": $.ajax({
  131. dataType: 'json',
  132. url: '/api/v1/get/mailbox/all',
  133. jsonp: false,
  134. error: function () {
  135. console.log('Cannot draw mailbox table');
  136. },
  137. success: function (data) {
  138. $.each(data, function (i, item) {
  139. item.quota = item.quota_used + "/" + item.quota;
  140. item.max_quota_for_mbox = humanFileSize(item.max_quota_for_mbox);
  141. item.chkbox = '<input type="checkbox" data-id="mailbox" name="multi_select" value="' + item.username + '" />';
  142. if (role == "admin") {
  143. item.action = '<div class="btn-group">' +
  144. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  145. '<a href="#" id="delete_selected" data-id="single-mailbox" data-api-url="delete/mailbox" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  146. '<a href="/index.php?duallogin=' + encodeURI(item.username) + '" class="btn btn-xs btn-success"><span class="glyphicon glyphicon-user"></span> Login</a>' +
  147. '</div>';
  148. }
  149. else {
  150. item.action = '<div class="btn-group">' +
  151. '<a href="/edit.php?mailbox=' + encodeURI(item.username) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  152. '<a href="#" id="delete_selected" data-id="single-mailbox" data-api-url="delete/mailbox" data-item="' + encodeURI(item.username) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  153. '</div>';
  154. }
  155. item.in_use = '<div class="progress">' +
  156. '<div class="progress-bar progress-bar-' + item.percent_class + ' role="progressbar" aria-valuenow="' + item.percent_in_use + '" aria-valuemin="0" aria-valuemax="100" ' +
  157. 'style="min-width:2em;width:' + item.percent_in_use + '%">' + item.percent_in_use + '%' + '</div></div>';
  158. });
  159. }
  160. }),
  161. "paging": {
  162. "enabled": true,
  163. "limit": 5,
  164. "size": pagination_size
  165. },
  166. "filtering": {
  167. "enabled": true,
  168. "position": "left",
  169. "placeholder": lang.filter_table
  170. },
  171. "sorting": {
  172. "enabled": true
  173. }
  174. });
  175. }
  176. function draw_resource_table() {
  177. ft_resource_table = FooTable.init('#resource_table', {
  178. "columns": [
  179. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  180. {"sorted": true,"name":"description","title":lang.description,"style":{"width":"250px"}},
  181. {"name":"kind","title":lang.kind},
  182. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  183. {"name":"multiple_bookings","filterable": false,"style":{"maxWidth":"120px","width":"120px"},"title":lang.multiple_bookings,"breakpoints":"xs sm"},
  184. {"name":"active","filterable": false,"style":{"maxWidth":"80px","width":"80px"},"title":lang.active},
  185. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  186. ],
  187. "empty": lang.empty,
  188. "rows": $.ajax({
  189. dataType: 'json',
  190. url: '/api/v1/get/resource/all',
  191. jsonp: false,
  192. error: function () {
  193. console.log('Cannot draw resource table');
  194. },
  195. success: function (data) {
  196. $.each(data, function (i, item) {
  197. item.action = '<div class="btn-group">' +
  198. '<a href="/edit.php?resource=' + encodeURI(item.name) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  199. '<a href="#" id="delete_selected" data-id="single-resource" data-api-url="delete/resource" data-item="' + encodeURI(item.name) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  200. '</div>';
  201. item.chkbox = '<input type="checkbox" data-id="resource" name="multi_select" value="' + item.name + '" />';
  202. });
  203. }
  204. }),
  205. "paging": {
  206. "enabled": true,
  207. "limit": 5,
  208. "size": pagination_size
  209. },
  210. "filtering": {
  211. "enabled": true,
  212. "position": "left",
  213. "placeholder": lang.filter_table
  214. },
  215. "sorting": {
  216. "enabled": true
  217. }
  218. });
  219. }
  220. function draw_alias_table() {
  221. ft_alias_table = FooTable.init('#alias_table', {
  222. "columns": [
  223. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  224. {"sorted": true,"name":"address","title":lang.alias,"style":{"width":"250px"}},
  225. {"name":"goto","title":lang.target_address},
  226. {"name":"domain","title":lang.domain,"breakpoints":"xs sm"},
  227. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  228. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  229. ],
  230. "empty": lang.empty,
  231. "rows": $.ajax({
  232. dataType: 'json',
  233. url: '/api/v1/get/alias/all',
  234. jsonp: false,
  235. error: function () {
  236. console.log('Cannot draw alias table');
  237. },
  238. success: function (data) {
  239. $.each(data, function (i, item) {
  240. item.action = '<div class="btn-group">' +
  241. '<a href="/edit.php?alias=' + encodeURI(item.address) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  242. '<a href="#" id="delete_selected" data-id="single-alias" data-api-url="delete/alias" data-item="' + encodeURI(item.address) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  243. '</div>';
  244. item.chkbox = '<input type="checkbox" data-id="alias" name="multi_select" value="' + item.address + '" />';
  245. if (item.is_catch_all == 1) {
  246. item.address = '<div class="label label-default">Catch-All</div> ' + item.address;
  247. }
  248. if (item.in_primary_domain !== "") {
  249. item.domain = "↳ " + item.domain + " (" + item.in_primary_domain + ")";
  250. }
  251. });
  252. }
  253. }),
  254. "paging": {
  255. "enabled": true,
  256. "limit": 5,
  257. "size": pagination_size
  258. },
  259. "filtering": {
  260. "enabled": true,
  261. "position": "left",
  262. "placeholder": lang.filter_table
  263. },
  264. "sorting": {
  265. "enabled": true
  266. }
  267. });
  268. }
  269. function draw_aliasdomain_table() {
  270. ft_aliasdomain_table = FooTable.init('#aliasdomain_table', {
  271. "columns": [
  272. {"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
  273. {"sorted": true,"name":"alias_domain","title":lang.alias,"style":{"width":"250px"}},
  274. {"name":"target_domain","title":lang.target_domain},
  275. {"name":"active","filterable": false,"style":{"maxWidth":"50px","width":"70px"},"title":lang.active},
  276. {"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
  277. ],
  278. "empty": lang.empty,
  279. "rows": $.ajax({
  280. dataType: 'json',
  281. url: '/api/v1/get/alias-domain/all',
  282. jsonp: false,
  283. error: function () {
  284. console.log('Cannot draw alias domain table');
  285. },
  286. success: function (data) {
  287. $.each(data, function (i, item) {
  288. item.action = '<div class="btn-group">' +
  289. '<a href="/edit.php?aliasdomain=' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> ' + lang.edit + '</a>' +
  290. '<a href="#" id="delete_selected" data-id="single-alias-domain" data-api-url="delete/alias-domain" data-item="' + encodeURI(item.alias_domain) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
  291. '</div>';
  292. item.chkbox = '<input type="checkbox" data-id="alias-domain" name="multi_select" value="' + item.alias_domain + '" />';
  293. });
  294. }
  295. }),
  296. "paging": {
  297. "enabled": true,
  298. "limit": 5,
  299. "size": pagination_size
  300. },
  301. "filtering": {
  302. "enabled": true,
  303. "position": "left",
  304. "placeholder": lang.filter_table
  305. },
  306. "sorting": {
  307. "enabled": true
  308. }
  309. });
  310. }
  311. draw_domain_table();
  312. draw_mailbox_table();
  313. draw_resource_table();
  314. draw_alias_table();
  315. draw_aliasdomain_table();
  316. });