custom.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. $(document).ready(function() {
  2. var lastOpenedBoxData; // Stores JSON-data of last opened editBox
  3. var notifications = []; // Stores noty-Notifications
  4. var reminders = []; // Stores reminder-noty-Notifications
  5. function noty_error_retry() {
  6. notifications.push(
  7. noty({
  8. layout : 'topCenter',
  9. text : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
  10. type : 'error',
  11. timeout : 3000
  12. }));
  13. }
  14. window.noty_reminder = noty_reminder;
  15. function noty_reminder(reminderID) {
  16. var reminderID = reminderID;
  17. reminders[reminderID] = noty({
  18. layout : 'topCenter',
  19. text : 'Erinnerung:<br>',
  20. type : 'alert',
  21. buttons : [
  22. {addClass: 'btn btn-primary', text: 'OK', onClick: function(noty) {
  23. data = {
  24. reminderID: reminderID
  25. };
  26. $.getJSON("ajax.php?action=setReminderReminded", data, function(r) {
  27. if(r['status'] == "OK") {
  28. noty.close();
  29. reminders[reminderID] = undefined;
  30. } else {
  31. noty_error_retry();
  32. }
  33. });
  34. }},
  35. {addClass: 'btn btn-danger', text: 'Snooze', onClick: function(noty) {
  36. data = {
  37. reminderID: reminderID
  38. };
  39. $.getJSON("ajax.php?action=setReminderSnooze", data, function(r) {
  40. if(r['status'] == "OK") {
  41. noty.close();
  42. reminders[reminderID] = undefined;
  43. } else {
  44. noty_error_retry();
  45. }
  46. });
  47. }}
  48. ]
  49. });
  50. $("audio")[0].play()
  51. }
  52. function getUrlGetParameter(val) {
  53. var result = "Not found",
  54. tmp = [];
  55. location.search
  56. .substr(1)
  57. .split("&")
  58. .forEach(function (item) {
  59. tmp = item.split("=");
  60. if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
  61. });
  62. return result;
  63. }
  64. function handleEvents() {
  65. data = {
  66. userID: $("body").attr("data-user-id")
  67. };
  68. $.getJSON("ajax.php?action=getEvents", data, function(r) {
  69. switch (r['type']) {
  70. case 'reminder':
  71. r['reminders'].forEach(function(item) {
  72. if(reminders[item['id']] == undefined) {
  73. noty_reminder(item['id']);
  74. }
  75. });
  76. break;
  77. case 'ping':
  78. break;
  79. default:
  80. console.log("unknown event type:" + r['type']);
  81. }
  82. });
  83. }
  84. function addSpinner(element) {
  85. element.append("<div class=\"spinner\"><div class=\"bounce1\"></div><div class=\"bounce2\"></div><div class=\"bounce3\"></div></div>");
  86. }
  87. function removeSpinner(element) {
  88. element.children(".spinner").remove();
  89. }
  90. function loadBox(element, boxType, saveID, callback) {
  91. var saveButtonIDs = {
  92. saveMailFolder: {id: "save-mail-folder", text: "Mailkonto speichern"},
  93. saveDocument: {id: "save-document", text: "Dokument speichern"},
  94. saveLabel: {id: "save-label", text: "Label speichern"},
  95. saveCall: {id: "save-call", text: "Anruf speichern"}
  96. };
  97. if(boxType == "editBox") {
  98. $(document).on("click", element, function(e) {
  99. e.preventDefault();
  100. $.getJSON($(this).attr('href'), function(r) {
  101. lastOpenedBoxData = r;
  102. try {
  103. var optionsContainer = document.createElement('div');
  104. $.each(r['options'], function(i) {
  105. switch(this.type) {
  106. case 'text':
  107. $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
  108. break;
  109. case 'select':
  110. selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
  111. if(typeof this['values'] !== "undefined" && this['values']) {
  112. this['values'].forEach(function(item) {
  113. if(r['options'][0]['value'] == item) {
  114. selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
  115. } else {
  116. selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
  117. }
  118. });
  119. }
  120. selectHtml += "</select>";
  121. $(optionsContainer).append(this.name + selectHtml + "<br><br>");
  122. break;
  123. case 'datetime':
  124. nowDate = new Date();
  125. nowDateString = "" + nowDate.getDate() + "." + nowDate.getMonth() + "." + nowDate.getFullYear() + " " + nowDate.getHours() + ":" + nowDate.getMinutes();
  126. $(optionsContainer).append(this.name + "<input id=\"editbox-datetime-" + this.name + "\" class=\"datetimepicker editbox-input\" type=\"text\" vaue=\"" + nowDateString + "\"><br><br>");
  127. break;
  128. case 'textarea':
  129. $(optionsContainer).append(this.name + "<textarea id=\"editbox-textarea-" + this.name + "\" class=\"editbox-input\">" + this.value + "</textarea><br><br><br>");
  130. break;
  131. case 'checkbox':
  132. $(optionsContainer).append(this.name + "<input type=\"checkbox\" id=\"editbox-checkbox-" + this.name + "\" class=\"editbox-input\"><br><br>");
  133. break;
  134. default:
  135. console.log("Missing input-type: " + this.type);
  136. break;
  137. }
  138. });
  139. $.fancybox({
  140. maxWidth : 800,
  141. maxHeight : 600,
  142. fitToView : true,
  143. width : '70%',
  144. height : '70%',
  145. autoSize : false,
  146. title : "<h3>" + r['title'] + "</h3><hr>",
  147. content : optionsContainer.innerHTML,
  148. helpers : {
  149. title: {
  150. type : 'inside',
  151. position: 'top'
  152. }
  153. },
  154. afterShow : function() {
  155. callback();
  156. $('.fancybox-inner').append("<a href=\"#\"type=\"button\" id=\"" + saveButtonIDs[saveID]['id'] + "\" class=\"btn btn-success save-button\">" + saveButtonIDs[saveID]['text'] + "</a>");
  157. $(".datetimepicker").datetimepicker({ format: 'Y-m-d H:i', startDate: new Date() });
  158. }
  159. });
  160. } catch(e) {
  161. console.log(e); // DBG
  162. noty_error_retry();
  163. }
  164. });
  165. });
  166. }
  167. }
  168. function getMailboxStatus() {
  169. $("#settings-mailboxes tr").each(function(item) {
  170. var thisTr = this;
  171. if(this.attributes.length > 0) {
  172. if($(this).children("td:first").children().length > 0) {
  173. $(this).children("td:first").children().remove();
  174. }
  175. addSpinner($(this).children("td:first"));
  176. $.getJSON("ajax.php?action=getMailboxStatus&mailboxID=" + $(this).attr("data-mailbox-id"), function(r) {
  177. if(r['connected'] == true) {
  178. removeSpinner($(thisTr).children("td:first"));
  179. $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
  180. } else {
  181. removeSpinner($(thisTr).children("td:first"));
  182. $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
  183. }
  184. });
  185. }
  186. });
  187. }
  188. function settingsListener() {
  189. $(document).on("click", "#refresh-mailaccounts", function(e) {
  190. e.preventDefault();
  191. getMailboxStatus();
  192. });
  193. }
  194. function manageLabelListener() {
  195. $(document).on("click", "#save-mail-folder", function(e) {
  196. e.preventDefault();
  197. data = {
  198. mailboxFolderID: lastOpenedBoxData['mailboxFolderID'],
  199. account: $("#editbox-input-account").val(),
  200. folder: $("#editbox-input-folder").val(),
  201. labelID: getUrlGetParameter("labelID")
  202. };
  203. $.getJSON("ajax.php?action=saveMailFolder", data, function(r) {
  204. if(r['status'] == "OK") {
  205. location.reload();
  206. } else {
  207. console.log(r);
  208. noty_error_retry();
  209. }
  210. });
  211. });
  212. }
  213. function manageLabelsListener() {
  214. $(document).on("click", "#save-label", function(e) {
  215. e.preventDefault();
  216. data = {
  217. name: $("#editbox-input-name").val(),
  218. path: $("#editbox-input-path").val()
  219. };
  220. $.getJSON("ajax.php?action=saveNewLabel", data, function(r) {
  221. if(r['status'] == "OK") {
  222. location.reload();
  223. } else {
  224. console.log(r);
  225. noty_error_retry();
  226. }
  227. });
  228. });
  229. $(document).on("click", ".remove-label", function(e) {
  230. e.preventDefault();
  231. tRow = $(this).parent().parent();
  232. data = {
  233. labelID: $(this).attr("data-id")
  234. };
  235. $.getJSON("ajax.php?action=removeLabel", data, function(r) {
  236. if(r['status'] == "OK") {
  237. tRow.fadeOut(400, function() {
  238. tRow.remove();
  239. });
  240. } else {
  241. console.log(r);
  242. noty_error_retry();
  243. }
  244. });
  245. });
  246. }
  247. function LabelListener() {
  248. $(document).on("change", ".fancybox-inner select", function(e) {
  249. reloadDraftVars();
  250. });
  251. $(document).on("click", "#save-document", function(e) {
  252. e.preventDefault();
  253. data = {
  254. draft: $("#editbox-input-vorlage").val(),
  255. filename: $("#editbox-input-filename").val(),
  256. labelID: getUrlGetParameter("labelID"),
  257. draftVars: {}
  258. };
  259. $(".editbox-draft-vars").each(function(i) {
  260. data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
  261. });
  262. $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
  263. console.log(r);
  264. });
  265. });
  266. $(document).on("click", "#save-call", function(e) {
  267. e.preventDefault();
  268. data = {
  269. userID: $("body").attr("data-user-id"),
  270. callDate: $('#editbox-datetime-call-date').val(),
  271. callerTelNr: $('#editbox-input-caller-telnr').val(),
  272. labelID: $('#editbox-input-label-id').val(),
  273. callNotes: $('#editbox-textarea-call-notes').val(),
  274. callSetReminder: $('#editbox-checkbox-call-set-reminder').val()
  275. }
  276. $.getJSON("ajax.php?action=saveNewCall", data, function(r) {
  277. if(r['status'] == "OK") {
  278. $.fancybox.close();
  279. //$("a[href=\"#calls\"]").tab("show");
  280. } else {
  281. noty_error_retry();
  282. }
  283. });
  284. });
  285. $(document).on("click", "#refresh-documents", function(e) {
  286. e.preventDefault();
  287. $("#refresh-documents").hide();
  288. addSpinner($("#document-list th:nth-child(6)"));
  289. $.getJSON("ajax.php?action=scanDocuments", {}, function(r) {
  290. if(r['status'] == "OK") {
  291. if(r['new'] > 0) {
  292. location.reload();
  293. } else {
  294. removeSpinner($("#document-list th:nth-child(6)"));
  295. $("#refresh-documents").show();
  296. }
  297. } else {
  298. noty_error_retry();
  299. }
  300. });
  301. });
  302. $(document).on("click", ".reply-mail", function(e) {
  303. e.preventDefault();
  304. data = {
  305. mailID: $(this).attr("data-mail-id")
  306. };
  307. $.getJSON("ajax.php?action=getMailContent", data, function(r) {
  308. window.location.href = "mailto:" + r['to'] + "?subject=" + r['subject'] + "&body=" + r['body'];
  309. });
  310. });
  311. $(document).on("change", ".mail-processed", function(e) {
  312. data = {
  313. mailID: $(this).attr("id").replace("mail-processed-", ""),
  314. value: $(this).is(':checked')
  315. };
  316. $.getJSON("ajax.php?action=changeMailProcessed", data, function(r) {
  317. if(r['status'] != "OK") {
  318. noty_error_retry();
  319. }
  320. });
  321. })
  322. }
  323. function reloadDraftVars() {
  324. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  325. draftVarsContainer = $("#draft-vars");
  326. if(draftVarsContainer.length > 0) {
  327. console.log(draftVarsContainer);
  328. draftVarsContainer.remove();
  329. }
  330. $(".fancybox-inner .save-button").before("<div id=\"draft-vars\"></div>");
  331. r.forEach(function(item) {
  332. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  333. })
  334. });
  335. }
  336. $.fn.editable.defaults.mode = 'inline';
  337. $.fn.editableform.buttons =
  338. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  339. '<i class="fa fa-fw fa-check"></i>'+
  340. '</button>'+
  341. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  342. '<i class="fa fa-fw fa-times"></i>'+
  343. '</button>';
  344. $('.editable-element-text').editable({
  345. escape: false,
  346. success: function(response, newValue) {
  347. console.log(response); // Debug output from ajax.php
  348. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  349. },
  350. error: function (xhr, status, error) {
  351. //var err = eval("(" + xhr.responseText + ")");
  352. return xhr.statusText;
  353. },
  354. params: function(params) {
  355. params.action = 'updateMailaccounts';
  356. return params;
  357. }
  358. });
  359. $('.editable-element-select-protocol').editable({
  360. defaultValue: 'imap',
  361. source: [
  362. {value: 'imap', text: 'IMAP'},
  363. {value: 'pop3', text: 'POP3'}
  364. ],
  365. success: function(response, newValue) {
  366. console.log(response); // Debug output from ajax.php
  367. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  368. },
  369. error: function (xhr, status, error) {
  370. //var err = eval("(" + xhr.responseText + ")");
  371. return xhr.statusText;
  372. },
  373. params: function(params) {
  374. params.action = 'updateMailaccounts';
  375. return params;
  376. }
  377. });
  378. $('.editable-element-select-use-ssl').editable({
  379. defaultValue: '1',
  380. source: [
  381. {value: '1', text: 'On'},
  382. {value: '0', text: 'Off'}
  383. ],
  384. success: function(response, newValue) {
  385. console.log(response); // Debug output from ajax.php
  386. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  387. },
  388. error: function (xhr, status, error) {
  389. //var err = eval("(" + xhr.responseText + ")");
  390. return xhr.statusText;
  391. },
  392. params: function(params) {
  393. params.action = 'updateMailaccounts';
  394. return params;
  395. }
  396. });
  397. $('.editable-element-select-no-valid-cert').editable({
  398. defaultValue: '0',
  399. source: [
  400. {value: '0', text: 'On'},
  401. {value: '1', text: 'Off'}
  402. ],
  403. success: function(response, newValue) {
  404. console.log(response); // Debug output from ajax.php
  405. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  406. },
  407. error: function (xhr, status, error) {
  408. //var err = eval("(" + xhr.responseText + ")");
  409. return xhr.statusText;
  410. },
  411. params: function(params) {
  412. params.action = 'updateMailaccounts';
  413. return params;
  414. }
  415. });
  416. $('#add-mailaccount').on('click', function(e) {
  417. e.preventDefault();
  418. var uID = $(this).attr('data-uid');
  419. $.ajax({
  420. url: 'ajax.php',
  421. type: 'POST',
  422. data: {'action': 'addDefaultMailaccount', 'user-id': uID},
  423. success: function (result) {
  424. console.log(result);
  425. location.reload();
  426. }
  427. });
  428. });
  429. function fancyBoxLoader() {
  430. $.ajax({
  431. url: 'ajax.php',
  432. data: {'action': 'getMailAccountsByUid', 'uID': $('.manage-mailboxfolder').attr('data-uid')},
  433. type: 'GET',
  434. global: false,
  435. async: true,
  436. dataType: 'json',
  437. success: function(data) {
  438. result = data;
  439. $('.editable-element-select-mailaccount').editable({
  440. //defaultValue: '0',
  441. source: result,
  442. success: function(response, newValue) {
  443. console.log(response); // Debug output from ajax.php
  444. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  445. },
  446. error: function (xhr, status, error) {
  447. //var err = eval("(" + xhr.responseText + ")");
  448. return xhr.statusText;
  449. },
  450. params: function(params) {
  451. params.action = 'updateMailfolder';
  452. return params;
  453. }
  454. });
  455. }
  456. });
  457. $('.editable-element-text').editable({
  458. escape: false,
  459. success: function(response, newValue) {
  460. console.log(response); // Debug output from ajax.php
  461. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  462. },
  463. error: function (xhr, status, error) {
  464. //var err = eval("(" + xhr.responseText + ")");
  465. return xhr.statusText;
  466. },
  467. params: function(params) {
  468. params.action = 'updateMailfolder';
  469. return params;
  470. }
  471. });
  472. }
  473. $('.remove-mailaccount').on('click', function(e) {
  474. e.preventDefault();
  475. var id = $(this).attr('data-id');
  476. $.ajax({
  477. url: 'ajax.php',
  478. type: 'POST',
  479. data: {'action': 'removeMailaccount', 'id': id},
  480. success: function (result) {
  481. console.log(result);
  482. location.reload();
  483. }
  484. });
  485. });
  486. $('.remove-mailboxfolder').on('click', function(e) {
  487. e.preventDefault();
  488. var id = $(this).attr('data-id');
  489. $.ajax({
  490. url: 'ajax.php',
  491. type: 'POST',
  492. data: {'action': 'removeMailboxFolder', 'id': id},
  493. success: function (result) {
  494. console.log(result);
  495. location.reload();
  496. }
  497. });
  498. });
  499. $('#add-mailboxfolder').on('click', function(e) {
  500. e.preventDefault();
  501. var uID = $(this).attr('data-uid');
  502. var lID = $(this).attr('data-lid');
  503. $.ajax({
  504. url: 'ajax.php',
  505. type: 'POST',
  506. data: {'action': 'addDefaultMailfolder', 'user-id': uID, 'label-id': lID},
  507. success: function (result) {
  508. console.log(result);
  509. location.reload();
  510. },
  511. error: function(result) {
  512. console.log(result);
  513. }
  514. });
  515. });
  516. $(".datetimepicker").datetimepicker({
  517. format: 'Y-m-d H:i',
  518. startDate: new Date()
  519. });
  520. switch(getUrlGetParameter("action")) {
  521. case 'settings':
  522. getMailboxStatus();
  523. settingsListener();
  524. break;
  525. case 'manage-label':
  526. loadBox('.manage-mailboxfolder', "editBox", "saveMailFolder", function() {});
  527. manageLabelListener();
  528. break;
  529. case 'manage-labels':
  530. loadBox('#add-label', "editBox", "saveLabel", function() {});
  531. manageLabelsListener();
  532. break;
  533. case 'label':
  534. loadBox('#new-document', "editBox", "saveDocument", reloadDraftVars);
  535. loadBox('#new-call', 'editBox', 'saveCall', function() {});
  536. LabelListener();
  537. break;
  538. default:
  539. break;
  540. }
  541. handleEvents();
  542. setInterval(handleEvents, 60000);
  543. });