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