custom.js 17 KB

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