blob: b59a21229d1053de36718c774a2e28b9a49c108a [file] [log] [blame]
Rushabh Mehtafdea9662012-02-27 18:03:54 +05301wn.provide('erpnext.messages');
2
3wn.pages.messages.onload = function(wrapper) {
4 erpnext.messages.show_active_users();
5 erpnext.messages.make_list();
6
7 // post message
8 $('#message-post').click(function() {
9 var txt = $('#message-post-text').val();
10 if(txt) {
11 wn.call({
12 module:'utilities',
13 page:'messages',
14 method:'post',
15 args: {
16 txt: txt,
17 contact: erpnext.messages.contact
18 },
19 callback:function(r,rt) {
20 $('#message-post-text').val('')
21 erpnext.messages.list.run();
22 },
23 btn: this
24 });
25 }
26 });
27
28 // enable, disable button
29 $('#message-post-text').keyup(function() {
30 if($(this).val()) {
31 $('#message-post').attr('disabled', false);
32 } else {
33 $('#message-post').attr('disabled', true);
34 }
35 })
36}
37
38wn.pages.messages.onshow = function(wrapper) {
39 erpnext.messages.show();
40 setTimeout(erpnext.messages.refresh, 5000);
41}
42
43erpnext.messages = {
44 show: function() {
45 var contact = erpnext.messages.get_contact();
46
47 // can't send message to self
48 $(wn.pages.messages).find('.well').toggle(contact==user ? false : true);
49
50 $(wn.pages.messages).find('h1:first').html('Messages: '
51 + (user==contact ? 'From everyone' : wn.boot.user_fullnames[contact]))
52
53 erpnext.messages.contact = contact;
54 erpnext.messages.list.opts.args.contact = contact;
55 erpnext.messages.list.run();
56
57 },
58 // check for updates every 5 seconds if page is active
59 refresh: function() {
60 setTimeout(erpnext.messages.refresh, 10000);
61 if(page_body.cur_page_label != 'messages') return;
62 erpnext.messages.show();
63 },
64 get_contact: function() {
65 var route = location.hash;
66 if(route.indexOf('/')!=-1) {
67 var name = decodeURIComponent(route.split('/')[1]);
68 if(name.indexOf('__at__')!=-1) {
69 name = name.replace('__at__', '@');
70 }
71 return name;
72 }
73 return user;
74 },
75 make_list: function() {
76 erpnext.messages.list = new wn.widgets.Listing({
77 parent: $('#message-list').get(0),
78 method: 'utilities.page.messages.messages.get_list',
79 args: {
80 contact: null
81 },
82 render_row: function(wrapper, data) {
83 data.creation = dateutil.comment_when(data.creation);
84 data.comment_by_fullname = wn.boot.user_fullnames[data.owner];
85
86 if(data.owner==user) {
87 data.cls = 'message-self';
88 data.comment_by_fullname = 'You';
89 } else {
90 data.cls = 'message-other'
91 }
92
93 wrapper.innerHTML = repl('<div class="message %(cls)s"><b>%(comment)s</b>\
94 <div class="help">by %(comment_by_fullname)s, %(creation)s</div></div>\
95 <div style="clear: both;"></div>', data);
96 }
97 });
98 },
99 show_active_users: function() {
100 wn.call({
101 module:'utilities',
102 page:'messages',
103 method:'get_active_users',
104 callback: function(r,rt) {
105 var $body = $(wn.pages.messages).find('.section-body');
106 for(var i in r.message) {
107 var p = r.message[i];
108 p.fullname = wn.boot.user_fullnames[p.name];
109 p.name = p.name.replace('@', '__at__');
110 $body.append(repl('<div class="section-item">\
111 <a href="#!messages/%(name)s">%(fullname)s</a></div>', p))
112 }
113 }
114 });
115 }
116}
117
118