custom.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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) { // TODO: jQuery-each?
  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. callDate: $('#editbox-datetime-call-date').val(),
  267. callerTelNr: $('#editbox-input-caller-telnr').val(),
  268. labelID: $('#editbox-input-label-id').val(),
  269. callNotes: $('#editbox-textarea-call-notes').val(),
  270. callSetReminder: $('#editbox-checkbox-call-set-reminder').val()
  271. }
  272. $.getJSON("ajax.php?action=saveNewCall", data, function(r) {
  273. if(r['status'] == "OK") {
  274. $.fancybox.close();
  275. //$("a[href=\"#calls\"]").tab("show");
  276. } else {
  277. noty_error_retry();
  278. }
  279. });
  280. });
  281. }
  282. function reloadDraftVars() {
  283. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  284. draftVarsContainer = $("#draft-vars");
  285. if(draftVarsContainer.length > 0) {
  286. console.log(draftVarsContainer);
  287. draftVarsContainer.remove();
  288. }
  289. $(".fancybox-inner .save-button").before("<div id=\"draft-vars\"></div>");
  290. r.forEach(function(item) {
  291. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  292. })
  293. });
  294. }
  295. $.fn.editable.defaults.mode = 'inline';
  296. $.fn.editableform.buttons =
  297. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  298. '<i class="fa fa-fw fa-check"></i>'+
  299. '</button>'+
  300. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  301. '<i class="fa fa-fw fa-times"></i>'+
  302. '</button>';
  303. $('.editable-element-text').editable({
  304. escape: false,
  305. success: function(response, newValue) {
  306. console.log(response); // Debug output from ajax.php
  307. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  308. },
  309. error: function (xhr, status, error) {
  310. //var err = eval("(" + xhr.responseText + ")");
  311. return xhr.statusText;
  312. },
  313. params: function(params) {
  314. params.action = 'updateMailaccounts';
  315. return params;
  316. }
  317. });
  318. $('.editable-element-select-protocol').editable({
  319. defaultValue: 'imap',
  320. source: [
  321. {value: 'imap', text: 'IMAP'},
  322. {value: 'pop3', text: 'POP3'}
  323. ],
  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-use-ssl').editable({
  338. defaultValue: '1',
  339. source: [
  340. {value: '1', text: 'On'},
  341. {value: '0', text: 'Off'}
  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-no-valid-cert').editable({
  357. defaultValue: '0',
  358. source: [
  359. {value: '0', text: 'On'},
  360. {value: '1', 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. $('#add-mailaccount').on('click', function(e) {
  376. e.preventDefault();
  377. var uId = $(this).attr('data-uid');
  378. $.ajax({
  379. url: 'ajax.php',
  380. type: 'POST',
  381. data: {'action': 'addDefaultMailaccount', 'user-id': uId},
  382. success: function (result) {
  383. console.log(result);
  384. location.reload();
  385. }
  386. });
  387. });
  388. function fancyBoxLoader() {
  389. $.ajax({
  390. url: 'ajax.php',
  391. data: {'action': 'getMailAccountsByUid', 'uId': $('.manage-mailboxfolder').attr('data-uid')},
  392. type: 'GET',
  393. global: false,
  394. async: true,
  395. dataType: 'json',
  396. success: function(data) {
  397. result = data;
  398. $('.editable-element-select-mailaccount').editable({
  399. //defaultValue: '0',
  400. source: result,
  401. success: function(response, newValue) {
  402. console.log(response); // Debug output from ajax.php
  403. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  404. },
  405. error: function (xhr, status, error) {
  406. //var err = eval("(" + xhr.responseText + ")");
  407. return xhr.statusText;
  408. },
  409. params: function(params) {
  410. params.action = 'updateMailfolder';
  411. return params;
  412. }
  413. });
  414. }
  415. });
  416. $('.editable-element-text').editable({
  417. escape: false,
  418. success: function(response, newValue) {
  419. console.log(response); // Debug output from ajax.php
  420. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  421. },
  422. error: function (xhr, status, error) {
  423. //var err = eval("(" + xhr.responseText + ")");
  424. return xhr.statusText;
  425. },
  426. params: function(params) {
  427. params.action = 'updateMailfolder';
  428. return params;
  429. }
  430. });
  431. }
  432. $('.remove-mailaccount').on('click', function(e) {
  433. e.preventDefault();
  434. var id = $(this).attr('data-id');
  435. $.ajax({
  436. url: 'ajax.php',
  437. type: 'POST',
  438. data: {'action': 'removeMailaccount', 'id': id},
  439. success: function (result) {
  440. console.log(result);
  441. location.reload();
  442. }
  443. });
  444. });
  445. $('.remove-mailboxfolder').on('click', function(e) {
  446. e.preventDefault();
  447. var id = $(this).attr('data-id');
  448. $.ajax({
  449. url: 'ajax.php',
  450. type: 'POST',
  451. data: {'action': 'removeMailboxFolder', 'id': id},
  452. success: function (result) {
  453. console.log(result);
  454. location.reload();
  455. }
  456. });
  457. });
  458. $('#add-mailboxfolder').on('click', function(e) {
  459. e.preventDefault();
  460. var uId = $(this).attr('data-uid');
  461. var lId = $(this).attr('data-lid');
  462. $.ajax({
  463. url: 'ajax.php',
  464. type: 'POST',
  465. data: {'action': 'addDefaultMailfolder', 'user-id': uId, 'label-id': lId},
  466. success: function (result) {
  467. console.log(result);
  468. location.reload();
  469. },
  470. error: function(result) {
  471. console.log(result);
  472. }
  473. });
  474. });
  475. $(".datetimepicker").datetimepicker({
  476. format: 'Y-m-d H:i',
  477. startDate: new Date()
  478. });
  479. switch(getUrlGetParameter("action")) {
  480. case 'settings':
  481. getMailboxStatus();
  482. settingsListener();
  483. break;
  484. case 'manage-label':
  485. loadBox('.manage-mailboxfolder', "editBox", "saveMailFolder", function() {});
  486. manageLabelListener();
  487. break;
  488. case 'manage-labels':
  489. loadBox('#add-label', "editBox", "saveLabel", function() {});
  490. manageLabelsListener();
  491. break;
  492. case 'label':
  493. loadBox('#new-document', "editBox", "saveDocument", reloadDraftVars);
  494. loadBox('#new-call', 'editBox', 'saveCall', function() {});
  495. LabelListener();
  496. break;
  497. default:
  498. break;
  499. }
  500. handleEvents();
  501. setInterval(handleEvents, 60000);
  502. });