custom.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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, subject) {
  16. var reminderID = reminderID;
  17. reminders[reminderID] = noty({
  18. layout : 'topCenter',
  19. text : 'Erinnerung:<br>' + subject,
  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'], item['subject']);
  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. var option = this;
  106. switch(this.type) {
  107. case 'text':
  108. $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
  109. break;
  110. case 'select':
  111. selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
  112. if(typeof this['values'] !== "undefined" && this['values']) {
  113. this['values'].forEach(function(item) {
  114. if(option['value'] == item) {
  115. selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
  116. } else {
  117. selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
  118. }
  119. });
  120. }
  121. selectHtml += "</select>";
  122. $(optionsContainer).append(this.name + selectHtml + "<br><br>");
  123. break;
  124. case 'datetime':
  125. nowDate = new Date();
  126. nowDateString = "" + nowDate.getDate() + "." + nowDate.getMonth() + "." + nowDate.getFullYear() + " " + nowDate.getHours() + ":" + nowDate.getMinutes();
  127. $(optionsContainer).append(this.name + "<input id=\"editbox-datetime-" + this.name + "\" class=\"datetimepicker editbox-input\" type=\"text\" vaue=\"" + nowDateString + "\"><br><br>");
  128. break;
  129. case 'textarea':
  130. $(optionsContainer).append(this.name + "<textarea id=\"editbox-textarea-" + this.name + "\" class=\"editbox-input\">" + this.value + "</textarea><br><br><br>");
  131. break;
  132. case 'checkbox':
  133. $(optionsContainer).append(this.name + "<input type=\"checkbox\" id=\"editbox-checkbox-" + this.name + "\" class=\"editbox-input\"><br><br>");
  134. break;
  135. default:
  136. console.log("Missing input-type: " + this.type);
  137. break;
  138. }
  139. });
  140. $.fancybox({
  141. maxWidth : 800,
  142. maxHeight : 600,
  143. fitToView : true,
  144. width : '70%',
  145. height : '70%',
  146. autoSize : false,
  147. title : "<h3>" + r['title'] + "</h3><hr>",
  148. content : optionsContainer.innerHTML,
  149. helpers : {
  150. title: {
  151. type : 'inside',
  152. position: 'top'
  153. }
  154. },
  155. afterShow : function() {
  156. callback();
  157. $('.fancybox-inner').append("<a href=\"#\"type=\"button\" id=\"" + saveButtonIDs[saveID]['id'] + "\" class=\"btn btn-success save-button\">" + saveButtonIDs[saveID]['text'] + "</a>");
  158. $(".datetimepicker").datetimepicker({ format: 'Y-m-d H:i', startDate: new Date() });
  159. }
  160. });
  161. } catch(e) {
  162. console.log(e); // DBG
  163. noty_error_retry();
  164. }
  165. });
  166. });
  167. }
  168. }
  169. function getMailboxStatus() {
  170. $("#settings-mailboxes tr").each(function(item) {
  171. var thisTr = this;
  172. if(this.attributes.length > 0) {
  173. if($(this).children("td:first").children().length > 0) {
  174. $(this).children("td:first").children().remove();
  175. }
  176. addSpinner($(this).children("td:first"));
  177. $.getJSON("ajax.php?action=getMailboxStatus&mailboxID=" + $(this).attr("data-mailbox-id"), function(r) {
  178. if(r['connected'] == true) {
  179. removeSpinner($(thisTr).children("td:first"));
  180. $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
  181. } else {
  182. removeSpinner($(thisTr).children("td:first"));
  183. $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
  184. }
  185. });
  186. }
  187. });
  188. }
  189. function settingsListener() {
  190. $(document).on("click", "#refresh-mailaccounts", function(e) {
  191. e.preventDefault();
  192. getMailboxStatus();
  193. });
  194. }
  195. function manageLabelListener() {
  196. $(document).on("click", "#save-mail-folder", function(e) {
  197. e.preventDefault();
  198. data = {
  199. mailboxFolderID: lastOpenedBoxData['mailboxFolderID'],
  200. account: $("#editbox-input-account").val(),
  201. folder: $("#editbox-input-folder").val(),
  202. labelID: getUrlGetParameter("labelID")
  203. };
  204. $.getJSON("ajax.php?action=saveMailFolder", data, function(r) {
  205. if(r['status'] == "OK") {
  206. location.reload();
  207. } else {
  208. noty_error_retry();
  209. }
  210. });
  211. });
  212. $(document).on("change", "#editbox-input-account", function(e) {
  213. e.preventDefault();
  214. data = {
  215. account: $("#editbox-input-account").val()
  216. };
  217. $.getJSON("ajax.php?action=getMailaccountFoldersByUsername", data, function(r) {
  218. $("#editbox-input-folder").empty();
  219. $.each(r, function(val, key) {
  220. $("#editbox-input-folder").append($("<option></option>").attr("value", key).text(key));
  221. });
  222. });
  223. });
  224. }
  225. function manageLabelsListener() {
  226. $(document).on("click", "#save-label", function(e) {
  227. e.preventDefault();
  228. data = {
  229. name: $("#editbox-input-name").val(),
  230. path: $("#editbox-input-path").val()
  231. };
  232. $.getJSON("ajax.php?action=saveNewLabel", data, function(r) {
  233. if(r['status'] == "OK") {
  234. location.reload();
  235. } else {
  236. console.log(r);
  237. noty_error_retry();
  238. }
  239. });
  240. });
  241. $(document).on("click", ".remove-label", function(e) {
  242. e.preventDefault();
  243. tRow = $(this).parent().parent();
  244. data = {
  245. labelID: $(this).attr("data-id")
  246. };
  247. $.getJSON("ajax.php?action=removeLabel", data, function(r) {
  248. if(r['status'] == "OK") {
  249. tRow.fadeOut(400, function() {
  250. tRow.remove();
  251. });
  252. } else {
  253. console.log(r);
  254. noty_error_retry();
  255. }
  256. });
  257. });
  258. }
  259. function LabelListener() {
  260. $(document).on("change", ".fancybox-inner select", function(e) {
  261. reloadDraftVars();
  262. });
  263. $(document).on("click", "#save-document", function(e) {
  264. e.preventDefault();
  265. data = {
  266. draft: $("#editbox-input-vorlage").val(),
  267. filename: $("#editbox-input-filename").val(),
  268. labelID: getUrlGetParameter("labelID"),
  269. draftVars: {}
  270. };
  271. $(".editbox-draft-vars").each(function(i) {
  272. data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
  273. });
  274. $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
  275. console.log(r);
  276. });
  277. });
  278. $(document).on("click", "#save-call", function(e) {
  279. e.preventDefault();
  280. data = {
  281. userID: $("body").attr("data-user-id"),
  282. callDate: $('#editbox-datetime-call-date').val(),
  283. callerTelNr: $('#editbox-input-caller-telnr').val(),
  284. labelID: $('#editbox-input-label-id').val(),
  285. callNotes: $('#editbox-textarea-call-notes').val(),
  286. callSetReminder: $('#editbox-checkbox-call-set-reminder').val()
  287. }
  288. $.getJSON("ajax.php?action=saveNewCall", data, function(r) {
  289. if(r['status'] == "OK") {
  290. $.fancybox.close();
  291. //$("a[href=\"#calls\"]").tab("show");
  292. } else {
  293. noty_error_retry();
  294. }
  295. });
  296. });
  297. $(document).on("click", "#refresh-documents", function(e) {
  298. e.preventDefault();
  299. $("#refresh-documents").hide();
  300. addSpinner($("#document-list th:nth-child(6)"));
  301. $.getJSON("ajax.php?action=scanDocuments", {}, function(r) {
  302. if(r['status'] == "OK") {
  303. if(r['new'] > 0) {
  304. location.reload();
  305. } else {
  306. removeSpinner($("#document-list th:nth-child(6)"));
  307. $("#refresh-documents").show();
  308. }
  309. } else {
  310. noty_error_retry();
  311. }
  312. });
  313. });
  314. $(document).on("click", ".reply-mail", function(e) {
  315. e.preventDefault();
  316. data = {
  317. mailID: $(this).attr("data-mail-id")
  318. };
  319. $.getJSON("ajax.php?action=getMailContent", data, function(r) {
  320. window.location.href = "mailto:" + r['to'] + "?subject=" + r['subject'] + "&body=" + r['body'];
  321. });
  322. });
  323. $(document).on("change", ".mail-processed", function(e) {
  324. data = {
  325. mailID: $(this).attr("id").replace("mail-processed-", ""),
  326. value: $(this).is(':checked')
  327. };
  328. $.getJSON("ajax.php?action=changeMailProcessed", data, function(r) {
  329. if(r['status'] != "OK") {
  330. noty_error_retry();
  331. }
  332. });
  333. });
  334. var calendar = $("#calendar").calendar({
  335. tmpl_path: "/tmpls/",
  336. events_source: "ajax.php?action=getCalendarEvents",
  337. first_day: 1, // Monday
  338. onAfterViewLoad: function(view) {
  339. $('.btn-group button').removeClass('active');
  340. $('button[data-calendar-view="' + view + '"]').addClass('active');
  341. }
  342. });
  343. $('.btn-group button[data-calendar-view]').each(function() {
  344. var $this = $(this);
  345. $this.click(function() {
  346. calendar.view($this.data('calendar-view'));
  347. });
  348. });
  349. }
  350. function reloadDraftVars() {
  351. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  352. draftVarsContainer = $("#draft-vars");
  353. if(draftVarsContainer.length > 0) {
  354. console.log(draftVarsContainer);
  355. draftVarsContainer.remove();
  356. }
  357. $(".fancybox-inner .save-button").before("<div id=\"draft-vars\"></div>");
  358. r.forEach(function(item) {
  359. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  360. })
  361. });
  362. }
  363. $.fn.editable.defaults.mode = 'inline';
  364. $.fn.editableform.buttons =
  365. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  366. '<i class="fa fa-fw fa-check"></i>'+
  367. '</button>'+
  368. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  369. '<i class="fa fa-fw fa-times"></i>'+
  370. '</button>';
  371. $('.editable-element-text').editable({
  372. escape: false,
  373. success: function(response, newValue) {
  374. console.log(response); // Debug output from ajax.php
  375. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  376. },
  377. error: function (xhr, status, error) {
  378. //var err = eval("(" + xhr.responseText + ")");
  379. return xhr.statusText;
  380. },
  381. params: function(params) {
  382. params.action = 'updateMailaccounts';
  383. return params;
  384. }
  385. });
  386. $('.editable-element-select-protocol').editable({
  387. defaultValue: 'imap',
  388. source: [
  389. {value: 'imap', text: 'IMAP'},
  390. {value: 'pop3', text: 'POP3'}
  391. ],
  392. success: function(response, newValue) {
  393. console.log(response); // Debug output from ajax.php
  394. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  395. },
  396. error: function (xhr, status, error) {
  397. //var err = eval("(" + xhr.responseText + ")");
  398. return xhr.statusText;
  399. },
  400. params: function(params) {
  401. params.action = 'updateMailaccounts';
  402. return params;
  403. }
  404. });
  405. $('.editable-element-select-use-ssl').editable({
  406. defaultValue: '1',
  407. source: [
  408. {value: '1', text: 'On'},
  409. {value: '0', text: 'Off'}
  410. ],
  411. success: function(response, newValue) {
  412. console.log(response); // Debug output from ajax.php
  413. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  414. },
  415. error: function (xhr, status, error) {
  416. //var err = eval("(" + xhr.responseText + ")");
  417. return xhr.statusText;
  418. },
  419. params: function(params) {
  420. params.action = 'updateMailaccounts';
  421. return params;
  422. }
  423. });
  424. $('.editable-element-select-no-valid-cert').editable({
  425. defaultValue: '0',
  426. source: [
  427. {value: '0', text: 'On'},
  428. {value: '1', text: 'Off'}
  429. ],
  430. success: function(response, newValue) {
  431. console.log(response); // Debug output from ajax.php
  432. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  433. },
  434. error: function (xhr, status, error) {
  435. //var err = eval("(" + xhr.responseText + ")");
  436. return xhr.statusText;
  437. },
  438. params: function(params) {
  439. params.action = 'updateMailaccounts';
  440. return params;
  441. }
  442. });
  443. $('#add-mailaccount').on('click', function(e) {
  444. e.preventDefault();
  445. var uID = $(this).attr('data-uid');
  446. $.ajax({
  447. url: 'ajax.php',
  448. type: 'POST',
  449. data: {'action': 'addDefaultMailaccount', 'user-id': uID},
  450. success: function (result) {
  451. console.log(result);
  452. location.reload();
  453. }
  454. });
  455. });
  456. function fancyBoxLoader() {
  457. $.ajax({
  458. url: 'ajax.php',
  459. data: {'action': 'getMailAccountsByUid', 'uID': $('.manage-mailboxfolder').attr('data-uid')},
  460. type: 'GET',
  461. global: false,
  462. async: true,
  463. dataType: 'json',
  464. success: function(data) {
  465. result = data;
  466. $('.editable-element-select-mailaccount').editable({
  467. //defaultValue: '0',
  468. source: result,
  469. success: function(response, newValue) {
  470. console.log(response); // Debug output from ajax.php
  471. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  472. },
  473. error: function (xhr, status, error) {
  474. //var err = eval("(" + xhr.responseText + ")");
  475. return xhr.statusText;
  476. },
  477. params: function(params) {
  478. params.action = 'updateMailfolder';
  479. return params;
  480. }
  481. });
  482. }
  483. });
  484. $('.editable-element-text').editable({
  485. escape: false,
  486. success: function(response, newValue) {
  487. console.log(response); // Debug output from ajax.php
  488. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  489. },
  490. error: function (xhr, status, error) {
  491. //var err = eval("(" + xhr.responseText + ")");
  492. return xhr.statusText;
  493. },
  494. params: function(params) {
  495. params.action = 'updateMailfolder';
  496. return params;
  497. }
  498. });
  499. }
  500. $('.remove-mailaccount').on('click', function(e) {
  501. e.preventDefault();
  502. var id = $(this).attr('data-id');
  503. $.ajax({
  504. url: 'ajax.php',
  505. type: 'POST',
  506. data: {'action': 'removeMailaccount', 'id': id},
  507. success: function (result) {
  508. console.log(result);
  509. location.reload();
  510. }
  511. });
  512. });
  513. $('.remove-mailboxfolder').on('click', function(e) {
  514. e.preventDefault();
  515. var id = $(this).attr('data-id');
  516. $.ajax({
  517. url: 'ajax.php',
  518. type: 'POST',
  519. data: {'action': 'removeMailboxFolder', 'id': id},
  520. success: function (result) {
  521. console.log(result);
  522. location.reload();
  523. }
  524. });
  525. });
  526. $('#add-mailboxfolder').on('click', function(e) {
  527. e.preventDefault();
  528. var uID = $(this).attr('data-uid');
  529. var lID = $(this).attr('data-lid');
  530. $.ajax({
  531. url: 'ajax.php',
  532. type: 'POST',
  533. data: {'action': 'addDefaultMailfolder', 'user-id': uID, 'label-id': lID},
  534. success: function (result) {
  535. console.log(result);
  536. location.reload();
  537. },
  538. error: function(result) {
  539. console.log(result);
  540. }
  541. });
  542. });
  543. $(".datetimepicker").datetimepicker({
  544. format: 'Y-m-d H:i',
  545. startDate: new Date()
  546. });
  547. switch(getUrlGetParameter("action")) {
  548. case 'settings':
  549. getMailboxStatus();
  550. settingsListener();
  551. break;
  552. case 'manage-label':
  553. loadBox('.manage-mailboxfolder', "editBox", "saveMailFolder", function() {});
  554. manageLabelListener();
  555. break;
  556. case 'manage-labels':
  557. loadBox('#add-label', "editBox", "saveLabel", function() {});
  558. manageLabelsListener();
  559. break;
  560. case 'label':
  561. loadBox('#new-document', "editBox", "saveDocument", reloadDraftVars);
  562. loadBox('#new-call', 'editBox', 'saveCall', function() {});
  563. LabelListener();
  564. break;
  565. default:
  566. break;
  567. }
  568. handleEvents();
  569. setInterval(handleEvents, 60000);
  570. });