custom.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. addSpinner($(this).children("td:first"));
  82. $.getJSON("ajax.php?action=getMailboxStatus&mailboxId=" + $(this).attr("data-mailbox-id"), function(r) {
  83. if(r['connected'] == true) {
  84. removeSpinner($(thisTr).children("td:first"));
  85. $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
  86. } else {
  87. removeSpinner($(thisTr).children("td:first"));
  88. $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
  89. }
  90. });
  91. }
  92. });
  93. }
  94. function newDocumentListener() {
  95. $(document).on("change", ".fancybox-inner select", function(e) {
  96. reloadDraftVars();
  97. });
  98. $(document).on("click", "#save-new-document", function(e) {
  99. e.preventDefault();
  100. data = {
  101. draft: $("#editbox-input-vorlage").val(),
  102. filename: $("#editbox-input-filename").val(),
  103. labelId: getUrlGetParameter("labelId"),
  104. draftVars: {}
  105. }
  106. $(".editbox-draft-vars").each(function(i) {
  107. data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
  108. });
  109. $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
  110. console.log(r);
  111. });
  112. });
  113. }
  114. function reloadDraftVars() {
  115. $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
  116. draftVarsContainer = $("#draft-vars");
  117. if(draftVarsContainer.length > 0) {
  118. console.log(draftVarsContainer);
  119. draftVarsContainer.remove();
  120. }
  121. $(".fancybox-inner").append("<div id=\"draft-vars\"></div>")
  122. r.forEach(function(item) {
  123. //$(".fancybox-inner #draft-vars").append(item + "<input id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\" type=\"text\" /><br><br>");
  124. $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
  125. })
  126. $(".fancybox-inner #draft-vars").append("<a href=\"#\"type=\"button\" id=\"save-new-document\" class=\"btn btn-success\">Dokument speichern</a>");
  127. });
  128. }
  129. $.fn.editable.defaults.mode = 'inline';
  130. $.fn.editableform.buttons =
  131. '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
  132. '<i class="fa fa-fw fa-check"></i>'+
  133. '</button>'+
  134. '<button type="button" class="btn btn-default btn-sm editable-cancel">'+
  135. '<i class="fa fa-fw fa-times"></i>'+
  136. '</button>';
  137. $('.editable-element-text').editable({
  138. escape: false,
  139. success: function(response, newValue) {
  140. console.log(response); // Debug output from ajax.php
  141. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  142. },
  143. error: function (xhr, status, error) {
  144. //var err = eval("(" + xhr.responseText + ")");
  145. return xhr.statusText;
  146. },
  147. params: function(params) {
  148. params.action = 'updateMailaccounts';
  149. return params;
  150. }
  151. });
  152. $('.editable-element-select-protocol').editable({
  153. defaultValue: 'imap',
  154. source: [
  155. {value: 'imap', text: 'IMAP'},
  156. {value: 'pop3', text: 'POP3'}
  157. ],
  158. success: function(response, newValue) {
  159. console.log(response); // Debug output from ajax.php
  160. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  161. },
  162. error: function (xhr, status, error) {
  163. //var err = eval("(" + xhr.responseText + ")");
  164. return xhr.statusText;
  165. },
  166. params: function(params) {
  167. params.action = 'updateMailaccounts';
  168. return params;
  169. }
  170. });
  171. $('.editable-element-select-use-ssl').editable({
  172. defaultValue: '1',
  173. source: [
  174. {value: '1', text: 'On'},
  175. {value: '0', text: 'Off'}
  176. ],
  177. success: function(response, newValue) {
  178. console.log(response); // Debug output from ajax.php
  179. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  180. },
  181. error: function (xhr, status, error) {
  182. //var err = eval("(" + xhr.responseText + ")");
  183. return xhr.statusText;
  184. },
  185. params: function(params) {
  186. params.action = 'updateMailaccounts';
  187. return params;
  188. }
  189. });
  190. $('.editable-element-select-no-valid-cert').editable({
  191. defaultValue: '0',
  192. source: [
  193. {value: '0', text: 'On'},
  194. {value: '1', text: 'Off'}
  195. ],
  196. success: function(response, newValue) {
  197. console.log(response); // Debug output from ajax.php
  198. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  199. },
  200. error: function (xhr, status, error) {
  201. //var err = eval("(" + xhr.responseText + ")");
  202. return xhr.statusText;
  203. },
  204. params: function(params) {
  205. params.action = 'updateMailaccounts';
  206. return params;
  207. }
  208. });
  209. $('#add-mailaccount').on('click', function(e) {
  210. e.preventDefault();
  211. var uId = $(this).attr('data-uid');
  212. $.ajax({
  213. url: 'ajax.php',
  214. type: 'POST',
  215. data: {'action': 'addDefaultMailaccount', 'user-id': uId},
  216. success: function (result) {
  217. console.log(result);
  218. location.reload();
  219. }
  220. });
  221. });
  222. function fancyBoxLoader() {
  223. $.ajax({
  224. url: 'ajax.php',
  225. data: {'action': 'getMailAccountsByUid', 'uId': $('.manage-mailboxfolder').attr('data-uid')},
  226. type: 'GET',
  227. global: false,
  228. async: true,
  229. dataType: 'json',
  230. success: function(data) {
  231. result = data;
  232. $('.editable-element-select-mailaccount').editable({
  233. //defaultValue: '0',
  234. source: result,
  235. success: function(response, newValue) {
  236. console.log(response); // Debug output from ajax.php
  237. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  238. },
  239. error: function (xhr, status, error) {
  240. //var err = eval("(" + xhr.responseText + ")");
  241. return xhr.statusText;
  242. },
  243. params: function(params) {
  244. params.action = 'updateMailfolder';
  245. return params;
  246. }
  247. });
  248. }
  249. });
  250. $('.editable-element-text').editable({
  251. escape: false,
  252. success: function(response, newValue) {
  253. console.log(response); // Debug output from ajax.php
  254. if(response.status == 'error') return response.msg; //msg will be shown in editable form
  255. },
  256. error: function (xhr, status, error) {
  257. //var err = eval("(" + xhr.responseText + ")");
  258. return xhr.statusText;
  259. },
  260. params: function(params) {
  261. params.action = 'updateMailfolder';
  262. return params;
  263. }
  264. });
  265. }
  266. $('.remove-mailaccount').on('click', function(e) {
  267. e.preventDefault();
  268. var id = $(this).attr('data-id');
  269. $.ajax({
  270. url: 'ajax.php',
  271. type: 'POST',
  272. data: {'action': 'removeMailaccount', 'id': id},
  273. success: function (result) {
  274. console.log(result);
  275. location.reload();
  276. }
  277. });
  278. });
  279. $('.remove-mailboxfolder').on('click', function(e) {
  280. e.preventDefault();
  281. var id = $(this).attr('data-id');
  282. $.ajax({
  283. url: 'ajax.php',
  284. type: 'POST',
  285. data: {'action': 'removeMailboxFolder', 'id': id},
  286. success: function (result) {
  287. console.log(result);
  288. location.reload();
  289. }
  290. });
  291. });
  292. $('#add-mailboxfolder').on('click', function(e) {
  293. e.preventDefault();
  294. var uId = $(this).attr('data-uid');
  295. var lId = $(this).attr('data-lid');
  296. $.ajax({
  297. url: 'ajax.php',
  298. type: 'POST',
  299. data: {'action': 'addDefaultMailfolder', 'user-id': uId, 'label-id': lId},
  300. success: function (result) {
  301. console.log(result);
  302. location.reload();
  303. },
  304. error: function(result) {
  305. console.log(result);
  306. }
  307. });
  308. });
  309. switch(getUrlGetParameter("action")) {
  310. case 'settings':
  311. getMailboxStatus();
  312. break;
  313. case 'manage-label':
  314. loadBox('.manage-mailboxfolder', function() {});
  315. break;
  316. case 'label':
  317. loadBox('#new-document', function() { reloadDraftVars(); });
  318. newDocumentListener();
  319. break;
  320. default:
  321. break;
  322. }
  323. });