openerp.mail = function(session) { var _t = session.web._t, _lt = session.web._lt; var mail = session.mail = {}; /** * Thread widget: this widget handles the display of a thread of * messages. The [thread_level] parameter sets the thread level number: * - root message * - - sub message (parent_id = root message) * - - - sub sub message (parent id = sub message) * - - sub message (parent_id = root message) * This widget has 2 ways of initialization, either you give records to be rendered, * either it will fetch [limit] messages related to [res_model]:[res_id]. */ /* Add ThreadDisplay widget to registry */ session.web.form.widgets.add( 'Thread', 'openerp.mail.Thread'); /* Thread is an extension of a Widget */ mail.Thread = session.web.Widget.extend({ template: 'Thread', /** * @param {Object} parent parent * @param {Object} [params] * @param {String} [params.res_model] res_model of document [REQUIRED] * @param {Number} [params.res_id] res_id of record [REQUIRED] * @param {Number} [params.uid] user id [REQUIRED] * @param {Bool} [params.parent_id=false] parent_id of message * @param {Number} [params.thread_level=0] number of levels in the thread (only 0 or 1 currently) * @param {Bool} [params.wall=false] thread is displayed in the wall * @param {Number} [params.msg_more_limit=150] number of character to display before having a "show more" link; * note that the text will not be truncated if it does not have 110% of * the parameter (ex: 110 characters needed to be truncated and be displayed * as a 100-characters message) * @param {Number} [params.limit=100] maximum number of messages to fetch * @param {Number} [params.offset=0] offset for fetching messages * @param {Number} [params.records=null] records to show instead of fetching messages */ init: function(parent, params) { this._super(parent); this.params = params; this.params.parent_id = this.params.parent_id || false; this.params.thread_level = this.params.thread_level || 0; this.params.is_wall = this.params.is_wall || this.params.records || false; this.params.msg_more_limit = this.params.msg_more_limit || 150; this.params.limit = this.params.limit || 100; this.params.offset = this.params.offset || 0; this.params.records = this.params.records || null; // datasets and internal vars this.ds = new session.web.DataSet(this, this.params.res_model); this.ds_users = new session.web.DataSet(this, 'res.users'); this.ds_msg = new session.web.DataSet(this, 'mail.message'); this.sorted_comments = {'root_ids': [], 'root_id_msg_list': {}}; this.comments_structure = {'root_ids': [], 'new_root_ids': [], 'msgs': {}, 'tree_struct': {}, 'model_to_root_ids': {}}; // display customization vars this.display = {}; this.display.show_post_comment = this.params.show_post_comment || false; this.display.show_msg_menu = this.params.is_wall; this.display.show_reply = (this.params.thread_level > 0); this.display.show_delete = ! this.params.is_wall; this.display.show_hide = this.params.is_wall; this.display.show_reply_by_email = ! this.params.is_wall; this.display.show_more = (this.params.thread_level == 0); // internal links mapping this.intlinks_mapping = {}; }, start: function() { this._super.apply(this, arguments); // customize display if (this.display.show_post_comment) { this.$element.find('div.oe_mail_thread_act').show(); } if (this.display.show_reply_by_email) { this.$element.find('a.oe_mail_compose').show(); } // add events this.add_events(); /* display user, fetch comments */ this.display_current_user(); if (this.params.records) var display_done = this.display_comments_from_parameters(this.params.records); else var display_done = this.init_comments(); return display_done }, add_events: function() { var self = this; // event: click on 'more' at bottom of thread this.$element.find('button.oe_mail_button_more').click(function () { self.do_more(); }); // event: writing in textarea this.$element.find('textarea.oe_mail_action_textarea').keyup(function (event) { var charCode = (event.which) ? event.which : window.event.keyCode; if (event.shiftKey && charCode == 13) { this.value = this.value+"\n"; } else if (charCode == 13) { return self.do_comment(); } }); // event: click on 'reply' in msg this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_reply', 'click', function (event) { var act_dom = $(this).parents('div.oe_mail_thread_display').find('div.oe_mail_thread_act:first'); act_dom.toggle(); event.preventDefault(); }); // event: click on 'delete' in msg this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_delete', 'click', function (event) { if (! confirm(_t("Do you really want to delete this message?"))) { return false; } var msg_id = event.srcElement.dataset.id; if (! msg_id) return false; var call_defer = self.ds_msg.unlink([parseInt(msg_id)]); $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).hide(); if (self.params.thread_level > 0) { $(event.srcElement).parents('ul.oe_mail_thread').eq(0).hide(); } return false; }); // event: click on 'hide' in msg this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_hide', 'click', function (event) { if (! confirm(_t("Do you really want to hide this thread ?"))) { return false; } var msg_id = event.srcElement.dataset.id; if (! msg_id) return false; var call_defer = self.ds.call('message_remove_pushed_notifications', [[self.params.res_id], [parseInt(msg_id)], true]); $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).hide(); if (self.params.thread_level > 0) { $(event.srcElement).parents('ul.oe_mail_thread').eq(0).hide(); } return false; }); // event: click on "reply by email" in msg this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_reply_by_email', 'click', function (event) { console.log('reply by e-mail'); var msg_id = event.srcElement.dataset.id; if (! msg_id) return false; self.do_action({ type: 'ir.actions.act_window', res_model: 'mail.compose.message', views: [[false, 'form']], view_type: 'form', view_mode: 'form', target: 'new', context: {'active_model': self.params.res_model, 'active_id': self.params.res_id, 'message_id': msg_id, 'mail.compose.message.mode': 'reply'}, key2: 'client_action_multi', }); return false; }); // event: click on 'hide this type' in wheel_menu this.$element.find('div.oe_mail_thread_display').delegate('a.oe_mail_msg_hide_type', 'click', function (event) { console.log('hiding type'); if (! confirm(_t("Do you really want to hide this type of thread ?"))) { return false; } var subtype = event.srcElement.dataset.subtype; if (! subtype) return false; console.log(subtype); var call_defer = self.ds.call('message_subscription_hide', [[self.params.res_id], subtype]); $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).hide(); if (self.params.thread_level > 0) { $(event.srcElement).parents('ul.oe_mail_thread').eq(0).hide(); } return false; }); // event: click on an internal link this.$element.find('div.oe_mail_thread_display').delegate('a.intlink', 'click', function (event) { // lazy implementation: fetch data and try to redirect if (! event.srcElement.dataset.resModel) return false; else var res_model = event.srcElement.dataset.resModel; var res_login = event.srcElement.dataset.resLogin; var res_id = event.srcElement.dataset.resId; if ((! res_login) && (! res_id)) return false; if (! res_id) { var ds = new session.web.DataSet(self, res_model); var defer = ds.call('search', [[['login', '=', res_login]]]).then(function (records) { if (records[0]) { self.do_action({ type: 'ir.actions.act_window', res_model: res_model, res_id: parseInt(records[0]), views: [[false, 'form']]}); } else return false; }); } else self.do_action({ type: 'ir.actions.act_window', res_model: res_model, res_id: parseInt(res_id), views: [[false, 'form']]}); }); // event: click on "send an email" this.$element.find('div.oe_mail_thread_act').delegate('a.oe_mail_compose', 'click', function (event) { self.do_action({ type: 'ir.actions.act_window', res_model: 'mail.compose.message', views: [[false, 'form']], view_type: 'form', view_mode: 'form', target: 'new', context: {'active_model': self.params.res_model, 'active_id': self.params.res_id, 'mail.compose.message.mode': 'document'}, key2: 'client_action_multi', }); return false; }); }, destroy: function () { this._super.apply(this, arguments); }, init_comments: function() { var self = this; this.params.offset = 0; this.sorted_comments = {'root_ids': [], 'root_id_msg_list': {}}; this.comments_structure = {'root_ids': [], 'new_root_ids': [], 'msgs': {}, 'tree_struct': {}, 'model_to_root_ids': {}}; this.$element.find('div.oe_mail_thread_display').empty(); domain = this.get_fetch_domain(this.sorted_comments); return this.fetch_comments(this.params.limit, this.params.offset, domain).then(); }, fetch_comments: function (limit, offset, domain) { var self = this; var defer = this.ds.call('message_load', [[this.params.res_id], (this.params.thread_level > 0), (this.sorted_comments['root_ids']), (limit+1) || (this.params.limit+1), offset||this.params.offset, domain||undefined ]); $.when(defer).then(function (records) { if (records.length <= self.params.limit) self.display.show_more = false; else { self.display.show_more = true; records.pop(); } self.display_comments(records); if (self.display.show_more == true) self.$element.find('div.oe_mail_thread_more:last').show(); else self.$element.find('div.oe_mail_thread_more:last').hide(); }); return defer; }, display_comments_from_parameters: function (records) { if (records.length > 0 && records.length < (records[0].child_ids.length+1) ) this.display.show_more = true; else this.display.show_more = false; var defer = this.display_comments(records); if (this.display.show_more == true) $('div.oe_mail_thread_more').eq(-2).show(); else $('div.oe_mail_thread_more').eq(-2).hide(); return defer; }, display_comments: function (records) { var self = this; this.cs = this.sort_comments_tmp(records); /* WIP: map matched regexp -> records to browse with name */ //_(records).each(function (record) { //self.do_check_internal_links(record.body_text); //}); _(records).each(function (record) { var sub_msgs = []; if ((record.parent_id == false || record.parent_id[0] == self.params.parent_id) && self.params.thread_level > 0 ) { var sub_list = self.cs['tree_struct'][record.id]['direct_childs']; _(records).each(function (record) { //if (record.parent_id == false || record.parent_id[0] == self.params.parent_id) return; if (_.indexOf(sub_list, record.id) != -1) { sub_msgs.push(record); } }); self.display_comment(record); self.thread = new mail.Thread(self, {'res_model': self.params.res_model, 'res_id': self.params.res_id, 'uid': self.params.uid, 'records': sub_msgs, 'thread_level': (self.params.thread_level-1), 'parent_id': record.id}); self.$element.find('li.oe_mail_thread_msg:last').append('
'); self.thread.appendTo(self.$element.find('div.oe_mail_thread_subthread:last')); } else if (self.params.thread_level == 0) { self.display_comment(record); } }); // update offset for "More" buttons if (this.params.thread_level == 0) this.params.offset += records.length; }, /** * Display a record */ display_comment: function (record) { record.body = this.do_text_nl2br(record.body, true); if (record.type == 'email') { record.mini_url = ('/mail/static/src/img/email_icon.png'); } else { record.mini_url = this.thread_get_avatar('res.users', 'avatar', record.user_id[0]); } // body text manipulation record.body = this.do_clean_text(record.body); record.body = this.do_replace_internal_links(record.body); // format date according to the user timezone record.date = session.web.format_value(record.date, {type:"datetime"}); // render $(session.web.qweb.render('ThreadMsg', {'record': record, 'thread': this, 'params': this.params, 'display': this.display}) ).appendTo(this.$element.children('div.oe_mail_thread_display:first')); // expand feature this.$element.find('span.oe_mail_msg_body:last').expander({slicePoint: this.params.msg_more_limit, moreClass: 'oe_mail_expand', lesClass: 'oe_mail_reduce'}); }, /** * Add records to sorted_comments array * @param {Array} records records from mail.message sorted by date desc * @returns {Object} sc sorted_comments: dict { * 'root_id_list': list or root_ids * 'root_id_msg_list': {'record_id': [ancestor_ids]}, still sorted by date desc * 'id_to_root': {'root_id': [records]}, still sorted by date desc * } */ sort_comments: function (records) { var self = this; sc = {'root_id_list': [], 'root_id_msg_list': {}, 'id_to_root': {}} var cur_iter = 0; var max_iter = 10; var modif = true; /* step1: get roots */ while ( modif && (cur_iter++) < max_iter) { modif = false; _(records).each(function (record) { if ( (record.parent_id == false || record.parent_id[0] == self.params.parent_id) && (_.indexOf(sc['root_id_list'], record.id) == -1)) { sc['root_id_list'].push(record.id); sc['root_id_msg_list'][record.id] = []; self.sorted_comments['root_ids'].push(record.id); modif = true; } else { if (_.indexOf(sc['root_id_list'], record.parent_id[0]) != -1) { sc['id_to_root'][record.id] = record.parent_id[0]; modif = true; } else if ( sc['id_to_root'][record.parent_id[0]] ) { sc['id_to_root'][record.id] = sc['id_to_root'][record.parent_id[0]]; modif = true; } } }); } /* step2: add records */ _(records).each(function (record) { var root_id = sc['id_to_root'][record.id]; if (! root_id) return; sc['root_id_msg_list'][root_id].push(record); //self.sorted_comments['root_id_msg_list'][root_id].push(record.id); }); return sc; }, /** * Add records to comments_structure object: see function for details */ sort_comments_tmp: function(records) { return tools_sort_comments(this.comments_structure, records, this.params.parent_id); }, display_current_user: function () { return this.$element.find('img.oe_mail_msg_image').attr('src', this.thread_get_avatar('res.users', 'avatar', this.params.uid)); }, do_comment: function () { var comment_node = this.$element.find('textarea'); var body_text = comment_node.val(); comment_node.val(''); return this.ds.call('message_append_note', [[this.params.res_id], 'Reply', body_text, this.params.parent_id, 'comment', 'html', 'comment']).then( this.proxy('init_comments')); }, /** * Create a domain to fetch new comments according to * comment already present in sorted_comments * @param {Object} sorted_comments (see sort_comments) * @returns {Array} fetch_domain (OpenERP domain style) */ get_fetch_domain: function (sorted_comments) { var domain = []; var ids = sorted_comments.root_ids.slice(); var ids2 = []; // must be child of current parent if (this.params.parent_id) { domain.push(['id', 'child_of', this.params.parent_id]); } _(sorted_comments.root_ids).each(function (id) { // each record ids.push(id); ids2.push(id); }); if (this.params.parent_id != false) { ids2.push(this.params.parent_id); } // must not be children of already fetched messages if (ids.length > 0) { domain.push('&'); domain.push('!'); domain.push(['id', 'child_of', ids]); } if (ids2.length > 0) { domain.push(['id', 'not in', ids2]); } return domain; }, do_more: function () { domain = this.get_fetch_domain(this.sorted_comments); return this.fetch_comments(this.params.limit, this.params.offset, domain); }, /** * * var regex_login = new RegExp(/(^|\s)@((\w|@|\.)*)/g); * var regex_intlink = new RegExp(/(^|\s)#(\w*[a-zA-Z_]+\w*)\.(\w+[a-zA-Z_]+\w*),(\w+)/g); */ do_replace_internal_links: function (string) { var self = this; var icon_list = ['al', 'pinky'] /* shortcut to user: @login */ var regex_login = new RegExp(/(^|\s)@((\w|@|\.)*)/g); var regex_res = regex_login.exec(string); while (regex_res != null) { var login = regex_res[2]; string = string.replace(regex_res[0], regex_res[1] + '@' + login + ''); regex_res = regex_login.exec(string); } /* special shortcut: :name, try to find an icon if in list */ var regex_login = new RegExp(/(^|\s):((\w)*)/g); var regex_res = regex_login.exec(string); while (regex_res != null) { var icon_name = regex_res[2]; if (_.include(icon_list, icon_name)) string = string.replace(regex_res[0], regex_res[1] + ''); regex_res = regex_login.exec(string); } return string; }, thread_get_avatar: function(model, field, id) { return this.session.prefix + '/web/binary/image?session_id=' + this.session.session_id + '&model=' + model + '&field=' + field + '&id=' + (id || ''); }, do_clean_text: function (string) { var html = $('').text(string.replace(/\s+/g, ' ')).html().replace(new RegExp('<(/)?(b|em|br|br /)\\s*>', 'gi'), '<$1$2>'); return html; }, do_text_nl2br: function (str, is_xhtml) { var break_tag = (is_xhtml || typeof is_xhtml === 'undefined') ? '