mailbox.js 15 KB

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