custom.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. $(document).ready(function() {
  2. function getUrlGetParameter(val) {
  3. var result = "Not found",
  4. tmp = [];
  5. location.search
  6. .substr(1)
  7. .split("&")
  8. .forEach(function (item) {
  9. tmp = item.split("=");
  10. if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
  11. });
  12. return result;
  13. }
  14. function addSpinner(element) {
  15. element.append("<div class=\"spinner\"><div class=\"bounce1\"></div><div class=\"bounce2\"></div><div class=\"bounce3\"></div></div>");
  16. }
  17. function removeSpinner(element) {
  18. element.children(".spinner").remove();
  19. }
  20. function loadBox(element, callback) {
  21. $(document).on("click", element, function(e) {
  22. e.preventDefault();
  23. $.get($(this).attr('href'), function(r) {
  24. try {
  25. editBox = $.parseJSON(r);
  26. var optionsContainer = document.createElement('div');
  27. $.each(editBox['options'], function(i) {
  28. switch(this.type) {
  29. case 'text':
  30. $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
  31. break;
  32. case 'select':
  33. selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
  34. this['values'].forEach(function(item) { // TODO: jQuery-each?
  35. if(editBox['options'][0]['value'] == item) {
  36. selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
  37. } else {
  38. selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
  39. }
  40. });
  41. selectHtml += "</select>";
  42. $(optionsContainer).append(this.name + selectHtml + "<br><br>");
  43. break;
  44. default:
  45. break;
  46. }
  47. });
  48. $.fancybox({
  49. maxWidth : 800,
  50. maxHeight : 600,
  51. fitToView : true,
  52. width : '70%',
  53. height : '70%',
  54. autoSize : false,
  55. title : "<h3>" + editBox['title'] + "</h3><hr>",
  56. content : optionsContainer.innerHTML,
  57. helpers : {
  58. title: {
  59. type : 'inside',
  60. position: 'top'
  61. }
  62. },
  63. afterShow : callback
  64. });
  65. } catch(e) {
  66. console.log(e); // DBG
  67. var n = noty({
  68. layout : 'topCenter',
  69. text : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
  70. type : 'error',
  71. timeout : 5000
  72. });
  73. }
  74. });
  75. });
  76. }
  77. function getMailboxStatus() {
  78. $("#settings-mailboxes tr").each(function(item) {
  79. var thisTr = this;
  80. if(this.attributes.length > 0) {
  81. addSpinner($(this).children("td:first"));
  82. $.getJSON("ajax.php?action=getMailboxStatus&mailboxId=" + $(this).attr("data-mailbox-id"), function(r) {
  83. if(r['connected'] == true) {
  84. removeSpinner($(thisTr).children("td:first"));
  85. $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
  86. } else {
  87. removeSpinner($(thisTr).children("td:first"));
  88. $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
  89. }
  90. });
  91. }
  92. });
  93. }
  94. function newDocumentListener() {
  95. $(document).on("change", ".fancybox-inner select", function(e) {
  96. reloadDraftVars();
  97. });
  98. $(document).on("click", "#save-new-document", function(e) {
  99. e.preventDefault();
  100. data = {
  101. draft: $("#editbox-input-vorlage").val(),
  102. filename: $("#editbox-input-filename").val(),
  103. draftVars: {}
  104. }
  105. $(".editbox-draft-vars").each(function(i) {
  106. data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
  107. });
  108. $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
  109. console.log(r);
  110. });
  111. });
  112. }
  113. function reloadDraftVars() {
  114. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  115. draftVarsContainer = $("#draft-vars");
  116. if(draftVarsContainer.length > 0) {
  117. console.log(draftVarsContainer);
  118. draftVarsContainer.remove();
  119. }
  120. $(".fancybox-inner").append("<div id=\"draft-vars\"></div>")
  121. r.forEach(function(item) {
  122. $(".fancybox-inner #draft-vars").append(item + "<input id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\" type=\"text\" /><br><br>");
  123. })
  124. $(".fancybox-inner #draft-vars").append("<a href=\"#\"type=\"button\" id=\"save-new-document\" class=\"btn btn-success\">Dokument speichern</a>");
  125. });
  126. }
  127. $.fn.editable.defaults.mode = 'inline';
  128. $.fn.editableform.buttons =
  129. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  130. '<i class="fa fa-fw fa-check"></i>'+
  131. '</button>'+
  132. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  133. '<i class="fa fa-fw fa-times"></i>'+
  134. '</button>';
  135. $('.editable-element-text').editable({
  136. escape: false,
  137. success: function(response, newValue) {
  138. console.log(response); // Debug output from ajax.php
  139. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  140. },
  141. error: function (xhr, status, error) {
  142. //var err = eval("(" + xhr.responseText + ")");
  143. return xhr.statusText;
  144. },
  145. params: function(params) {
  146. params.action = 'updateMailaccounts';
  147. return params;
  148. }
  149. });
  150. $('.editable-element-select-protocol').editable({
  151. defaultValue: 'imap',
  152. source: [
  153. {value: 'imap', text: 'IMAP'},
  154. {value: 'pop3', text: 'POP3'}
  155. ],
  156. success: function(response, newValue) {
  157. console.log(response); // Debug output from ajax.php
  158. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  159. },
  160. error: function (xhr, status, error) {
  161. //var err = eval("(" + xhr.responseText + ")");
  162. return xhr.statusText;
  163. },
  164. params: function(params) {
  165. params.action = 'updateMailaccounts';
  166. return params;
  167. }
  168. });
  169. $('.editable-element-select-use-ssl').editable({
  170. defaultValue: '1',
  171. source: [
  172. {value: '1', text: 'On'},
  173. {value: '0', text: 'Off'}
  174. ],
  175. success: function(response, newValue) {
  176. console.log(response); // Debug output from ajax.php
  177. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  178. },
  179. error: function (xhr, status, error) {
  180. //var err = eval("(" + xhr.responseText + ")");
  181. return xhr.statusText;
  182. },
  183. params: function(params) {
  184. params.action = 'updateMailaccounts';
  185. return params;
  186. }
  187. });
  188. $('.editable-element-select-no-valid-cert').editable({
  189. defaultValue: '0',
  190. source: [
  191. {value: '0', text: 'On'},
  192. {value: '1', text: 'Off'}
  193. ],
  194. success: function(response, newValue) {
  195. console.log(response); // Debug output from ajax.php
  196. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  197. },
  198. error: function (xhr, status, error) {
  199. //var err = eval("(" + xhr.responseText + ")");
  200. return xhr.statusText;
  201. },
  202. params: function(params) {
  203. params.action = 'updateMailaccounts';
  204. return params;
  205. }
  206. });
  207. $('#add-mailaccount').on('click', function(e) {
  208. e.preventDefault();
  209. var uId = $(this).attr('data-uid');
  210. $.ajax({
  211. url: 'ajax.php',
  212. type: 'POST',
  213. data: {'action': 'addDefaultMailaccount', 'user-id': uId},
  214. success: function (result) {
  215. console.log(result);
  216. location.reload();
  217. }
  218. });
  219. });
  220. function fancyBoxLoader() {
  221. $.ajax({
  222. url: 'ajax.php',
  223. data: {'action': 'getMailAccountsByUid', 'uId': $('.manage-mailboxfolder').attr('data-uid')},
  224. type: 'GET',
  225. global: false,
  226. async: true,
  227. dataType: 'json',
  228. success: function(data) {
  229. result = data;
  230. $('.editable-element-select-mailaccount').editable({
  231. //defaultValue: '0',
  232. source: result,
  233. success: function(response, newValue) {
  234. console.log(response); // Debug output from ajax.php
  235. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  236. },
  237. error: function (xhr, status, error) {
  238. //var err = eval("(" + xhr.responseText + ")");
  239. return xhr.statusText;
  240. },
  241. params: function(params) {
  242. params.action = 'updateMailfolder';
  243. return params;
  244. }
  245. });
  246. }
  247. });
  248. $('.editable-element-text').editable({
  249. escape: false,
  250. success: function(response, newValue) {
  251. console.log(response); // Debug output from ajax.php
  252. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  253. },
  254. error: function (xhr, status, error) {
  255. //var err = eval("(" + xhr.responseText + ")");
  256. return xhr.statusText;
  257. },
  258. params: function(params) {
  259. params.action = 'updateMailfolder';
  260. return params;
  261. }
  262. });
  263. }
  264. $('.remove-mailaccount').on('click', function(e) {
  265. e.preventDefault();
  266. var id = $(this).attr('data-id');
  267. $.ajax({
  268. url: 'ajax.php',
  269. type: 'POST',
  270. data: {'action': 'removeMailaccount', 'id': id},
  271. success: function (result) {
  272. console.log(result);
  273. location.reload();
  274. }
  275. });
  276. });
  277. $('.remove-mailboxfolder').on('click', function(e) {
  278. e.preventDefault();
  279. var id = $(this).attr('data-id');
  280. $.ajax({
  281. url: 'ajax.php',
  282. type: 'POST',
  283. data: {'action': 'removeMailboxFolder', 'id': id},
  284. success: function (result) {
  285. console.log(result);
  286. location.reload();
  287. }
  288. });
  289. });
  290. $('#add-mailboxfolder').on('click', function(e) {
  291. e.preventDefault();
  292. var uId = $(this).attr('data-uid');
  293. var lId = $(this).attr('data-lid');
  294. $.ajax({
  295. url: 'ajax.php',
  296. type: 'POST',
  297. data: {'action': 'addDefaultMailfolder', 'user-id': uId, 'label-id': lId},
  298. success: function (result) {
  299. console.log(result);
  300. location.reload();
  301. },
  302. error: function(result) {
  303. console.log(result);
  304. }
  305. });
  306. });
  307. switch(getUrlGetParameter("action")) {
  308. case 'settings':
  309. getMailboxStatus();
  310. break;
  311. case 'manage-label':
  312. loadBox('.manage-mailboxfolder', function() {});
  313. break;
  314. case 'label':
  315. loadBox('#new-document', function() { reloadDraftVars(); });
  316. newDocumentListener();
  317. break;
  318. default:
  319. break;
  320. }
  321. });