custom.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. $(document).ready(function() {
  2. var lastOpenedBoxData; // Stores JSON-data of last opened editBox
  3. var notifications = []; // Stores noty-Notifications
  4. function noty_error_retry() {
  5. notifications.push(
  6. noty({
  7. layout : 'topCenter',
  8. text : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
  9. type : 'error',
  10. timeout : 3000
  11. }));
  12. }
  13. function getUrlGetParameter(val) {
  14. var result = "Not found",
  15. tmp = [];
  16. location.search
  17. .substr(1)
  18. .split("&")
  19. .forEach(function (item) {
  20. tmp = item.split("=");
  21. if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
  22. });
  23. return result;
  24. }
  25. function addSpinner(element) {
  26. element.append("<div class=\"spinner\"><div class=\"bounce1\"></div><div class=\"bounce2\"></div><div class=\"bounce3\"></div></div>");
  27. }
  28. function removeSpinner(element) {
  29. element.children(".spinner").remove();
  30. }
  31. function loadBox(element, boxType, saveID, callback) {
  32. var saveButtonIDs = {
  33. saveMailFolder: {id: "save-mail-folder", text: "Mailkonto speichern"},
  34. saveDocument: {id: "save-document", text: "Dokument speichern"},
  35. saveLabel: {id: "save-label", text: "Label speichern"},
  36. saveCall: {id: "save-call", text: "Anruf speichern"}
  37. };
  38. if(boxType == "editBox") {
  39. $(document).on("click", element, function(e) {
  40. e.preventDefault();
  41. $.getJSON($(this).attr('href'), function(r) {
  42. lastOpenedBoxData = r;
  43. try {
  44. var optionsContainer = document.createElement('div');
  45. $.each(r['options'], function(i) {
  46. switch(this.type) {
  47. case 'text':
  48. $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
  49. break;
  50. case 'select':
  51. selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
  52. if(typeof this['values'] !== "undefined" && this['values']) {
  53. this['values'].forEach(function(item) { // TODO: jQuery-each?
  54. if(r['options'][0]['value'] == item) {
  55. selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
  56. } else {
  57. selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
  58. }
  59. });
  60. }
  61. selectHtml += "</select>";
  62. $(optionsContainer).append(this.name + selectHtml + "<br><br>");
  63. break;
  64. case 'datetime':
  65. nowDate = new Date();
  66. nowDateString = "" + nowDate.getDate() + "." + nowDate.getMonth() + "." + nowDate.getFullYear() + " " + nowDate.getHours() + ":" + nowDate.getMinutes();
  67. $(optionsContainer).append(this.name + "<input id=\"editbox-datetime-" + this.name + "\" class=\"datetimepicker editbox-input\" type=\"text\" vaue=\"" + nowDateString + "\"><br><br>");
  68. break;
  69. case 'textarea':
  70. $(optionsContainer).append(this.name + "<textarea id=\"editbox-textarea-" + this.name + "\" class=\"editbox-input\">" + this.value + "</textarea><br><br><br>");
  71. break;
  72. case 'checkbox':
  73. $(optionsContainer).append(this.name + "<input type=\"checkbox\" id=\"editbox-checkbox-" + this.name + "\" class=\"editbox-input\"><br><br>");
  74. break;
  75. default:
  76. console.log("Missing input-type: " + this.type);
  77. break;
  78. }
  79. });
  80. $.fancybox({
  81. maxWidth : 800,
  82. maxHeight : 600,
  83. fitToView : true,
  84. width : '70%',
  85. height : '70%',
  86. autoSize : false,
  87. title : "<h3>" + r['title'] + "</h3><hr>",
  88. content : optionsContainer.innerHTML,
  89. helpers : {
  90. title: {
  91. type : 'inside',
  92. position: 'top'
  93. }
  94. },
  95. afterShow : function() {
  96. callback();
  97. $('.fancybox-inner').append("<a href=\"#\"type=\"button\" id=\"" + saveButtonIDs[saveID]['id'] + "\" class=\"btn btn-success save-button\">" + saveButtonIDs[saveID]['text'] + "</a>");
  98. $(".datetimepicker").datetimepicker({ format: 'Y-m-d H:i', startDate: new Date() });
  99. }
  100. });
  101. } catch(e) {
  102. console.log(e); // DBG
  103. noty_error_retry();
  104. }
  105. });
  106. });
  107. }
  108. }
  109. function getMailboxStatus() {
  110. $("#settings-mailboxes tr").each(function(item) {
  111. var thisTr = this;
  112. if(this.attributes.length > 0) {
  113. if($(this).children("td:first").children().length > 0) {
  114. $(this).children("td:first").children().remove();
  115. }
  116. addSpinner($(this).children("td:first"));
  117. $.getJSON("ajax.php?action=getMailboxStatus&mailboxId=" + $(this).attr("data-mailbox-id"), function(r) {
  118. if(r['connected'] == true) {
  119. removeSpinner($(thisTr).children("td:first"));
  120. $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
  121. } else {
  122. removeSpinner($(thisTr).children("td:first"));
  123. $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
  124. }
  125. });
  126. }
  127. });
  128. }
  129. function settingsListener() {
  130. $(document).on("click", "#refresh-mailaccounts", function(e) {
  131. e.preventDefault();
  132. getMailboxStatus();
  133. });
  134. }
  135. function manageLabelListener() {
  136. $(document).on("click", "#save-mail-folder", function(e) {
  137. e.preventDefault();
  138. data = {
  139. mailboxFolderID: lastOpenedBoxData['mailboxFolderID'],
  140. account: $("#editbox-input-account").val(),
  141. folder: $("#editbox-input-folder").val(),
  142. labelID: getUrlGetParameter("labelId")
  143. };
  144. $.getJSON("ajax.php?action=saveMailFolder", data, function(r) {
  145. if(r['status'] == "OK") {
  146. location.reload();
  147. } else {
  148. console.log(r);
  149. noty_error_retry();
  150. }
  151. });
  152. });
  153. }
  154. function manageLabelsListener() {
  155. $(document).on("click", "#save-label", function(e) {
  156. e.preventDefault();
  157. data = {
  158. name: $("#editbox-input-name").val(),
  159. path: $("#editbox-input-path").val()
  160. };
  161. $.getJSON("ajax.php?action=saveNewLabel", data, function(r) {
  162. if(r['status'] == "OK") {
  163. location.reload();
  164. } else {
  165. console.log(r);
  166. noty_error_retry();
  167. }
  168. });
  169. });
  170. $(document).on("click", ".remove-label", function(e) {
  171. e.preventDefault();
  172. tRow = $(this).parent().parent();
  173. data = {
  174. labelId: $(this).attr("data-id")
  175. };
  176. $.getJSON("ajax.php?action=removeLabel", data, function(r) {
  177. if(r['status'] == "OK") {
  178. tRow.fadeOut(400, function() {
  179. tRow.remove();
  180. });
  181. } else {
  182. console.log(r);
  183. noty_error_retry();
  184. }
  185. });
  186. });
  187. }
  188. function LabelListener() {
  189. $(document).on("change", ".fancybox-inner select", function(e) {
  190. reloadDraftVars();
  191. });
  192. $(document).on("click", "#save-document", function(e) {
  193. e.preventDefault();
  194. data = {
  195. draft: $("#editbox-input-vorlage").val(),
  196. filename: $("#editbox-input-filename").val(),
  197. labelId: getUrlGetParameter("labelId"),
  198. draftVars: {}
  199. };
  200. $(".editbox-draft-vars").each(function(i) {
  201. data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
  202. });
  203. $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
  204. console.log(r);
  205. });
  206. });
  207. $(document).on("click", "#save-call", function(e) {
  208. e.preventDefault();
  209. data = {
  210. callDate: $('#editbox-datetime-call-date').val(),
  211. callerTelNr: $('#editbox-input-caller-telnr').val(),
  212. labelID: $('#editbox-input-label-id').val(),
  213. callNotes: $('#editbox-textarea-call-notes').val(),
  214. callSetReminder: $('#editbox-checkbox-call-set-reminder').val()
  215. }
  216. $.getJSON("ajax.php?action=saveNewCall", data, function(r) {
  217. if(r['status'] == "OK") {
  218. $.fancybox.close();
  219. //$("a[href=\"#calls\"]").tab("show");
  220. } else {
  221. noty_error_retry();
  222. }
  223. });
  224. });
  225. }
  226. function reloadDraftVars() {
  227. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  228. draftVarsContainer = $("#draft-vars");
  229. if(draftVarsContainer.length > 0) {
  230. console.log(draftVarsContainer);
  231. draftVarsContainer.remove();
  232. }
  233. $(".fancybox-inner .save-button").before("<div id=\"draft-vars\"></div>");
  234. r.forEach(function(item) {
  235. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  236. })
  237. });
  238. }
  239. $.fn.editable.defaults.mode = 'inline';
  240. $.fn.editableform.buttons =
  241. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  242. '<i class="fa fa-fw fa-check"></i>'+
  243. '</button>'+
  244. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  245. '<i class="fa fa-fw fa-times"></i>'+
  246. '</button>';
  247. $('.editable-element-text').editable({
  248. escape: false,
  249. success: function(response, newValue) {
  250. console.log(response); // Debug output from ajax.php
  251. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  252. },
  253. error: function (xhr, status, error) {
  254. //var err = eval("(" + xhr.responseText + ")");
  255. return xhr.statusText;
  256. },
  257. params: function(params) {
  258. params.action = 'updateMailaccounts';
  259. return params;
  260. }
  261. });
  262. $('.editable-element-select-protocol').editable({
  263. defaultValue: 'imap',
  264. source: [
  265. {value: 'imap', text: 'IMAP'},
  266. {value: 'pop3', text: 'POP3'}
  267. ],
  268. success: function(response, newValue) {
  269. console.log(response); // Debug output from ajax.php
  270. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  271. },
  272. error: function (xhr, status, error) {
  273. //var err = eval("(" + xhr.responseText + ")");
  274. return xhr.statusText;
  275. },
  276. params: function(params) {
  277. params.action = 'updateMailaccounts';
  278. return params;
  279. }
  280. });
  281. $('.editable-element-select-use-ssl').editable({
  282. defaultValue: '1',
  283. source: [
  284. {value: '1', text: 'On'},
  285. {value: '0', text: 'Off'}
  286. ],
  287. success: function(response, newValue) {
  288. console.log(response); // Debug output from ajax.php
  289. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  290. },
  291. error: function (xhr, status, error) {
  292. //var err = eval("(" + xhr.responseText + ")");
  293. return xhr.statusText;
  294. },
  295. params: function(params) {
  296. params.action = 'updateMailaccounts';
  297. return params;
  298. }
  299. });
  300. $('.editable-element-select-no-valid-cert').editable({
  301. defaultValue: '0',
  302. source: [
  303. {value: '0', text: 'On'},
  304. {value: '1', text: 'Off'}
  305. ],
  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. $('#add-mailaccount').on('click', function(e) {
  320. e.preventDefault();
  321. var uId = $(this).attr('data-uid');
  322. $.ajax({
  323. url: 'ajax.php',
  324. type: 'POST',
  325. data: {'action': 'addDefaultMailaccount', 'user-id': uId},
  326. success: function (result) {
  327. console.log(result);
  328. location.reload();
  329. }
  330. });
  331. });
  332. function fancyBoxLoader() {
  333. $.ajax({
  334. url: 'ajax.php',
  335. data: {'action': 'getMailAccountsByUid', 'uId': $('.manage-mailboxfolder').attr('data-uid')},
  336. type: 'GET',
  337. global: false,
  338. async: true,
  339. dataType: 'json',
  340. success: function(data) {
  341. result = data;
  342. $('.editable-element-select-mailaccount').editable({
  343. //defaultValue: '0',
  344. source: result,
  345. success: function(response, newValue) {
  346. console.log(response); // Debug output from ajax.php
  347. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  348. },
  349. error: function (xhr, status, error) {
  350. //var err = eval("(" + xhr.responseText + ")");
  351. return xhr.statusText;
  352. },
  353. params: function(params) {
  354. params.action = 'updateMailfolder';
  355. return params;
  356. }
  357. });
  358. }
  359. });
  360. $('.editable-element-text').editable({
  361. escape: false,
  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 = 'updateMailfolder';
  372. return params;
  373. }
  374. });
  375. }
  376. $('.remove-mailaccount').on('click', function(e) {
  377. e.preventDefault();
  378. var id = $(this).attr('data-id');
  379. $.ajax({
  380. url: 'ajax.php',
  381. type: 'POST',
  382. data: {'action': 'removeMailaccount', 'id': id},
  383. success: function (result) {
  384. console.log(result);
  385. location.reload();
  386. }
  387. });
  388. });
  389. $('.remove-mailboxfolder').on('click', function(e) {
  390. e.preventDefault();
  391. var id = $(this).attr('data-id');
  392. $.ajax({
  393. url: 'ajax.php',
  394. type: 'POST',
  395. data: {'action': 'removeMailboxFolder', 'id': id},
  396. success: function (result) {
  397. console.log(result);
  398. location.reload();
  399. }
  400. });
  401. });
  402. $('#add-mailboxfolder').on('click', function(e) {
  403. e.preventDefault();
  404. var uId = $(this).attr('data-uid');
  405. var lId = $(this).attr('data-lid');
  406. $.ajax({
  407. url: 'ajax.php',
  408. type: 'POST',
  409. data: {'action': 'addDefaultMailfolder', 'user-id': uId, 'label-id': lId},
  410. success: function (result) {
  411. console.log(result);
  412. location.reload();
  413. },
  414. error: function(result) {
  415. console.log(result);
  416. }
  417. });
  418. });
  419. $(".datetimepicker").datetimepicker({
  420. format: 'Y-m-d H:i',
  421. startDate: new Date()
  422. });
  423. switch(getUrlGetParameter("action")) {
  424. case 'settings':
  425. getMailboxStatus();
  426. settingsListener();
  427. break;
  428. case 'manage-label':
  429. loadBox('.manage-mailboxfolder', "editBox", "saveMailFolder", function() {});
  430. manageLabelListener();
  431. break;
  432. case 'manage-labels':
  433. loadBox('#add-label', "editBox", "saveLabel", function() {});
  434. manageLabelsListener();
  435. break;
  436. case 'label':
  437. loadBox('#new-document', "editBox", "saveDocument", reloadDraftVars);
  438. loadBox('#new-call', 'editBox', 'saveCall', function() {});
  439. LabelListener();
  440. break;
  441. default:
  442. break;
  443. }
  444. });