blob: 70a08affecdf1752ff9a03cd64c3eaf034bca668 [file] [log] [blame]
Rushabh Mehtac4e7b682012-11-26 18:18:10 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Rushabh Mehta80127bf2012-11-27 18:41:35 +053017// opts - parent, list, doc, email
Rushabh Mehtac4e7b682012-11-26 18:18:10 +053018erpnext.CommunicationView = Class.extend({
19 init: function(opts) {
20 this.comm_list = [];
21 $.extend(this, opts);
22
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +053023 if(this.doc.__islocal) {
24 return;
25 }
26
Anand Doshi319ac972012-11-27 13:30:03 +053027 this.list.sort(function(a, b) { return
28 (new Date(a.modified) > new Date(b.modified))
29 ? -1 : 1; })
Rushabh Mehtac4e7b682012-11-26 18:18:10 +053030
31 this.make();
32 },
33 make: function() {
34 var me = this;
35 this.make_body();
Rushabh Mehta80127bf2012-11-27 18:41:35 +053036
37 if(this.list && this.list.length) {
38 $.each(this.list, function(i, d) {
39 me.prepare(d);
40 me.make_line(d);
41 });
42 // show first
43 this.comm_list[0].find('.comm-content').toggle(true);
44 } else {
45 this.body.remove()
46 $("<div class='alert'>No Communication with this "
47 + this.doc.doctype +" yet.</div>").appendTo(this.wrapper);
48 }
49
Rushabh Mehtac4e7b682012-11-26 18:18:10 +053050 },
51 make_body: function() {
52 $(this.parent)
53 .html("")
54 .css({"margin":"10px 0px"});
55
56 this.wrapper = $("<div><h4>Communication History</h4>\
Rushabh Mehta80127bf2012-11-27 18:41:35 +053057 <div style='margin-bottom: 8px;'>\
58 <button class='btn btn-small' \
59 onclick='cur_frm.communication_view.add_reply()'>\
60 <i class='icon-plus'></i> Add Reply</button></div>\
61 </div>")
Rushabh Mehtac4e7b682012-11-26 18:18:10 +053062 .appendTo(this.parent);
63
64 this.body = $("<table class='table table-bordered table-hover table-striped'>")
65 .appendTo(this.wrapper);
66 },
Rushabh Mehta80127bf2012-11-27 18:41:35 +053067 add_reply: function() {
68 var me = this;
69 var d = new wn.ui.Dialog({
70 width: 640,
71 title: "Add Reply: " + (this.doc.subject || ""),
72 fields: [
73 {label:"Subject", fieldtype:"Data", reqd: 1},
74 {label:"Message", fieldtype:"Text Editor", reqd: 1, fieldname:"content"},
75 {label:"Send Email", fieldtype:"Check"},
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +053076 {label:"Add Reply", fieldtype:"Button"},
Rushabh Mehta80127bf2012-11-27 18:41:35 +053077 ]
78 });
79
80 $(d.fields_dict.send_email.input).attr("checked", "checked")
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +053081 $(d.fields_dict.add_reply.input).click(function() {
Rushabh Mehta80127bf2012-11-27 18:41:35 +053082 var args = d.get_values();
83 if(!args) return;
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +053084 $(this).set_working();
Rushabh Mehta80127bf2012-11-27 18:41:35 +053085 wn.call({
86 method:"support.doctype.communication.communication.make",
87 args: $.extend(args, {
88 doctype: me.doc.doctype,
89 name: me.doc.name,
90 lead: me.doc.lead,
91 contact: me.doc.contact,
92 recipients: me.email
93 }),
94 callback: function(r) {
95 d.hide();
96 cur_frm.reload_doc();
97 }
98 });
99 });
100
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +0530101 if(me.list.length > 0) {
102 d.fields_dict.content.input.set_input("<p></p>"
103 + (wn.boot.profile.email_signature || "")
104 +"<p></p>"
105 +"-----In response to-----<p></p>"
106 + me.list[0].content)
107 } else {
108
109 }
Rushabh Mehta80127bf2012-11-27 18:41:35 +0530110 $(d.fields_dict.subject.input).val(this.doc.subject || "").change();
111
112 d.show();
113 },
114
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530115 prepare: function(doc) {
116 //doc.when = comment_when(this.doc.modified);
117 doc.when = doc.modified;
118 if(doc.content.indexOf("<br>")== -1 && doc.content.indexOf("<p>")== -1) {
119 doc.content = doc.content.replace(/\n/g, "<br>");
120 }
Rushabh Mehta80127bf2012-11-27 18:41:35 +0530121 if(!doc.sender) doc.sender = "[unknown sender]";
122 doc.sender = doc.sender.replace(/</, "&lt;").replace(/>/, "&gt;");
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +0530123 doc.content = doc.content.split("-----In response to-----")[0];
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530124 doc.content = doc.content.split("-----Original Message-----")[0];
125 },
126 make_line: function(doc) {
127 var me = this;
128 var comm = $(repl('<tr><td title="Click to Expand / Collapse">\
Rushabh Mehta80127bf2012-11-27 18:41:35 +0530129 <p><b>%(sender)s on %(when)s</b> \
130 <a href="#Form/Communication/%(name)s" style="font-size: 90%">\
131 Show Details</a></p>\
Rushabh Mehtacee2b8f2012-11-28 10:47:51 +0530132 <div class="comm-content" style="border-top: 1px solid #ddd; \
133 padding: 10px; overflow-x: auto; display: none;"></div>\
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530134 </td></tr>', doc))
135 .appendTo(this.body)
136 .css({"cursor":"pointer"})
137 .click(function() {
138 $(this).find(".comm-content").toggle();
139 });
140
141 this.comm_list.push(comm);
142 comm.find(".comm-content").html(doc.content);
143 }
144})