brotherton-erpnext/utilities/page/messages/messages.js

210 lines
6.0 KiB
JavaScript
Raw Normal View History

2012-02-28 12:10:13 +00:00
// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-02-27 12:33:54 +00:00
wn.provide('erpnext.messages');
wn.pages.messages.onload = function(wrapper) {
2012-09-27 13:11:46 +00:00
wn.ui.make_app_page({
parent: wrapper,
title: "Messages"
2012-02-27 12:33:54 +00:00
});
2012-10-03 13:02:10 +00:00
$('<div><div class="avatar avatar-large">\
<img id="avatar-image" src="lib/images/ui/avatar.png"></div>\
<h3 style="display: inline-block" id="message-title">Everyone</h3>\
</div><hr>\
<div id="post-message">\
2012-09-27 13:11:46 +00:00
<textarea style="width: 100%; height: 24px;"></textarea>\
<div><button class="btn btn-small">Post</button></div><hr>\
</div>\
<div class="all-messages"></div>').appendTo($(wrapper).find('.layout-main-section'));
erpnext.messages = new erpnext.Messages(wrapper);
2012-02-27 12:33:54 +00:00
}
2012-03-22 08:14:04 +00:00
$(wn.pages.messages).bind('show', function() {
// remove alerts
$('#alert-container .alert').remove();
2012-02-27 12:33:54 +00:00
erpnext.messages.show();
2012-10-03 13:02:10 +00:00
setTimeout("erpnext.messages.refresh()", 17000);
2012-03-22 08:14:04 +00:00
})
2012-02-27 12:33:54 +00:00
2012-09-27 13:11:46 +00:00
erpnext.Messages = Class.extend({
init: function(wrapper) {
this.wrapper = wrapper;
this.show_active_users();
this.make_post_message();
this.make_list();
//this.update_messages('reset'); //Resets notification icons
},
make_post_message: function() {
var me = this;
$('#post-message textarea').keydown(function(e) {
if(e.which==13) {
$('#post-message .btn').click();
return false;
}
});
$('#post-message .btn').click(function() {
var txt = $('#post-message textarea').val();
if(txt) {
wn.call({
module:'utilities',
page:'messages',
method:'post',
args: {
txt: txt,
contact: me.contact
},
callback:function(r,rt) {
$('#post-message textarea').val('')
me.list.run();
},
btn: this
});
}
});
},
2012-02-27 12:33:54 +00:00
show: function() {
var contact = this.get_contact() || this.contact || user;
2012-02-27 12:33:54 +00:00
2012-10-03 13:02:10 +00:00
$('#message-title').html(contact==user ? "Everyone" :
wn.user_info(contact).fullname)
$('#avatar-image').attr("src", wn.user_info(contact).image);
2012-02-27 12:33:54 +00:00
2012-09-27 13:11:46 +00:00
$("#show-everyone").toggle(contact!=user);
2012-10-03 13:02:10 +00:00
$("#post-message button").text(contact==user ? "Post Publicly" : "Post to user")
2012-09-27 13:11:46 +00:00
this.contact = contact;
this.list.opts.args.contact = contact;
this.list.run();
2012-02-27 12:33:54 +00:00
},
// check for updates every 5 seconds if page is active
refresh: function() {
2012-10-03 13:02:10 +00:00
setTimeout("erpnext.messages.refresh()", 17000);
2012-03-22 08:14:04 +00:00
if(wn.container.page.label != 'Messages') return;
2012-09-27 13:11:46 +00:00
this.show();
2012-02-27 12:33:54 +00:00
},
get_contact: function() {
var route = location.hash;
if(route.indexOf('/')!=-1) {
var name = decodeURIComponent(route.split('/')[1]);
if(name.indexOf('__at__')!=-1) {
name = name.replace('__at__', '@');
}
return name;
}
},
make_list: function() {
2012-09-27 13:11:46 +00:00
this.list = new wn.ui.Listing({
parent: $(this.wrapper).find('.all-messages'),
2012-02-27 12:33:54 +00:00
method: 'utilities.page.messages.messages.get_list',
args: {
contact: null
},
2012-09-27 13:11:46 +00:00
hide_refresh: true,
no_loading: true,
2012-02-27 12:33:54 +00:00
render_row: function(wrapper, data) {
2012-03-22 08:14:04 +00:00
$(wrapper).removeClass('list-row');
2012-02-27 12:33:54 +00:00
data.creation = dateutil.comment_when(data.creation);
2012-02-29 13:39:20 +00:00
data.comment_by_fullname = wn.user_info(data.owner).fullname;
2012-10-03 13:02:10 +00:00
data.image = wn.user_info(data.owner).image;
data.mark_html = "";
2012-02-27 12:33:54 +00:00
2012-02-27 13:26:31 +00:00
data.reply_html = '';
2012-02-27 12:33:54 +00:00
if(data.owner==user) {
data.cls = 'message-self';
data.comment_by_fullname = 'You';
} else {
2012-02-27 13:11:11 +00:00
data.cls = 'message-other';
2012-02-27 12:33:54 +00:00
}
// delete
data.delete_html = "";
if(data.owner==user || data.comment.indexOf("assigned to")!=-1) {
data.delete_html = repl('<a class="close" \
onclick="erpnext.messages.delete(this)"\
data-name="%(name)s">&times;</a>', data);
}
2012-10-03 13:02:10 +00:00
if(data.owner==data.comment_docname) {
data.mark_html = "<div class='message-mark' title='Public'\
style='background-color: green'></div>"
}
2012-10-03 13:02:10 +00:00
wrapper.innerHTML = repl('<div class="message %(cls)s">%(mark_html)s\
<span class="avatar avatar-small"><img src="%(image)s"></span><b>%(comment)s</b>\
%(delete_html)s\
2012-02-27 13:26:31 +00:00
<div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
2012-10-03 13:02:10 +00:00
</div>\
2012-02-27 12:33:54 +00:00
<div style="clear: both;"></div>', data);
}
});
},
2012-02-27 13:11:11 +00:00
delete: function(ele) {
$(ele).parent().css('opacity', 0.6);
wn.call({
method:'utilities.page.messages.messages.delete',
args: {name : $(ele).attr('data-name')},
callback: function() {
$(ele).parent().toggle(false);
}
});
},
2012-02-27 12:33:54 +00:00
show_active_users: function() {
2012-09-27 13:11:46 +00:00
var me = this;
2012-02-27 12:33:54 +00:00
wn.call({
module:'utilities',
page:'messages',
method:'get_active_users',
callback: function(r,rt) {
2012-09-27 13:11:46 +00:00
var $body = $(me.wrapper).find('.layout-side-section');
2012-10-03 13:02:10 +00:00
$('<h4>Users</h4><hr>\
<div id="show-everyone">\
<a href="#messages/'+user+'" class="btn btn-small">\
Show messages from everyone</a><hr></div>\
').appendTo($body);
r.message.sort(function(a, b) { return b.has_session - a.has_session; });
2012-02-27 12:33:54 +00:00
for(var i in r.message) {
var p = r.message[i];
2012-09-27 13:18:43 +00:00
if(p.name != user) {
p.fullname = wn.user_info(p.name).fullname;
2012-10-03 13:02:10 +00:00
p.image = wn.user_info(p.name).image;
2012-09-27 13:18:43 +00:00
p.name = p.name.replace('@', '__at__');
2012-10-03 13:02:10 +00:00
p.status_color = p.has_session ? "green" : "#ddd";
2012-09-27 13:18:43 +00:00
p.status = p.has_session ? "Online" : "Offline";
2012-10-03 13:02:10 +00:00
$(repl('<p>\
<span class="avatar avatar-small" \
style="border: 3px solid %(status_color)s" \
title="%(status)s"><img src="%(image)s"></span>\
2012-09-27 13:18:43 +00:00
<a href="#!messages/%(name)s">%(fullname)s</a>\
</p>', p))
.appendTo($body);
}
2012-02-27 12:33:54 +00:00
}
}
});
}
2012-09-27 13:11:46 +00:00
});
2012-02-27 12:33:54 +00:00