You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

161 line
4.5 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // EMAIL
  23. // Autosuggest defaults
  24. _e.email_as_field = 'email_id';
  25. _e.email_as_dt = 'Contact';
  26. _e.email_as_in = 'email_id,contact_name';
  27. sendmail = function(emailto, emailfrom, cc, subject, message, fmt, with_attachments) {
  28. var fn = function(html) {
  29. $c('webnotes.utils.email_lib.send_form', {
  30. 'sendto':emailto,
  31. 'sendfrom': emailfrom?emailfrom:'',
  32. 'cc':cc?cc:'',
  33. 'subject':subject,
  34. 'message':replace_newlines(message),
  35. 'body':html,
  36. 'full_domain': wn.urllib.get_base_url(),
  37. 'with_attachments':with_attachments ? 1 : 0,
  38. 'dt':cur_frm.doctype,
  39. 'dn':cur_frm.docname
  40. },
  41. function(r, rtxt) {
  42. //
  43. }
  44. );
  45. }
  46. // build print format
  47. _p.build(fmt, fn);
  48. }
  49. _e.make = function() {
  50. var d = new Dialog(440, 440, "Send Email");
  51. var email_go = function() {
  52. var emailfrom = d.widgets['From'].value;
  53. var emailto = d.widgets['To'].value;
  54. if(!emailfrom)
  55. emailfrom = user_email;
  56. // validate email ids
  57. var email_list = emailto.split(/[,|;]/);
  58. var valid = 1;
  59. for(var i=0;i<email_list.length;i++){
  60. if(!validate_email(email_list[i])) {
  61. msgprint('error:'+email_list[i] + ' is not a valid email id');
  62. valid = 0;
  63. }
  64. }
  65. // validate from
  66. if(emailfrom && !validate_email(emailfrom)) {
  67. msgprint('error:'+ emailfrom + ' is not a valid email id. To change the default please click on Profile on the top right of the screen and change it.');
  68. return;
  69. }
  70. if(!valid)return;
  71. var cc= emailfrom;
  72. if(!emailfrom) {
  73. emailfrom = wn.control_panel.auto_email_id;
  74. cc = '';
  75. }
  76. sendmail(emailto, emailfrom, emailfrom, d.widgets['Subject'].value, d.widgets['Message'].value, sel_val(cur_frm.print_sel), d.widgets['Send With Attachments'].checked);
  77. _e.dialog.hide();
  78. }
  79. d.onhide = function() {
  80. }
  81. d.make_body([
  82. ['Data','To','Example: abc@hotmail.com, xyz@yahoo.com']
  83. ,['Select','Format']
  84. ,['Data','Subject']
  85. ,['Data','From','Optional']
  86. ,['Check','Send With Attachments','Will send all attached documents (if any)']
  87. ,['Text','Message']
  88. ,['Button','Send',email_go]]
  89. );
  90. d.widgets['From'].value = (user_email ? user_email:'');
  91. $td(d.rows['Format'].tab,0,1).cur_sel = d.widgets['Format'];
  92. function split( val ) {
  93. return val.split( /,\s*/ );
  94. }
  95. function extractLast( term ) {
  96. return split(term).pop();
  97. }
  98. $(d.widgets['To'])
  99. .bind( "keydown", function(event) {
  100. if (event.keyCode === $.ui.keyCode.TAB &&
  101. $(this).data( "autocomplete" ).menu.active ) {
  102. event.preventDefault();
  103. }
  104. })
  105. .autocomplete({
  106. source: function(request, response) {
  107. wn.call({
  108. method:'webnotes.utils.email_lib.get_contact_list',
  109. args: {
  110. 'select': _e.email_as_field,
  111. 'from': _e.email_as_dt,
  112. 'where': _e.email_as_in,
  113. 'txt': extractLast(request.term).value || '%'
  114. },
  115. callback: function(r) {
  116. response($.ui.autocomplete.filter(
  117. r.cl || [], extractLast(request.term)));
  118. }
  119. });
  120. },
  121. focus: function() {
  122. // prevent value inserted on focus
  123. return false;
  124. },
  125. select: function( event, ui ) {
  126. var terms = split( this.value );
  127. // remove the current input
  128. terms.pop();
  129. // add the selected item
  130. terms.push( ui.item.value );
  131. // add placeholder to get the comma-and-space at the end
  132. terms.push( "" );
  133. this.value = terms.join( ", " );
  134. return false;
  135. }
  136. });
  137. _e.dialog = d;
  138. }