inbox.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. var Inbox = function () {
  2. var main_content = $('.emailContent');
  3. /*-----------------------------------------------------------------------------------*/
  4. /* Show single email view
  5. /*-----------------------------------------------------------------------------------*/
  6. var showSingleEmail = function (el, name, resetMenu) {
  7. var url = 'inbox_email.html';
  8. main_content.html('');
  9. toggleButton(el);
  10. $.ajax({
  11. type: "GET",
  12. cache: false,
  13. url: url,
  14. dataType: "html",
  15. success: function(res)
  16. {
  17. toggleButton(el);
  18. main_content.html(res);
  19. App.initUniform();
  20. },
  21. error: function(xhr, ajaxOptions, thrownError)
  22. {
  23. toggleButton(el);
  24. },
  25. async: false
  26. });
  27. }
  28. /*-----------------------------------------------------------------------------------*/
  29. /* Show WYSIWYG Editor
  30. /*-----------------------------------------------------------------------------------*/
  31. var showWYSIWYG = function () {
  32. function initToolbarBootstrapBindings() {
  33. var fonts = ['Serif', 'Sans', 'Arial', 'Arial Black', 'Courier',
  34. 'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande', 'Lucida Sans', 'Tahoma', 'Times',
  35. 'Times New Roman', 'Verdana'],
  36. fontTarget = $('[title=Font]').siblings('.dropdown-menu');
  37. $.each(fonts, function (idx, fontName) {
  38. fontTarget.append($('<li><a data-edit="fontName ' + fontName +'" style="font-family:\''+ fontName +'\'">'+fontName + '</a></li>'));
  39. });
  40. $('a[title]').tooltip({container:'body'});
  41. $('.dropdown-menu input').click(function() {return false;})
  42. .change(function () {$(this).parent('.dropdown-menu').siblings('.dropdown-toggle').dropdown('toggle');})
  43. .keydown('esc', function () {this.value='';$(this).change();});
  44. $('[data-role=magic-overlay]').each(function () {
  45. var overlay = $(this), target = $(overlay.data('target'));
  46. overlay.css('opacity', 0).css('position', 'absolute').offset(target.offset()).width(target.outerWidth()).height(target.outerHeight());
  47. });
  48. if ("onwebkitspeechchange" in document.createElement("input")) {
  49. var editorOffset = $('#editor').offset();
  50. $('#voiceBtn').css('position','absolute').offset({top: editorOffset.top, left: editorOffset.left+$('#editor').innerWidth()-35});
  51. } else {
  52. $('#voiceBtn').hide();
  53. }
  54. };
  55. function showErrorAlert (reason, detail) {
  56. var msg='';
  57. if (reason==='unsupported-file-type') { msg = "Unsupported format " +detail; }
  58. else {
  59. console.log("error uploading file", reason, detail);
  60. }
  61. $('<div class="alert"> <button type="button" class="close" data-dismiss="alert">&times;</button>'+
  62. '<strong>File upload error</strong> '+msg+' </div>').prependTo('#alerts');
  63. };
  64. initToolbarBootstrapBindings();
  65. $('#editor').wysiwyg({ fileUploadError: showErrorAlert} );
  66. }
  67. /*-----------------------------------------------------------------------------------*/
  68. /* Show Inbox view
  69. /*-----------------------------------------------------------------------------------*/
  70. var showInbox = function (el, name) {
  71. var url = 'inbox_main.html';
  72. main_content.html('');
  73. toggleButton(el);
  74. $.ajax({
  75. type: "GET",
  76. cache: false,
  77. url: url,
  78. dataType: "html",
  79. success: function(res)
  80. {
  81. toggleButton(el);
  82. main_content.html(res);
  83. App.initUniform();
  84. },
  85. error: function(xhr, ajaxOptions, thrownError)
  86. {
  87. toggleButton(el);
  88. },
  89. async: false
  90. });
  91. }
  92. /*-----------------------------------------------------------------------------------*/
  93. /* Show email reply view
  94. /*-----------------------------------------------------------------------------------*/
  95. var showEmailReply = function (el) {
  96. var url = 'inbox_email_reply.html';
  97. main_content.html('');
  98. toggleButton(el);
  99. $.ajax({
  100. type: "GET",
  101. cache: false,
  102. url: url,
  103. dataType: "html",
  104. success: function(res)
  105. {
  106. toggleButton(el);
  107. main_content.html(res);
  108. handleCCControl();
  109. handleCCBCC();
  110. showWYSIWYG();
  111. $('#editor').html($('#reply-content').html());
  112. $('#reply-content').hide();
  113. App.initUniform();
  114. $('#editor').focus();
  115. },
  116. error: function(xhr, ajaxOptions, thrownError)
  117. {
  118. toggleButton(el);
  119. },
  120. async: false
  121. });
  122. }
  123. /*-----------------------------------------------------------------------------------*/
  124. /* Show Compose view
  125. /*-----------------------------------------------------------------------------------*/
  126. var showCompose = function (el) {
  127. var url = 'inbox_compose.html';
  128. main_content.html('');
  129. toggleButton(el);
  130. $.ajax({
  131. type: "GET",
  132. cache: false,
  133. url: url,
  134. dataType: "html",
  135. success: function(res)
  136. {
  137. toggleButton(el);
  138. main_content.html(res);
  139. showWYSIWYG();
  140. handleCCBCC();
  141. $('#editor').focus();
  142. App.initUniform();
  143. },
  144. error: function(xhr, ajaxOptions, thrownError)
  145. {
  146. toggleButton(el);
  147. },
  148. async: false
  149. });
  150. }
  151. /*-----------------------------------------------------------------------------------*/
  152. /* Show Compose view
  153. /*-----------------------------------------------------------------------------------*/
  154. var handleCCBCC = function () {
  155. $('.emailCompose .address').on('click', '.emailCC', function () {
  156. handleCCControl();
  157. });
  158. $('.emailCompose .address').on('click', '.emailBCC', function () {
  159. handleBCCControl();
  160. });
  161. }
  162. /*-----------------------------------------------------------------------------------*/
  163. /* Show Compose view
  164. /*-----------------------------------------------------------------------------------*/
  165. var handleCCControl = function () {
  166. var the = $('.emailCompose .address .emailCC');
  167. var input = $('.emailCompose .inputCC');
  168. the.hide();
  169. input.show();
  170. $('.close', input).click(function () {
  171. input.hide();
  172. the.show();
  173. });
  174. }
  175. /*-----------------------------------------------------------------------------------*/
  176. /* Show Compose view
  177. /*-----------------------------------------------------------------------------------*/
  178. var handleBCCControl = function () {
  179. var the = $('.emailCompose .address .emailBCC');
  180. var input = $('.emailCompose .inputBCC');
  181. the.hide();
  182. input.show();
  183. $('.close', input).click(function () {
  184. input.hide();
  185. the.show();
  186. });
  187. }
  188. /*-----------------------------------------------------------------------------------*/
  189. /* Toggle button
  190. /*-----------------------------------------------------------------------------------*/
  191. var toggleButton = function(el) {
  192. if (typeof el == 'undefined') {
  193. return;
  194. }
  195. if (el.attr("disabled")) {
  196. el.attr("disabled", false);
  197. } else {
  198. el.attr("disabled", true);
  199. }
  200. }
  201. return {
  202. init: function () {
  203. /* Show main inbox screen */
  204. $('.emailNav > li.inbox > a').click(function () {
  205. showInbox($(this), 'inbox');
  206. });
  207. /* Show compose screen */
  208. $('.email .composeBtn').on('click', 'a', function () {
  209. showCompose($(this));
  210. });
  211. /* Show email reply screen */
  212. $('.email').on('click', '.replyBtn', function () {
  213. showEmailReply($(this));
  214. });
  215. /* Show single email screen */
  216. $('.emailContent').on('click', '.viewEmail', function () {
  217. showSingleEmail($(this));
  218. });
  219. /* Handle CC control links */
  220. $('.emailCompose .address').on('click', '.emailCC', function () {
  221. handleCCControl();
  222. });
  223. /* Handle BCC control links */
  224. $('.emailCompose .address').on('click', '.emailBCC', function () {
  225. handleBCCControl();
  226. });
  227. /* Show main inbox for the first load */
  228. $('.emailNav > li.inbox > a').click();
  229. }
  230. };
  231. }();