main.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /** SETTINGS **/
  2. //Enregistrement des configurations
  3. function sendmail_setting_save(){
  4. $.action({
  5. action: 'sendmail_setting_save',
  6. fields: $('#sendmail-setting-form').toJson()
  7. },function(){
  8. $.message('success','Enregistré');
  9. });
  10. }
  11. /** USER SETTINGS **/
  12. //Enregistrement des préférences utilisateur
  13. function sendmail_user_save_preference(){
  14. $.action({
  15. action: 'sendmail_user_save_preference',
  16. preferences: $('#sendmail-user-setting-form').toJson()
  17. },function(){
  18. $.message('success','Enregistré');
  19. });
  20. }
  21. /** MAIL **/
  22. //Ajout d'un contact
  23. function sendmail_contact_add(from,data){
  24. var modal = $('#sendmail-modal');
  25. if(!from) from = $('.recipientLine:eq(0)',modal);
  26. recipientVal = $('.recipient',from).val();
  27. var clone = recipientVal != '' ? from.clone() : from;
  28. if(recipientVal != '') clone.addClass('offset-md-2');
  29. clone.find('.typeahead.dropdown-menu').remove();
  30. var component = clone.find('select[data-type="dropdown-select"]');
  31. if(recipientVal != '') clone.find('div.recipient-type.data-type-dropdown-select').remove();
  32. component.removeClass('hidden');
  33. init_components(component);
  34. var input = clone.find('.recipient');
  35. input.val('');
  36. $('.recipientBox',modal).append(clone);
  37. input.focus();
  38. var type = from.find('.recipient-type .dropdown-menu .dropdown-item.active').attr('data-value');
  39. clone.find('.recipient-type').val(type);
  40. if(data){
  41. input.val(data.mail);
  42. clone.find('.recipient-type').val(data.type);
  43. }
  44. var options = $('#sendmail-modal').data('data');
  45. if(options && options.recipientComplete) sendmail_autocomplete(options.recipientComplete);
  46. init_components('#sendmail-modal');
  47. }
  48. //Suppression d'un contact
  49. function sendmail_contact_delete(from){
  50. var modal = $('#sendmail-modal');
  51. if($('.recipientLine',modal).length<=1){
  52. $('.recipientLine .recipient',modal).val('').focus();
  53. return;
  54. }
  55. $(from).closest('.recipientLine').remove();
  56. $('.recipientLine:first-of-type',modal).removeClass('offset-md-2');
  57. }
  58. //Envoi d'un mail
  59. function sendmail_send(data,callback){
  60. if(isProcessing) return;
  61. isProcessing = true;
  62. var modal = $('#sendmail-modal');
  63. if(!$('#subject',modal).val().length && !confirm("Le message n'a pas de sujet.\nEnvoyer quand-même ?")) return;
  64. if($('.btn-send', modal).hasClass('btn-preloader')) return;
  65. $('.btn-send', modal).addClass('btn-preloader');
  66. var recipients = {'to':[],'cc':[],'cci':[]};
  67. $('.recipientBox .recipientLine',modal).each(function(i,element){
  68. var line = $(element);
  69. var input = line.find('.recipient');
  70. if(!input.val().length) return;
  71. recipients[line.find('select.recipient-type').val()].push(input.val());
  72. });
  73. var attachments = $('#attachments_temporary',modal).val().length ? JSON.parse($('#attachments_temporary',modal).val()) : [];
  74. var data = $.extend(modal.data('data'),{
  75. action: 'sendmail_mail_send',
  76. from: $('#from',modal).val(),
  77. origin: data.origin,
  78. recipient: recipients,
  79. subject: $('#subject',modal).val(),
  80. files: JSON.stringify(attachments),
  81. message: $('#message',modal).val(),
  82. });
  83. $.action(data,function(r){
  84. $.message('info', r.message);
  85. modal.modal('hide');
  86. isProcessing = false;
  87. if(callback){
  88. if(typeof callback == 'string' && window[callback]!=null){
  89. window[callback](r);
  90. }else{
  91. callback(r);
  92. }
  93. }
  94. }, function(){
  95. isProcessing = false;
  96. });
  97. }
  98. function sendmail_autocomplete(data){
  99. var modal = $('#sendmail-modal');
  100. $('.recipient',modal).autocomplete({
  101. action : data.action,
  102. data : data.data,
  103. force : false,
  104. skin : function(item,input){
  105. var re = new RegExp(input.val(),"gi");
  106. name = item.name.replace(re, function(x) {
  107. return '<strong>'+x+'</strong>';
  108. });
  109. mail = item.mail ? item.mail.replace(re, function(x) {
  110. return '<strong>'+x+'</strong>';
  111. }) : '';
  112. return '<span class="sendmail-autocomplete-line"><span class="sendmail-autocomplete-label">'+name+
  113. '</span> <span class="sendmail-autocomplete-mail">&lt;'+mail+'&gt;</span></span>';
  114. },
  115. highlight : function(item){
  116. return item;
  117. },
  118. onClick : function(selected,element){
  119. element.val(selected.id);
  120. sendmail_contact_add(element.closest('.recipientLine'));
  121. element.closest('.recipientLine').next('li').focus();
  122. }
  123. });
  124. }
  125. //Edition d'un mail
  126. /* Le parametre data peut contenir les parametres suivants :
  127. * from : valeur de l'adresse de l'expediteur à l'affichage
  128. * subject : valeur de l'objet à l'affichage
  129. * message : valeur du message à l'affichage
  130. * attachments : pièces jointes a afficher lors de la preview au format
  131. [{
  132. path : 'contact/documents/12/myFile.docx',
  133. url : 'action.php?action=contact_download_document&path=myfile.docx',
  134. name : 'myFile.docx',
  135. icon : 'far fa-file-archive text-warning'
  136. }]
  137. * signature (true/false): si la signature est à false, on ajoutera pas automatiquement la signature par défaut au message (défaut true)
  138. * recipients : auto-rempli les destinataires, le parametre doit être au format {'to':[],'cc':[],'cci':[]};
  139. * recipientComplete : Si une action est spécifiée, l'autocomplete sur les récipient est activé, l'action recoit le parametre "keyword" et doit retourner les
  140. résultats au format: { rows : [{id:'mail@mail.com',name:'John DOE','mail':'mail@mail.com'}]
  141. */
  142. function sendmail_preview(data, onPreviewComplete, onSendComplete){
  143. $('#sendmail-modal').remove();
  144. if(!data) data = {};
  145. $(window).on('shown.bs.modal', function (e) {
  146. var modal = $('#sendmail-modal');
  147. if(data.attachments){
  148. $('#attachments', modal).replaceWith(('<div data-type="dropzone" data-label="Faites glisser vos pièces jointes ici" data-delete="dropzone_delete_file" class="form-control w-100 h-100 mb-2" id="attachments" name="attachments"></div>'));
  149. $('#attachments', modal).html(JSON.stringify(data.attachments));
  150. }
  151. init_components(modal);
  152. //On set les documents en PJ pré-settés pour gérer la suppression par la suite
  153. if(data.attachments) $('#attachments_temporary', modal).val(JSON.stringify(data.attachments));
  154. if(onPreviewComplete) onPreviewComplete(data);
  155. });
  156. $.ajax({
  157. url: 'action.php?action=sendmail_mail_preview',
  158. data: data,
  159. method : 'POST',
  160. success: function(response){
  161. $('body').append(response);
  162. var modal = $('#sendmail-modal');
  163. if(data.recipients){
  164. var types = ['to','cc','cci'];
  165. for (var u in types) {
  166. var type = types[u];
  167. for (var i in data.recipients[type]) {
  168. sendmail_contact_add(null,{
  169. type : type,
  170. mail : data.recipients[type][i]
  171. });
  172. }
  173. }
  174. }
  175. modal.modal('show');
  176. modal.data('data',data);
  177. var form = $('#sendForm',modal);
  178. form.on('click','.recipientBox .recipient-type .dropdown-menu a',function(){
  179. var target = $(this);
  180. var recipientBox = target.closest('.recipientBox');
  181. $('.recipient', recipientBox).attr('data-type',target.attr('data-value')).focus();
  182. recipientBox.find('.recipientType').text(target.html());
  183. }).on('keydown','.recipient',function(e){
  184. if(['+',',',';','Enter'].indexOf(e.key)>=0) {
  185. e.preventDefault();
  186. sendmail_contact_add($(this).closest('.recipientLine'));
  187. }
  188. });
  189. if(data.recipientComplete) sendmail_autocomplete(data.recipientComplete);
  190. modal.on('keyup', 'input, textarea, *[contenteditable="true"]', function(e){
  191. //Press Del (touche "Suppr")
  192. if(e.keyCode == 46) e.stopPropagation();
  193. })
  194. $('.btn-send',modal).click(function(){
  195. sendmail_send(data,onSendComplete);
  196. });
  197. $('.trumbowyg-editor', modal).on('keyup',function(event){
  198. //Press Ctrl + Enter
  199. if(event.keyCode == 13 && event.ctrlKey)
  200. $(this).trigger('insertCarriageReturn');
  201. });
  202. }
  203. });
  204. }