custom.js 22 KB

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