custom.js 22 KB

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