// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. // MIT License. See license.txt // opts - parent, list, doc, email wn.views.CommunicationList = Class.extend({ init: function(opts) { this.comm_list = []; $.extend(this, opts); if(this.doc.__islocal) { $(this.parent).empty(); return; } if(!this.list) this.list = wn.model.get("Communication", {"parenttype": this.doc.doctype, "parent": this.doc.name}); var sortfn = function (a, b) { return (b.creation > a.creation) ? 1 : -1; } this.list = this.list.sort(sortfn); this.make(); }, make: function() { var me = this; this.make_body(); if(this.list && this.list.length) { $.each(this.list, function(i, d) { me.prepare(d); me.make_line(d); }); // show first this.comm_list[0].find('.comm-content').toggle(true); } else { this.clear_list() } }, clear_list: function() { this.body.remove(); $("
" + wn._("No Communication tagged with this ") + this.doc.doctype +" yet.
").appendTo(this.wrapper); }, make_body: function() { $(this.parent) .empty() this.wrapper = $(""+wn._("Add Attachments")+":
").appendTo(attach.empty()); $.each(files, function(i, f) { $(repl("%(file)s
", {file:f})).appendTo(attach) }); } }, setup_email: function() { // email var me = this; var fields = this.dialog.fields_dict; if(this.attach_document_print) { $(fields.send_me_a_copy.input).click(); $(fields.attach_document_print.input).click(); $(fields.select_print_format.wrapper).toggle(true); } $(fields.send_email.input).prop("checked", true) // toggle print format $(fields.send_email.input).click(function() { $(fields.communication_medium.wrapper).toggle(!!!$(this).prop("checked")); $(fields.sent_or_received.wrapper).toggle(!!!$(this).prop("checked")); $(fields.send.input).html($(this).prop("checked") ? "Send" : "Add Communication"); }); // select print format $(fields.communication_medium.wrapper).toggle(false); $(fields.sent_or_received.wrapper).toggle(false); $(fields.send.input).click(function() { var btn = this; var form_values = me.dialog.get_values(); if(!form_values) return; var selected_attachments = $.map($(me.dialog.wrapper) .find("[data-file-name]:checked"), function(element) { return $(element).attr("data-file-name"); }) if(form_values.attach_document_print) { _p.build(form_values.select_print_format || "", function(print_format_html) { me.send_email(btn, form_values, selected_attachments, print_format_html); }); } else { me.send_email(btn, form_values, selected_attachments); } }); }, send_email: function(btn, form_values, selected_attachments, print_format_html) { var me = this; if(form_values.attach_document_print) { var print_html = print_format_html; if(cint(wn.boot.send_print_in_body_and_attachment)) { form_values.content = form_values.content + "" + "Please see attachment for document details.
" } } else { var print_html = ""; } if(form_values.send_email) { form_values.communication_medium = "Email"; form_values.sent_or_received = "Sent"; }; return wn.call({ method:"core.doctype.communication.communication.make", args: { sender: [wn.user_info(user).fullname, wn.boot.profile.email], recipients: form_values.recipients, subject: form_values.subject, content: form_values.content, doctype: me.doc.doctype, name: me.doc.name, send_me_a_copy: form_values.send_me_a_copy, send_email: form_values.send_email, print_html: print_html, communication_medium: form_values.communication_medium, sent_or_received: form_values.sent_or_received, attachments: selected_attachments }, btn: btn, callback: function(r) { if(!r.exc) { if(form_values.send_email) msgprint("Email sent to " + form_values.recipients); me.dialog.hide(); cur_frm.reload_doc(); } else { msgprint("There were errors while sending email. Please try again.") } } }); }, setup_earlier_reply: function() { var fields = this.dialog.fields_dict; var comm_list = cur_frm.communication_view ? cur_frm.communication_view.list : []; var signature = wn.boot.profile.email_signature || ""; if(!wn.utils.is_html(signature)) { signature = signature.replace(/\n/g, "'+wn._('Dear') +' ' + this.real_name + ",
" + (this.message || ""); } var reply = (this.message || "") + "" + signature; if(comm_list.length > 0) { fields.content.set_input(reply + "" +"-----"+wn._("In response to")+"-----" + comm_list[0].content); } else { fields.content.set_input(reply); } }, setup_autosuggest: function() { var me = this; function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split(term).pop(); } $(this.dialog.fields_dict.recipients.input) .bind( "keydown", function(event) { if (event.keyCode === $.ui.keyCode.TAB && $(this).data( "autocomplete" ).menu.active ) { event.preventDefault(); } }) .autocomplete({ source: function(request, response) { return wn.call({ method:'webnotes.utils.email_lib.get_contact_list', args: { 'select': "email_id", 'from': "Contact", 'where': "email_id", 'txt': extractLast(request.term).value || '%' }, callback: function(r) { response($.ui.autocomplete.filter( r.cl || [], extractLast(request.term))); } }); }, appendTo: this.dialog.$wrapper, focus: function() { // prevent value inserted on focus return false; }, select: function( event, ui ) { var terms = split( this.value ); // remove the current input terms.pop(); // add the selected item terms.push( ui.item.value ); // add placeholder to get the comma-and-space at the end terms.push( "" ); this.value = terms.join( ", " ); return false; } }); } });