custom.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. $(document).ready(function() {
  2. function getUrlGetParameter(val) {
  3. var result = "Not found",
  4. tmp = [];
  5. location.search
  6. .substr(1)
  7. .split("&")
  8. .forEach(function (item) {
  9. tmp = item.split("=");
  10. if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
  11. });
  12. return result;
  13. }
  14. function addSpinner(element) {
  15. element.append("<div class=\"spinner\"><div class=\"bounce1\"></div><div class=\"bounce2\"></div><div class=\"bounce3\"></div></div>");
  16. }
  17. function removeSpinner(element) {
  18. element.children(".spinner").remove();
  19. }
  20. function loadBox(element, callback) {
  21. $(document).on("click", element, function(e) {
  22. e.preventDefault();
  23. $.get($(this).attr('href'), function(r) {
  24. try {
  25. editBox = $.parseJSON(r);
  26. var optionsContainer = document.createElement('div');
  27. $.each(editBox['options'], function(i) {
  28. switch(this.type) {
  29. case 'text':
  30. $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
  31. break;
  32. case 'select':
  33. selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
  34. this['values'].forEach(function(item) { // TODO: jQuery-each?
  35. if(editBox['options'][0]['value'] == item) {
  36. selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
  37. } else {
  38. selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
  39. }
  40. });
  41. selectHtml += "</select>";
  42. $(optionsContainer).append(this.name + selectHtml + "<br><br>");
  43. break;
  44. default:
  45. break;
  46. }
  47. });
  48. $.fancybox({
  49. maxWidth : 800,
  50. maxHeight : 600,
  51. fitToView : true,
  52. width : '70%',
  53. height : '70%',
  54. autoSize : false,
  55. title : "<h3>" + editBox['title'] + "</h3><hr>",
  56. content : optionsContainer.innerHTML,
  57. helpers : {
  58. title: {
  59. type : 'inside',
  60. position: 'top'
  61. }
  62. },
  63. afterShow : callback
  64. });
  65. } catch(e) {
  66. console.log(e); // DBG
  67. var n = noty({
  68. layout : 'topCenter',
  69. text : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
  70. type : 'error',
  71. timeout : 5000
  72. });
  73. }
  74. });
  75. });
  76. }
  77. function getMailboxStatus() {
  78. $("#settings-mailboxes tr").each(function(item) {
  79. var thisTr = this;
  80. if(this.attributes.length > 0) {
  81. if($(this).children("td:first").children().length > 0) {
  82. $(this).children("td:first").children().remove();
  83. }
  84. addSpinner($(this).children("td:first"));
  85. $.getJSON("ajax.php?action=getMailboxStatus&mailboxId=" + $(this).attr("data-mailbox-id"), function(r) {
  86. if(r['connected'] == true) {
  87. removeSpinner($(thisTr).children("td:first"));
  88. $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
  89. } else {
  90. removeSpinner($(thisTr).children("td:first"));
  91. $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
  92. }
  93. });
  94. }
  95. });
  96. }
  97. function settingsListener() {
  98. $(document).on("click", "#refresh-mailaccounts", function(e) {
  99. e.preventDefault();
  100. getMailboxStatus();
  101. });
  102. }
  103. function newDocumentListener() {
  104. $(document).on("change", ".fancybox-inner select", function(e) {
  105. reloadDraftVars();
  106. });
  107. $(document).on("click", "#save-new-document", function(e) {
  108. e.preventDefault();
  109. data = {
  110. draft: $("#editbox-input-vorlage").val(),
  111. filename: $("#editbox-input-filename").val(),
  112. labelId: getUrlGetParameter("labelId"),
  113. draftVars: {}
  114. }
  115. $(".editbox-draft-vars").each(function(i) {
  116. data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
  117. });
  118. $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
  119. console.log(r);
  120. });
  121. });
  122. }
  123. function reloadDraftVars() {
  124. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  125. draftVarsContainer = $("#draft-vars");
  126. if(draftVarsContainer.length > 0) {
  127. console.log(draftVarsContainer);
  128. draftVarsContainer.remove();
  129. }
  130. $(".fancybox-inner").append("<div id=\"draft-vars\"></div>")
  131. r.forEach(function(item) {
  132. //$(".fancybox-inner #draft-vars").append(item + "<input id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\" type=\"text\" /><br><br>");
  133. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  134. })
  135. $(".fancybox-inner #draft-vars").append("<a href=\"#\"type=\"button\" id=\"save-new-document\" class=\"btn btn-success\">Dokument speichern</a>");
  136. });
  137. }
  138. $.fn.editable.defaults.mode = 'inline';
  139. $.fn.editableform.buttons =
  140. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  141. '<i class="fa fa-fw fa-check"></i>'+
  142. '</button>'+
  143. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  144. '<i class="fa fa-fw fa-times"></i>'+
  145. '</button>';
  146. $('.editable-element-text').editable({
  147. escape: false,
  148. success: function(response, newValue) {
  149. console.log(response); // Debug output from ajax.php
  150. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  151. },
  152. error: function (xhr, status, error) {
  153. //var err = eval("(" + xhr.responseText + ")");
  154. return xhr.statusText;
  155. },
  156. params: function(params) {
  157. params.action = 'updateMailaccounts';
  158. return params;
  159. }
  160. });
  161. $('.editable-element-select-protocol').editable({
  162. defaultValue: 'imap',
  163. source: [
  164. {value: 'imap', text: 'IMAP'},
  165. {value: 'pop3', text: 'POP3'}
  166. ],
  167. success: function(response, newValue) {
  168. console.log(response); // Debug output from ajax.php
  169. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  170. },
  171. error: function (xhr, status, error) {
  172. //var err = eval("(" + xhr.responseText + ")");
  173. return xhr.statusText;
  174. },
  175. params: function(params) {
  176. params.action = 'updateMailaccounts';
  177. return params;
  178. }
  179. });
  180. $('.editable-element-select-use-ssl').editable({
  181. defaultValue: '1',
  182. source: [
  183. {value: '1', text: 'On'},
  184. {value: '0', text: 'Off'}
  185. ],
  186. success: function(response, newValue) {
  187. console.log(response); // Debug output from ajax.php
  188. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  189. },
  190. error: function (xhr, status, error) {
  191. //var err = eval("(" + xhr.responseText + ")");
  192. return xhr.statusText;
  193. },
  194. params: function(params) {
  195. params.action = 'updateMailaccounts';
  196. return params;
  197. }
  198. });
  199. $('.editable-element-select-no-valid-cert').editable({
  200. defaultValue: '0',
  201. source: [
  202. {value: '0', text: 'On'},
  203. {value: '1', text: 'Off'}
  204. ],
  205. success: function(response, newValue) {
  206. console.log(response); // Debug output from ajax.php
  207. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  208. },
  209. error: function (xhr, status, error) {
  210. //var err = eval("(" + xhr.responseText + ")");
  211. return xhr.statusText;
  212. },
  213. params: function(params) {
  214. params.action = 'updateMailaccounts';
  215. return params;
  216. }
  217. });
  218. $('#add-mailaccount').on('click', function(e) {
  219. e.preventDefault();
  220. var uId = $(this).attr('data-uid');
  221. $.ajax({
  222. url: 'ajax.php',
  223. type: 'POST',
  224. data: {'action': 'addDefaultMailaccount', 'user-id': uId},
  225. success: function (result) {
  226. console.log(result);
  227. location.reload();
  228. }
  229. });
  230. });
  231. function fancyBoxLoader() {
  232. $.ajax({
  233. url: 'ajax.php',
  234. data: {'action': 'getMailAccountsByUid', 'uId': $('.manage-mailboxfolder').attr('data-uid')},
  235. type: 'GET',
  236. global: false,
  237. async: true,
  238. dataType: 'json',
  239. success: function(data) {
  240. result = data;
  241. $('.editable-element-select-mailaccount').editable({
  242. //defaultValue: '0',
  243. source: result,
  244. success: function(response, newValue) {
  245. console.log(response); // Debug output from ajax.php
  246. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  247. },
  248. error: function (xhr, status, error) {
  249. //var err = eval("(" + xhr.responseText + ")");
  250. return xhr.statusText;
  251. },
  252. params: function(params) {
  253. params.action = 'updateMailfolder';
  254. return params;
  255. }
  256. });
  257. }
  258. });
  259. $('.editable-element-text').editable({
  260. escape: false,
  261. success: function(response, newValue) {
  262. console.log(response); // Debug output from ajax.php
  263. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  264. },
  265. error: function (xhr, status, error) {
  266. //var err = eval("(" + xhr.responseText + ")");
  267. return xhr.statusText;
  268. },
  269. params: function(params) {
  270. params.action = 'updateMailfolder';
  271. return params;
  272. }
  273. });
  274. }
  275. $('.remove-mailaccount').on('click', function(e) {
  276. e.preventDefault();
  277. var id = $(this).attr('data-id');
  278. $.ajax({
  279. url: 'ajax.php',
  280. type: 'POST',
  281. data: {'action': 'removeMailaccount', 'id': id},
  282. success: function (result) {
  283. console.log(result);
  284. location.reload();
  285. }
  286. });
  287. });
  288. $('.remove-mailboxfolder').on('click', function(e) {
  289. e.preventDefault();
  290. var id = $(this).attr('data-id');
  291. $.ajax({
  292. url: 'ajax.php',
  293. type: 'POST',
  294. data: {'action': 'removeMailboxFolder', 'id': id},
  295. success: function (result) {
  296. console.log(result);
  297. location.reload();
  298. }
  299. });
  300. });
  301. $('#add-mailboxfolder').on('click', function(e) {
  302. e.preventDefault();
  303. var uId = $(this).attr('data-uid');
  304. var lId = $(this).attr('data-lid');
  305. $.ajax({
  306. url: 'ajax.php',
  307. type: 'POST',
  308. data: {'action': 'addDefaultMailfolder', 'user-id': uId, 'label-id': lId},
  309. success: function (result) {
  310. console.log(result);
  311. location.reload();
  312. },
  313. error: function(result) {
  314. console.log(result);
  315. }
  316. });
  317. });
  318. switch(getUrlGetParameter("action")) {
  319. case 'settings':
  320. getMailboxStatus();
  321. settingsListener();
  322. break;
  323. case 'manage-label':
  324. loadBox('.manage-mailboxfolder', function() {});
  325. break;
  326. case 'label':
  327. loadBox('#new-document', function() { reloadDraftVars(); });
  328. newDocumentListener();
  329. break;
  330. default:
  331. break;
  332. }
  333. });