blob: ee6cce214c784a5cd9d43b78a8ee98853a16c116 [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
23 this.list.sort(function(a, b) { return new Date(a.modified) > new Date(b.modified)
24 ? -1 : 1 })
25
26 this.make();
27 },
28 make: function() {
29 var me = this;
30 this.make_body();
Rushabh Mehta80127bf2012-11-27 18:41:35 +053031
32 if(this.list && this.list.length) {
33 $.each(this.list, function(i, d) {
34 me.prepare(d);
35 me.make_line(d);
36 });
37 // show first
38 this.comm_list[0].find('.comm-content').toggle(true);
39 } else {
40 this.body.remove()
41 $("<div class='alert'>No Communication with this "
42 + this.doc.doctype +" yet.</div>").appendTo(this.wrapper);
43 }
44
Rushabh Mehtac4e7b682012-11-26 18:18:10 +053045 },
46 make_body: function() {
47 $(this.parent)
48 .html("")
49 .css({"margin":"10px 0px"});
50
51 this.wrapper = $("<div><h4>Communication History</h4>\
Rushabh Mehta80127bf2012-11-27 18:41:35 +053052 <div style='margin-bottom: 8px;'>\
53 <button class='btn btn-small' \
54 onclick='cur_frm.communication_view.add_reply()'>\
55 <i class='icon-plus'></i> Add Reply</button></div>\
56 </div>")
Rushabh Mehtac4e7b682012-11-26 18:18:10 +053057 .appendTo(this.parent);
58
59 this.body = $("<table class='table table-bordered table-hover table-striped'>")
60 .appendTo(this.wrapper);
61 },
Rushabh Mehta80127bf2012-11-27 18:41:35 +053062 add_reply: function() {
63 var me = this;
64 var d = new wn.ui.Dialog({
65 width: 640,
66 title: "Add Reply: " + (this.doc.subject || ""),
67 fields: [
68 {label:"Subject", fieldtype:"Data", reqd: 1},
69 {label:"Message", fieldtype:"Text Editor", reqd: 1, fieldname:"content"},
70 {label:"Send Email", fieldtype:"Check"},
71 {label:"Send", fieldtype:"Button"},
72 ]
73 });
74
75 $(d.fields_dict.send_email.input).attr("checked", "checked")
76 $(d.fields_dict.send.input).click(function() {
77 var args = d.get_values();
78 if(!args) return;
79 wn.call({
80 method:"support.doctype.communication.communication.make",
81 args: $.extend(args, {
82 doctype: me.doc.doctype,
83 name: me.doc.name,
84 lead: me.doc.lead,
85 contact: me.doc.contact,
86 recipients: me.email
87 }),
88 callback: function(r) {
89 d.hide();
90 cur_frm.reload_doc();
91 }
92 });
93 });
94
95 d.fields_dict.content.input.set_input("<p></p><p></p>=== In response to ===<p></p>"
96 + me.list[0].content)
97 $(d.fields_dict.subject.input).val(this.doc.subject || "").change();
98
99 d.show();
100 },
101
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530102 prepare: function(doc) {
103 //doc.when = comment_when(this.doc.modified);
104 doc.when = doc.modified;
105 if(doc.content.indexOf("<br>")== -1 && doc.content.indexOf("<p>")== -1) {
106 doc.content = doc.content.replace(/\n/g, "<br>");
107 }
Rushabh Mehta80127bf2012-11-27 18:41:35 +0530108 if(!doc.sender) doc.sender = "[unknown sender]";
109 doc.sender = doc.sender.replace(/</, "&lt;").replace(/>/, "&gt;");
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530110 doc.content = doc.content.split("=== In response to ===")[0];
111 doc.content = doc.content.split("-----Original Message-----")[0];
112 },
113 make_line: function(doc) {
114 var me = this;
115 var comm = $(repl('<tr><td title="Click to Expand / Collapse">\
Rushabh Mehta80127bf2012-11-27 18:41:35 +0530116 <p><b>%(sender)s on %(when)s</b> \
117 <a href="#Form/Communication/%(name)s" style="font-size: 90%">\
118 Show Details</a></p>\
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530119 <div class="comm-content" style="border-top: 1px solid #ddd; padding: 10px; \
120 display: none;"></div>\
121 </td></tr>', doc))
122 .appendTo(this.body)
123 .css({"cursor":"pointer"})
124 .click(function() {
125 $(this).find(".comm-content").toggle();
126 });
127
128 this.comm_list.push(comm);
129 comm.find(".comm-content").html(doc.content);
130 }
131})