PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/public/assets/pages/scripts/inbox.js

https://gitlab.com/SFMSP/Hagape
JavaScript | 336 lines | 262 code | 56 blank | 18 comment | 11 complexity | 4c35ad39b4cfd131588284bf30a45e53 MD5 | raw file
  1. var AppInbox = function () {
  2. var content = $('.inbox-content');
  3. var loading = $('.inbox-loading');
  4. var listListing = '';
  5. var loadInbox = function (el, name) {
  6. var url = 'app_inbox_inbox.html';
  7. var title = $('.inbox-nav > li.' + name + ' a').attr('data-title');
  8. listListing = name;
  9. loading.show();
  10. content.html('');
  11. toggleButton(el);
  12. $.ajax({
  13. type: "GET",
  14. cache: false,
  15. url: url,
  16. dataType: "html",
  17. success: function(res)
  18. {
  19. toggleButton(el);
  20. $('.inbox-nav > li.active').removeClass('active');
  21. $('.inbox-nav > li.' + name).addClass('active');
  22. $('.inbox-header > h1').text(title);
  23. loading.hide();
  24. content.html(res);
  25. if (Layout.fixContentHeight) {
  26. Layout.fixContentHeight();
  27. }
  28. App.initUniform();
  29. },
  30. error: function(xhr, ajaxOptions, thrownError)
  31. {
  32. toggleButton(el);
  33. },
  34. async: false
  35. });
  36. // handle group checkbox:
  37. jQuery('body').on('change', '.mail-group-checkbox', function () {
  38. var set = jQuery('.mail-checkbox');
  39. var checked = jQuery(this).is(":checked");
  40. jQuery(set).each(function () {
  41. $(this).attr("checked", checked);
  42. });
  43. jQuery.uniform.update(set);
  44. });
  45. }
  46. var loadMessage = function (el, name, resetMenu) {
  47. var url = 'app_inbox_view.html';
  48. loading.show();
  49. content.html('');
  50. toggleButton(el);
  51. var message_id = el.parent('tr').attr("data-messageid");
  52. $.ajax({
  53. type: "GET",
  54. cache: false,
  55. url: url,
  56. dataType: "html",
  57. data: {'message_id': message_id},
  58. success: function(res)
  59. {
  60. toggleButton(el);
  61. if (resetMenu) {
  62. $('.inbox-nav > li.active').removeClass('active');
  63. }
  64. $('.inbox-header > h1').text('View Message');
  65. loading.hide();
  66. content.html(res);
  67. Layout.fixContentHeight();
  68. App.initUniform();
  69. },
  70. error: function(xhr, ajaxOptions, thrownError)
  71. {
  72. toggleButton(el);
  73. },
  74. async: false
  75. });
  76. }
  77. var initWysihtml5 = function () {
  78. $('.inbox-wysihtml5').wysihtml5({
  79. "stylesheets": ["../assets/global/plugins/bootstrap-wysihtml5/wysiwyg-color.css"]
  80. });
  81. }
  82. var initFileupload = function () {
  83. $('#fileupload').fileupload({
  84. // Uncomment the following to send cross-domain cookies:
  85. //xhrFields: {withCredentials: true},
  86. url: '../assets/global/plugins/jquery-file-upload/server/php/',
  87. autoUpload: true
  88. });
  89. // Upload server status check for browsers with CORS support:
  90. if ($.support.cors) {
  91. $.ajax({
  92. url: '../assets/global/plugins/jquery-file-upload/server/php/',
  93. type: 'HEAD'
  94. }).fail(function () {
  95. $('<span class="alert alert-error"/>')
  96. .text('Upload server currently unavailable - ' +
  97. new Date())
  98. .appendTo('#fileupload');
  99. });
  100. }
  101. }
  102. var loadCompose = function (el) {
  103. var url = 'app_inbox_compose.html';
  104. loading.show();
  105. content.html('');
  106. toggleButton(el);
  107. // load the form via ajax
  108. $.ajax({
  109. type: "GET",
  110. cache: false,
  111. url: url,
  112. dataType: "html",
  113. success: function(res)
  114. {
  115. toggleButton(el);
  116. $('.inbox-nav > li.active').removeClass('active');
  117. $('.inbox-header > h1').text('Compose');
  118. loading.hide();
  119. content.html(res);
  120. initFileupload();
  121. initWysihtml5();
  122. $('.inbox-wysihtml5').focus();
  123. Layout.fixContentHeight();
  124. App.initUniform();
  125. },
  126. error: function(xhr, ajaxOptions, thrownError)
  127. {
  128. toggleButton(el);
  129. },
  130. async: false
  131. });
  132. }
  133. var loadReply = function (el) {
  134. var messageid = $(el).attr("data-messageid");
  135. var url = 'app_inbox_reply.html&messageid=' + messageid;
  136. loading.show();
  137. content.html('');
  138. toggleButton(el);
  139. // load the form via ajax
  140. $.ajax({
  141. type: "GET",
  142. cache: false,
  143. url: url,
  144. dataType: "html",
  145. success: function(res)
  146. {
  147. toggleButton(el);
  148. $('.inbox-nav > li.active').removeClass('active');
  149. $('.inbox-header > h1').text('Reply');
  150. loading.hide();
  151. content.html(res);
  152. $('[name="message"]').val($('#reply_email_content_body').html());
  153. handleCCInput(); // init "CC" input field
  154. initFileupload();
  155. initWysihtml5();
  156. Layout.fixContentHeight();
  157. App.initUniform();
  158. },
  159. error: function(xhr, ajaxOptions, thrownError)
  160. {
  161. toggleButton(el);
  162. },
  163. async: false
  164. });
  165. }
  166. var loadSearchResults = function (el) {
  167. var url = 'app_inbox_inbox.html';
  168. loading.show();
  169. content.html('');
  170. toggleButton(el);
  171. $.ajax({
  172. type: "GET",
  173. cache: false,
  174. url: url,
  175. dataType: "html",
  176. success: function(res)
  177. {
  178. toggleButton(el);
  179. $('.inbox-nav > li.active').removeClass('active');
  180. $('.inbox-header > h1').text('Search');
  181. loading.hide();
  182. content.html(res);
  183. Layout.fixContentHeight();
  184. App.initUniform();
  185. },
  186. error: function(xhr, ajaxOptions, thrownError)
  187. {
  188. toggleButton(el);
  189. },
  190. async: false
  191. });
  192. }
  193. var handleCCInput = function () {
  194. var the = $('.inbox-compose .mail-to .inbox-cc');
  195. var input = $('.inbox-compose .input-cc');
  196. the.hide();
  197. input.show();
  198. $('.close', input).click(function () {
  199. input.hide();
  200. the.show();
  201. });
  202. }
  203. var handleBCCInput = function () {
  204. var the = $('.inbox-compose .mail-to .inbox-bcc');
  205. var input = $('.inbox-compose .input-bcc');
  206. the.hide();
  207. input.show();
  208. $('.close', input).click(function () {
  209. input.hide();
  210. the.show();
  211. });
  212. }
  213. var toggleButton = function(el) {
  214. if (typeof el == 'undefined') {
  215. return;
  216. }
  217. if (el.attr("disabled")) {
  218. el.attr("disabled", false);
  219. } else {
  220. el.attr("disabled", true);
  221. }
  222. }
  223. return {
  224. //main function to initiate the module
  225. init: function () {
  226. // handle compose btn click
  227. $('.inbox').on('click', '.compose-btn a', function () {
  228. loadCompose($(this));
  229. });
  230. // handle discard btn
  231. $('.inbox').on('click', '.inbox-discard-btn', function(e) {
  232. e.preventDefault();
  233. loadInbox($(this), listListing);
  234. });
  235. // handle reply and forward button click
  236. $('.inbox').on('click', '.reply-btn', function () {
  237. loadReply($(this));
  238. });
  239. // handle view message
  240. $('.inbox-content').on('click', '.view-message', function () {
  241. loadMessage($(this));
  242. });
  243. // handle inbox listing
  244. $('.inbox-nav > li.inbox > a').click(function () {
  245. loadInbox($(this), 'inbox');
  246. });
  247. // handle sent listing
  248. $('.inbox-nav > li.sent > a').click(function () {
  249. loadInbox($(this), 'sent');
  250. });
  251. // handle draft listing
  252. $('.inbox-nav > li.draft > a').click(function () {
  253. loadInbox($(this), 'draft');
  254. });
  255. // handle trash listing
  256. $('.inbox-nav > li.trash > a').click(function () {
  257. loadInbox($(this), 'trash');
  258. });
  259. //handle compose/reply cc input toggle
  260. $('.inbox-content').on('click', '.mail-to .inbox-cc', function () {
  261. handleCCInput();
  262. });
  263. //handle compose/reply bcc input toggle
  264. $('.inbox-content').on('click', '.mail-to .inbox-bcc', function () {
  265. handleBCCInput();
  266. });
  267. //handle loading content based on URL parameter
  268. if (App.getURLParameter("a") === "view") {
  269. loadMessage();
  270. } else if (App.getURLParameter("a") === "compose") {
  271. loadCompose();
  272. } else {
  273. $('.inbox-nav > li.inbox > a').click();
  274. }
  275. }
  276. };
  277. }();
  278. jQuery(document).ready(function() {
  279. AppInbox.init();
  280. });