mailbox.js 16 KB

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