1 openerp.mail = function (session) {
2 var _t = session.web._t,
5 var mail = session.mail;
7 openerp_mail_followers(session, mail); // import mail_followers.js
8 openerp_FieldMany2ManyTagsEmail(session); // import manyy2many_tags_email.js
9 openerp_announcement(session);
12 * ------------------------------------------------------------
14 * ------------------------------------------------------------
16 * This class holds a few tools method for Chatter.
17 * Some regular expressions not used anymore, kept because I want to
18 * - (^|\s)@((\w|@|\.)*): @login@log.log
19 * - (^|\s)\[(\w+).(\w+),(\d)\|*((\w|[@ .,])*)\]: [ir.attachment,3|My Label],
25 /** parse text to find email: Tagada <address@mail.fr> -> [Tagada, address@mail.fr] or False */
26 parse_email: function (text) {
27 var result = text.match(/(.*)<(.*@.*)>/);
29 return [_.str.trim(result[1]), _.str.trim(result[2])];
31 result = text.match(/(.*@.*)/);
33 return [_.str.trim(result[1]), _.str.trim(result[1])];
38 /* Get an image in /web/binary/image?... */
39 get_image: function (session, model, field, id, resize) {
40 var r = resize ? encodeURIComponent(resize) : '';
42 return session.url('/web/binary/image', {model: model, field: field, id: id, resize: r});
45 /* Get the url of an attachment {'id': id} */
46 get_attachment_url: function (session, message_id, attachment_id) {
47 return session.url('/mail/download_attachment', {
48 'model': 'mail.message',
50 'method': 'download_attachment',
51 'attachment_id': attachment_id
56 * Replaces some expressions
57 * - :name - shortcut to an image
59 do_replace_expressions: function (string) {
60 var icon_list = ['al', 'pinky']
61 /* special shortcut: :name, try to find an icon if in list */
62 var regex_login = new RegExp(/(^|\s):((\w)*)/g);
63 var regex_res = regex_login.exec(string);
64 while (regex_res != null) {
65 var icon_name = regex_res[2];
66 if (_.include(icon_list, icon_name))
67 string = string.replace(regex_res[0], regex_res[1] + '<img src="/mail/static/src/img/_' + icon_name + '.png" width="22px" height="22px" alt="' + icon_name + '"/>');
68 regex_res = regex_login.exec(string);
74 * Replaces textarea text into html text (add <p>, <a>)
75 * TDE note : should be done server-side, in Python -> use mail.compose.message ?
77 get_text2html: function (text) {
79 .replace(/((?:https?|ftp):\/\/[\S]+)/g,'<a href="$1">$1</a> ')
80 .replace(/[\n\r]/g,'<br/>')
83 /* Returns the complete domain with "&"
84 * TDE note: please add some comments to explain how/why
86 expand_domain: function (domain) {
89 // TDE note: smarted code maybe ?
90 for ( var k = domain.length-1; k >= 0 ; k-- ) {
91 if ( typeof domain[k] != 'array' && typeof domain[k] != 'object' ) {
98 for (var k = 0; k < nb_and ; k++) {
105 // inserts zero width space between each letter of a string so that
106 // the word will correctly wrap in html boxes smaller than the text
107 breakword: function(str){
112 for(var i = 0, len = str.length; i < len; i++){
113 out += _.str.escapeHTML(str[i]) + '​';
121 * ------------------------------------------------------------
123 * ------------------------------------------------------------
125 * Common base for expandables, chatter messages and composer. It manages
126 * the various variables common to those models.
129 mail.MessageCommon = session.web.Widget.extend({
132 * ------------------------------------------------------------
133 * FIXME: this comment was moved as is from the ThreadMessage Init as
134 * part of a refactoring. Check that it is still correct
135 * ------------------------------------------------------------
136 * This widget handles the display of a messages in a thread.
137 * Displays a record and performs some formatting on the record :
138 * - record.date: formatting according to the user timezone
139 * - record.timerelative: relative time givein by timeago lib
140 * - record.avatar: image url
141 * - record.attachment_ids[].url: url of each attachmentThe
144 * - - sub message (parent_id = root message)
146 * - - - - sub sub message (parent id = sub thread)
147 * - - sub message (parent_id = root message)
151 init: function (parent, datasets, options) {
152 this._super(parent, options);
155 this.options = datasets.options || options || {};
156 // record domain and context
157 this.domain = datasets.domain || options.domain || [];
158 this.context = _.extend({
159 default_model: false,
161 default_parent_id: false }, options.context || {});
163 // data of this message
164 this.id = datasets.id || false,
165 this.last_id = this.id,
166 this.model = datasets.model || this.context.default_model || false,
167 this.res_id = datasets.res_id || this.context.default_res_id || false,
168 this.parent_id = datasets.parent_id || false,
169 this.type = datasets.type || false,
170 this.subtype = datasets.subtype || false,
171 this.is_author = datasets.is_author || false,
172 this.author_avatar = datasets.author_avatar || false,
173 this.is_private = datasets.is_private || false,
174 this.subject = datasets.subject || false,
175 this.name = datasets.name || false,
176 this.record_name = datasets.record_name || false,
177 this.body = datasets.body || '',
178 this.body_short = datasets.body_short || '',
179 this.vote_nb = datasets.vote_nb || 0,
180 this.has_voted = datasets.has_voted || false,
181 this.is_favorite = datasets.is_favorite || false,
182 this.thread_level = datasets.thread_level || 0,
183 this.to_read = datasets.to_read || false,
184 this.author_id = datasets.author_id || false,
185 this.attachment_ids = datasets.attachment_ids || [],
186 this.partner_ids = datasets.partner_ids || [];
187 this.date = datasets.date;
188 this.user_pid = datasets.user_pid || false;
191 // update record_name: Partner profile
192 if (this.model == 'res.partner') {
193 this.record_name = 'Partner Profile of ' + this.record_name;
195 else if (this.model == 'hr.employee') {
196 this.record_name = 'News from ' + this.record_name;
198 // record options and data
199 this.show_record_name = this.options.show_record_name && this.record_name && !this.thread_level;
200 this.options.show_read = false;
201 this.options.show_unread = false;
202 if (this.options.show_read_unread_button) {
203 if (this.options.read_action == 'read') this.options.show_read = true;
204 else if (this.options.read_action == 'unread') this.options.show_unread = true;
206 this.options.show_read = this.to_read;
207 this.options.show_unread = !this.to_read;
209 this.options.rerender = true;
210 this.options.toggle_read = true;
212 this.parent_thread = typeof parent.on_message_detroy == 'function' ? parent : this.options.root_thread;
216 /* Convert date, timerelative and avatar in displayable data. */
217 format_data: function () {
219 //formating and add some fields for render
220 this.date = this.date ? session.web.str_to_datetime(this.date) : false;
221 this.display_date = this.date.toString('ddd MMM dd yyyy HH:mm');
222 if (this.date && new Date().getTime()-this.date.getTime() < 7*24*60*60*1000) {
223 this.timerelative = $.timeago(this.date);
225 if (this.author_avatar) {
226 this.avatar = "data:image/png;base64," + this.author_avatar;
227 } else if (this.type == 'email' && (!this.author_id || !this.author_id[0])) {
228 this.avatar = ('/mail/static/src/img/email_icon.png');
229 } else if (this.author_id && this.template != 'mail.compose_message') {
230 this.avatar = mail.ChatterUtils.get_image(this.session, 'res.partner', 'image_small', this.author_id[0]);
232 this.avatar = mail.ChatterUtils.get_image(this.session, 'res.users', 'image_small', this.session.uid);
234 if (this.author_id && this.author_id[1]) {
235 var parsed_email = mail.ChatterUtils.parse_email(this.author_id[1]);
236 this.author_id.push(parsed_email[0], parsed_email[1]);
238 if (this.partner_ids && this.partner_ids.length > 3) {
239 this.extra_partners_nbr = this.partner_ids.length - 3;
240 this.extra_partners_str = ''
241 var extra_partners = this.partner_ids.slice(3);
242 for (var key in extra_partners) {
243 this.extra_partners_str += extra_partners[key][1];
249 /* upload the file on the server, add in the attachments list and reload display
251 display_attachments: function () {
252 for (var l in this.attachment_ids) {
253 var attach = this.attachment_ids[l];
254 if (!attach.formating) {
255 attach.url = mail.ChatterUtils.get_attachment_url(this.session, this.id, attach.id);
256 attach.name = mail.ChatterUtils.breakword(attach.name || attach.filename);
257 attach.formating = true;
260 this.$(".oe_msg_attachment_list").html( session.web.qweb.render('mail.thread.message.attachments', {'widget': this}) );
263 /* return the link to resized image
265 attachments_resize_image: function (id, resize) {
266 return mail.ChatterUtils.get_image(this.session, 'ir.attachment', 'datas', id, resize);
269 /* get all child message id linked.
270 * @return array of id
272 get_child_ids: function () {
273 return _.map(this.get_childs(), function (val) { return val.id; });
276 /* get all child message linked.
277 * @return array of message object
279 get_childs: function (nb_thread_level) {
281 if (arguments[1] && this.id) res.push(this);
282 if ((isNaN(nb_thread_level) || nb_thread_level>0) && this.thread) {
283 _(this.thread.messages).each(function (val, key) {
284 res = res.concat( val.get_childs((isNaN(nb_thread_level) ? undefined : nb_thread_level-1), true) );
291 * search a message in all thread and child thread.
292 * This method return an object message.
293 * @param {object}{int} option.id
294 * @param {object}{string} option.model
295 * @param {object}{boolean} option._go_thread_wall
296 * private for check the top thread
297 * @return thread object
299 browse_message: function (options) {
300 // goto the wall thread for launch browse
301 if (!options._go_thread_wall) {
302 options._go_thread_wall = true;
303 for (var i in this.options.root_thread.messages) {
304 var res=this.options.root_thread.messages[i].browse_message(options);
309 if (this.id==options.id)
312 for (var i in this.thread.messages) {
313 if (this.thread.messages[i].thread) {
314 var res=this.thread.messages[i].browse_message(options);
323 * call on_message_delete on his parent thread
325 destroy: function () {
328 this.parent_thread.on_message_detroy(this);
335 * ------------------------------------------------------------
336 * ComposeMessage widget
337 * ------------------------------------------------------------
339 * This widget handles the display of a form to compose a new message.
340 * This form is a mail.compose.message form_view.
341 * On first time : display a compact textarea that is not the compose form.
342 * When the user focuses the textarea, the compose message is instantiated.
345 mail.ThreadComposeMessage = mail.MessageCommon.extend({
346 template: 'mail.compose_message',
349 * @param {Object} parent parent
350 * @param {Object} [options]
351 * @param {Object} [context] context passed to the
352 * mail.compose.message DataSetSearch. Please refer to this model
353 * for more details about fields and default values.
354 * @param {Object} recipients = [
356 'email_address': [str],
357 'partner_id': False/[int],
359 'full_name': name<email_address>,
365 init: function (parent, datasets, options) {
366 this._super(parent, datasets, options);
367 this.show_compact_message = false;
368 this.show_delete_attachment = true;
370 this.recipients = [];
371 this.recipient_ids = [];
375 this._super.apply(this, arguments);
377 this.ds_attachment = new session.web.DataSetSearch(this, 'ir.attachment');
378 this.fileupload_id = _.uniqueId('oe_fileupload_temp');
379 $(window).on(this.fileupload_id, this.on_attachment_loaded);
381 this.display_attachments();
385 /* when a user click on the upload button, send file read on_attachment_loaded
387 on_attachment_change: function (event) {
388 event.stopPropagation();
390 var $target = $(event.target);
391 if ($target.val() !== '') {
393 var filename = $target.val().replace(/.*[\\\/]/,'');
395 // if the files exits for this answer, delete the file before upload
397 for (var i in this.attachment_ids) {
398 if ((this.attachment_ids[i].filename || this.attachment_ids[i].name) == filename) {
399 if (this.attachment_ids[i].upload) {
402 this.ds_attachment.unlink([this.attachment_ids[i].id]);
404 attachments.push(this.attachment_ids[i]);
407 this.attachment_ids = attachments;
410 this.$('form.oe_form_binary_form').submit();
412 this.$(".oe_attachment_file").hide();
414 this.attachment_ids.push({
417 'filename': filename,
421 this.display_attachments();
425 /* when the file is uploaded
427 on_attachment_loaded: function (event, result) {
429 if (result.erorr || !result.id ) {
430 this.do_warn( session.web.qweb.render('mail.error_upload'), result.error);
431 this.attachment_ids = _.filter(this.attachment_ids, function (val) { return !val.upload; });
433 for (var i in this.attachment_ids) {
434 if (this.attachment_ids[i].filename == result.filename && this.attachment_ids[i].upload) {
435 this.attachment_ids[i]={
438 'filename': result.filename,
439 'url': mail.ChatterUtils.get_attachment_url(this.session, this.id, result.id)
444 this.display_attachments();
445 var $input = this.$('input.oe_form_binary_file');
446 $input.after($input.clone(true)).remove();
447 this.$(".oe_attachment_file").show();
450 /* unlink the file on the server and reload display
452 on_attachment_delete: function (event) {
453 event.stopPropagation();
454 var attachment_id=$(event.target).data("id");
457 for (var i in this.attachment_ids) {
458 if (attachment_id!=this.attachment_ids[i].id) {
459 attachments.push(this.attachment_ids[i]);
462 this.ds_attachment.unlink([attachment_id]);
465 this.attachment_ids = attachments;
466 this.display_attachments();
470 bind_events: function () {
472 this.$('.oe_compact_inbox').on('click', self.on_toggle_quick_composer);
473 this.$('.oe_compose_post').on('click', self.on_toggle_quick_composer);
474 this.$('.oe_compose_log').on('click', self.on_toggle_quick_composer);
475 this.$('input.oe_form_binary_file').on('change', _.bind( this.on_attachment_change, this));
476 this.$('.oe_cancel').on('click', _.bind( this.on_cancel, this));
477 this.$('.oe_post').on('click', self.on_message_post);
478 this.$('.oe_full').on('click', _.bind( this.on_compose_fullmail, this, this.id ? 'reply' : 'comment'));
479 /* stack for don't close the compose form if the user click on a button */
480 this.$('.oe_msg_left, .oe_msg_center').on('mousedown', _.bind( function () { this.stay_open = true; }, this));
481 this.$('.oe_msg_left, .oe_msg_content').on('mouseup', _.bind( function () { this.$('textarea').focus(); }, this));
483 ev_stay.mouseup = ev_stay.keydown = ev_stay.focus = function () { self.stay_open = false; };
484 this.$('textarea').on(ev_stay);
485 this.$('textarea').autosize();
488 this.$('textarea').on('blur', self.on_toggle_quick_composer);
490 // event: delete child attachments off the oe_msg_attachment_list box
491 this.$(".oe_msg_attachment_list").on('click', '.oe_delete', this.on_attachment_delete);
493 this.$(".oe_recipients").on('change', 'input', this.on_checked_recipient);
496 on_compose_fullmail: function (default_composition_mode) {
498 if(!this.do_check_attachment_upload()) {
501 var recipient_done = $.Deferred();
503 recipient_done.resolve([]);
506 recipient_done = this.check_recipient_partners();
508 $.when(recipient_done).done(function (partner_ids) {
510 'default_composition_mode': default_composition_mode,
511 'default_parent_id': self.id,
512 'default_body': mail.ChatterUtils.get_text2html(self.$el ? (self.$el.find('textarea:not(.oe_compact)').val() || '') : ''),
513 'default_attachment_ids': _.map(self.attachment_ids, function (file) {return file.id;}),
514 'default_partner_ids': partner_ids,
515 'mail_post_autofollow': true,
516 'mail_post_autofollow_partner_ids': partner_ids,
517 'is_private': self.is_private
520 _.extend(context, {'mail_compose_log': true});
522 if (default_composition_mode != 'reply' && self.context.default_model && self.context.default_res_id) {
523 context.default_model = self.context.default_model;
524 context.default_res_id = self.context.default_res_id;
528 type: 'ir.actions.act_window',
529 res_model: 'mail.compose.message',
532 views: [[false, 'form']],
537 self.do_action(action);
544 var $render = $( session.web.qweb.render('mail.compose_message', {'widget': this}) );
546 $render.insertAfter(this.$el.last());
550 this.display_attachments();
554 on_cancel: function (event) {
555 if (event) event.stopPropagation();
556 this.attachment_ids=[];
557 this.stay_open = false;
558 this.show_composer = false;
562 /* return true if all file are complete else return false and make an alert */
563 do_check_attachment_upload: function () {
564 if (_.find(this.attachment_ids, function (file) {return file.upload;})) {
565 this.do_warn(session.web.qweb.render('mail.error_upload'), session.web.qweb.render('mail.error_upload_please_wait'));
572 check_recipient_partners: function () {
574 var check_done = $.Deferred();
575 var recipients = _.filter(this.recipients, function (recipient) { return recipient.checked });
576 var recipients_to_find = _.filter(recipients, function (recipient) { return (! recipient.partner_id) });
577 var names_to_find = _.pluck(recipients_to_find, 'full_name');
578 var recipients_to_check = _.filter(recipients, function (recipient) { return (recipient.partner_id && ! recipient.email_address) });
579 var recipient_ids = _.pluck(_.filter(recipients, function (recipient) { return recipient.partner_id && recipient.email_address }), 'partner_id');
580 var names_to_remove = [];
581 var recipient_ids_to_remove = [];
583 // have unknown names -> call message_get_partner_info_from_emails to try to find partner_id
584 var find_done = $.Deferred();
585 if (names_to_find.length > 0) {
586 find_done = self.parent_thread.ds_thread._model.call('message_partner_info_from_emails', [this.context.default_res_id, names_to_find]);
589 find_done.resolve([]);
592 // for unknown names + incomplete partners -> open popup - cancel = remove from recipients
593 $.when(find_done).pipe(function (result) {
594 var emails_deferred = [];
595 var recipient_popups = result.concat(recipients_to_check);
597 _.each(recipient_popups, function (partner_info) {
598 var deferred = $.Deferred()
599 emails_deferred.push(deferred);
601 var partner_name = partner_info.full_name;
602 var partner_id = partner_info.partner_id;
603 var parsed_email = mail.ChatterUtils.parse_email(partner_name);
605 var pop = new session.web.form.FormOpenPopup(this);
609 { 'force_email': true,
610 'ref': "compound_context",
611 'default_name': parsed_email[0],
612 'default_email': parsed_email[1],
614 title: _t("Please complete partner's informations"),
617 pop.on('closed', self, function () {
620 pop.view_form.on('on_button_cancel', self, function () {
621 names_to_remove.push(partner_name);
623 recipient_ids_to_remove.push(partner_id);
628 $.when.apply($, emails_deferred).then(function () {
629 var new_names_to_find = _.difference(names_to_find, names_to_remove);
630 find_done = $.Deferred();
631 if (new_names_to_find.length > 0) {
632 find_done = self.parent_thread.ds_thread._model.call('message_partner_info_from_emails', [self.context.default_res_id, new_names_to_find, true]);
635 find_done.resolve([]);
637 $.when(find_done).pipe(function (result) {
638 var recipient_popups = result.concat(recipients_to_check);
639 _.each(recipient_popups, function (partner_info) {
640 if (partner_info.partner_id && _.indexOf(partner_info.partner_id, recipient_ids_to_remove) == -1) {
641 recipient_ids.push(partner_info.partner_id);
644 }).pipe(function () {
645 check_done.resolve(recipient_ids);
653 on_message_post: function (event) {
655 if (self.flag_post) {
658 self.flag_post = true;
659 if (this.do_check_attachment_upload() && (this.attachment_ids.length || this.$('textarea').val().match(/\S+/))) {
661 this.do_send_message_post([], this.is_log);
664 this.check_recipient_partners().done(function (partner_ids) {
665 self.do_send_message_post(partner_ids, self.is_log);
671 /* do post a message and fetch the message */
672 do_send_message_post: function (partner_ids, log) {
675 'body': this.$('textarea').val(),
677 'parent_id': this.context.default_parent_id,
678 'attachment_ids': _.map(this.attachment_ids, function (file) {return file.id;}),
679 'partner_ids': partner_ids,
680 'context': _.extend(this.parent_thread.context, {
681 'mail_post_autofollow': true,
682 'mail_post_autofollow_partner_ids': partner_ids,
685 'content_subtype': 'plaintext',
688 values['subtype'] = false;
691 values['subtype'] = 'mail.mt_comment';
693 this.parent_thread.ds_thread._model.call('message_post', [this.context.default_res_id], values).done(function (message_id) {
694 var thread = self.parent_thread;
695 var root = thread == self.options.root_thread;
696 if (self.options.display_indented_thread < self.thread_level && thread.parent_message) {
697 var thread = thread.parent_message.parent_thread;
699 // create object and attach to the thread object
700 thread.message_fetch([["id", "=", message_id]], false, [message_id], function (arg, data) {
701 var message = thread.create_message_object( data.slice(-1)[0] );
702 // insert the message on dom
703 thread.insert_message( message, root ? undefined : self.$el, root );
706 self.flag_post = false;
710 /* Quick composer: toggle minimal / expanded mode
711 * - toggle minimal (one-liner) / expanded (textarea, buttons) mode
712 * - when going into expanded mode:
713 * - call `message_get_suggested_recipients` to have a list of partners to add
714 * - compute email_from list (list of unknown email_from to propose to create partners)
716 on_toggle_quick_composer: function (event) {
718 var $input = $(event.target);
719 this.compute_emails_from();
720 var email_addresses = _.pluck(this.recipients, 'email_address');
721 var suggested_partners = $.Deferred();
723 // if clicked: call for suggested recipients
724 if (event.type == 'click') {
725 this.is_log = $input.hasClass('oe_compose_log');
726 suggested_partners = this.parent_thread.ds_thread.call('message_get_suggested_recipients', [[this.context.default_res_id], this.context]).done(function (additional_recipients) {
727 var thread_recipients = additional_recipients[self.context.default_res_id];
728 _.each(thread_recipients, function (recipient) {
729 var parsed_email = mail.ChatterUtils.parse_email(recipient[1]);
730 if (_.indexOf(email_addresses, parsed_email[1]) == -1) {
731 self.recipients.push({
733 'partner_id': recipient[0],
734 'full_name': recipient[1],
735 'name': parsed_email[0],
736 'email_address': parsed_email[1],
737 'reason': recipient[2],
744 suggested_partners.resolve({});
747 // when call for suggested partners finished: re-render the widget
748 $.when(suggested_partners).pipe(function (additional_recipients) {
749 if ((!self.stay_open || (event && event.type == 'click')) && (!self.show_composer || !self.$('textarea:not(.oe_compact)').val().match(/\S+/) && !self.attachment_ids.length)) {
750 self.show_composer = !self.show_composer || self.stay_open;
753 if (!self.stay_open && self.show_composer && (!event || event.type != 'blur')) {
754 self.$('textarea:not(.oe_compact):first').focus();
758 return suggested_partners;
761 do_hide_compact: function () {
762 this.show_compact_message = false;
763 if (!this.show_composer) {
768 do_show_compact: function () {
769 this.show_compact_message = true;
770 if (!this.show_composer) {
775 /** Compute the list of unknown email_from the the given thread
776 * TDE FIXME: seems odd to delegate to the composer
777 * TDE TODO: please de-obfuscate and comment your code */
778 compute_emails_from: function () {
782 if (this.parent_thread.parent_message) {
783 // go to the parented message
784 var message = this.parent_thread.parent_message;
785 var parent_message = message.parent_id ? message.parent_thread.parent_message : message;
787 var messages = [parent_message].concat(parent_message.get_childs());
789 } else if (this.options.emails_from_on_composer) {
790 // get all wall messages if is not a mail.Wall
791 _.each(this.options.root_thread.messages, function (msg) {messages.push(msg); messages.concat(msg.get_childs());});
794 _.each(messages, function (thread) {
795 if (thread.author_id && !thread.author_id[0] &&
796 !_.find(self.recipients, function (recipient) {return recipient.email_address == thread.author_id[3];})) {
797 self.recipients.push({ 'full_name': thread.author_id[1],
798 'name': thread.author_id[2],
799 'email_address': thread.author_id[3],
802 'reason': 'Incoming email author'
806 return self.recipients;
809 on_checked_recipient: function (event) {
810 var $input = $(event.target);
811 var full_name = $input.attr("data");
812 _.each(this.recipients, function (recipient) {
813 if (recipient.full_name == full_name) {
814 recipient.checked = $input.is(":checked");
821 * ------------------------------------------------------------
822 * Thread Message Expandable Widget
823 * ------------------------------------------------------------
825 * This widget handles the display the expandable message in a thread.
827 * - - visible message
829 * - - visible message
830 * - - visible message
833 mail.ThreadExpandable = mail.MessageCommon.extend({
834 template: 'mail.thread.expandable',
836 init: function (parent, datasets, options) {
837 this._super(parent, datasets, options);
838 this.type = 'expandable';
839 this.max_limit = datasets.max_limit;
840 this.nb_messages = datasets.nb_messages;
841 this.flag_used = false;
845 this._super.apply(this, arguments);
849 reinit: function () {
850 var $render = $(session.web.qweb.render('mail.thread.expandable', {'widget': this}));
851 this.$el.replaceWith( $render );
857 * Bind events in the widget. Each event is slightly described
858 * in the function. */
859 bind_events: function () {
860 this.$('.oe_msg_more_message').on('click', this.on_expandable);
863 animated_destroy: function (fadeTime) {
865 this.$el.fadeOut(fadeTime, function () {
870 /*The selected thread and all childs (messages/thread) became read
871 * @param {object} mouse envent
873 on_expandable: function (event) {
874 if (event)event.stopPropagation();
875 if (this.flag_used) {
878 this.flag_used = true;
883 self.parent_thread.message_fetch(this.domain, this.context, false, function (arg, data) {
885 // insert the message on dom after this message
886 self.parent_thread.switch_new_message( data, self.$el );
887 self.animated_destroy(200);
895 mail.ThreadMessage = mail.MessageCommon.extend({
896 template: 'mail.thread.message',
899 this._super.apply(this, arguments);
901 if(this.thread_level < this.options.display_indented_thread) {
902 this.create_thread();
904 this.display_attachments();
906 this.ds_notification = new session.web.DataSetSearch(this, 'mail.notification');
907 this.ds_message = new session.web.DataSetSearch(this, 'mail.message');
911 * Bind events in the widget. Each event is slightly described
912 * in the function. */
913 bind_events: function () {
915 // header icons bindings
916 this.$('.oe_read').on('click', this.on_message_read);
917 this.$('.oe_unread').on('click', this.on_message_unread);
918 this.$('.oe_msg_delete').on('click', this.on_message_delete);
919 this.$('.oe_reply').on('click', this.on_message_reply);
920 this.$('.oe_star').on('click', this.on_star);
921 this.$('.oe_msg_vote').on('click', this.on_vote);
922 this.$('.oe_mail_expand').on('click', this.on_expand);
923 this.$('.oe_mail_reduce').on('click', this.on_expand);
924 this.$('.oe_mail_action_model').on('click', this.on_record_clicked);
927 on_record_clicked: function (event) {
931 'title': this.record_name
933 session.webclient.action_manager.do_push_state(state);
936 /* Call the on_compose_message on the thread of this message. */
937 on_message_reply:function (event) {
938 event.stopPropagation();
939 this.create_thread();
940 this.thread.on_compose_message(event);
944 on_expand: function (event) {
945 event.stopPropagation();
946 this.$('.oe_msg_body_short:first').toggle();
947 this.$('.oe_msg_body_long:first').toggle();
952 * Instantiate the thread object of this message.
953 * Each message have only one thread.
955 create_thread: function () {
960 this.thread = new mail.Thread(this, this, {
961 'domain': this.domain,
963 'default_model': this.model,
964 'default_res_id': this.res_id,
965 'default_parent_id': this.id
967 'options': this.options
970 /*insert thread in parent message*/
971 this.thread.insertAfter(this.$el);
975 * Fade out the message and his child thread.
976 * Then this object is destroyed.
978 animated_destroy: function (fadeTime) {
980 this.$el.fadeOut(fadeTime, function () {
981 self.parent_thread.message_to_expandable(self);
984 this.thread.$el.fadeOut(fadeTime);
989 * Wait a confirmation for delete the message on the DB.
990 * Make an animate destroy
992 on_message_delete: function (event) {
993 event.stopPropagation();
994 if (! confirm(_t("Do you really want to delete this message?"))) { return false; }
996 this.animated_destroy(150);
997 // delete this message and his childs
998 var ids = [this.id].concat( this.get_child_ids() );
999 this.ds_message.unlink(ids);
1003 /* Check if the message must be destroy and detroy it or check for re render widget
1004 * @param {callback} apply function
1006 check_for_rerender: function () {
1009 var messages = [this].concat(this.get_childs());
1010 var message_ids = _.map(messages, function (msg) { return msg.id;});
1011 var domain = mail.ChatterUtils.expand_domain( this.options.root_thread.domain )
1012 .concat([["id", "in", message_ids ]]);
1014 return this.parent_thread.ds_message.call('message_read', [undefined, domain, [], !!this.parent_thread.options.display_indented_thread, this.context, this.parent_thread.id])
1015 .then( function (records) {
1016 // remove message not loaded
1017 _.map(messages, function (msg) {
1018 if(!_.find(records, function (record) { return record.id == msg.id; })) {
1019 msg.animated_destroy(150);
1021 msg.renderElement();
1024 self.options.root_thread.MailWidget.do_reload_menu_emails();
1030 on_message_read: function (event) {
1031 event.stopPropagation();
1032 this.on_message_read_unread(true);
1036 on_message_unread: function (event) {
1037 event.stopPropagation();
1038 this.on_message_read_unread(false);
1042 /* Set the selected thread and all childs as read or unread, based on
1044 * @param {boolean} read_value
1046 on_message_read_unread: function (read_value) {
1048 var messages = [this].concat(this.get_childs());
1050 // inside the inbox, when the user mark a message as read/done, don't apply this value
1051 // for the stared/favorite message
1052 if (this.options.view_inbox && read_value) {
1053 var messages = _.filter(messages, function (val) { return !val.is_favorite && val.id; });
1054 if (!messages.length) {
1055 this.check_for_rerender();
1059 var message_ids = _.map(messages, function (val) { return val.id; });
1061 this.ds_message.call('set_message_read', [message_ids, read_value, true, this.context])
1063 // apply modification
1064 _.each(messages, function (msg) {
1065 msg.to_read = !read_value;
1066 if (msg.options.toggle_read) {
1067 msg.options.show_read = msg.to_read;
1068 msg.options.show_unread = !msg.to_read;
1071 // check if the message must be display, destroy or rerender
1072 self.check_for_rerender();
1078 * add or remove a vote for a message and display the result
1080 on_vote: function (event) {
1081 event.stopPropagation();
1082 this.ds_message.call('vote_toggle', [[this.id]])
1084 _.bind(function (vote) {
1085 this.has_voted = vote;
1086 this.vote_nb += this.has_voted ? 1 : -1;
1087 this.display_vote();
1093 * Display the render of this message's vote
1095 display_vote: function () {
1096 var vote_element = session.web.qweb.render('mail.thread.message.vote', {'widget': this});
1097 this.$(".oe_msg_footer:first .oe_mail_vote_count").remove();
1098 this.$(".oe_msg_footer:first .oe_msg_vote").replaceWith(vote_element);
1099 this.$('.oe_msg_vote').on('click', this.on_vote);
1103 * add or remove a favorite (or starred) for a message and change class on the DOM
1105 on_star: function (event) {
1106 event.stopPropagation();
1108 var button = self.$('.oe_star:first');
1110 this.ds_message.call('set_message_starred', [[self.id], !self.is_favorite, true])
1111 .then(function (star) {
1112 self.is_favorite=star;
1113 if (self.is_favorite) {
1114 button.addClass('oe_starred');
1116 button.removeClass('oe_starred');
1119 if (self.options.view_inbox && self.is_favorite) {
1120 self.on_message_read_unread(true);
1122 self.check_for_rerender();
1131 * ------------------------------------------------------------
1133 * ------------------------------------------------------------
1135 * This widget handles the display of a thread of messages. The
1138 * - - sub message (parent_id = root message)
1140 * - - - - sub sub message (parent id = sub thread)
1141 * - - sub message (parent_id = root message)
1144 mail.Thread = session.web.Widget.extend({
1145 template: 'mail.thread',
1148 * @param {Object} parent parent
1149 * @param {Array} [domain]
1150 * @param {Object} [context] context of the thread. It should
1151 contain at least default_model, default_res_id. Please refer to
1152 the ComposeMessage widget for more information about it.
1153 * @param {Object} [options]
1154 * @param {Object} [message] read about mail.ThreadMessage object
1155 * @param {Object} [thread]
1156 * @param {int} [display_indented_thread] number thread level to indented threads.
1157 * other are on flat mode
1158 * @param {Array} [parents] liked with the parents thread
1159 * use with browse, fetch... [O]= top parent
1161 init: function (parent, datasets, options) {
1163 this._super(parent, options);
1164 this.MailWidget = parent instanceof mail.Widget ? parent : false;
1165 this.domain = options.domain || [];
1166 this.context = _.extend(options.context || {});
1168 this.options = options.options;
1169 this.options.root_thread = (options.options.root_thread != undefined ? options.options.root_thread : this);
1170 this.options.show_compose_message = this.options.show_compose_message && !this.thread_level;
1172 // record options and data
1173 this.parent_message= parent.thread!= undefined ? parent : false ;
1175 // data of this thread
1176 this.id = datasets.id || false;
1177 this.last_id = datasets.last_id || false;
1178 this.parent_id = datasets.parent_id || false;
1179 this.is_private = datasets.is_private || false;
1180 this.author_id = datasets.author_id || false;
1181 this.thread_level = (datasets.thread_level+1) || 0;
1182 datasets.partner_ids = datasets.partner_ids || [];
1183 if (datasets.author_id && !_.contains(_.flatten(datasets.partner_ids),datasets.author_id[0]) && datasets.author_id[0]) {
1184 datasets.partner_ids.push(datasets.author_id);
1186 this.user_pid = datasets.user_pid || false;
1187 this.partner_ids = datasets.partner_ids;
1189 this.options.flat_mode = (this.options.display_indented_thread - this.thread_level > 0);
1191 // object compose message
1192 this.compose_message = false;
1194 this.ds_thread = new session.web.DataSetSearch(this, this.context.default_model || 'mail.thread');
1195 this.ds_message = new session.web.DataSetSearch(this, 'mail.message');
1196 this.render_mutex = new $.Mutex();
1199 start: function () {
1200 this._super.apply(this, arguments);
1204 /* instantiate the compose message object and insert this on the DOM.
1205 * The compose message is display in compact form.
1207 instantiate_compose_message: function () {
1208 // add message composition form view
1209 if (!this.compose_message) {
1210 this.compose_message = new mail.ThreadComposeMessage(this, this, {
1211 'context': this.options.compose_as_todo && !this.thread_level ? _.extend(this.context, { 'default_starred': true }) : this.context,
1212 'options': this.options,
1214 if (!this.thread_level || this.thread_level > this.options.display_indented_thread) {
1215 this.compose_message.insertBefore(this.$el);
1217 this.compose_message.prependTo(this.$el);
1222 /* When the expandable object is visible on screen (with scrolling)
1223 * then the on_expandable function is launch
1225 on_scroll: function () {
1227 _.each( _.filter(this.messages, function (val) {return val.max_limit && !val.parent_id;}), function (val) {
1228 var pos = val.$el.position();
1230 /* bottom of the screen */
1231 var bottom = $(window).scrollTop()+$(window).height()+200;
1232 if (bottom > pos.top) {
1233 val.on_expandable();
1240 * Bind events in the widget. Each event is slightly described
1241 * in the function. */
1242 bind_events: function () {
1244 self.$('.oe_mail_list_recipients .oe_more').on('click', self.on_show_recipients);
1245 self.$('.oe_mail_compose_textarea .oe_more_hidden').on('click', self.on_hide_recipients);
1249 *show all the partner list of this parent message
1251 on_show_recipients: function () {
1252 var p=$(this).parent();
1253 p.find('.oe_more_hidden, .oe_hidden').show();
1254 p.find('.oe_more').hide();
1259 *hide a part of the partner list of this parent message
1261 on_hide_recipients: function () {
1262 var p=$(this).parent();
1263 p.find('.oe_more_hidden, .oe_hidden').hide();
1264 p.find('.oe_more').show();
1268 /* get all child message/thread id linked.
1269 * @return array of id
1271 get_child_ids: function () {
1272 return _.map(this.get_childs(), function (val) { return val.id; });
1275 /* get all child message/thread linked.
1276 * @param {int} nb_thread_level, number of traversed thread level for this search
1277 * @return array of thread object
1279 get_childs: function (nb_thread_level) {
1281 if (arguments[1]) res.push(this);
1282 if (isNaN(nb_thread_level) || nb_thread_level>0) {
1283 _(this.messages).each(function (val, key) {
1285 res = res.concat( val.thread.get_childs((isNaN(nb_thread_level) ? undefined : nb_thread_level-1), true) );
1293 *search a thread in all thread and child thread.
1294 * This method return an object thread.
1295 * @param {object}{int} option.id
1296 * @param {object}{string} option.model
1297 * @param {object}{boolean} option._go_thread_wall
1298 * private for check the top thread
1299 * @param {object}{boolean} option.default_return_top_thread
1300 * return the top thread (wall) if no thread found
1301 * @return thread object
1303 browse_thread: function (options) {
1304 // goto the wall thread for launch browse
1305 if (!options._go_thread_wall) {
1306 options._go_thread_wall = true;
1307 return this.options.root_thread.browse_thread(options);
1310 if (this.id == options.id) {
1315 for (var i in this.messages) {
1316 if (this.messages[i].thread) {
1317 var res = this.messages[i].thread.browse_thread({'id':options.id, '_go_thread_wall':true});
1318 if (res) return res;
1323 //if option default_return_top_thread, return the top if no found thread
1324 if (options.default_return_top_thread) {
1332 *search a message in all thread and child thread.
1333 * This method return an object message.
1334 * @param {object}{int} option.id
1335 * @param {object}{string} option.model
1336 * @param {object}{boolean} option._go_thread_wall
1337 * private for check the top thread
1338 * @return message object
1340 browse_message: function (options) {
1341 if (this.options.root_thread.messages[0])
1342 return this.options.root_thread.messages[0].browse_message(options);
1346 *If compose_message doesn't exist, instantiate the compose message.
1347 * Call the on_toggle_quick_composer method to allow the user to write his message.
1348 * (Is call when a user click on "Reply" button)
1350 on_compose_message: function (event) {
1351 this.instantiate_compose_message();
1352 this.compose_message.on_toggle_quick_composer(event);
1357 *display the message "there are no message" on the thread
1359 no_message: function () {
1360 var no_message = $(session.web.qweb.render('mail.wall_no_message', {}));
1361 if (this.options.help) {
1362 no_message.html(this.options.help);
1364 if (!this.$el.find(".oe_view_nocontent").length)
1366 no_message.appendTo(this.$el);
1371 *make a request to read the message (calling RPC to "message_read").
1372 * The result of this method is send to the switch message for sending ach message to
1373 * his parented object thread.
1374 * @param {Array} replace_domain: added to this.domain
1375 * @param {Object} replace_context: added to this.context
1376 * @param {Array} ids read (if the are some ids, the method don't use the domain)
1378 message_fetch: function (replace_domain, replace_context, ids, callback) {
1379 return this.ds_message.call('message_read', [
1380 // ids force to read
1381 ids === false ? undefined : ids,
1382 // domain + additional
1383 (replace_domain ? replace_domain : this.domain),
1384 // ids allready loaded
1385 (this.id ? [this.id].concat( this.get_child_ids() ) : this.get_child_ids()),
1386 // option for sending in flat mode by server
1387 this.options.flat_mode,
1388 // context + additional
1389 (replace_context ? replace_context : this.context),
1391 this.context.default_parent_id || undefined
1392 ]).done(callback ? _.bind(callback, this, arguments) : this.proxy('switch_new_message')
1393 ).done(this.proxy('message_fetch_set_read'));
1396 message_fetch_set_read: function (message_list) {
1397 if (! this.context.mail_read_set_read) return;
1399 this.render_mutex.exec(function() {
1400 msg_ids = _.pluck(message_list, 'id');
1401 return self.ds_message.call('set_message_read', [msg_ids, true, false, self.context])
1402 .then(function (nb_read) {
1404 self.options.root_thread.MailWidget.do_reload_menu_emails();
1411 *create the message object and attached on this thread.
1412 * When the message object is create, this method call insert_message for,
1413 * displaying this message on the DOM.
1414 * @param : {object} data from calling RPC to "message_read"
1416 create_message_object: function (data) {
1419 data.thread_level = self.thread_level || 0;
1420 data.options = _.extend(data.options || {}, self.options);
1422 if (data.type=='expandable') {
1423 var message = new mail.ThreadExpandable(self, data, {'context':{
1424 'default_model': data.model || self.context.default_model,
1425 'default_res_id': data.res_id || self.context.default_res_id,
1426 'default_parent_id': self.id,
1429 data.record_name= (data.record_name != '' && data.record_name) || (self.parent_message && self.parent_message.record_name);
1430 var message = new mail.ThreadMessage(self, data, {'context':{
1431 'default_model': data.model,
1432 'default_res_id': data.res_id,
1433 'default_parent_id': data.id,
1437 // check if the message is already create
1438 for (var i in self.messages) {
1439 if (message.id && self.messages[i] && self.messages[i].id == message.id) {
1440 self.messages[i].destroy();
1443 self.messages.push( message );
1449 *insert the message on the DOM.
1450 * All message (and expandable message) are sorted. The method get the
1451 * older and newer message to insert the message (before, after).
1452 * If there are no older or newer, the message is prepend or append to
1453 * the thread (parent object or on root thread for flat view).
1454 * The sort is define by the thread_level (O for newer on top).
1455 * @param : {object} ThreadMessage object
1457 insert_message: function (message, dom_insert_after, prepend) {
1459 if (this.options.show_compact_message > this.thread_level) {
1460 this.instantiate_compose_message();
1461 this.compose_message.do_show_compact();
1464 this.$('.oe_view_nocontent').remove();
1465 if (dom_insert_after && dom_insert_after.parent()[0] == self.$el[0]) {
1466 message.insertAfter(dom_insert_after);
1467 } else if (prepend) {
1468 message.prependTo(self.$el);
1470 message.appendTo(self.$el);
1472 message.$el.hide().fadeIn(500);
1478 *get the parent thread of the messages.
1479 * Each message is send to his parent object (or parent thread flat mode) for creating the object message.
1480 * @param : {Array} datas from calling RPC to "message_read"
1482 switch_new_message: function (records, dom_insert_after) {
1484 var dom_insert_after = typeof dom_insert_after == 'object' ? dom_insert_after : false;
1485 _(records).each(function (record) {
1486 var thread = self.browse_thread({
1487 'id': record.parent_id,
1488 'default_return_top_thread':true
1490 // create object and attach to the thread object
1491 var message = thread.create_message_object( record );
1492 // insert the message on dom
1493 thread.insert_message( message, dom_insert_after);
1495 if (!records.length && this.options.root_thread == this) {
1501 * this method is call when the widget of a message or an expandable message is destroy
1502 * in this thread. The this.messages array is filter to remove this message
1504 on_message_detroy: function (message) {
1506 this.messages = _.filter(this.messages, function (val) { return !val.isDestroyed(); });
1507 if (this.options.root_thread == this && !this.messages.length) {
1515 * Convert a destroyed message into a expandable message
1517 message_to_expandable: function (message) {
1519 if (!this.thread_level || message.isDestroyed()) {
1524 var messages = _.sortBy( this.messages, function (val) { return val.id; });
1525 var it = _.indexOf( messages, message );
1527 var msg_up = message.$el.prev('.oe_msg');
1528 var msg_down = message.$el.next('.oe_msg');
1529 var msg_up = msg_up.hasClass('oe_msg_expandable') ? _.find( this.messages, function (val) { return val.$el[0] == msg_up[0]; }) : false;
1530 var msg_down = msg_down.hasClass('oe_msg_expandable') ? _.find( this.messages, function (val) { return val.$el[0] == msg_down[0]; }) : false;
1532 var message_dom = [ ["id", "=", message.id] ];
1534 if ( msg_up && msg_up.type == "expandable" && msg_down && msg_down.type == "expandable") {
1535 // concat two expandable message and add this message to this dom
1536 msg_up.domain = mail.ChatterUtils.expand_domain( msg_up.domain );
1537 msg_down.domain = mail.ChatterUtils.expand_domain( msg_down.domain );
1539 msg_down.domain = ['|','|'].concat( msg_up.domain ).concat( message_dom ).concat( msg_down.domain );
1541 if ( !msg_down.max_limit ) {
1542 msg_down.nb_messages += 1 + msg_up.nb_messages;
1545 msg_up.$el.remove();
1550 } else if ( msg_up && msg_up.type == "expandable") {
1551 // concat preview expandable message and this message to this dom
1552 msg_up.domain = mail.ChatterUtils.expand_domain( msg_up.domain );
1553 msg_up.domain = ['|'].concat( msg_up.domain ).concat( message_dom );
1555 msg_up.nb_messages++;
1559 } else if ( msg_down && msg_down.type == "expandable") {
1560 // concat next expandable message and this message to this dom
1561 msg_down.domain = mail.ChatterUtils.expand_domain( msg_down.domain );
1562 msg_down.domain = ['|'].concat( msg_down.domain ).concat( message_dom );
1564 // it's maybe a message expandable for the max limit read message
1565 if ( !msg_down.max_limit ) {
1566 msg_down.nb_messages++;
1572 // create a expandable message
1573 var expandable = new mail.ThreadExpandable(this, {
1574 'model': message.model,
1575 'parent_id': message.parent_id,
1577 'thread_level': message.thread_level,
1578 'parent_id': message.parent_id,
1579 'domain': message_dom,
1580 'options': message.options,
1583 'default_model': message.model || this.context.default_model,
1584 'default_res_id': message.res_id || this.context.default_res_id,
1585 'default_parent_id': this.id,
1589 // add object on array and DOM
1590 this.messages.push(expandable);
1591 expandable.insertAfter(message.$el);
1602 * ------------------------------------------------------------
1603 * mail : root Widget
1604 * ------------------------------------------------------------
1606 * This widget handles the display of messages with thread options. Its main
1607 * use is to receive a context and a domain, and to delegate the message
1608 * fetching and displaying to the Thread widget.
1610 session.web.client_actions.add('mail.Widget', 'session.mail.Widget');
1611 mail.Widget = session.web.Widget.extend({
1612 template: 'mail.Root',
1615 * @param {Object} parent parent
1616 * @param {Array} [domain]
1617 * @param {Object} [context] context of the thread. It should
1618 * contain at least default_model, default_res_id. Please refer to
1619 * the compose_message widget for more information about it.
1620 * @param {Object} [options]
1621 *... @param {Number} [truncate_limit=250] number of character to
1622 * display before having a "show more" link; note that the text
1623 * will not be truncated if it does not have 110% of the parameter
1624 *... @param {Boolean} [show_record_name] display the name and link for do action
1625 *... @param {boolean} [show_reply_button] display the reply button
1626 *... @param {boolean} [show_read_unread_button] display the read/unread button
1627 *... @param {int} [display_indented_thread] number thread level to indented threads.
1628 * other are on flat mode
1629 *... @param {Boolean} [show_compose_message] allow to display the composer
1630 *... @param {Boolean} [show_compact_message] display the compact message on the thread
1631 * when the user clic on this compact mode, the composer is open
1632 *... @param {Array} [message_ids] List of ids to fetch by the root thread.
1633 * When you use this option, the domain is not used for the fetch root.
1634 * @param {String} [no_message] Message to display when there are no message
1635 * @param {Boolean} [show_link] Display partner (authors, followers...) on link or not
1636 * @param {Boolean} [compose_as_todo] The root composer mark automatically the message as todo
1637 * @param {Boolean} [readonly] Read only mode, hide all action buttons and composer
1639 init: function (parent, action) {
1640 this._super(parent, action);
1642 this.action = _.clone(action);
1643 this.domain = this.action.domain || this.action.params.domain || [];
1644 this.context = this.action.context || this.action.params.context || {};
1646 this.action.params = _.extend({
1647 'display_indented_thread' : -1,
1648 'show_reply_button' : false,
1649 'show_read_unread_button' : false,
1650 'truncate_limit' : 250,
1651 'show_record_name' : false,
1652 'show_compose_message' : false,
1653 'show_compact_message' : false,
1654 'compose_placeholder': false,
1656 'view_inbox': false,
1657 'message_ids': undefined,
1658 'compose_as_todo' : false,
1660 'emails_from_on_composer': true,
1661 }, this.action.params);
1663 this.action.params.help = this.action.help || false;
1666 start: function (options) {
1667 this._super.apply(this, arguments);
1668 this.message_render();
1673 * create an object "related_menu"
1674 * contains the menu widget and the sub menu related of this wall
1676 do_reload_menu_emails: function () {
1677 var menu = session.webclient.menu;
1678 if (!menu || !menu.current_menu) {
1681 return menu.rpc("/web/menu/load_needaction", {'menu_ids': [menu.current_menu]}).done(function(r) {
1682 menu.on_needaction_loaded(r);
1683 }).then(function () {
1684 menu.trigger("need_action_reloaded");
1689 *Create the root thread and display this object in the DOM.
1690 * Call the no_message method then c all the message_fetch method
1691 * of this root thread to display the messages.
1693 message_render: function (search) {
1695 this.thread = new mail.Thread(this, {}, {
1696 'domain' : this.domain,
1697 'context' : this.context,
1698 'options': this.action.params,
1701 this.thread.appendTo( this.$el );
1703 if (this.action.params.show_compose_message) {
1704 this.thread.instantiate_compose_message();
1705 this.thread.compose_message.do_show_compact();
1708 this.thread.message_fetch(null, null, this.action.params.message_ids);
1712 bind_events: function () {
1713 $(document).scroll( _.bind(this.thread.on_scroll, this.thread) );
1714 $(window).resize( _.bind(this.thread.on_scroll, this.thread) );
1715 this.$el.resize( _.bind(this.thread.on_scroll, this.thread) );
1716 window.setTimeout( _.bind(this.thread.on_scroll, this.thread), 500 );
1722 * ------------------------------------------------------------
1723 * mail_thread Widget
1724 * ------------------------------------------------------------
1726 * This widget handles the display of messages on a document. Its main
1727 * use is to receive a context and a domain, and to delegate the message
1728 * fetching and displaying to the Thread widget.
1729 * Use Help on the field to display a custom "no message loaded"
1731 session.web.form.widgets.add('mail_thread', 'openerp.mail.RecordThread');
1732 mail.RecordThread = session.web.form.AbstractField.extend({
1733 template: 'mail.record_thread',
1735 init: function (parent, node) {
1736 this._super.apply(this, arguments);
1737 this.ParentViewManager = parent;
1738 this.node = _.clone(node);
1739 this.node.params = _.extend({
1740 'display_indented_thread': -1,
1741 'show_reply_button': false,
1742 'show_read_unread_button': true,
1743 'read_action': 'unread',
1744 'show_record_name': false,
1745 'show_compact_message': 1,
1746 'display_log_button' : true,
1747 }, this.node.params);
1748 if (this.node.attrs.placeholder) {
1749 this.node.params.compose_placeholder = this.node.attrs.placeholder;
1751 if (this.node.attrs.readonly) {
1752 this.node.params.readonly = this.node.attrs.readonly;
1754 if ('display_log_button' in this.options) {
1755 this.node.params.display_log_button = this.options.display_log_button;
1757 this.domain = (this.node.params && this.node.params.domain) || (this.field && this.field.domain) || [];
1759 if (!this.ParentViewManager.is_action_enabled('edit')) {
1760 this.node.params.show_link = false;
1764 start: function () {
1765 this._super.apply(this, arguments);
1766 // NB: check the actual_mode property on view to know if the view is in create mode anymore
1767 this.view.on("change:actual_mode", this, this._check_visibility);
1768 this._check_visibility();
1771 _check_visibility: function () {
1772 this.$el.toggle(this.view.get("actual_mode") !== "create");
1775 render_value: function () {
1778 if (! this.view.datarecord.id || session.web.BufferedDataSet.virtual_id_regex.test(this.view.datarecord.id)) {
1779 this.$('oe_mail_thread').hide();
1783 this.node.params = _.extend(this.node.params, {
1784 'message_ids': this.get_value(),
1785 'show_compose_message': this.view.is_action_enabled('edit'),
1787 this.node.context = {
1788 'mail_read_set_read': true, // set messages as read in Chatter
1789 'default_res_id': this.view.datarecord.id || false,
1790 'default_model': this.view.model || false,
1794 $('<span class="oe_mail-placeholder"/>').insertAfter(this.root.$el);
1795 this.root.destroy();
1797 // create and render Thread widget
1798 this.root = new mail.Widget(this, _.extend(this.node, {
1799 'domain' : (this.domain || []).concat([['model', '=', this.view.model], ['res_id', '=', this.view.datarecord.id]]),
1802 return this.root.replace(this.$('.oe_mail-placeholder'));
1808 * ------------------------------------------------------------
1810 * ------------------------------------------------------------
1812 * This widget handles the display of a sidebar on the Wall. Its main use
1813 * is to display group and employees suggestion (if hr is installed).
1815 mail.WallSidebar = session.web.Widget.extend({
1816 template: 'mail.wall.sidebar',
1821 * ------------------------------------------------------------
1823 * ------------------------------------------------------------
1825 * This widget handles the display of messages on a Wall. Its main
1826 * use is to receive a context and a domain, and to delegate the message
1827 * fetching and displaying to the Thread widget.
1830 session.web.client_actions.add('mail.wall', 'session.mail.Wall');
1831 mail.Wall = session.web.Widget.extend({
1832 template: 'mail.wall',
1835 * @param {Object} parent parent
1836 * @param {Object} [options]
1837 * @param {Array} [options.domain] domain on the Wall
1838 * @param {Object} [options.context] context, is an object. It should
1839 * contain default_model, default_res_id, to give it to the threads.
1841 init: function (parent, action) {
1842 this._super(parent, action);
1843 this.ActionManager = parent;
1845 this.action = _.clone(action);
1846 this.domain = this.action.params.domain || this.action.domain || [];
1847 this.context = _.extend(this.action.params.context || {}, this.action.context || {});
1849 // filter some parameters that we will propagate as search_default
1851 for (var key in this.action.context.params) {
1852 if (_.indexOf(['model', 'res_id'], key) == -1) {
1855 this.context['search_default_' + key] = this.action.context.params[key];
1857 for (var key in this.context) {
1858 if (key.match(/^search_default_/)) {
1859 this.defaults[key.replace(/^search_default_/, '')] = this.context[key];
1862 this.action.params = _.extend({
1863 'display_indented_thread': 1,
1864 'show_reply_button': true,
1865 'show_read_unread_button': true,
1866 'show_compose_message': true,
1867 'show_record_name': true,
1868 'show_compact_message': this.action.params.view_mailbox ? false : 1,
1869 'view_inbox': false,
1870 'emails_from_on_composer': false,
1871 }, this.action.params);
1874 start: function () {
1875 this._super.apply(this);
1877 var searchview_loaded = this.load_searchview(this.defaults);
1878 if (! this.searchview.has_defaults) {
1879 this.message_render();
1882 var wall_sidebar = new mail.WallSidebar(this);
1883 wall_sidebar.appendTo(this.$el.find('.oe_mail_wall_aside'));
1887 * Load the mail.message search view
1888 * @param {Object} defaults ??
1890 load_searchview: function (defaults) {
1892 var ds_msg = new session.web.DataSetSearch(this, 'mail.message');
1893 this.searchview = new session.web.SearchView(this, ds_msg, false, defaults || {}, false);
1894 this.searchview.appendTo(this.$('.oe_view_manager_view_search'))
1895 .then(function () { self.searchview.on('search_data', self, self.do_searchview_search); });
1896 if (this.searchview.has_defaults) {
1897 this.searchview.ready.then(this.searchview.do_search);
1899 return this.searchview
1903 * Get the domains, contexts and groupbys in parameter from search
1904 * view, then render the filtered threads.
1905 * @param {Array} domains
1906 * @param {Array} contexts
1907 * @param {Array} groupbys
1909 do_searchview_search: function (domains, contexts, groupbys) {
1911 session.web.pyeval.eval_domains_and_contexts({
1912 domains: domains || [],
1913 contexts: contexts || [],
1914 group_by_seq: groupbys || []
1915 }).then(function (results) {
1917 $('<span class="oe_mail-placeholder"/>').insertAfter(self.root.$el);
1918 self.root.destroy();
1920 return self.message_render(results);
1925 * Create the root thread widget and display this object in the DOM
1927 message_render: function (search) {
1928 var domain = this.domain.concat(search && search['domain'] ? search['domain'] : []);
1929 var context = _.extend(this.context, search && search['context'] ? search['context'] : {});
1931 this.root = new mail.Widget(this, _.extend(this.action, {
1933 'context' : context,
1935 return this.root.replace(this.$('.oe_mail-placeholder'));
1938 bind_events: function () {
1940 this.$(".oe_write_full").click(function (event) {
1941 event.stopPropagation();
1943 name: _t('Compose Email'),
1944 type: 'ir.actions.act_window',
1945 res_model: 'mail.compose.message',
1948 action_from: 'mail.ThreadComposeMessage',
1949 views: [[false, 'form']],
1954 session.client.action_manager.do_action(action);
1956 this.$(".oe_write_onwall").click(function (event) { self.root.thread.on_compose_message(event); });
1962 * ------------------------------------------------------------
1964 * ------------------------------------------------------------
1966 * Add a link on the top user bar for write a full mail
1968 session.web.ComposeMessageTopButton = session.web.Widget.extend({
1969 template:'mail.ComposeMessageTopButton',
1971 start: function () {
1972 this.$('button').on('click', this.on_compose_message );
1976 on_compose_message: function (event) {
1977 event.stopPropagation();
1979 type: 'ir.actions.act_window',
1980 res_model: 'mail.compose.message',
1983 views: [[false, 'form']],
1987 session.client.action_manager.do_action(action);
1991 session.web.UserMenu.include({
1992 do_update: function(){
1994 this._super.apply(this, arguments);
1995 this.update_promise.then(function() {
1996 var mail_button = new session.web.ComposeMessageTopButton();
1997 mail_button.appendTo(session.webclient.$el.find('.oe_systray'));
2004 * ------------------------------------------------------------
2005 * Sub-widgets loading
2006 * ------------------------------------------------------------
2008 * Load here widgets that could depend on widgets defined in mail.js
2011 openerp.mail.suggestions(session, mail); // import suggestion.js (suggestion widget)