custom.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. }
  283. function reloadDraftVars() {
  284. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  285. draftVarsContainer = $("#draft-vars");
  286. if(draftVarsContainer.length > 0) {
  287. console.log(draftVarsContainer);
  288. draftVarsContainer.remove();
  289. }
  290. $(".fancybox-inner .save-button").before("<div id=\"draft-vars\"></div>");
  291. r.forEach(function(item) {
  292. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  293. })
  294. });
  295. }
  296. $.fn.editable.defaults.mode = 'inline';
  297. $.fn.editableform.buttons =
  298. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  299. '<i class="fa fa-fw fa-check"></i>'+
  300. '</button>'+
  301. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  302. '<i class="fa fa-fw fa-times"></i>'+
  303. '</button>';
  304. $('.editable-element-text').editable({
  305. escape: false,
  306. success: function(response, newValue) {
  307. console.log(response); // Debug output from ajax.php
  308. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  309. },
  310. error: function (xhr, status, error) {
  311. //var err = eval("(" + xhr.responseText + ")");
  312. return xhr.statusText;
  313. },
  314. params: function(params) {
  315. params.action = 'updateMailaccounts';
  316. return params;
  317. }
  318. });
  319. $('.editable-element-select-protocol').editable({
  320. defaultValue: 'imap',
  321. source: [
  322. {value: 'imap', text: 'IMAP'},
  323. {value: 'pop3', text: 'POP3'}
  324. ],
  325. success: function(response, newValue) {
  326. console.log(response); // Debug output from ajax.php
  327. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  328. },
  329. error: function (xhr, status, error) {
  330. //var err = eval("(" + xhr.responseText + ")");
  331. return xhr.statusText;
  332. },
  333. params: function(params) {
  334. params.action = 'updateMailaccounts';
  335. return params;
  336. }
  337. });
  338. $('.editable-element-select-use-ssl').editable({
  339. defaultValue: '1',
  340. source: [
  341. {value: '1', text: 'On'},
  342. {value: '0', text: 'Off'}
  343. ],
  344. success: function(response, newValue) {
  345. console.log(response); // Debug output from ajax.php
  346. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  347. },
  348. error: function (xhr, status, error) {
  349. //var err = eval("(" + xhr.responseText + ")");
  350. return xhr.statusText;
  351. },
  352. params: function(params) {
  353. params.action = 'updateMailaccounts';
  354. return params;
  355. }
  356. });
  357. $('.editable-element-select-no-valid-cert').editable({
  358. defaultValue: '0',
  359. source: [
  360. {value: '0', text: 'On'},
  361. {value: '1', text: 'Off'}
  362. ],
  363. success: function(response, newValue) {
  364. console.log(response); // Debug output from ajax.php
  365. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  366. },
  367. error: function (xhr, status, error) {
  368. //var err = eval("(" + xhr.responseText + ")");
  369. return xhr.statusText;
  370. },
  371. params: function(params) {
  372. params.action = 'updateMailaccounts';
  373. return params;
  374. }
  375. });
  376. $('#add-mailaccount').on('click', function(e) {
  377. e.preventDefault();
  378. var uId = $(this).attr('data-uid');
  379. $.ajax({
  380. url: 'ajax.php',
  381. type: 'POST',
  382. data: {'action': 'addDefaultMailaccount', 'user-id': uId},
  383. success: function (result) {
  384. console.log(result);
  385. location.reload();
  386. }
  387. });
  388. });
  389. function fancyBoxLoader() {
  390. $.ajax({
  391. url: 'ajax.php',
  392. data: {'action': 'getMailAccountsByUid', 'uId': $('.manage-mailboxfolder').attr('data-uid')},
  393. type: 'GET',
  394. global: false,
  395. async: true,
  396. dataType: 'json',
  397. success: function(data) {
  398. result = data;
  399. $('.editable-element-select-mailaccount').editable({
  400. //defaultValue: '0',
  401. source: result,
  402. success: function(response, newValue) {
  403. console.log(response); // Debug output from ajax.php
  404. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  405. },
  406. error: function (xhr, status, error) {
  407. //var err = eval("(" + xhr.responseText + ")");
  408. return xhr.statusText;
  409. },
  410. params: function(params) {
  411. params.action = 'updateMailfolder';
  412. return params;
  413. }
  414. });
  415. }
  416. });
  417. $('.editable-element-text').editable({
  418. escape: false,
  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. $('.remove-mailaccount').on('click', function(e) {
  434. e.preventDefault();
  435. var id = $(this).attr('data-id');
  436. $.ajax({
  437. url: 'ajax.php',
  438. type: 'POST',
  439. data: {'action': 'removeMailaccount', 'id': id},
  440. success: function (result) {
  441. console.log(result);
  442. location.reload();
  443. }
  444. });
  445. });
  446. $('.remove-mailboxfolder').on('click', function(e) {
  447. e.preventDefault();
  448. var id = $(this).attr('data-id');
  449. $.ajax({
  450. url: 'ajax.php',
  451. type: 'POST',
  452. data: {'action': 'removeMailboxFolder', 'id': id},
  453. success: function (result) {
  454. console.log(result);
  455. location.reload();
  456. }
  457. });
  458. });
  459. $('#add-mailboxfolder').on('click', function(e) {
  460. e.preventDefault();
  461. var uId = $(this).attr('data-uid');
  462. var lId = $(this).attr('data-lid');
  463. $.ajax({
  464. url: 'ajax.php',
  465. type: 'POST',
  466. data: {'action': 'addDefaultMailfolder', 'user-id': uId, 'label-id': lId},
  467. success: function (result) {
  468. console.log(result);
  469. location.reload();
  470. },
  471. error: function(result) {
  472. console.log(result);
  473. }
  474. });
  475. });
  476. $(".datetimepicker").datetimepicker({
  477. format: 'Y-m-d H:i',
  478. startDate: new Date()
  479. });
  480. switch(getUrlGetParameter("action")) {
  481. case 'settings':
  482. getMailboxStatus();
  483. settingsListener();
  484. break;
  485. case 'manage-label':
  486. loadBox('.manage-mailboxfolder', "editBox", "saveMailFolder", function() {});
  487. manageLabelListener();
  488. break;
  489. case 'manage-labels':
  490. loadBox('#add-label', "editBox", "saveLabel", function() {});
  491. manageLabelsListener();
  492. break;
  493. case 'label':
  494. loadBox('#new-document', "editBox", "saveDocument", reloadDraftVars);
  495. loadBox('#new-call', 'editBox', 'saveCall', function() {});
  496. LabelListener();
  497. break;
  498. default:
  499. break;
  500. }
  501. handleEvents();
  502. setInterval(handleEvents, 60000);
  503. });